forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.py
More file actions
87 lines (73 loc) · 2.44 KB
/
metadata.py
File metadata and controls
87 lines (73 loc) · 2.44 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
"""Find details about this machine."""
# :license: MIT, see LICENSE for more details.
import click
import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting
META_CHOICES = [
'backend_ip',
'backend_mac',
'datacenter',
'datacenter_id',
'fqdn',
'frontend_mac',
'id',
'ip',
'network',
'provision_state',
'tags',
'user_data',
]
META_MAPPING = {
'backend_ip': 'primary_backend_ip',
'ip': 'primary_ip',
}
HELP = """Find details about this machine
\b
PROP Choices
%s
\b
Examples :
%s
""" % ('*' + '\n*'.join(META_CHOICES),
'slcli metadata ' + '\nslcli metadata '.join(META_CHOICES))
@click.command(help=HELP,
short_help="Find details about this machine.",
epilog="These commands only work on devices on the backend "
"SoftLayer network. This allows for self-discovery for "
"newly provisioned resources.")
@click.argument('prop', type=click.Choice(META_CHOICES))
@environment.pass_env
def cli(env, prop):
"""Find details about this machine."""
try:
if prop == 'network':
env.fout(get_network())
return
meta_prop = META_MAPPING.get(prop) or prop
env.fout(SoftLayer.MetadataManager().get(meta_prop))
except SoftLayer.TransportError:
raise exceptions.CLIAbort(
'Cannot connect to the backend service address. Make sure '
'this command is being ran from a device on the backend '
'network.')
def get_network():
"""Returns a list of tables with public and private network details."""
meta = SoftLayer.MetadataManager()
network_tables = []
for network_func in [meta.public_network, meta.private_network]:
network = network_func()
table = formatting.KeyValueTable(['name', 'value'])
table.align['name'] = 'r'
table.align['value'] = 'l'
table.add_row(['mac addresses',
formatting.listing(network['mac_addresses'],
separator=',')])
table.add_row(['router', network['router']])
table.add_row(['vlans',
formatting.listing(network['vlans'], separator=',')])
table.add_row(['vlan ids',
formatting.listing(network['vlan_ids'], separator=',')])
network_tables.append(table)
return network_tables