blob: 99cb8fe4e2588d09d1fb6718b9fa1f78040e9c02 [file] [log] [blame]
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +03001from neutronv2 import lists
2from neutronv2 import common
3import functools
4import inspect
5from uuid import UUID
6
7
8def _check_uuid(val):
9 try:
10 return str(UUID(val)) == val
11 except (TypeError, ValueError, AttributeError):
12 return False
13
14
15resource_lists = {
16 'network': lists.network_list,
17 'subnet': lists.subnet_list,
18 'subnetpool': lists.subnetpool_list,
19 'agent': lists.agent_list,
20 'router': lists.router_list,
Ann Taraday8204f722018-12-12 16:38:57 +040021 'port': lists.port_list,
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +030022}
23
24
25response_keys = {
26 'network': 'networks',
27 'subnet': 'subnets',
28 'subnetpool': 'subnetpools',
29 'agent': 'agents',
30 'router': 'routers',
Ann Taraday8204f722018-12-12 16:38:57 +040031 'port': 'ports',
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +030032}
33
34
35def get_by_name_or_uuid_multiple(resource_arg_name_pairs):
36 def wrap(func):
37 @functools.wraps(func)
38 def wrapped_f(*args, **kwargs):
39 largs = list(args)
40 inspect_args = inspect.getargspec(
41 func.func_closure[0].cell_contents)
42 for (resource, arg_name) in resource_arg_name_pairs:
43 arg_index = inspect_args.args.index(arg_name)
44 if arg_name in kwargs:
45 ref = kwargs.pop(arg_name, None)
46 else:
47 ref = largs.pop(arg_index)
48 cloud_name = kwargs['cloud_name']
49 if _check_uuid(ref):
50 kwargs[arg_name] = ref
51 else:
52 # Then we have name not uuid
53 resp_key = response_keys[resource]
54 resp = resource_lists[resource](
55 name=ref, cloud_name=cloud_name)[resp_key]
56 if len(resp) == 0:
57 raise common.ResourceNotFound(resp_key, ref)
58 elif len(resp) > 1:
59 raise common.MultipleResourcesFound(resp_key, ref)
60 kwargs[arg_name] = resp[0]['id']
61 return func(*largs, **kwargs)
62 return wrapped_f
63 return wrap