forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.py
More file actions
139 lines (122 loc) · 4.82 KB
/
create.py
File metadata and controls
139 lines (122 loc) · 4.82 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
131
132
133
134
135
136
137
138
139
"""Order/create a dedicated server."""
# :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
from SoftLayer.CLI import helpers
from SoftLayer.CLI import template
@click.command(epilog="See 'slcli server create-options' for valid options.")
@click.option('--hostname', '-H',
help="Host portion of the FQDN",
required=True,
prompt=True)
@click.option('--domain', '-D',
help="Domain portion of the FQDN",
required=True,
prompt=True)
@click.option('--size', '-s',
help="Hardware size",
required=True,
prompt=True)
@click.option('--os', '-o', help="OS install code",
required=True,
prompt=True)
@click.option('--datacenter', '-d', help="Datacenter shortname",
required=True,
prompt=True)
@click.option('--port-speed',
type=click.INT,
help="Port speeds",
required=True,
prompt=True)
@click.option('--billing',
type=click.Choice(['hourly', 'monthly']),
default='hourly',
show_default=True,
help="Billing rate")
@click.option('--postinstall', '-i', help="Post-install script to download")
@helpers.multi_option('--key', '-k',
help="SSH keys to add to the root user")
@click.option('--no-public',
is_flag=True,
help="Private network only")
@helpers.multi_option('--extra', '-e', help="Extra options")
@click.option('--test',
is_flag=True,
help="Do not actually create the server")
@click.option('--template', '-t',
is_eager=True,
callback=template.TemplateCallback(list_args=['key']),
help="A template file that defaults the command-line options",
type=click.Path(exists=True, readable=True, resolve_path=True))
@click.option('--export',
type=click.Path(writable=True, resolve_path=True),
help="Exports options to a template file")
@click.option('--wait',
type=click.INT,
help="Wait until the server is finished provisioning for up to "
"X seconds before returning")
@environment.pass_env
def cli(env, **args):
"""Order/create a dedicated server."""
mgr = SoftLayer.HardwareManager(env.client)
# Get the SSH keys
ssh_keys = []
for key in args.get('key'):
resolver = SoftLayer.SshKeyManager(env.client).resolve_ids
key_id = helpers.resolve_id(resolver, key, 'SshKey')
ssh_keys.append(key_id)
order = {
'hostname': args['hostname'],
'domain': args['domain'],
'size': args['size'],
'location': args.get('datacenter'),
'ssh_keys': ssh_keys,
'post_uri': args.get('postinstall'),
'os': args['os'],
'hourly': args.get('billing') == 'hourly',
'port_speed': args.get('port_speed'),
'no_public': args.get('no_public') or False,
'extras': args.get('extra'),
}
# Do not create hardware server with --test or --export
do_create = not (args['export'] or args['test'])
output = None
if args.get('test'):
result = mgr.verify_order(**order)
table = formatting.Table(['Item', 'cost'])
table.align['Item'] = 'r'
table.align['cost'] = 'r'
total = 0.0
for price in result['prices']:
total += float(price.get('recurringFee', 0.0))
rate = "%.2f" % float(price['recurringFee'])
table.add_row([price['item']['description'], rate])
table.add_row(['Total monthly cost', "%.2f" % total])
output = []
output.append(table)
output.append(formatting.FormattedItem(
'',
' -- ! Prices reflected here are retail and do not '
'take account level discounts and are not guaranteed.'))
if args['export']:
export_file = args.pop('export')
template.export_to_template(export_file, args,
exclude=['wait', 'test'])
env.fout('Successfully exported options to a template file.')
return
if do_create:
if not (env.skip_confirmations or formatting.confirm(
"This action will incur charges on your account. "
"Continue?")):
raise exceptions.CLIAbort('Aborting dedicated server order.')
result = mgr.place_order(**order)
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']])
output = table
env.fout(output)