Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c623829

Browse files
Merge pull request #1310 from ATGE/issues1302
#1302 fix lots of whitespace slcli vs create-options
2 parents c64ade3 + 9ff03c0 commit c623829

File tree

2 files changed

+147
-126
lines changed

2 files changed

+147
-126
lines changed

SoftLayer/CLI/virt/create_options.py

Lines changed: 123 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
"""Virtual server order options."""
22
# :license: MIT, see LICENSE for more details.
33
# pylint: disable=too-many-statements
4-
import os
5-
import os.path
6-
74
import click
85

96
import SoftLayer
@@ -18,82 +15,150 @@ def cli(env):
1815
"""Virtual server order options."""
1916

2017
vsi = SoftLayer.VSManager(env.client)
21-
result = vsi.get_create_options()
18+
options = vsi.get_create_options()
19+
20+
tables = [
21+
_get_datacenter_table(options),
22+
_get_flavors_table(options),
23+
_get_cpu_table(options),
24+
_get_memory_table(options),
25+
_get_os_table(options),
26+
_get_disk_table(options),
27+
_get_network_table(options),
28+
]
29+
30+
env.fout(formatting.listing(tables, separator='\n'))
2231

23-
table = formatting.KeyValueTable(['name', 'value'])
24-
table.align['name'] = 'r'
25-
table.align['value'] = 'l'
2632

27-
# Datacenters
33+
def _get_datacenter_table(create_options):
2834
datacenters = [dc['template']['datacenter']['name']
29-
for dc in result['datacenters']]
35+
for dc in create_options['datacenters']]
36+
3037
datacenters = sorted(datacenters)
3138

32-
table.add_row(['datacenter',
33-
formatting.listing(datacenters, separator='\n')])
39+
dc_table = formatting.Table(['datacenter'], title='Datacenters')
40+
dc_table.sortby = 'datacenter'
41+
dc_table.align = 'l'
42+
for datacenter in datacenters:
43+
dc_table.add_row([datacenter])
44+
return dc_table
45+
46+
47+
def _get_flavors_table(create_options):
48+
flavor_table = formatting.Table(['flavor', 'value'], title='Flavors')
49+
flavor_table.sortby = 'flavor'
50+
flavor_table.align = 'l'
51+
grouping = {
52+
'balanced': {'key_starts_with': 'B1', 'flavors': []},
53+
'balanced local - hdd': {'key_starts_with': 'BL1', 'flavors': []},
54+
'balanced local - ssd': {'key_starts_with': 'BL2', 'flavors': []},
55+
'compute': {'key_starts_with': 'C1', 'flavors': []},
56+
'memory': {'key_starts_with': 'M1', 'flavors': []},
57+
'GPU': {'key_starts_with': 'AC', 'flavors': []},
58+
'transient': {'transient': True, 'flavors': []},
59+
}
60+
61+
if create_options.get('flavors', None) is None:
62+
return flavor_table
63+
64+
for flavor_option in create_options['flavors']:
65+
flavor_key_name = utils.lookup(flavor_option, 'flavor', 'keyName')
3466

35-
_add_flavors_to_table(result, table)
67+
for name, group in grouping.items():
68+
if utils.lookup(flavor_option, 'template', 'transientGuestFlag') is True:
69+
if utils.lookup(group, 'transient') is True:
70+
group['flavors'].append(flavor_key_name)
71+
break
3672

