Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions SoftLayer/CLI/virt/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ def _update_with_like_args(ctx, _, value):
like_args = {
'hostname': like_details['hostname'],
'domain': like_details['domain'],
'cpu': like_details['maxCpu'],
'memory': '%smb' % like_details['maxMemory'],
'hourly': like_details['hourlyBillingFlag'],
'datacenter': like_details['datacenter']['name'],
'network': like_details['networkComponents'][0]['maxSpeed'],
Expand All @@ -36,6 +34,15 @@ def _update_with_like_args(ctx, _, value):
'private': like_details['privateNetworkOnlyFlag'],
}

like_args['flavor'] = utils.lookup(like_details,
'billingItem',
'orderItem',
'preset',
'keyName')
if not like_args['flavor']:
like_args['cpu'] = like_details['maxCpu']
like_args['memory'] = '%smb' % like_details['maxMemory']

tag_refs = like_details.get('tagReferences', None)
if tag_refs is not None and len(tag_refs) > 0:
like_args['tag'] = [t['tag']['name'] for t in tag_refs]
Expand Down Expand Up @@ -66,16 +73,22 @@ def _parse_create_args(client, args):
"""
data = {
"hourly": args['billing'] == 'hourly',
"cpus": args['cpu'],
"domain": args['domain'],
"hostname": args['hostname'],
"private": args['private'],
"dedicated": args['dedicated'],
"disks": args['disk'],
"local_disk": not args['san'],
"cpus": args.get('cpu', None),
"memory": args.get('memory', None),
"flavor": args.get('flavor', None)
}

data["memory"] = args['memory']
# The primary disk is included in the flavor and the local_disk flag is not needed
# Setting it to None prevents errors from the flag not matching the flavor
if not args.get('san') and args.get('flavor'):
data['local_disk'] = None
else:
data['local_disk'] = not args['san']

if args.get('os'):
data['os_code'] = args['os']
Expand Down Expand Up @@ -130,6 +143,9 @@ def _parse_create_args(client, args):
if args.get('tag'):
data['tags'] = ','.join(args['tag'])

if args.get('host_id'):
data['host_id'] = args['host_id']

return data


Expand All @@ -143,15 +159,14 @@ def _parse_create_args(client, args):
required=True,
prompt=True)
@click.option('--cpu', '-c',
help="Number of CPU cores",
type=click.INT,
required=True,
prompt=True)
help="Number of CPU cores (not available with flavors)",
type=click.INT)
@click.option('--memory', '-m',
help="Memory in mebibytes",
type=virt.MEM_TYPE,
required=True,
prompt=True)
help="Memory in mebibytes (not available with flavors)",
type=virt.MEM_TYPE)
@click.option('--flavor', '-f',
help="Public Virtual Server flavor key name",
type=click.STRING)
@click.option('--datacenter', '-d',
help="Datacenter shortname",
required=True,
Expand All @@ -167,7 +182,10 @@ def _parse_create_args(client, args):
help="Billing rate")
@click.option('--dedicated/--public',
is_flag=True,
help="Create a dedicated Virtual Server (Private Node)")
help="Create a Dedicated Virtual Server")
@click.option('--host-id',
type=click.INT,
help="Host Id to provision a Dedicated Host Virtual Server onto")
@click.option('--san',
is_flag=True,
help="Use SAN storage instead of local disk.")
Expand Down Expand Up @@ -305,6 +323,22 @@ def cli(env, **args):
def _validate_args(env, args):
"""Raises an ArgumentError if the given arguments are not valid."""

if all([args['cpu'], args['flavor']]):
raise exceptions.ArgumentError(
'[-c | --cpu] not allowed with [-f | --flavor]')

if all([args['memory'], args['flavor']]):
raise exceptions.ArgumentError(
'[-m | --memory] not allowed with [-f | --flavor]')

if all([args['dedicated'], args['flavor']]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also include one for host-id and flavor right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Adding.

raise exceptions.ArgumentError(
'[-d | --dedicated] not allowed with [-f | --flavor]')

if all([args['host_id'], args['flavor']]):
raise exceptions.ArgumentError(
'[-h | --host-id] not allowed with [-f | --flavor]')

if all([args['userdata'], args['userfile']]):
raise exceptions.ArgumentError(
'[-u | --userdata] not allowed with [-F | --userfile]')
Expand Down
102 changes: 79 additions & 23 deletions SoftLayer/CLI/virt/create_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer import utils


@click.command()
Expand All @@ -25,35 +26,63 @@ def cli(env):
# Datacenters
datacenters = [dc['template']['datacenter']['name']
for dc in result['datacenters']]
datacenters = sorted(datacenters)

table.add_row(['datacenter',
formatting.listing(datacenters, separator='\n')])

# CPUs
standard_cpu = [x for x in result['processors']
if not x['template'].get(
'dedicatedAccountHostOnlyFlag', False)]
def _add_flavor_rows(flavor_key, flavor_label, flavor_options):
flavors = []

for flavor_option in flavor_options:
flavor_key_name = utils.lookup(flavor_option, 'flavor', 'keyName')
if not flavor_key_name.startswith(flavor_key):
continue

ded_cpu = [x for x in result['processors']
if x['template'].get('dedicatedAccountHostOnlyFlag',
False)]
flavors.append(flavor_key_name)

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

table.add_row(['cpus (%s)' % name,
formatting.listing(cpus, separator=',')])
if result.get('flavors', None):
_add_flavor_rows('B1', 'balanced', result['flavors'])
_add_flavor_rows('BL1', 'balanced local - hdd', result['flavors'])
_add_flavor_rows('BL2', 'balanced local - ssd', result['flavors'])
_add_flavor_rows('C1', 'compute', result['flavors'])
_add_flavor_rows('M1', 'memory', result['flavors'])

add_cpus_row(ded_cpu, 'private')
add_cpus_row(standard_cpu, 'standard')
# CPUs
standard_cpus = [int(x['template']['startCpus']) for x in result['processors']
if not x['template'].get('dedicatedAccountHostOnlyFlag',
False)
and not x['template'].get('dedicatedHost', None)]
ded_cpus = [int(x['template']['startCpus']) for x in result['processors']
if x['template'].get('dedicatedAccountHostOnlyFlag', False)]
ded_host_cpus = [int(x['template']['startCpus']) for x in result['processors']
if x['template'].get('dedicatedHost', None)]

standard_cpus = sorted(standard_cpus)
table.add_row(['cpus (standard)', formatting.listing(standard_cpus, separator=',')])
ded_cpus = sorted(ded_cpus)
table.add_row(['cpus (dedicated)', formatting.listing(ded_cpus, separator=',')])
ded_host_cpus = sorted(ded_host_cpus)
table.add_row(['cpus (dedicated host)', formatting.listing(ded_host_cpus, separator=',')])

# Memory
memory = [str(m['template']['maxMemory']) for m in result['memory']]
memory = [int(m['template']['maxMemory']) for m in result['memory']
if not m['itemPrice'].get('dedicatedHostInstanceFlag', False)]
ded_host_memory = [int(m['template']['maxMemory']) for m in result['memory']
if m['itemPrice'].get('dedicatedHostInstanceFlag', False)]

memory = sorted(memory)
table.add_row(['memory',
formatting.listing(memory, separator=',')])

ded_host_memory = sorted(ded_host_memory)
table.add_row(['memory (dedicated host)',
formatting.listing(ded_host_memory, separator=',')])

# Operating Systems
op_sys = [o['template']['operatingSystemReferenceCode'] for o in
result['operatingSystems']]
Expand All @@ -73,7 +102,14 @@ def add_cpus_row(cpu_options, name):

# Disk
local_disks = [x for x in result['blockDevices']
if x['template'].get('localDiskFlag', False)]
if x['template'].get('localDiskFlag', False)
and not x['itemPrice'].get('dedicatedHostInstanceFlag',
False)]

ded_host_local_disks = [x for x in result['blockDevices']
if x['template'].get('localDiskFlag', False)
and x['itemPrice'].get('dedicatedHostInstanceFlag',
False)]

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

add_block_rows(local_disks, 'local')
add_block_rows(san_disks, 'san')
add_block_rows(local_disks, 'local')
add_block_rows(ded_host_local_disks, 'local (dedicated host)')

# Network
speeds = []
for comp in result['networkComponents']:
speed = comp['template']['networkComponents'][0]['maxSpeed']
speeds.append(str(speed))
ded_host_speeds = []
for option in result['networkComponents']:
template = option.get('template', None)
price = option.get('itemPrice', None)

if not template or not price \
or not template.get('networkComponents', None):
continue

if not template['networkComponents'][0] \
or not template['networkComponents'][0].get('maxSpeed', None):
continue

max_speed = str(template['networkComponents'][0]['maxSpeed'])
if price.get('dedicatedHostInstanceFlag', False) \
and max_speed not in ded_host_speeds:
ded_host_speeds.append(max_speed)
elif max_speed not in speeds:
speeds.append(max_speed)

speeds = sorted(speeds)

table.add_row(['nic', formatting.listing(speeds, separator=',')])

ded_host_speeds = sorted(ded_host_speeds)
table.add_row(['nic (dedicated host)',
formatting.listing(ded_host_speeds, separator=',')])

env.fout(table)
Loading