@@ -112,17 +112,11 @@ def find_price_by_category(package, price_category):
112112 :return: Returns the price for the given category, or an error if not found
113113 """
114114 for item in package ['items' ]:
115- for price in item ['prices' ]:
116- if price ['locationGroupId' ] != '' :
117- continue
118-
119- if not _has_category (price ['categories' ], price_category ):
120- continue
115+ price_id = _find_price_id (item ['prices' ], price_category )
116+ if price_id :
117+ return price_id
121118
122- return {'id' : price ['id' ]}
123-
124- raise ValueError ("Could not find price with the category, %s"
125- % price_category )
119+ raise ValueError ("Could not find price with the category, %s" % price_category )
126120
127121
128122def find_ent_space_price (package , category , size , tier_level ):
@@ -141,25 +135,14 @@ def find_ent_space_price(package, category, size, tier_level):
141135 else : # category == 'endurance'
142136 category_code = 'performance_storage_space'
143137
138+ level = ENDURANCE_TIERS .get (tier_level )
139+
144140 for item in package ['items' ]:
145141 if int (item ['capacity' ]) != size :
146142 continue
147-
148- for price in item ['prices' ]:
149- # Only collect prices from valid location groups.
150- if price ['locationGroupId' ] != '' :
151- continue
152-
153- level = ENDURANCE_TIERS .get (tier_level )
154- if price ['capacityRestrictionType' ] != 'STORAGE_TIER_LEVEL' \
155- or level < int (price ['capacityRestrictionMinimum' ])\
156- or level > int (price ['capacityRestrictionMaximum' ]):
157- continue
158-
159- if not _has_category (price ['categories' ], category_code ):
160- continue
161-
162- return {'id' : price ['id' ]}
143+ price_id = _find_price_id (item ['prices' ], category_code , 'STORAGE_TIER_LEVEL' , level )
144+ if price_id :
145+ return price_id
163146
164147 raise ValueError ("Could not find price for %s storage space" % category )
165148
@@ -178,15 +161,9 @@ def find_ent_endurance_tier_price(package, tier_level):
178161 else :
179162 continue
180163
181- for price in item ['prices' ]:
182- # Only collect prices from valid location groups.
183- if price ['locationGroupId' ] != '' :
184- continue
185-
186- if not _has_category (price ['categories' ], 'storage_tier_level' ):
187- continue
188-
189- return {'id' : price ['id' ]}
164+ price_id = _find_price_id (item ['prices' ], 'storage_tier_level' )
165+ if price_id :
166+ return price_id
190167
191168 raise ValueError ("Could not find price for endurance tier level" )
192169
@@ -225,16 +202,9 @@ def find_perf_space_price(package, size):
225202 if int (item ['capacity' ]) != size :
226203 continue
227204
228- for price in item ['prices' ]:
229- # Only collect prices from valid location groups.
230- if price ['locationGroupId' ] != '' :
231- continue
232-
233- if not _has_category (price ['categories' ],
234- 'performance_storage_space' ):
235- continue
236-
237- return {'id' : price ['id' ]}
205+ price_id = _find_price_id (item ['prices' ], 'performance_storage_space' )
206+ if price_id :
207+ return price_id
238208
239209 raise ValueError ("Could not find performance space price for this volume" )
240210
@@ -251,21 +221,9 @@ def find_perf_iops_price(package, size, iops):
251221 if int (item ['capacity' ]) != int (iops ):
252222 continue
253223
254- for price in item ['prices' ]:
255- # Only collect prices from valid location groups.
256- if price ['locationGroupId' ] != '' :
257- continue
258-
259- if not _has_category (price ['categories' ],
260- 'performance_storage_iops' ):
261- continue
262-
263- if price ['capacityRestrictionType' ] != 'STORAGE_SPACE' \
264- or size < int (price ['capacityRestrictionMinimum' ])\
265- or size > int (price ['capacityRestrictionMaximum' ]):
266- continue
267-
268- return {'id' : price ['id' ]}
224+ price_id = _find_price_id (item ['prices' ], 'performance_storage_iops' , 'STORAGE_SPACE' , size )
225+ if price_id :
226+ return price_id
269227
270228 raise ValueError ("Could not find price for iops for the given volume" )
271229
@@ -294,16 +252,9 @@ def find_saas_endurance_space_price(package, size, tier_level):
294252 if size < capacity_minimum or size > capacity_maximum :
295253 continue
296254
297- for price in item ['prices' ]:
298- # Only collect prices from valid location groups.
299- if price ['locationGroupId' ] != '' :
300- continue
301-
302- if not _has_category (price ['categories' ],
303- 'performance_storage_space' ):
304- continue
305-
306- return {'id' : price ['id' ]}
255+ price_id = _find_price_id (item ['prices' ], 'performance_storage_space' )
256+ if price_id :
257+ return price_id
307258
308259 raise ValueError ("Could not find price for endurance storage space" )
309260
@@ -326,15 +277,9 @@ def find_saas_endurance_tier_price(package, tier_level):
326277 if int (item ['capacity' ]) != target_capacity :
327278 continue
328279
329- for price in item ['prices' ]:
330- # Only collect prices from valid location groups.
331- if price ['locationGroupId' ] != '' :
332- continue
333-
334- if not _has_category (price ['categories' ], 'storage_tier_level' ):
335- continue
336-
337- return {'id' : price ['id' ]}
280+ price_id = _find_price_id (item ['prices' ], 'storage_tier_level' )
281+ if price_id :
282+ return price_id
338283
339284 raise ValueError ("Could not find price for endurance tier level" )
340285
@@ -364,17 +309,9 @@ def find_saas_perform_space_price(package, size):
364309 key_name = '{0}_{1}_GBS' .format (capacity_minimum , capacity_maximum )
365310 if item ['keyName' ] != key_name :
366311 continue
367-
368- for price in item ['prices' ]:
369- # Only collect prices from valid location groups.
370- if price ['locationGroupId' ] != '' :
371- continue
372-
373- if not _has_category (price ['categories' ],
374- 'performance_storage_space' ):
375- continue
376-
377- return {'id' : price ['id' ]}
312+ price_id = _find_price_id (item ['prices' ], 'performance_storage_space' )
313+ if price_id :
314+ return price_id
378315
379316 raise ValueError ("Could not find price for performance storage space" )
380317
@@ -402,21 +339,9 @@ def find_saas_perform_iops_price(package, size, iops):
402339 if iops < capacity_minimum or iops > capacity_maximum :
403340 continue
404341
405- for price in item ['prices' ]:
406- # Only collect prices from valid location groups.
407- if price ['locationGroupId' ] != '' :
408- continue
409-
410- if not _has_category (price ['categories' ],
411- 'performance_storage_iops' ):
412- continue
413-
414- if price ['capacityRestrictionType' ] != 'STORAGE_SPACE' \
415- or size < int (price ['capacityRestrictionMinimum' ])\
416- or size > int (price ['capacityRestrictionMaximum' ]):
417- continue
418-
419- return {'id' : price ['id' ]}
342+ price_id = _find_price_id (item ['prices' ], 'performance_storage_iops' , 'STORAGE_SPACE' , size )
343+ if price_id :
344+ return price_id
420345
421346 raise ValueError ("Could not find price for iops for the given volume" )
422347
@@ -441,21 +366,9 @@ def find_saas_snapshot_space_price(package, size, tier=None, iops=None):
441366 if int (item ['capacity' ]) != size :
442367 continue
443368
444- for price in item ['prices' ]:
445- # Only collect prices from valid location groups.
446- if price ['locationGroupId' ] != '' :
447- continue
448-
449- if target_restriction_type != price ['capacityRestrictionType' ]\
450- or target_value < int (price ['capacityRestrictionMinimum' ])\
451- or target_value > int (price ['capacityRestrictionMaximum' ]):
452- continue
453-
454- if not _has_category (price ['categories' ],
455- 'storage_snapshot_space' ):
456- continue
457-
458- return {'id' : price ['id' ]}
369+ price_id = _find_price_id (item ['prices' ], 'storage_snapshot_space' , target_restriction_type , target_value )
370+ if price_id :
371+ return price_id
459372
460373 raise ValueError ("Could not find price for snapshot space" )
461374
@@ -481,21 +394,14 @@ def find_saas_replication_price(package, tier=None, iops=None):
481394 if item ['keyName' ] != target_item_keyname :
482395 continue
483396
484- for price in item ['prices' ]:
485- # Only collect prices from valid location groups.
486- if price ['locationGroupId' ] != '' :
487- continue
488-
489- if target_restriction_type != price ['capacityRestrictionType' ]\
490- or target_value < int (price ['capacityRestrictionMinimum' ])\
491- or target_value > int (price ['capacityRestrictionMaximum' ]):
492- continue
493-
494- if not _has_category (price ['categories' ],
495- 'performance_storage_replication' ):
496- continue
497-
498- return {'id' : price ['id' ]}
397+ price_id = _find_price_id (
398+ item ['prices' ],
399+ 'performance_storage_replication' ,
400+ target_restriction_type ,
401+ target_value
402+ )
403+ if price_id :
404+ return price_id
499405
500406 raise ValueError ("Could not find price for replicant volume" )
501407
@@ -1090,3 +996,21 @@ def _has_category(categories, category_code):
1090996
1091997def _staas_version_is_v2_or_above (volume ):
1092998 return int (volume ['staasVersion' ]) > 1 and volume ['hasEncryptionAtRest' ]
999+
1000+
1001+ def _find_price_id (prices , category , restriction_type = None , restriction_value = None ):
1002+ for price in prices :
1003+ # Only collect prices from valid location groups.
1004+ if price ['locationGroupId' ]:
1005+ continue
1006+
1007+ if restriction_type is not None and restriction_value is not None :
1008+ if restriction_type != price ['capacityRestrictionType' ]\
1009+ or restriction_value < int (price ['capacityRestrictionMinimum' ])\
1010+ or restriction_value > int (price ['capacityRestrictionMaximum' ]):
1011+ continue
1012+
1013+ if not _has_category (price ['categories' ], category ):
1014+ continue
1015+
1016+ return {'id' : price ['id' ]}
0 commit comments