37-
# CPUs
38-
standard_cpus = [int(x['template']['startCpus']) for x in result['processors']
73+
elif utils.lookup(group, 'key_starts_with') is not None \
74+
and flavor_key_name.startswith(group['key_starts_with']):
75+
group['flavors'].append(flavor_key_name)
76+
break
77+
78+
for name, group in grouping.items():
79+
if len(group['flavors']) > 0:
80+
flavor_table.add_row(['{}'.format(name),
81+
formatting.listing(group['flavors'],
82+
separator='\n')])
83+
return flavor_table
84+
85+
86+
def _get_cpu_table(create_options):
87+
cpu_table = formatting.Table(['cpu', 'value'], title='CPUs')
88+
cpu_table.sortby = 'cpu'
89+
cpu_table.align = 'l'
90+
standard_cpus = [int(x['template']['startCpus']) for x in create_options['processors']
3991
if not x['template'].get('dedicatedAccountHostOnlyFlag',
4092
False)
4193
and not x['template'].get('dedicatedHost', None)]
42-
ded_cpus = [int(x['template']['startCpus']) for x in result['processors']
94+
ded_cpus = [int(x['template']['startCpus']) for x in create_options['processors']
4395
if x['template'].get('dedicatedAccountHostOnlyFlag', False)]
44-
ded_host_cpus = [int(x['template']['startCpus']) for x in result['processors']
96+
ded_host_cpus = [int(x['template']['startCpus']) for x in create_options['processors']
4597
if x['template'].get('dedicatedHost', None)]
4698

4799
standard_cpus = sorted(standard_cpus)
48-
table.add_row(['cpus (standard)', formatting.listing(standard_cpus, separator=',')])
100+
cpu_table.add_row(['standard', formatting.listing(standard_cpus, separator=',')])
49101
ded_cpus = sorted(ded_cpus)
50-
table.add_row(['cpus (dedicated)', formatting.listing(ded_cpus, separator=',')])
102+
cpu_table.add_row(['dedicated', formatting.listing(ded_cpus, separator=',')])
51103
ded_host_cpus = sorted(ded_host_cpus)
52-
table.add_row(['cpus (dedicated host)', formatting.listing(ded_host_cpus, separator=',')])
104+
cpu_table.add_row(['dedicated host', formatting.listing(ded_host_cpus, separator=',')])
105+
return cpu_table
53106

54-
# Memory
55-
memory = [int(m['template']['maxMemory']) for m in result['memory']
107+
108+
def _get_memory_table(create_options):
109+
memory_table = formatting.Table(['memory', 'value'], title='Memories')
110+
memory_table.sortby = 'memory'
111+
memory_table.align = 'l'
112+
memory = [int(m['template']['maxMemory']) for m in create_options['memory']
56113
if not m['itemPrice'].get('dedicatedHostInstanceFlag', False)]
57-
ded_host_memory = [int(m['template']['maxMemory']) for m in result['memory']
114+
ded_host_memory = [int(m['template']['maxMemory']) for m in create_options['memory']
58115
if m['itemPrice'].get('dedicatedHostInstanceFlag', False)]
59116

60117
memory = sorted(memory)
61-
table.add_row(['memory',
62-
formatting.listing(memory, separator=',')])
118+
memory_table.add_row(['standard',
119+
formatting.listing(memory, separator=',')])
63120

64121
ded_host_memory = sorted(ded_host_memory)
65-
table.add_row(['memory (dedicated host)',
66-
formatting.listing(ded_host_memory, separator=',')])
67-
68-
# Operating Systems
69-
op_sys = [o['template']['operatingSystemReferenceCode'] for o in
70-
result['operatingSystems']]
71-
72-
op_sys = sorted(op_sys)
73-
os_summary = set()
122+
memory_table.add_row(['dedicated host',
123+
formatting.listing(ded_host_memory, separator=',')])
124+
return memory_table
125+
126+
127+
def _get_os_table(create_options):
128+
os_table = formatting.Table(['KeyName', 'Description'], title='Operating Systems')
129+
os_table.sortby = 'KeyName'
130+
os_table.align = 'l'
131+
op_sys = []
132+
for operating_system in create_options['operatingSystems']:
133+
os_option = {
134+
'referenceCode': operating_system['template']['operatingSystemReferenceCode'],
135+
'description': operating_system['itemPrice']['item']['description']
136+
}
137+
op_sys.append(os_option)
74138

75139
for operating_system in op_sys:
76-
os_summary.add(operating_system[0:operating_system.find('_')])
77-
78-
for summary in sorted(os_summary):
79-
table.add_row([
80-
'os (%s)' % summary,
81-
os.linesep.join(sorted([x for x in op_sys
82-
if x[0:len(summary)] == summary]))
140+
os_table.add_row([
141+
operating_system['referenceCode'],
142+
operating_system['description']
83143
])
144+
return os_table
145+
84146

85-
# Disk
86-
local_disks = [x for x in result['blockDevices']
147+
def _get_disk_table(create_options):
148+
disk_table = formatting.Table(['disk', 'value'], title='Disks')
149+
disk_table.sortby = 'disk'
150+
disk_table.align = 'l'
151+
local_disks = [x for x in create_options['blockDevices']
87152
if x['template'].get('localDiskFlag', False)
88153
and not x['itemPrice'].get('dedicatedHostInstanceFlag',
89154
False)]
90155

91-
ded_host_local_disks = [x for x in result['blockDevices']
156+
ded_host_local_disks = [x for x in create_options['blockDevices']
92157
if x['template'].get('localDiskFlag', False)
93158
and x['itemPrice'].get('dedicatedHostInstanceFlag',
94159
False)]
95160

96-
san_disks = [x for x in result['blockDevices']
161+
san_disks = [x for x in create_options['blockDevices']
97162
if not x['template'].get('localDiskFlag', False)]
98163

99164
def add_block_rows(disks, name):
@@ -109,18 +174,23 @@ def add_block_rows(disks, name):
109174
simple[bid].append(str(block['diskImage']['capacity']))
110175

111176
for label in sorted(simple):
112-
table.add_row(['%s disk(%s)' % (name, label),
113-
formatting.listing(simple[label],
114-
separator=',')])
177+
disk_table.add_row(['%s disk(%s)' % (name, label),
178+
formatting.listing(simple[label],
179+
separator=',')])
115180

116181
add_block_rows(san_disks, 'san')
117182
add_block_rows(local_disks, 'local')
118183
add_block_rows(ded_host_local_disks, 'local (dedicated host)')
184+
return disk_table
119185

120-
# Network
186+
187+
def _get_network_table(create_options):
188+
network_table = formatting.Table(['network', 'value'], title='Network')
189+
network_table.sortby = 'network'
190+
network_table.align = 'l'
121191
speeds = []
122192
ded_host_speeds = []
123-
for option in result['networkComponents']:
193+
for option in create_options['networkComponents']:
124194
template = option.get('template', None)
125195
price = option.get('itemPrice', None)
126196

@@ -140,43 +210,9 @@ def add_block_rows(disks, name):
140210
speeds.append(max_speed)
141211

142212
speeds = sorted(speeds)
143-
table.add_row(['nic', formatting.listing(speeds, separator=',')])
213+
network_table.add_row(['nic', formatting.listing(speeds, separator=',')])
144214

145215
ded_host_speeds = sorted(ded_host_speeds)
146-
table.add_row(['nic (dedicated host)',
147-
formatting.listing(ded_host_speeds, separator=',')])
148-
149-
env.fout(table)
150-
151-
152-
def _add_flavors_to_table(result, table):
153-
grouping = {
154-
'balanced': {'key_starts_with': 'B1', 'flavors': []},
155-
'balanced local - hdd': {'key_starts_with': 'BL1', 'flavors': []},
156-
'balanced local - ssd': {'key_starts_with': 'BL2', 'flavors': []},
157-
'compute': {'key_starts_with': 'C1', 'flavors': []},
158-
'memory': {'key_starts_with': 'M1', 'flavors': []},
159-
'GPU': {'key_starts_with': 'AC', 'flavors': []},
160-
'transient': {'transient': True, 'flavors': []},
161-
}
162-
163-
if result.get('flavors', None) is None:
164-
return
165-
166-
for flavor_option in result['flavors']:
167-
flavor_key_name = utils.lookup(flavor_option, 'flavor', 'keyName')
168-
169-
for name, group in grouping.items():
170-
if utils.lookup(flavor_option, 'template', 'transientGuestFlag') is True:
171-
if utils.lookup(group, 'transient') is True:
172-
group['flavors'].append(flavor_key_name)
173-
break
174-
175-
elif utils.lookup(group, 'key_starts_with') is not None \
176-
and flavor_key_name.startswith(group['key_starts_with']):
177-
group['flavors'].append(flavor_key_name)
178-
break
179-
180-
for name, group in grouping.items():
181-
if len(group['flavors']) > 0:
182-
table.add_row(['flavors (%s)' % name, formatting.listing(group['flavors'], separator='\n')])
216+
network_table.add_row(['nic (dedicated host)',
217+
formatting.listing(ded_host_speeds, separator=',')])
218+
return network_table

tests/CLI/modules/vs/vs_tests.py

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -315,28 +315,13 @@ def test_detail_vs_ptr_error(self):
315315

316316
def test_create_options(self):
317317
result = self.run_command(['vs', 'create-options'])
318-
319318
self.assert_no_fail(result)
320-
self.assertEqual({'cpus (dedicated host)': [4, 56],
321-
'cpus (dedicated)': [1],
322-
'cpus (standard)': [1, 2, 3, 4],
323-
'datacenter': ['ams01', 'dal05'],
324-
'flavors (balanced)': ['B1_1X2X25', 'B1_1X2X100'],
325-
'flavors (balanced local - hdd)': ['BL1_1X2X100'],
326-
'flavors (balanced local - ssd)': ['BL2_1X2X100'],
327-
'flavors (compute)': ['C1_1X2X25'],
328-
'flavors (memory)': ['M1_1X2X100'],
329-
'flavors (GPU)': ['AC1_1X2X100', 'ACL1_1X2X100'],
330-
'flavors (transient)': ['B1_1X2X25_TRANSIENT'],
331-
'local disk(0)': ['25', '100'],
332-
'memory': [1024, 2048, 3072, 4096],
333-
'memory (dedicated host)': [8192, 65536],
334-
'nic': ['10', '100', '1000'],
335-
'nic (dedicated host)': ['1000'],
336-
'os (CENTOS)': 'CENTOS_6_64',
337-
'os (DEBIAN)': 'DEBIAN_7_64',
338-
'os (UBUNTU)': 'UBUNTU_12_64'},
339-
json.loads(result.output))
319+
self.assertIn('datacenter', result.output)
320+
self.assertIn('flavor', result.output)
321+
self.assertIn('memory', result.output)
322+
self.assertIn('cpu', result.output)
323+
self.assertIn('OS', result.output)
324+
self.assertIn('network', result.output)
340325

341326
@mock.patch('SoftLayer.CLI.formatting.confirm')
342327
def test_dns_sync_both(self, confirm_mock):
@@ -357,19 +342,19 @@ def test_dns_sync_both(self, confirm_mock):
357342
'getResourceRecords')
358343
getResourceRecords.return_value = []
359344
createAargs = ({
360-
'type': 'a',
361-
'host': 'vs-test1',
362-
'domainId': 12345, # from SoftLayer_Account::getDomains
363-
'data': '172.16.240.2',
364-
'ttl': 7200
365-
},)
345+
'type': 'a',
346+
'host': 'vs-test1',
347+
'domainId': 12345, # from SoftLayer_Account::getDomains
348+
'data': '172.16.240.2',
349+
'ttl': 7200
350+
},)
366351
createPTRargs = ({
367-
'type': 'ptr',
368-
'host': '2',
369-
'domainId': 123456,
370-
'data': 'vs-test1.test.sftlyr.ws',
371-
'ttl': 7200
372-
},)
352+
'type': 'ptr',
353+
'host': '2',
354+
'domainId': 123456,
355+
'data': 'vs-test1.test.sftlyr.ws',
356+
'ttl': 7200
357+
},)
373358

374359
result = self.run_command(['vs', 'dns-sync', '100'])
375360

@@ -412,12 +397,12 @@ def test_dns_sync_v6(self, confirm_mock):
412397
}
413398
}
414399
createV6args = ({
415-
'type': 'aaaa',
416-
'host': 'vs-test1',
417-
'domainId': 12345,
418-
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
419-
'ttl': 7200
420-
},)
400+
'type': 'aaaa',
401+
'host': 'vs-test1',
402+
'domainId': 12345,
403+
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
404+
'ttl': 7200
405+
},)
421406
guest.return_value = test_guest
422407
result = self.run_command(['vs', 'dns-sync', '--aaaa-record', '100'])
423408
self.assert_no_fail(result)

0 commit comments

Comments
 (0)