-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsl_getInstances.py
More file actions
130 lines (108 loc) · 3.19 KB
/
sl_getInstances.py
File metadata and controls
130 lines (108 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'takechika'
import sys
from prettytable import PrettyTable
import SoftLayer
import sluser
SL_USERNAME = sluser.SL_USERNAME
SL_API_KEY = sluser.SL_API_KEY
_maskVirtualGuest = '''
id,
fullyQualifiedDomainName,
primaryIpAddress,
primaryBackendIpAddress,
datacenter,
location.pathString,
billingItem.orderItem.order.userRecord.username,
frontendRouters.hostname,
backendRouters.hostname,
billingItem.orderItem.recurringFee,
billingItem.orderItem.hourlyRecurringFee,
outboundPublicBandwidthUsage,
networkVlans,
networkVlanCount
'''
_maskHardware = '''
id,
bareMetalInstanceFlag,
fullyQualifiedDomainName,
primaryIpAddress,
primaryBackendIpAddress,
datacenter,
location.pathString,
billingItem.orderItem.order.userRecord.username,
frontendRouters.hostname,
backendRouters.hostname,
billingItem.orderItem.recurringFee,
billingItem.orderItem.hourlyRecurringFee,
outboundPublicBandwidthUsage,
networkVlans,
networkVlanCount
'''
_tableHeader = [
'id',
'bm?',
'fqdn',
'public_ip',
'backend_ip',
'dc',
'location',
'owner',
'fcr',
'bcr',
'mfee$',
'hfee$',
'out_bw',
'vlan'
]
def lookup(dic, key, *keys):
"""A generic dictionary access helper.
This helps simplify code that uses heavily nested dictionaries. It will
return None if any of the keys in *keys do not exist.
::
>>> lookup({'this': {'is': 'nested'}}, 'this', 'is')
nested
>>> lookup({}, 'this', 'is')
None
"""
if keys:
return lookup(dic.get(key, {}), *keys)
return dic.get(key)
client = SoftLayer.Client(username=SL_USERNAME, api_key=SL_API_KEY)
virtualGuests = client['Account'].getVirtualGuests(mask=_maskVirtualGuest)
hardwares = client['Account'].getHardware(mask=_maskHardware)
# for v in virtualGuests:
# print(v)
#exit()
instances = hardwares + virtualGuests
#instances = virtualGuests
# Table definition
table = PrettyTable(_tableHeader)
table.padding_width = 1
# Get virtualGuests
count = 0
for inst in instances:
table.add_row(
[
inst['id'],
inst.get('bareMetalInstanceFlag','-'),
inst['fullyQualifiedDomainName'],
inst.get('primaryIpAddress', '--'),
inst['primaryBackendIpAddress'],
inst['datacenter']['name'],
inst['location']['pathString'],
lookup(inst, 'billingItem', 'orderItem', 'order', 'userRecord','username') or '--',
inst['frontendRouters']['hostname'].split('.')[0] if not isinstance(inst['frontendRouters'],list) else inst['frontendRouters'][0]['hostname'].split('.')[0],
inst['backendRouters'][0]['hostname'].split('.')[0],
lookup(inst, 'billingItem', 'orderItem', 'recurringFee') or '-',
lookup(inst, 'billingItem', 'orderItem', 'hourlyRecurringFee') or '-',
round(float(inst['outboundPublicBandwidthUsage']),1),
inst['networkVlanCount']
# inst['networkVlans'][0]['vlanNumber'] or '-'
]
)
count = count + 1
print(table)
print(count, "instances")
exit()