|
| 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_info |
| 17 | +short_description: Get SWR organization permissions info |
| 18 | +extends_documentation_fragment: opentelekomcloud.cloud.otc |
| 19 | +version_added: "0.14.2" |
| 20 | +author: "Ziukina Valeriia (@RusselSand)" |
| 21 | +description: |
| 22 | + - Get repository permissions info from Software Repository for Containers |
| 23 | +options: |
| 24 | + namespace: |
| 25 | + description: |
| 26 | + - Mandatory name of an organisation |
| 27 | + type: str |
| 28 | + required: true |
| 29 | + user_name: |
| 30 | + description: |
| 31 | + - Optional user name |
| 32 | + type: str |
| 33 | +requirements: ["openstacksdk", "otcextensions"] |
| 34 | +''' |
| 35 | + |
| 36 | +RETURN = ''' |
| 37 | +permissions: |
| 38 | + description: Dictionary describing permissions |
| 39 | + type: complex |
| 40 | + returned: On Success. |
| 41 | + contains: |
| 42 | + namespace: |
| 43 | + description: Name of organization. |
| 44 | + type: str |
| 45 | + user_id: |
| 46 | + description: User ID |
| 47 | + type: str |
| 48 | + user_name: |
| 49 | + description: Username |
| 50 | + type: str |
| 51 | + user_auth: |
| 52 | + description: User permission (7 — manage, 3 — write, 1 — read) |
| 53 | + type: int |
| 54 | + self_auth: |
| 55 | + description: Check if this permission is for user who is making request |
| 56 | + type: bool |
| 57 | +''' |
| 58 | + |
| 59 | +EXAMPLES = ''' |
| 60 | +# Get SWR repository permissions information |
| 61 | +- opentelekomcloud.cloud.swr_organization_permissions_info: |
| 62 | + namespace: org_name |
| 63 | + register: swr_organization_permissions |
| 64 | +''' |
| 65 | + |
| 66 | +from ansible_collections.opentelekomcloud.cloud.plugins.module_utils.otc import OTCModule |
| 67 | + |
| 68 | + |
| 69 | +class SwrOrgPermissionInfoModule(OTCModule): |
| 70 | + argument_spec = dict( |
| 71 | + namespace=dict(required=True), |
| 72 | + user_name=dict(required=False) |
| 73 | + ) |
| 74 | + module_kwargs = dict( |
| 75 | + supports_check_mode=True |
| 76 | + ) |
| 77 | + |
| 78 | + def run(self): |
| 79 | + permissions = self.conn.swr.organization_permissions(namespace=self.params['namespace']) |
| 80 | + all_auth = list() |
| 81 | + for permission in permissions: |
| 82 | + permission_dict = {'namespace': permission['namespace'], |
| 83 | + 'user_id': permission['self_auth']['user_id'], |
| 84 | + 'user_name': permission['self_auth']['user_name'], |
| 85 | + 'user_auth': permission['self_auth']['auth'], |
| 86 | + 'self_auth': True} |
| 87 | + all_auth.append(permission_dict) |
| 88 | + for other_permission in permission['others_auths']: |
| 89 | + permission_dict = {'namespace': permission['namespace'], |
| 90 | + 'user_id': other_permission['user_id'], |
| 91 | + 'user_name': other_permission['user_name'], |
| 92 | + 'user_auth': other_permission['auth'], |
| 93 | + 'self_auth': False} |
| 94 | + all_auth.append(permission_dict) |
| 95 | + if self.params['user_name']: |
| 96 | + all_auth = list(filter(lambda x: x['user_name'] == self.params['user_name'], all_auth)) |
| 97 | + self.exit_json( |
| 98 | + changed=False, |
| 99 | + permissions=all_auth |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def main(): |
| 104 | + module = SwrOrgPermissionInfoModule() |
| 105 | + module() |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments