blob: 2ef40aaa774af7bac2d719267e30222a125e251c [file] [log] [blame]
Oleksiy Petrenkoe03e2c72018-08-10 13:24:32 +03001import uuid
2from keystonev3 import common
3from keystonev3 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
14class DomainCheckId(CheckId):
15 def check_id(self, val):
16 if val == 'default':
17 return True
18 return super(DomainCheckId, self).check_id(val)
19
20
21resource_lists = {
22 'project': lists.project_list,
23 'role': lists.role_list,
24 'service': lists.service_list,
25 'user': lists.user_list,
26 'domain': lists.domain_list,
27 'group': lists.group_list,
28}
29
30
31response_keys = {
32 'project': 'projects',
33 'role': 'roles',
34 'service': 'services',
35 'user': 'users',
36 'domain': 'domains',
37 'group': 'groups'
38}
39
40
41def get_by_name_or_uuid_multiple(resource_arg_name_pairs):
42 def wrap(func):
43 def wrapped_f(*args, **kwargs):
44 results = []
45 args_start = 0
46 for index, (resource, arg_name) in enumerate(
47 resource_arg_name_pairs):
48 if arg_name in kwargs:
49 ref = kwargs.pop(arg_name, None)
50 else:
51 ref = args[index]
52 args_start += 1
53 cloud_name = kwargs['cloud_name']
54 if resource == 'domain':
55 checker = DomainCheckId()
56 else:
57 checker = CheckId()
58 if checker.check_id(ref):
59 results.append(ref)
60 else:
61 # Then we have name not uuid
62 resp_key = response_keys[resource]
63 resp = resource_lists[resource](
64 name=ref, cloud_name=cloud_name)[resp_key]
65 if len(resp) == 0:
66 raise common.ResourceNotFound(resp_key, ref)
67 elif len(resp) > 1:
68 raise common.MultipleResourcesFound(resp_key, ref)
69 results.append(resp[0]['id'])
70 results.extend(args[args_start:])
71 return func(*results, **kwargs)
72 return wrapped_f
73 return wrap