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-
74import click
85
96import 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
0 commit comments