blob: 79db4799742b088d82e2f7c3a7b77a146aaea3b0 [file] [log] [blame]
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +03001from neutronv2 import lists
2from neutronv2 import common
3import functools
4import inspect
Pavlo Shchelokovskyy4d1b91d2019-04-19 15:19:13 +03005import time
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +03006from uuid import UUID
7
8
9def _check_uuid(val):
10 try:
11 return str(UUID(val)) == val
12 except (TypeError, ValueError, AttributeError):
13 return False
14
15
16resource_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 Taraday8204f722018-12-12 16:38:57 +040022 'port': lists.port_list,
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +030023}
24
25
26response_keys = {
27 'network': 'networks',
28 'subnet': 'subnets',
29 'subnetpool': 'subnetpools',
30 'agent': 'agents',
31 'router': 'routers',
Ann Taraday8204f722018-12-12 16:38:57 +040032 'port': 'ports',
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +030033}
34
35
36def 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 Shchelokovskyy4d1b91d2019-04-19 15:19:13 +030055 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 Petrenko5bfb8bc2018-08-23 15:08:17 +030063 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