1- """Metric Utilities"""
2- import datetime
3- import itertools
4- import sys
5-
1+ """Report on Resources in closing datacenters"""
62import click
73
84from SoftLayer .CLI import environment
95from SoftLayer .CLI import formatting
106from SoftLayer import utils
117
128
13- from pprint import pprint as pp
14-
159@click .command (short_help = """Report on Resources in closing datacenters""" )
1610@environment .pass_env
1711def cli (env ):
1812 """Report on Resources in closing datacenters
1913
20- Displays a list of Datacenters soon to be shutdown, and any resources on the account
14+ Displays a list of Datacenters soon to be shutdown, and any resources on the account
2115in those locations
2216 """
2317
@@ -33,7 +27,7 @@ def cli(env):
3327 }
3428 mask = """mask[name, datacenterLongName, frontendRouterId, capabilities, datacenterId, backendRouterId,
3529backendRouterName, frontendRouterName]"""
36- closing_pods = env .client .call ('SoftLayer_Network_Pod' , 'getAllObjects' , mask = mask )
30+ closing_pods = env .client .call ('SoftLayer_Network_Pod' , 'getAllObjects' , mask = mask , filter = closing_filter )
3731 # Find all VLANs in the POD that is going to close.
3832 search = "_objectType:SoftLayer_Network_Vlan primaryRouter.hostname: \" {}\" || primaryRouter.hostname: \" {}\" "
3933 resource_mask = """mask[
@@ -54,16 +48,17 @@ def cli(env):
5448 for pod in closing_pods :
5549 resources = {'hardware' : {}, 'virtual' : {}, 'firewall' : {}, 'gateway' : {}}
5650 vlans = env .client .call ('SoftLayer_Search' , 'advancedSearch' ,
57- search .format (pod .get ('backendRouterName' ), pod .get ('frontendRouterName' )),
58- iter = True , mask = resource_mask )
51+ search .format (pod .get ('backendRouterName' ), pod .get ('frontendRouterName' )),
52+ iter = True , mask = resource_mask )
53+ # Go through the vlans and coalate the resources into a data structure that is easy to print out
5954 for vlan in vlans :
6055 resources = process_vlan (vlan .get ('resource' , {}), resources )
61-
62- for resource_type in resources . keys ():
63-
64- for resource_object in resources [ resource_type ]. values ():
56+
57+ # Go through each resource and add it to the table
58+ for resource_type , resource_values in resources . items ():
59+ for resource_id , resource_object in resource_values . items ():
6560 resource_table .add_row ([
66- resource_object [ 'id' ] ,
61+ resource_id ,
6762 resource_object ['name' ],
6863 resource_object ['vlan' ].get ('PUBLIC' , '-' ),
6964 resource_object ['vlan' ].get ('PRIVATE' , '-' ),
@@ -72,46 +67,51 @@ def cli(env):
7267 pod .get ('backendRouterName' ),
7368 resource_object ['cancelDate' ]
7469 ])
75-
70+
7671 env .fout (resource_table )
7772
7873
7974# returns a Table Row for a given resource
8075def process_vlan (vlan , resources = None ):
76+ """Takes in a vlan object and pulls out the needed resources"""
8177 if resources is None :
8278 resources = {'hardware' : {}, 'virtual' : {}, 'firewall' : {}, 'gateway' : {}}
8379
8480 type_x = "virtual"
85- for x in vlan .get ('virtualGuests' , {}):
86- existing = resources [type_x ].get (x .get ('id' ))
87- resources [type_x ][x ['id' ]] = build_resource_object ('fullyQualifiedDomainName' , vlan , x , existing )
81+ for obj_x in vlan .get ('virtualGuests' , {}):
82+ existing = resources [type_x ].get (obj_x .get ('id' ))
83+ resources [type_x ][obj_x ['id' ]] = build_resource_object ('fullyQualifiedDomainName' , vlan , obj_x , existing )
8884
8985 type_x = 'hardware'
90- for x in vlan .get ('hardware' , {}):
91- existing = resources [type_x ].get (x .get ('id' ))
92- resources [type_x ][x ['id' ]] = build_resource_object ('fullyQualifiedDomainName' , vlan , x , existing )
86+ for obj_x in vlan .get ('hardware' , {}):
87+ existing = resources [type_x ].get (obj_x .get ('id' ))
88+ resources [type_x ][obj_x ['id' ]] = build_resource_object ('fullyQualifiedDomainName' , vlan , obj_x , existing )
9389
9490 type_x = 'firewall'
95- for x in vlan .get ('networkVlanFirewall' , {}):
96- existing = resources [type_x ].get (x .get ('id' ))
97- resources [type_x ][x ['id' ]] = build_resource_object ('primaryIpAddress' , vlan , x , existing )
91+ for obj_x in vlan .get ('networkVlanFirewall' , {}):
92+ existing = resources [type_x ].get (obj_x .get ('id' ))
93+ resources [type_x ][obj_x ['id' ]] = build_resource_object ('primaryIpAddress' , vlan , obj_x , existing )
9894
9995 type_x = 'gateway'
100- for x in vlan .get ('privateNetworkGateways' , {}):
101- existing = resources [type_x ].get (x .get ('id' ))
102- resources [type_x ][x ['id' ]] = build_resource_object ('name' , vlan , x , existing )
103- for x in vlan .get ('publicNetworkGateways' , {}):
104- existing = resources [type_x ].get (x .get ('id' ))
105- resources [type_x ][x ['id' ]] = build_resource_object ('name' , vlan , x , existing )
96+ for obj_x in vlan .get ('privateNetworkGateways' , {}):
97+ existing = resources [type_x ].get (obj_x .get ('id' ))
98+ resources [type_x ][obj_x ['id' ]] = build_resource_object ('name' , vlan , obj_x , existing )
99+ for obj_x in vlan .get ('publicNetworkGateways' , {}):
100+ existing = resources [type_x ].get (obj_x .get ('id' ))
101+ resources [type_x ][obj_x ['id' ]] = build_resource_object ('name' , vlan , obj_x , existing )
106102
107103 return resources
108104
109- # name_property is what property to use as the name from resource
110- # vlan is the vlan object
111- # resource has the data we want
112- # entry is for any existing data
105+
113106def build_resource_object (name_property , vlan , resource , entry ):
114- new_entry = {
107+ """builds out a resource object and puts the required values in the right place.
108+
109+ :param: name_property is what property to use as the name from resource
110+ :param: vlan is the vlan object
111+ :param: resource has the data we want
112+ :param: entry is for any existing data
113+ """
114+ new_entry = {
115115 'id' : resource .get ('id' ),
116116 'name' : resource .get (name_property ),
117117 'vlan' : {vlan .get ('networkSpace' ): vlan .get ('vlanNumber' )},
@@ -122,4 +122,4 @@ def build_resource_object(name_property, vlan, resource, entry):
122122 else :
123123 entry = new_entry
124124
125- return entry
125+ return entry
0 commit comments