Oleksiy Petrenko | 5013e68 | 2018-11-16 14:26:36 +0200 | [diff] [blame] | 1 | import uuid |
| 2 | from aodhv2 import common |
| 3 | from aodhv2 import queries |
| 4 | |
| 5 | |
| 6 | class CheckId(object): |
| 7 | def check_id(self, val): |
| 8 | try: |
| 9 | return str(uuid.UUID(val)).replace('-', '') == val |
| 10 | except (TypeError, ValueError, AttributeError): |
| 11 | return False |
| 12 | |
| 13 | |
| 14 | resource_lists = { |
| 15 | 'alarm': queries.query_post, |
| 16 | } |
| 17 | |
| 18 | |
| 19 | def get_by_name_or_uuid_multiple(resource_arg_name_pairs): |
| 20 | def wrap(func): |
| 21 | def wrapped_f(*args, **kwargs): |
| 22 | results = [] |
| 23 | args_start = 0 |
| 24 | for index, (resource, arg_name) in enumerate( |
| 25 | resource_arg_name_pairs): |
| 26 | if arg_name in kwargs: |
| 27 | ref = kwargs.pop(arg_name, None) |
| 28 | else: |
| 29 | ref = args[index] |
| 30 | args_start += 1 |
| 31 | cloud_name = kwargs['cloud_name'] |
| 32 | checker = CheckId() |
| 33 | if checker.check_id(ref): |
| 34 | results.append(ref) |
| 35 | else: |
| 36 | # Then we have name not uuid |
| 37 | resp = resource_lists[resource]( |
| 38 | name=ref, cloud_name=cloud_name) |
| 39 | if len(resp) == 0: |
| 40 | raise common.ResourceNotFound(resource, ref) |
| 41 | elif len(resp) > 1: |
| 42 | raise common.MultipleResourcesFound(resource, ref) |
| 43 | results.append(resp[0]['alarm_id']) |
| 44 | results.extend(args[args_start:]) |
| 45 | return func(*results, **kwargs) |
| 46 | return wrapped_f |
| 47 | return wrap |