forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.py
67 lines (59 loc) · 2.71 KB
/
create.py
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
"""Order/create a VLAN instance."""
# :license: MIT, see LICENSE for more details.
import click
import SoftLayer
from SoftLayer.managers import ordering
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.option('--name', required=False, prompt=True, help="Vlan name")
@click.option('--datacenter', '-d', required=False, help="Datacenter shortname")
@click.option('--pod', '-p', required=False, help="Pod name. E.g dal05.pod01")
@click.option('--network', default='public', show_default=True, type=click.Choice(['public', 'private']),
help='Network vlan type')
@click.option('--billing', default='hourly', show_default=True, type=click.Choice(['hourly', 'monthly']),
help="Billing rate")
@environment.pass_env
def cli(env, name, datacenter, pod, network, billing):
"""Order/create a VLAN instance.
Example:
slcli vlan create --name myvlan --datacenter dal13
or
slcli vlan create --name myvlan --pod dal10.pod01
"""
if not (datacenter or pod):
raise exceptions.CLIAbort("Is required Datacenter or Pod")
item_package = ['PUBLIC_NETWORK_VLAN']
complex_type = 'SoftLayer_Container_Product_Order_Network_Vlan'
extras = {'name': name}
if pod:
datacenter = pod.split('.')[0]
mgr = SoftLayer.NetworkManager(env.client)
pods = mgr.get_pods()
for router in pods:
if router.get('name') == pod:
if network == 'public':
extras['routerId'] = router.get('frontendRouterId')
elif network == 'private':
extras['routerId'] = router.get('backendRouterId')
break
if not extras.get('routerId'):
raise exceptions.CLIAbort(
"Unable to find pod name: {}".format(pod))
if network == 'private':
item_package = ['PRIVATE_NETWORK_VLAN']
ordering_manager = ordering.OrderingManager(env.client)
result = ordering_manager.place_order(package_keyname='NETWORK_VLAN',
location=datacenter,
item_keynames=item_package,
complex_type=complex_type,
hourly=billing,
extras=extras)
table = formatting.KeyValueTable(['name', 'value'])
table.align['name'] = 'r'
table.align['value'] = 'l'
table.add_row(['id', result['orderId']])
table.add_row(['created', result['orderDate']])
table.add_row(['name', result['orderDetails']['orderContainers'][0]['name']])
env.fout(table)