blob: 76569e800fe1a788fbb2f7d1014031ac71a85a3d [file] [log] [blame]
Oleksiy Petrenko777d7822018-11-06 13:32:44 +02001import uuid
2from designatev2 import common
3from designatev2 import lists
4
5
6class 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
14resource_lists = {
15 'zone': lists.zone_list,
16}
17
18
19response_keys = {
20 'zone': 'zones',
21}
22
23
24def get_by_name_or_uuid_multiple(resource_arg_name_pairs):
25 def wrap(func):
26 def wrapped_f(*args, **kwargs):
27 results = []
28 args_start = 0
29 for index, (resource, arg_name) in enumerate(
30 resource_arg_name_pairs):
31 if arg_name in kwargs:
32 ref = kwargs.pop(arg_name, None)
33 else:
34 ref = args[index]
35 args_start += 1
36 cloud_name = kwargs['cloud_name']
37 checker = CheckId()
38 if checker.check_id(ref):
39 results.append(ref)
40 else:
41 # Then we have name not uuid
42 resp_key = response_keys[resource]
43 resp = resource_lists[resource](
44 name=ref, cloud_name=cloud_name)[resp_key]
45 if len(resp) == 0:
46 raise common.ResourceNotFound(resp_key, ref)
47 elif len(resp) > 1:
48 raise common.MultipleResourcesFound(resp_key, ref)
49 results.append(resp[0]['id'])
50 results.extend(args[args_start:])
51 return func(*results, **kwargs)
52 return wrapped_f
53 return wrap