1
1
"""Virtual server order options."""
2
2
# :license: MIT, see LICENSE for more details.
3
3
# pylint: disable=too-many-statements
4
- import os
5
- import os .path
6
-
7
4
import click
8
5
9
6
import SoftLayer
@@ -18,82 +15,150 @@ def cli(env):
18
15
"""Virtual server order options."""
19
16
20
17
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 ' ))
22
31
23
- table = formatting .KeyValueTable (['name' , 'value' ])
24
- table .align ['name' ] = 'r'
25
- table .align ['value' ] = 'l'
26
32
27
- # Datacenters
33
+ def _get_datacenter_table ( create_options ):
28
34
datacenters = [dc ['template' ]['datacenter' ]['name' ]
29
- for dc in result ['datacenters' ]]
35
+ for dc in create_options ['datacenters' ]]
36
+
30
37
datacenters = sorted (datacenters )
31
38
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' )
34
66
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
36
72
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' ]
39
91
if not x ['template' ].get ('dedicatedAccountHostOnlyFlag' ,
40
92
False )
41
93
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' ]
43
95
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' ]
45
97
if x ['template' ].get ('dedicatedHost' , None )]
46
98
47
99
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 = ',' )])
49
101
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 = ',' )])
51
103
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
53
106
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' ]
56
113
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' ]
58
115
if m ['itemPrice' ].get ('dedicatedHostInstanceFlag' , False )]
59
116
60
117
memory = sorted (memory )
61
- table .add_row (['memory ' ,
62
- formatting .listing (memory , separator = ',' )])
118
+ memory_table .add_row (['standard ' ,
119
+ formatting .listing (memory , separator = ',' )])
63
120
64
121
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 )
74
138
75
139
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' ]
83
143
])
144
+ return os_table
145
+
84
146
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' ]
87
152
if x ['template' ].get ('localDiskFlag' , False )
88
153
and not x ['itemPrice' ].get ('dedicatedHostInstanceFlag' ,
89
154
False )]
90
155
91
- ded_host_local_disks = [x for x in result ['blockDevices' ]
156
+ ded_host_local_disks = [x for x in create_options ['blockDevices' ]
92
157
if x ['template' ].get ('localDiskFlag' , False )
93
158
and x ['itemPrice' ].get ('dedicatedHostInstanceFlag' ,
94
159
False )]
95
160
96
- san_disks = [x for x in result ['blockDevices' ]
161
+ san_disks = [x for x in create_options ['blockDevices' ]
97
162
if not x ['template' ].get ('localDiskFlag' , False )]
98
163
99
164
def add_block_rows (disks , name ):
@@ -109,18 +174,23 @@ def add_block_rows(disks, name):
109
174
simple [bid ].append (str (block ['diskImage' ]['capacity' ]))
110
175
111
176
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 = ',' )])
115
180
116
181
add_block_rows (san_disks , 'san' )
117
182
add_block_rows (local_disks , 'local' )
118
183
add_block_rows (ded_host_local_disks , 'local (dedicated host)' )
184
+ return disk_table
119
185
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'
121
191
speeds = []
122
192
ded_host_speeds = []
123
- for option in result ['networkComponents' ]:
193
+ for option in create_options ['networkComponents' ]:
124
194
template = option .get ('template' , None )
125
195
price = option .get ('itemPrice' , None )
126
196
@@ -140,43 +210,9 @@ def add_block_rows(disks, name):
140
210
speeds .append (max_speed )
141
211
142
212
speeds = sorted (speeds )
143
- table .add_row (['nic' , formatting .listing (speeds , separator = ',' )])
213
+ network_table .add_row (['nic' , formatting .listing (speeds , separator = ',' )])
144
214
145
215
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