Skip to content

Commit df3ceca

Browse files
authored
Merge pull request #864 from kwienken/vs-create-updates
updating the virt cli create and create-options commands
2 parents cb53a46 + 610d464 commit df3ceca

File tree

5 files changed

+477
-57
lines changed

5 files changed

+477
-57
lines changed

SoftLayer/CLI/virt/create.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ def _update_with_like_args(ctx, _, value):
2525
like_args = {
2626
'hostname': like_details['hostname'],
2727
'domain': like_details['domain'],
28-
'cpu': like_details['maxCpu'],
29-
'memory': '%smb' % like_details['maxMemory'],
3028
'hourly': like_details['hourlyBillingFlag'],
3129
'datacenter': like_details['datacenter']['name'],
3230
'network': like_details['networkComponents'][0]['maxSpeed'],
@@ -36,6 +34,15 @@ def _update_with_like_args(ctx, _, value):
3634
'private': like_details['privateNetworkOnlyFlag'],
3735
}
3836

37+
like_args['flavor'] = utils.lookup(like_details,
38+
'billingItem',
39+
'orderItem',
40+
'preset',
41+
'keyName')
42+
if not like_args['flavor']:
43+
like_args['cpu'] = like_details['maxCpu']
44+
like_args['memory'] = '%smb' % like_details['maxMemory']
45+
3946
tag_refs = like_details.get('tagReferences', None)
4047
if tag_refs is not None and len(tag_refs) > 0:
4148
like_args['tag'] = [t['tag']['name'] for t in tag_refs]
@@ -66,16 +73,22 @@ def _parse_create_args(client, args):
6673
"""
6774
data = {
6875
"hourly": args['billing'] == 'hourly',
69-
"cpus": args['cpu'],
7076
"domain": args['domain'],
7177
"hostname": args['hostname'],
7278
"private": args['private'],
7379
"dedicated": args['dedicated'],
7480
"disks": args['disk'],
75-
"local_disk": not args['san'],
81+
"cpus": args.get('cpu', None),
82+
"memory": args.get('memory', None),
83+
"flavor": args.get('flavor', None)
7684
}
7785

78-
data["memory"] = args['memory']
86+
# The primary disk is included in the flavor and the local_disk flag is not needed
87+
# Setting it to None prevents errors from the flag not matching the flavor
88+
if not args.get('san') and args.get('flavor'):
89+
data['local_disk'] = None
90+
else:
91+
data['local_disk'] = not args['san']
7992

8093
if args.get('os'):
8194
data['os_code'] = args['os']
@@ -130,6 +143,9 @@ def _parse_create_args(client, args):
130143
if args.get('tag'):
131144
data['tags'] = ','.join(args['tag'])
132145

146+
if args.get('host_id'):
147+
data['host_id'] = args['host_id']
148+
133149
return data
134150

135151

@@ -143,15 +159,14 @@ def _parse_create_args(client, args):
143159
required=True,
144160
prompt=True)
145161
@click.option('--cpu', '-c',
146-
help="Number of CPU cores",
147-
type=click.INT,
148-
required=True,
149-
prompt=True)
162+
help="Number of CPU cores (not available with flavors)",
163+
type=click.INT)
150164
@click.option('--memory', '-m',
151-
help="Memory in mebibytes",
152-
type=virt.MEM_TYPE,
153-
required=True,
154-
prompt=True)
165+
help="Memory in mebibytes (not available with flavors)",
166+
type=virt.MEM_TYPE)
167+
@click.option('--flavor', '-f',
168+
help="Public Virtual Server flavor key name",
169+
type=click.STRING)
155170
@click.option('--datacenter', '-d',
156171
help="Datacenter shortname",
157172
required=True,
@@ -167,7 +182,10 @@ def _parse_create_args(client, args):
167182
help="Billing rate")
168183
@click.option('--dedicated/--public',
169184
is_flag=True,
170-
help="Create a dedicated Virtual Server (Private Node)")
185+
help="Create a Dedicated Virtual Server")
186+
@click.option('--host-id',
187+
type=click.INT,
188+
help="Host Id to provision a Dedicated Host Virtual Server onto")
171189
@click.option('--san',
172190
is_flag=True,
173191
help="Use SAN storage instead of local disk.")
@@ -305,6 +323,22 @@ def cli(env, **args):
305323
def _validate_args(env, args):
306324
"""Raises an ArgumentError if the given arguments are not valid."""
307325

326+
if all([args['cpu'], args['flavor']]):
327+
raise exceptions.ArgumentError(
328+
'[-c | --cpu] not allowed with [-f | --flavor]')
329+
330+
if all([args['memory'], args['flavor']]):
331+
raise exceptions.ArgumentError(
332+
'[-m | --memory] not allowed with [-f | --flavor]')
333+
334+
if all([args['dedicated'], args['flavor']]):
335+
raise exceptions.ArgumentError(
336+
'[-d | --dedicated] not allowed with [-f | --flavor]')
337+
338+
if all([args['host_id'], args['flavor']]):
339+
raise exceptions.ArgumentError(
340+
'[-h | --host-id] not allowed with [-f | --flavor]')
341+
308342
if all([args['userdata'], args['userfile']]):
309343
raise exceptions.ArgumentError(
310344
'[-u | --userdata] not allowed with [-F | --userfile]')

SoftLayer/CLI/virt/create_options.py

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import SoftLayer
99
from SoftLayer.CLI import environment
1010
from SoftLayer.CLI import formatting
11+
from SoftLayer import utils
1112

1213

1314
@click.command()
@@ -25,35 +26,63 @@ def cli(env):
2526
# Datacenters
2627
datacenters = [dc['template']['datacenter']['name']
2728
for dc in result['datacenters']]
29+
datacenters = sorted(datacenters)
30+
2831
table.add_row(['datacenter',
2932
formatting.listing(datacenters, separator='\n')])
3033

31-
# CPUs
32-
standard_cpu = [x for x in result['processors']
33-
if not x['template'].get(
34-
'dedicatedAccountHostOnlyFlag', False)]
34+
def _add_flavor_rows(flavor_key, flavor_label, flavor_options):
35+
flavors = []
36+
37+
for flavor_option in flavor_options:
38+
flavor_key_name = utils.lookup(flavor_option, 'flavor', 'keyName')
39+
if not flavor_key_name.startswith(flavor_key):
40+
continue
3541

36-
ded_cpu = [x for x in result['processors']
37-
if x['template'].get('dedicatedAccountHostOnlyFlag',
38-
False)]
42+
flavors.append(flavor_key_name)
3943

40-
def add_cpus_row(cpu_options, name):
41-
"""Add CPU rows to the table."""
42-
cpus = []
43-
for cpu_option in cpu_options:
44-
cpus.append(str(cpu_option['template']['startCpus']))
44+
if len(flavors) > 0:
45+
table.add_row(['flavors (%s)' % flavor_label,
46+
formatting.listing(flavors, separator='\n')])
4547

46-
table.add_row(['cpus (%s)' % name,
47-
formatting.listing(cpus, separator=',')])
48+
if result.get('flavors', None):
49+
_add_flavor_rows('B1', 'balanced', result['flavors'])
50+
_add_flavor_rows('BL1', 'balanced local - hdd', result['flavors'])
51+
_add_flavor_rows('BL2', 'balanced local - ssd', result['flavors'])
52+
_add_flavor_rows('C1', 'compute', result['flavors'])
53+
_add_flavor_rows('M1', 'memory', result['flavors'])
4854

49-
add_cpus_row(ded_cpu, 'private')
50-
add_cpus_row(standard_cpu, 'standard')
55+
# CPUs
56+
standard_cpus = [int(x['template']['startCpus']) for x in result['processors']
57+
if not x['template'].get('dedicatedAccountHostOnlyFlag',
58+
False)
59+
and not x['template'].get('dedicatedHost', None)]
60+
ded_cpus = [int(x['template']['startCpus']) for x in result['processors']
61+
if x['template'].get('dedicatedAccountHostOnlyFlag', False)]
62+
ded_host_cpus = [int(x['template']['startCpus']) for x in result['processors']
63+
if x['template'].get('dedicatedHost', None)]
64+
65+
standard_cpus = sorted(standard_cpus)
66+
table.add_row(['cpus (standard)', formatting.listing(standard_cpus, separator=',')])
67+
ded_cpus = sorted(ded_cpus)
68+
table.add_row(['cpus (dedicated)', formatting.listing(ded_cpus, separator=',')])
69+
ded_host_cpus = sorted(ded_host_cpus)
70+
table.add_row(['cpus (dedicated host)', formatting.listing(ded_host_cpus, separator=',')])
5171

5272
# Memory
53-
memory = [str(m['template']['maxMemory']) for m in result['memory']]
73+
memory = [int(m['template']['maxMemory']) for m in result['memory']
74+
if not m['itemPrice'].get('dedicatedHostInstanceFlag', False)]
75+
ded_host_memory = [int(m['template']['maxMemory']) for m in result['memory']
76+
if m['itemPrice'].get('dedicatedHostInstanceFlag', False)]
77+
78+
memory = sorted(memory)
5479
table.add_row(['memory',
5580
formatting.listing(memory, separator=',')])
5681

82+
ded_host_memory = sorted(ded_host_memory)
83+
table.add_row(['memory (dedicated host)',
84+
formatting.listing(ded_host_memory, separator=',')])
85+
5786
# Operating Systems
5887
op_sys = [o['template']['operatingSystemReferenceCode'] for o in
5988
result['operatingSystems']]
@@ -73,7 +102,14 @@ def add_cpus_row(cpu_options, name):
73102

74103
# Disk
75104
local_disks = [x for x in result['blockDevices']
76-
if x['template'].get('localDiskFlag', False)]
105+
if x['template'].get('localDiskFlag', False)
106+
and not x['itemPrice'].get('dedicatedHostInstanceFlag',
107+
False)]
108+
109+
ded_host_local_disks = [x for x in result['blockDevices']
110+
if x['template'].get('localDiskFlag', False)
111+
and x['itemPrice'].get('dedicatedHostInstanceFlag',
112+
False)]
77113

78114
san_disks = [x for x in result['blockDevices']
79115
if not x['template'].get('localDiskFlag', False)]
@@ -95,17 +131,37 @@ def add_block_rows(disks, name):
95131
formatting.listing(simple[label],
96132
separator=',')])
97133

98-
add_block_rows(local_disks, 'local')
99134
add_block_rows(san_disks, 'san')
135+
add_block_rows(local_disks, 'local')
136+
add_block_rows(ded_host_local_disks, 'local (dedicated host)')
100137

101138
# Network
102139
speeds = []
103-
for comp in result['networkComponents']:
104-
speed = comp['template']['networkComponents'][0]['maxSpeed']
105-
speeds.append(str(speed))
140+
ded_host_speeds = []
141+
for option in result['networkComponents']:
142+
template = option.get('template', None)
143+
price = option.get('itemPrice', None)
144+
145+
if not template or not price \
146+
or not template.get('networkComponents', None):
147+
continue
148+
149+
if not template['networkComponents'][0] \
150+
or not template['networkComponents'][0].get('maxSpeed', None):
151+
continue
152+
153+
max_speed = str(template['networkComponents'][0]['maxSpeed'])
154+
if price.get('dedicatedHostInstanceFlag', False) \
155+
and max_speed not in ded_host_speeds:
156+
ded_host_speeds.append(max_speed)
157+
elif max_speed not in speeds:
158+
speeds.append(max_speed)
106159

107160
speeds = sorted(speeds)
108-
109161
table.add_row(['nic', formatting.listing(speeds, separator=',')])
110162

163+
ded_host_speeds = sorted(ded_host_speeds)
164+
table.add_row(['nic (dedicated host)',
165+
formatting.listing(ded_host_speeds, separator=',')])
166+
111167
env.fout(table)

0 commit comments

Comments
 (0)