diff --git a/SoftLayer/CLI/virt/create_options.py b/SoftLayer/CLI/virt/create_options.py index 601c0f3ac..693de74a2 100644 --- a/SoftLayer/CLI/virt/create_options.py +++ b/SoftLayer/CLI/virt/create_options.py @@ -1,9 +1,6 @@ """Virtual server order options.""" # :license: MIT, see LICENSE for more details. # pylint: disable=too-many-statements -import os -import os.path - import click import SoftLayer @@ -18,82 +15,150 @@ def cli(env): """Virtual server order options.""" vsi = SoftLayer.VSManager(env.client) - result = vsi.get_create_options() + options = vsi.get_create_options() + + tables = [ + _get_datacenter_table(options), + _get_flavors_table(options), + _get_cpu_table(options), + _get_memory_table(options), + _get_os_table(options), + _get_disk_table(options), + _get_network_table(options), + ] + + env.fout(formatting.listing(tables, separator='\n')) - table = formatting.KeyValueTable(['name', 'value']) - table.align['name'] = 'r' - table.align['value'] = 'l' - # Datacenters +def _get_datacenter_table(create_options): datacenters = [dc['template']['datacenter']['name'] - for dc in result['datacenters']] + for dc in create_options['datacenters']] + datacenters = sorted(datacenters) - table.add_row(['datacenter', - formatting.listing(datacenters, separator='\n')]) + dc_table = formatting.Table(['datacenter'], title='Datacenters') + dc_table.sortby = 'datacenter' + dc_table.align = 'l' + for datacenter in datacenters: + dc_table.add_row([datacenter]) + return dc_table + + +def _get_flavors_table(create_options): + flavor_table = formatting.Table(['flavor', 'value'], title='Flavors') + flavor_table.sortby = 'flavor' + flavor_table.align = 'l' + grouping = { + 'balanced': {'key_starts_with': 'B1', 'flavors': []}, + 'balanced local - hdd': {'key_starts_with': 'BL1', 'flavors': []}, + 'balanced local - ssd': {'key_starts_with': 'BL2', 'flavors': []}, + 'compute': {'key_starts_with': 'C1', 'flavors': []}, + 'memory': {'key_starts_with': 'M1', 'flavors': []}, + 'GPU': {'key_starts_with': 'AC', 'flavors': []}, + 'transient': {'transient': True, 'flavors': []}, + } + + if create_options.get('flavors', None) is None: + return flavor_table + + for flavor_option in create_options['flavors']: + flavor_key_name = utils.lookup(flavor_option, 'flavor', 'keyName') - _add_flavors_to_table(result, table) + for name, group in grouping.items(): + if utils.lookup(flavor_option, 'template', 'transientGuestFlag') is True: + if utils.lookup(group, 'transient') is True: + group['flavors'].append(flavor_key_name) + break - # CPUs - standard_cpus = [int(x['template']['startCpus']) for x in result['processors'] + elif utils.lookup(group, 'key_starts_with') is not None \ + and flavor_key_name.startswith(group['key_starts_with']): + group['flavors'].append(flavor_key_name) + break + + for name, group in grouping.items(): + if len(group['flavors']) > 0: + flavor_table.add_row(['{}'.format(name), + formatting.listing(group['flavors'], + separator='\n')]) + return flavor_table + + +def _get_cpu_table(create_options): + cpu_table = formatting.Table(['cpu', 'value'], title='CPUs') + cpu_table.sortby = 'cpu' + cpu_table.align = 'l' + standard_cpus = [int(x['template']['startCpus']) for x in create_options['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'] + ded_cpus = [int(x['template']['startCpus']) for x in create_options['processors'] if x['template'].get('dedicatedAccountHostOnlyFlag', False)] - ded_host_cpus = [int(x['template']['startCpus']) for x in result['processors'] + ded_host_cpus = [int(x['template']['startCpus']) for x in create_options['processors'] if x['template'].get('dedicatedHost', None)] standard_cpus = sorted(standard_cpus) - table.add_row(['cpus (standard)', formatting.listing(standard_cpus, separator=',')]) + cpu_table.add_row(['standard', formatting.listing(standard_cpus, separator=',')]) ded_cpus = sorted(ded_cpus) - table.add_row(['cpus (dedicated)', formatting.listing(ded_cpus, separator=',')]) + cpu_table.add_row(['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=',')]) + cpu_table.add_row(['dedicated host', formatting.listing(ded_host_cpus, separator=',')]) + return cpu_table - # Memory - memory = [int(m['template']['maxMemory']) for m in result['memory'] + +def _get_memory_table(create_options): + memory_table = formatting.Table(['memory', 'value'], title='Memories') + memory_table.sortby = 'memory' + memory_table.align = 'l' + memory = [int(m['template']['maxMemory']) for m in create_options['memory'] if not m['itemPrice'].get('dedicatedHostInstanceFlag', False)] - ded_host_memory = [int(m['template']['maxMemory']) for m in result['memory'] + ded_host_memory = [int(m['template']['maxMemory']) for m in create_options['memory'] if m['itemPrice'].get('dedicatedHostInstanceFlag', False)] memory = sorted(memory) - table.add_row(['memory', - formatting.listing(memory, separator=',')]) + memory_table.add_row(['standard', + 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']] - - op_sys = sorted(op_sys) - os_summary = set() + memory_table.add_row(['dedicated host', + formatting.listing(ded_host_memory, separator=',')]) + return memory_table + + +def _get_os_table(create_options): + os_table = formatting.Table(['KeyName', 'Description'], title='Operating Systems') + os_table.sortby = 'KeyName' + os_table.align = 'l' + op_sys = [] + for operating_system in create_options['operatingSystems']: + os_option = { + 'referenceCode': operating_system['template']['operatingSystemReferenceCode'], + 'description': operating_system['itemPrice']['item']['description'] + } + op_sys.append(os_option) for operating_system in op_sys: - os_summary.add(operating_system[0:operating_system.find('_')]) - - for summary in sorted(os_summary): - table.add_row([ - 'os (%s)' % summary, - os.linesep.join(sorted([x for x in op_sys - if x[0:len(summary)] == summary])) + os_table.add_row([ + operating_system['referenceCode'], + operating_system['description'] ]) + return os_table + - # Disk - local_disks = [x for x in result['blockDevices'] +def _get_disk_table(create_options): + disk_table = formatting.Table(['disk', 'value'], title='Disks') + disk_table.sortby = 'disk' + disk_table.align = 'l' + local_disks = [x for x in create_options['blockDevices'] if x['template'].get('localDiskFlag', False) and not x['itemPrice'].get('dedicatedHostInstanceFlag', False)] - ded_host_local_disks = [x for x in result['blockDevices'] + ded_host_local_disks = [x for x in create_options['blockDevices'] if x['template'].get('localDiskFlag', False) and x['itemPrice'].get('dedicatedHostInstanceFlag', False)] - san_disks = [x for x in result['blockDevices'] + san_disks = [x for x in create_options['blockDevices'] if not x['template'].get('localDiskFlag', False)] def add_block_rows(disks, name): @@ -109,18 +174,23 @@ def add_block_rows(disks, name): simple[bid].append(str(block['diskImage']['capacity'])) for label in sorted(simple): - table.add_row(['%s disk(%s)' % (name, label), - formatting.listing(simple[label], - separator=',')]) + disk_table.add_row(['%s disk(%s)' % (name, label), + formatting.listing(simple[label], + separator=',')]) add_block_rows(san_disks, 'san') add_block_rows(local_disks, 'local') add_block_rows(ded_host_local_disks, 'local (dedicated host)') + return disk_table - # Network + +def _get_network_table(create_options): + network_table = formatting.Table(['network', 'value'], title='Network') + network_table.sortby = 'network' + network_table.align = 'l' speeds = [] ded_host_speeds = [] - for option in result['networkComponents']: + for option in create_options['networkComponents']: template = option.get('template', None) price = option.get('itemPrice', None) @@ -140,43 +210,9 @@ def add_block_rows(disks, name): speeds.append(max_speed) speeds = sorted(speeds) - table.add_row(['nic', formatting.listing(speeds, separator=',')]) + network_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) - - -def _add_flavors_to_table(result, table): - grouping = { - 'balanced': {'key_starts_with': 'B1', 'flavors': []}, - 'balanced local - hdd': {'key_starts_with': 'BL1', 'flavors': []}, - 'balanced local - ssd': {'key_starts_with': 'BL2', 'flavors': []}, - 'compute': {'key_starts_with': 'C1', 'flavors': []}, - 'memory': {'key_starts_with': 'M1', 'flavors': []}, - 'GPU': {'key_starts_with': 'AC', 'flavors': []}, - 'transient': {'transient': True, 'flavors': []}, - } - - if result.get('flavors', None) is None: - return - - for flavor_option in result['flavors']: - flavor_key_name = utils.lookup(flavor_option, 'flavor', 'keyName') - - for name, group in grouping.items(): - if utils.lookup(flavor_option, 'template', 'transientGuestFlag') is True: - if utils.lookup(group, 'transient') is True: - group['flavors'].append(flavor_key_name) - break - - elif utils.lookup(group, 'key_starts_with') is not None \ - and flavor_key_name.startswith(group['key_starts_with']): - group['flavors'].append(flavor_key_name) - break - - for name, group in grouping.items(): - if len(group['flavors']) > 0: - table.add_row(['flavors (%s)' % name, formatting.listing(group['flavors'], separator='\n')]) + network_table.add_row(['nic (dedicated host)', + formatting.listing(ded_host_speeds, separator=',')]) + return network_table diff --git a/tests/CLI/modules/vs/vs_tests.py b/tests/CLI/modules/vs/vs_tests.py index 94b913923..06d3147ac 100644 --- a/tests/CLI/modules/vs/vs_tests.py +++ b/tests/CLI/modules/vs/vs_tests.py @@ -315,28 +315,13 @@ def test_detail_vs_ptr_error(self): def test_create_options(self): result = self.run_command(['vs', 'create-options']) - self.assert_no_fail(result) - self.assertEqual({'cpus (dedicated host)': [4, 56], - 'cpus (dedicated)': [1], - 'cpus (standard)': [1, 2, 3, 4], - 'datacenter': ['ams01', 'dal05'], - 'flavors (balanced)': ['B1_1X2X25', 'B1_1X2X100'], - 'flavors (balanced local - hdd)': ['BL1_1X2X100'], - 'flavors (balanced local - ssd)': ['BL2_1X2X100'], - 'flavors (compute)': ['C1_1X2X25'], - 'flavors (memory)': ['M1_1X2X100'], - 'flavors (GPU)': ['AC1_1X2X100', 'ACL1_1X2X100'], - 'flavors (transient)': ['B1_1X2X25_TRANSIENT'], - 'local disk(0)': ['25', '100'], - 'memory': [1024, 2048, 3072, 4096], - 'memory (dedicated host)': [8192, 65536], - 'nic': ['10', '100', '1000'], - 'nic (dedicated host)': ['1000'], - 'os (CENTOS)': 'CENTOS_6_64', - 'os (DEBIAN)': 'DEBIAN_7_64', - 'os (UBUNTU)': 'UBUNTU_12_64'}, - json.loads(result.output)) + self.assertIn('datacenter', result.output) + self.assertIn('flavor', result.output) + self.assertIn('memory', result.output) + self.assertIn('cpu', result.output) + self.assertIn('OS', result.output) + self.assertIn('network', result.output) @mock.patch('SoftLayer.CLI.formatting.confirm') def test_dns_sync_both(self, confirm_mock): @@ -357,19 +342,19 @@ def test_dns_sync_both(self, confirm_mock): 'getResourceRecords') getResourceRecords.return_value = [] createAargs = ({ - 'type': 'a', - 'host': 'vs-test1', - 'domainId': 12345, # from SoftLayer_Account::getDomains - 'data': '172.16.240.2', - 'ttl': 7200 - },) + 'type': 'a', + 'host': 'vs-test1', + 'domainId': 12345, # from SoftLayer_Account::getDomains + 'data': '172.16.240.2', + 'ttl': 7200 + },) createPTRargs = ({ - 'type': 'ptr', - 'host': '2', - 'domainId': 123456, - 'data': 'vs-test1.test.sftlyr.ws', - 'ttl': 7200 - },) + 'type': 'ptr', + 'host': '2', + 'domainId': 123456, + 'data': 'vs-test1.test.sftlyr.ws', + 'ttl': 7200 + },) result = self.run_command(['vs', 'dns-sync', '100']) @@ -412,12 +397,12 @@ def test_dns_sync_v6(self, confirm_mock): } } createV6args = ({ - 'type': 'aaaa', - 'host': 'vs-test1', - 'domainId': 12345, - 'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004', - 'ttl': 7200 - },) + 'type': 'aaaa', + 'host': 'vs-test1', + 'domainId': 12345, + 'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004', + 'ttl': 7200 + },) guest.return_value = test_guest result = self.run_command(['vs', 'dns-sync', '--aaaa-record', '100']) self.assert_no_fail(result)