|
| 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() |
0 commit comments