Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 1 | import uuid |
| 2 | from cinderv3 import common |
| 3 | from cinderv3 import lists |
| 4 | |
| 5 | |
| 6 | class CheckId(object): |
| 7 | def check_id(self, val): |
| 8 | try: |
| 9 | return str(uuid.UUID(val)) == val |
| 10 | except (TypeError, ValueError, AttributeError): |
| 11 | return False |
| 12 | |
| 13 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 14 | def named_checker(resource, ref, cloud_name, **kwargs): |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 15 | resp_key = response_keys[resource] |
| 16 | resp = resource_lists[resource]( |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 17 | name=ref, cloud_name=cloud_name, **kwargs)[resp_key] |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 18 | if len(resp) == 0: |
| 19 | raise common.ResourceNotFound(resp_key, ref) |
| 20 | elif len(resp) > 1: |
| 21 | raise common.MultipleResourcesFound(resp_key, ref) |
| 22 | return resp[0]['id'] |
| 23 | |
| 24 | |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 25 | def nameless_checker(resource, ref, cloud_name, **kwargs): |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 26 | item_id = None |
| 27 | resp_key = response_keys[resource] |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 28 | resp = resource_lists[resource]( |
| 29 | cloud_name=cloud_name, **kwargs)[resp_key] |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 30 | for item in resp: |
| 31 | if item["name"] == ref: |
| 32 | if item_id is not None: |
| 33 | raise common.MultipleResourcesFound(resp_key, ref) |
| 34 | item_id = item["id"] |
| 35 | if not item_id: |
| 36 | raise common.ResourceNotFound(resp_key, ref) |
| 37 | return item_id |
| 38 | |
| 39 | |
| 40 | |
| 41 | resource_lists = { |
| 42 | 'volume': lists.volume_list, |
| 43 | 'volume_type': lists.volume_type_list |
| 44 | } |
| 45 | |
| 46 | |
| 47 | response_keys = { |
| 48 | 'volume': 'volumes', |
| 49 | 'volume_type': 'volume_types', |
| 50 | } |
| 51 | |
| 52 | |
| 53 | name_checkers = { |
| 54 | 'volume': named_checker, |
| 55 | 'volume_type': nameless_checker, |
| 56 | } |
| 57 | |
| 58 | |
| 59 | def get_by_name_or_uuid_multiple(resource_arg_name_pairs): |
| 60 | def wrap(func): |
| 61 | def wrapped_f(*args, **kwargs): |
| 62 | results = [] |
| 63 | args_start = 0 |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 64 | connection_params = kwargs.pop('connection_params', {}) or {} |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 65 | for index, (resource, arg_name) in enumerate( |
| 66 | resource_arg_name_pairs): |
| 67 | if arg_name in kwargs: |
| 68 | ref = kwargs.pop(arg_name, None) |
| 69 | else: |
| 70 | ref = args[index] |
| 71 | args_start += 1 |
| 72 | cloud_name = kwargs['cloud_name'] |
| 73 | checker = CheckId() |
| 74 | if checker.check_id(ref): |
| 75 | results.append(ref) |
| 76 | else: |
| 77 | # Then we have name not uuid |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 78 | res = name_checkers[resource](resource, ref, cloud_name, |
| 79 | connection_params=connection_params) |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 80 | results.append(res) |
| 81 | results.extend(args[args_start:]) |
| 82 | return func(*results, **kwargs) |
| 83 | return wrapped_f |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 84 | return wrap |