1+ """Metric Utilities"""
2+ import datetime
3+ import itertools
4+ import sys
5+
6+ import click
7+
8+ from SoftLayer .CLI import environment
9+ from SoftLayer .CLI import formatting
10+ from SoftLayer import utils
11+
12+
13+ from pprint import pprint as pp
14+
15+ @click .command (short_help = """Report on Resources in closing datacenters""" )
16+ @environment .pass_env
17+ def cli (env ):
18+ """Report on Resources in closing datacenters
19+
20+ Displays a list of Datacenters soon to be shutdown, and any resources on the account
21+ in those locations
22+ """
23+
24+ closing_filter = {
25+ 'capabilities' : {
26+ 'operation' : 'in' ,
27+ 'options' : [{'name' : 'data' , 'value' : ['CLOSURE_ANNOUNCED' ]}]
28+ },
29+ 'name' : {
30+ 'operation' : 'orderBy' ,
31+ 'options' : [{'name' : 'sort' , 'value' : ['DESC' ]}]
32+ }
33+ }
34+ mask = """mask[name, datacenterLongName, frontendRouterId, capabilities, datacenterId, backendRouterId,
35+ backendRouterName, frontendRouterName]"""
36+ closing_pods = env .client .call ('SoftLayer_Network_Pod' , 'getAllObjects' , mask = mask )
37+ # Find all VLANs in the POD that is going to close.
38+ search = "_objectType:SoftLayer_Network_Vlan primaryRouter.hostname: \" {}\" || primaryRouter.hostname: \" {}\" "
39+ resource_mask = """mask[
40+ resource(SoftLayer_Network_Vlan)[
41+ id,fullyQualifiedName,name,note,vlanNumber,networkSpace,
42+ virtualGuests[id,fullyQualifiedDomainName,billingItem[cancellationDate]],
43+ hardware[id,fullyQualifiedDomainName,billingItem[cancellationDate]],
44+ networkVlanFirewall[id,primaryIpAddress,billingItem[cancellationDate]],
45+ privateNetworkGateways[id,name,networkSpace],
46+ publicNetworkGateways[id,name,networkSpace]
47+ ]
48+ ]
49+ """
50+ table_title = "Resources in closing datacenters"
51+ resource_table = formatting .Table (["Id" , "Name" , "Public VLAN" , "Private VLAN" , "Type" , "Datacenter" ,
52+ "POD" , "Cancellation Date" ], title = table_title )
53+ resource_table .align = 'l'
54+ for pod in closing_pods :
55+ resources = {'hardware' : {}, 'virtual' : {}, 'firewall' : {}, 'gateway' : {}}
56+ vlans = env .client .call ('SoftLayer_Search' , 'advancedSearch' ,
57+ search .format (pod .get ('backendRouterName' ), pod .get ('frontendRouterName' )),
58+ iter = True , mask = resource_mask )
59+ for vlan in vlans :
60+ 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 ():
65+ resource_table .add_row ([
66+ resource_object ['id' ],
67+ resource_object ['name' ],
68+ resource_object ['vlan' ].get ('PUBLIC' , '-' ),
69+ resource_object ['vlan' ].get ('PRIVATE' , '-' ),
70+ resource_type ,
71+ pod .get ('datacenterLongName' ),
72+ pod .get ('backendRouterName' ),
73+ resource_object ['cancelDate' ]
74+ ])
75+
76+ env .fout (resource_table )
77+
78+
79+ # returns a Table Row for a given resource
80+ def process_vlan (vlan , resources = None ):
81+ if resources is None :
82+ resources = {'hardware' : {}, 'virtual' : {}, 'firewall' : {}, 'gateway' : {}}
83+
84+ 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 )
88+
89+ 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 )
93+
94+ 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 )
98+
99+ 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 )
106+
107+ return resources
108+
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
113+ def build_resource_object (name_property , vlan , resource , entry ):
114+ new_entry = {
115+ 'id' : resource .get ('id' ),
116+ 'name' : resource .get (name_property ),
117+ 'vlan' : {vlan .get ('networkSpace' ): vlan .get ('vlanNumber' )},
118+ 'cancelDate' : utils .clean_time (utils .lookup (resource , 'billingItem' , 'cancellationDate' ))
119+ }
120+ if entry :
121+ entry ['vlan' ][vlan .get ('networkSpace' )] = vlan .get ('vlanNumber' )
122+ else :
123+ entry = new_entry
124+
125+ return entry
0 commit comments