blob: f2bc76dc983070d8806ad1feb78b0210b4ec2557 [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,
21}
22
23
24response_keys = {
25 'network': 'networks',
26 'subnet': 'subnets',
27 'subnetpool': 'subnetpools',
28 'agent': 'agents',
29 'router': 'routers',
30}
31
32
33def get_by_name_or_uuid_multiple(resource_arg_name_pairs):
34 def wrap(func):
35 @functools.wraps(func)
36 def wrapped_f(*args, **kwargs):
37 largs = list(args)
38 inspect_args = inspect.getargspec(
39 func.func_closure[0].cell_contents)
40 for (resource, arg_name) in resource_arg_name_pairs:
41 arg_index = inspect_args.args.index(arg_name)
42 if arg_name in kwargs:
43 ref = kwargs.pop(arg_name, None)
44 else:
45 ref = largs.pop(arg_index)
46 cloud_name = kwargs['cloud_name']
47 if _check_uuid(ref):
48 kwargs[arg_name] = ref
49 else:
50 # Then we have name not uuid
51 resp_key = response_keys[resource]
52 resp = resource_lists[resource](
53 name=ref, cloud_name=cloud_name)[resp_key]
54 if len(resp) == 0:
55 raise common.ResourceNotFound(resp_key, ref)
56 elif len(resp) > 1:
57 raise common.MultipleResourcesFound(resp_key, ref)
58 kwargs[arg_name] = resp[0]['id']
59 return func(*largs, **kwargs)
60 return wrapped_f
61 return wrap