Skip to content

Commit b05be0d

Browse files
authored
329 organization permissions and tests implemented (#332)
329 organization permissions and tests implemented Reviewed-by: Anton Sidelnikov
1 parent 44b7e42 commit b05be0d

File tree

7 files changed

+311
-15
lines changed

7 files changed

+311
-15
lines changed

doc/source/swr.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ Software Repository for Containers (SWR) Modules
1111
swr_domain_info <swr_domain_info_module>
1212
swr_domain <swr_domain_module>
1313
swr_repository_permissions <swr_repository_permissions_module>
14-
swr_repository_permissions_info <swr_repository_permissions_info_module>
14+
swr_repository_permissions_info <swr_repository_permissions_info_module>
15+
swr_organization_permissions <swr_organization_permissions_module>

meta/runtime.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@ action_groups:
9898
- swr_domain.py
9999
- swr_repository_permissions.py
100100
- swr_repository_permissions_info.py
101+
- swr_organization_permissions.py
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#!/usr/bin/python
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
DOCUMENTATION = '''
15+
---
16+
module: swr_organization_permissions
17+
short_description: Create, update or delete organization permissions in SWR
18+
extends_documentation_fragment: opentelekomcloud.cloud.otc
19+
version_added: "0.14.2"
20+
author: "Ziukina Valeriia (@RusselSand)"
21+
description:
22+
- Create, update or delete repository permissions in Software Repository for Containers
23+
options:
24+
namespace:
25+
description: Mandatory name of organization.
26+
type: str
27+
required: true
28+
user_id:
29+
description: User ID
30+
type: str
31+
required: true
32+
user_name:
33+
description: Username
34+
type: str
35+
required: true
36+
user_auth:
37+
description: User permission (7 — manage, 3 — write, 1 — read)
38+
default: 1
39+
choices: [1, 3, 7]
40+
type: int
41+
state:
42+
description:
43+
- Whether resource should be present or absent.
44+
choices: ['present', 'absent']
45+
type: str
46+
default: 'present'
47+
requirements: ["openstacksdk", "otcextensions"]
48+
'''
49+
50+
RETURN = '''
51+
permission:
52+
description: Repository permission
53+
type: complex
54+
returned: On Success.
55+
contains:
56+
namespace:
57+
description: Specifies the name of the organization.
58+
type: str
59+
'''
60+
61+
EXAMPLES = '''
62+
# Create or delete SWR organization permission
63+
- name: Create new repository permission
64+
opentelekomcloud.cloud.swr_organization_permissions:
65+
namespace: organization_name
66+
user_id: user_id
67+
user_name: user_name
68+
user_auth: 7
69+
register: permission
70+
71+
- name: Delete an repository permission
72+
opentelekomcloud.cloud.swr_organization_permissions:
73+
namespace: organization_name
74+
user_id: user_id
75+
user_name: user_name
76+
state: absent
77+
register: permission
78+
'''
79+
80+
from ansible_collections.opentelekomcloud.cloud.plugins.module_utils.otc import OTCModule
81+
from ansible_collections.openstack.cloud.plugins.module_utils.resource import StateMachine
82+
83+
84+
class SwrOrgPermissionMachine(StateMachine):
85+
def __call__(self, attributes, check_mode, state, timeout, wait,
86+
updateable_attributes, non_updateable_attributes, **kwargs):
87+
resource = self._find(attributes, **kwargs)
88+
if check_mode:
89+
return self._simulate(state, resource, attributes, timeout, wait,
90+
updateable_attributes,
91+
non_updateable_attributes, **kwargs)
92+
93+
if state == 'present' and not resource:
94+
# Create resource
95+
resource = self._create(attributes, timeout, wait, **kwargs)
96+
return resource, True
97+
98+
elif state == 'present' and resource:
99+
# Do not update resource
100+
resource = self._update(attributes, timeout, wait, **kwargs)
101+
return resource, True
102+
103+
elif state == 'absent' and resource:
104+
# Delete resource
105+
self._delete(resource, attributes, timeout, wait, **kwargs)
106+
return None, True
107+
108+
elif state == 'absent' and not resource:
109+
# Do nothing
110+
return None, False
111+
112+
def _update(self, attributes, timeout, wait, **kwargs):
113+
resource = self.update_function(**attributes)
114+
if wait:
115+
resource = self.sdk.resource.wait_for_status(self.session,
116+
resource,
117+
status='active',
118+
failures=['error'],
119+
wait=timeout,
120+
attribute='status')
121+
return resource
122+
123+
def _delete(self, resource, attributes, timeout, wait, **kwargs):
124+
self.delete_function(namespace=attributes['namespace'],
125+
user_ids=[attributes['permissions'][0]['user_id']])
126+
if wait:
127+
for count in self.sdk.utils.iterate_timeout(
128+
timeout=timeout,
129+
message="Timeout waiting for resource to be absent"
130+
):
131+
if self._find(attributes) is None:
132+
break
133+
134+
def _find(self, attributes, **kwargs):
135+
permissions = self.list_function(
136+
namespace=attributes['namespace'])
137+
user_id = attributes['permissions'][0]['user_id']
138+
all_auth = list()
139+
for permission in permissions:
140+
all_auth.append(permission['self_auth'])
141+
all_auth += permission['others_auths']
142+
current_user = list(filter(lambda x: x['user_id'] == user_id, all_auth))
143+
if len(current_user) > 1:
144+
self.fail_json(msg='Found more than a single resource'
145+
' which matches the given attributes.')
146+
elif len(current_user) == 0:
147+
return None
148+
else:
149+
return current_user[0]
150+
151+
152+
class SwrRepoPermissionModule(OTCModule):
153+
argument_spec = dict(
154+
namespace=dict(required=True),
155+
user_id=dict(required=True),
156+
user_name=dict(required=True),
157+
user_auth=dict(required=False,
158+
type='int',
159+
choices=[1, 3, 7],
160+
default=1),
161+
state=dict(type='str', required=False,
162+
choices=['present', 'absent'],
163+
default='present'),
164+
)
165+
module_kwargs = dict(
166+
supports_check_mode=True
167+
)
168+
169+
def run(self):
170+
service_name = "swr"
171+
type_name = "organization_permissions"
172+
session = getattr(self.conn, service_name)
173+
create_function = getattr(session, 'create_{0}'.format(type_name))
174+
delete_function = getattr(session, 'delete_{0}'.format(type_name))
175+
update_function = getattr(session, 'update_{0}'.format(type_name))
176+
list_function = getattr(session, 'organization_permissions')
177+
crud = dict(
178+
create=create_function,
179+
delete=delete_function,
180+
find=None,
181+
get=None,
182+
list=list_function,
183+
update=update_function,
184+
)
185+
sm = SwrOrgPermissionMachine(connection=self.conn,
186+
sdk=self.sdk,
187+
service_name=service_name,
188+
type_name=type_name,
189+
crud_functions=crud)
190+
kwargs = {'state': self.params['state'],
191+
'attributes': dict((k, self.params[k]) for k in
192+
['namespace']
193+
if self.params[k] is not None)}
194+
kwargs['attributes']['permissions'] = [{
195+
'user_id': self.params['user_id'],
196+
'user_name': self.params['user_name'],
197+
'user_auth': self.params['user_auth']
198+
}]
199+
permission, is_changed = sm(check_mode=self.ansible.check_mode,
200+
non_updateable_attributes=['namespace', 'repository'],
201+
updateable_attributes=['permissions'],
202+
wait=False,
203+
timeout=600,
204+
**kwargs)
205+
self.exit_json(permission=permission, changed=is_changed)
206+
207+
208+
def main():
209+
module = SwrRepoPermissionModule()
210+
module()
211+
212+
213+
if __name__ == "__main__":
214+
main()

plugins/modules/swr_repository_permissions.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,33 @@
5757
type: complex
5858
returned: On Success.
5959
contains:
60-
id:
61-
description: Specifies the ID of the organization.
62-
type: int
63-
name:
60+
namespace:
6461
description: Specifies the name of the organization.
6562
type: str
66-
auth:
67-
description: User permission
68-
type: int
69-
creator_name:
70-
description: Name of the creator og the organization
63+
repository:
64+
description: Specifies the name of the repository.
7165
type: str
7266
'''
7367

7468
EXAMPLES = '''
7569
# Create or delete SWR repository permission
7670
- name: Create new repository permission
77-
opentelekomcloud.cloud.swr_organization:
78-
namespace: org_name
71+
opentelekomcloud.cloud.swr_repository_permissions:
72+
namespace: organization_name
73+
repository: repository_name
74+
user_id: user_id
75+
user_name: user_name
76+
user_auth: 7
77+
register: permission
7978
8079
- name: Delete an repository permission
81-
opentelekomcloud.cloud.swr_organization:
82-
namespace: org_name
80+
opentelekomcloud.cloud.swr_repository_permissions:
81+
namespace: organization_name
82+
repository: repository_name
83+
user_id: user_id
84+
user_name: user_name
8385
state: absent
86+
register: permission
8487
'''
8588

8689
from ansible_collections.opentelekomcloud.cloud.plugins.module_utils.otc import OTCModule
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
swr/group1
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
- name: SWR organization permissions tests
3+
module_defaults:
4+
opentelekomcloud.cloud.swr_organization_permissions:
5+
cloud: "{{ test_cloud }}"
6+
block:
7+
- name: Set random prefix
8+
ansible.builtin.set_fact:
9+
prefix: "{{ 99999999 | random | to_uuid | hash('md5') }}"
10+
11+
- name: Set initial facts
12+
ansible.builtin.set_fact:
13+
organization_name: "{{ ( 'org_' + prefix) }}"
14+
repository_name: "{{ ( 'repo_' + prefix) }}"
15+
user_id: "cfe93b289ece46cd84a22b17c4e6671e"
16+
user_name: "test_user"
17+
18+
- name: Create organization
19+
opentelekomcloud.cloud.swr_organization:
20+
namespace: "{{ organization_name }}"
21+
register: organization
22+
23+
- name: Assert result
24+
ansible.builtin.assert:
25+
that:
26+
- organization is success
27+
28+
- name: Create user permission in this organization
29+
opentelekomcloud.cloud.swr_organization_permissions:
30+
namespace: "{{ organization_name }}"
31+
user_id: "{{ user_id }}"
32+
user_name: "{{ user_name }}"
33+
user_auth: 7
34+
register: permission
35+
36+
- name: Assert result
37+
ansible.builtin.assert:
38+
that:
39+
- permission is success
40+
- permission is changed
41+
42+
- name: Update user permission in this organization
43+
opentelekomcloud.cloud.swr_organization_permissions:
44+
namespace: "{{ organization_name }}"
45+
user_id: "{{ user_id }}"
46+
user_name: "{{ user_name }}"
47+
user_auth: 1
48+
register: permission
49+
50+
- name: Assert result
51+
ansible.builtin.assert:
52+
that:
53+
- permission is success
54+
- permission is changed
55+
56+
- name: Delete user permission in this organization
57+
opentelekomcloud.cloud.swr_organization_permissions:
58+
namespace: "{{ organization_name }}"
59+
user_id: "{{ user_id }}"
60+
user_name: "{{ user_name }}"
61+
state: absent
62+
register: permission
63+
64+
- name: Assert result
65+
ansible.builtin.assert:
66+
that:
67+
- permission is success
68+
- permission is changed
69+
70+
always:
71+
- name: Delete organization
72+
opentelekomcloud.cloud.swr_organization:
73+
namespace: "{{ organization_name }}"
74+
state: absent
75+
failed_when: false

tests/sanity/ignore.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,5 @@ plugins/modules/swr_repository.py validate-modules:missing-gplv3-license
113113
plugins/modules/swr_domain_info.py validate-modules:missing-gplv3-license
114114
plugins/modules/swr_domain.py validate-modules:missing-gplv3-license
115115
plugins/modules/swr_repository_permissions.py validate-modules:missing-gplv3-license
116-
plugins/modules/swr_repository_permissions_info.py validate-modules:missing-gplv3-license
116+
plugins/modules/swr_repository_permissions_info.py validate-modules:missing-gplv3-license
117+
plugins/modules/swr_organization_permissions.py validate-modules:missing-gplv3-license

0 commit comments

Comments
 (0)