|
| 1 | +"""Report on Resources in closing datacenters""" |
| 2 | +import click |
| 3 | + |
| 4 | +from SoftLayer.CLI import environment |
| 5 | +from SoftLayer.CLI import formatting |
| 6 | +from SoftLayer import utils |
| 7 | + |
| 8 | + |
| 9 | +@click.command(short_help="""Report on Resources in closing datacenters""") |
| 10 | +@environment.pass_env |
| 11 | +def cli(env): |
| 12 | + """Report on Resources in closing datacenters |
| 13 | +
|
| 14 | + Displays a list of Datacenters soon to be shutdown, and any resources on the account |
| 15 | +in those locations |
| 16 | + """ |
| 17 | + |
| 18 | + closing_filter = { |
| 19 | + 'capabilities': { |
| 20 | + 'operation': 'in', |
| 21 | + 'options': [{'name': 'data', 'value': ['CLOSURE_ANNOUNCED']}] |
| 22 | + }, |
| 23 | + 'name': { |
| 24 | + 'operation': 'orderBy', |
| 25 | + 'options': [{'name': 'sort', 'value': ['DESC']}] |
| 26 | + } |
| 27 | + } |
| 28 | + mask = """mask[name, datacenterLongName, frontendRouterId, capabilities, datacenterId, backendRouterId, |
| 29 | +backendRouterName, frontendRouterName]""" |
| 30 | + closing_pods = env.client.call('SoftLayer_Network_Pod', 'getAllObjects', mask=mask, filter=closing_filter) |
| 31 | + # Find all VLANs in the POD that is going to close. |
| 32 | + search = "_objectType:SoftLayer_Network_Vlan primaryRouter.hostname: \"{}\" || primaryRouter.hostname: \"{}\"" |
| 33 | + resource_mask = """mask[ |
| 34 | + resource(SoftLayer_Network_Vlan)[ |
| 35 | + id,fullyQualifiedName,name,note,vlanNumber,networkSpace, |
| 36 | + virtualGuests[id,fullyQualifiedDomainName,billingItem[cancellationDate]], |
| 37 | + hardware[id,fullyQualifiedDomainName,billingItem[cancellationDate]], |
| 38 | + networkVlanFirewall[id,primaryIpAddress,billingItem[cancellationDate]], |
| 39 | + privateNetworkGateways[id,name,networkSpace], |
| 40 | + publicNetworkGateways[id,name,networkSpace] |
| 41 | + ] |
| 42 | + ] |
| 43 | + """ |
| 44 | + table_title = "Resources in closing datacenters" |
| 45 | + resource_table = formatting.Table(["Id", "Name", "Public VLAN", "Private VLAN", "Type", "Datacenter", |
| 46 | + "POD", "Cancellation Date"], title=table_title) |
| 47 | + resource_table.align = 'l' |
| 48 | + for pod in closing_pods: |
| 49 | + resources = {'hardware': {}, 'virtual': {}, 'firewall': {}, 'gateway': {}} |
| 50 | + vlans = env.client.call('SoftLayer_Search', 'advancedSearch', |
| 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 |
| 54 | + for vlan in vlans: |
| 55 | + resources = process_vlan(vlan.get('resource', {}), resources) |
| 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(): |
| 60 | + resource_table.add_row([ |
| 61 | + resource_id, |
| 62 | + resource_object['name'], |
| 63 | + resource_object['vlan'].get('PUBLIC', '-'), |
| 64 | + resource_object['vlan'].get('PRIVATE', '-'), |
| 65 | + resource_type, |
| 66 | + pod.get('datacenterLongName'), |
| 67 | + pod.get('backendRouterName'), |
| 68 | + resource_object['cancelDate'] |
| 69 | + ]) |
| 70 | + |
| 71 | + env.fout(resource_table) |
| 72 | + |
| 73 | + |
| 74 | +# returns a Table Row for a given resource |
| 75 | +def process_vlan(vlan, resources=None): |
| 76 | + """Takes in a vlan object and pulls out the needed resources""" |
| 77 | + if resources is None: |
| 78 | + resources = {'hardware': {}, 'virtual': {}, 'firewall': {}, 'gateway': {}} |
| 79 | + |
| 80 | + type_x = "virtual" |
| 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) |
| 84 | + |
| 85 | + type_x = 'hardware' |
| 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) |
| 89 | + |
| 90 | + type_x = 'firewall' |
| 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) |
| 94 | + |
| 95 | + type_x = 'gateway' |
| 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) |
| 102 | + |
| 103 | + return resources |
| 104 | + |
| 105 | + |
| 106 | +def build_resource_object(name_property, vlan, resource, 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 = { |
| 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