Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 1 | from neutronv2 import lists |
| 2 | from neutronv2 import common |
| 3 | import functools |
| 4 | import inspect |
Pavlo Shchelokovskyy | 4d1b91d | 2019-04-19 15:19:13 +0300 | [diff] [blame^] | 5 | import time |
Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 6 | from uuid import UUID |
| 7 | |
| 8 | |
| 9 | def _check_uuid(val): |
| 10 | try: |
| 11 | return str(UUID(val)) == val |
| 12 | except (TypeError, ValueError, AttributeError): |
| 13 | return False |
| 14 | |
| 15 | |
| 16 | resource_lists = { |
| 17 | 'network': lists.network_list, |
| 18 | 'subnet': lists.subnet_list, |
| 19 | 'subnetpool': lists.subnetpool_list, |
| 20 | 'agent': lists.agent_list, |
| 21 | 'router': lists.router_list, |
Ann Taraday | 8204f72 | 2018-12-12 16:38:57 +0400 | [diff] [blame] | 22 | 'port': lists.port_list, |
Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | |
| 26 | response_keys = { |
| 27 | 'network': 'networks', |
| 28 | 'subnet': 'subnets', |
| 29 | 'subnetpool': 'subnetpools', |
| 30 | 'agent': 'agents', |
| 31 | 'router': 'routers', |
Ann Taraday | 8204f72 | 2018-12-12 16:38:57 +0400 | [diff] [blame] | 32 | 'port': 'ports', |
Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | |
| 36 | def get_by_name_or_uuid_multiple(resource_arg_name_pairs): |
| 37 | def wrap(func): |
| 38 | @functools.wraps(func) |
| 39 | def wrapped_f(*args, **kwargs): |
| 40 | largs = list(args) |
| 41 | inspect_args = inspect.getargspec( |
| 42 | func.func_closure[0].cell_contents) |
| 43 | for (resource, arg_name) in resource_arg_name_pairs: |
| 44 | arg_index = inspect_args.args.index(arg_name) |
| 45 | if arg_name in kwargs: |
| 46 | ref = kwargs.pop(arg_name, None) |
| 47 | else: |
| 48 | ref = largs.pop(arg_index) |
| 49 | cloud_name = kwargs['cloud_name'] |
| 50 | if _check_uuid(ref): |
| 51 | kwargs[arg_name] = ref |
| 52 | else: |
| 53 | # Then we have name not uuid |
| 54 | resp_key = response_keys[resource] |
Pavlo Shchelokovskyy | 4d1b91d | 2019-04-19 15:19:13 +0300 | [diff] [blame^] | 55 | retries = 30 |
| 56 | while retries: |
| 57 | resp = resource_lists[resource]( |
| 58 | name=ref, cloud_name=cloud_name).get(resp_key) |
| 59 | if resp is not None: |
| 60 | break |
| 61 | retries -= 1 |
| 62 | time.sleep(1) |
Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 63 | if len(resp) == 0: |
| 64 | raise common.ResourceNotFound(resp_key, ref) |
| 65 | elif len(resp) > 1: |
| 66 | raise common.MultipleResourcesFound(resp_key, ref) |
| 67 | kwargs[arg_name] = resp[0]['id'] |
| 68 | return func(*largs, **kwargs) |
| 69 | return wrapped_f |
| 70 | return wrap |