From 819dad94c2b6840121f8d72f13a91c94892d8688 Mon Sep 17 00:00:00 2001 From: Jonathan Piron Date: Fri, 10 Jan 2025 16:25:15 +0100 Subject: [PATCH] feat: add public cloud user modules --- README.md | 5 + plugins/modules/public_cloud_user.py | 119 ++++++++++++++++++ plugins/modules/public_cloud_user_info.py | 65 ++++++++++ .../public_cloud_user_s3credentials.py | 93 ++++++++++++++ .../public_cloud_user_s3credentials_info.py | 65 ++++++++++ plugins/modules/public_cloud_users_info.py | 60 +++++++++ 6 files changed, 407 insertions(+) create mode 100755 plugins/modules/public_cloud_user.py create mode 100755 plugins/modules/public_cloud_user_info.py create mode 100755 plugins/modules/public_cloud_user_s3credentials.py create mode 100755 plugins/modules/public_cloud_user_s3credentials_info.py create mode 100755 plugins/modules/public_cloud_users_info.py diff --git a/README.md b/README.md index 9f44789..11866e6 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,11 @@ public_cloud_monthly_billing public_cloud_object_storage public_cloud_object_storage_policy public_cloud_private_network_info +public_cloud_user_info +public_cloud_user_s3credentials_info +public_cloud_user_s3credentials +public_cloud_user +public_cloud_users_info vps_display_name vps_info ``` diff --git a/plugins/modules/public_cloud_user.py b/plugins/modules/public_cloud_user.py new file mode 100755 index 0000000..6c065f5 --- /dev/null +++ b/plugins/modules/public_cloud_user.py @@ -0,0 +1,119 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from __future__ import (absolute_import, division, print_function) + +from ansible.module_utils.basic import AnsibleModule + +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: public_cloud_user +short_description: Manage a OVH public cloud user +description: + - This module manages a OVH public cloud user +author: Jonathan Piron +requirements: + - ovh >= 0.5.0 +options: + service_name: + required: true + description: + - The service_name + role: + required: false + description: + - the role to assign to the user + roles: + required: false + description: + - the roles to assign to the user + description: + required: false + user_id: + required: false + description: The user_id to manage. Required with state: absent + state: + required: false + default: present + choices: ['present', 'absent'] + description: Indicate the desired state of the public cloud user + +''' + +EXAMPLES = ''' +- name: "Create a user on public cloud OVH" + synthesio.ovh.public_cloud_user: + service_name: "{{ service_name }}" + role: "{{ role }}" + roles: "{{ roles }}" + description: "{{ description }}" + delegate_to: localhost + register: user_creation + +- name: "Wait for user creation completion" + public_cloud_user_info: + service_name: "{{ service_name }}" + user_id: "{{ user_creation.json.id }}" + delegate_to: localhost + register: user_retrieval + until: user_retrieval.status == "ok" + retries: 6 + delay: 5 +''' + +RETURN = ''' # ''' + +from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import OVH, OVHResourceNotFound, ovh_argument_spec + + +def run_module(): + module_args = ovh_argument_spec() + module_args.update(dict( + service_name=dict(required=True), + role=dict(required=False, default=None), + roles=dict(required=False, default=None), + description=dict(required=False, default=None), + user_id=dict(required=False, default=None), + state=dict(choices=['present', 'absent'], default='present') + )) + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True + ) + client = OVH(module) + + service_name = module.params['service_name'] + role = module.params['role'] + roles = module.params['roles'] + description = module.params['description'] + user_id = module.params['user_id'] + state = module.params['state'] + + if state == 'absent': + if user_id is None: + module.fail_json(msg="user_id is required with state: absent") + try: + client.wrap_call("DELETE", + f"/cloud/project/{service_name}/user/{user_id}") + except OVHResourceNotFound: + module.exit_json(changed=False) + else: + module.exit_json(changed=True) + else: + user = client.wrap_call("POST", + f"/cloud/project/{service_name}/user", + role=role, + description=description, + roles=roles) + module.exit_json(msg="User was created on OVH public cloud", changed=True, **user) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/public_cloud_user_info.py b/plugins/modules/public_cloud_user_info.py new file mode 100755 index 0000000..7faa146 --- /dev/null +++ b/plugins/modules/public_cloud_user_info.py @@ -0,0 +1,65 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from __future__ import (absolute_import, division, print_function) + +from ansible.module_utils.basic import AnsibleModule + +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: public_cloud_user_info +short_description: Retrieve info for a OVH public cloud user +description: + - This module retrieves info for a OVH public cloud user +author: Jonathan Piron +requirements: + - ovh >= 0.5.0 +options: + service_name: + required: true + description: The service_name + user_id: + required: true + description: The user_id to retrieve information about +''' + +EXAMPLES = ''' +synthesio.ovh.public_cloud_user_info: + service_name: "{{ service_name }}" + user_id: "{{ user_id }}" +delegate_to: localhost +''' + +RETURN = ''' # ''' + +from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import OVH, ovh_argument_spec + + +def run_module(): + module_args = ovh_argument_spec() + module_args.update(dict( + service_name=dict(required=True), + user_id=dict(required=True), + )) + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True + ) + client = OVH(module) + + service_name = module.params['service_name'] + user_id = module.params['user_id'] + result = client.wrap_call("GET", + f"/cloud/project/{service_name}/user/{user_id}") + module.exit_json(changed=False, **result) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/public_cloud_user_s3credentials.py b/plugins/modules/public_cloud_user_s3credentials.py new file mode 100755 index 0000000..26681b7 --- /dev/null +++ b/plugins/modules/public_cloud_user_s3credentials.py @@ -0,0 +1,93 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from __future__ import (absolute_import, division, print_function) + +from ansible.module_utils.basic import AnsibleModule + +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: public_cloud_user_s3credentials +short_description: Manage s3 credentials for an OVH public cloud user +description: + - This module manages s3 credentials for an OVH public cloud user +author: Jonathan Piron +requirements: + - ovh >= 0.5.0 +options: + service_name: + required: true + description: + - The service_name + user_id: + required: true + description: The user_id to manage s3 credentials force + access: + required: false + description: The access to delete. Required with state: absent + state: + required: false + default: present + choices: ['present', 'absent'] + description: Indicate the desired state of the S3 credential + +''' + +EXAMPLES = ''' +synthesio.ovh.public_cloud_user_s3credentials: + service_name: "{{ service_name }}" + user_id: "{{ user_id }}" +delegate_to: localhost +''' + +RETURN = ''' # ''' + +from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import OVH, OVHResourceNotFound, ovh_argument_spec + + +def run_module(): + module_args = ovh_argument_spec() + module_args.update(dict( + service_name=dict(required=True), + user_id=dict(required=True), + access=dict(required=False, default=None), + state=dict(choices=['present', 'absent'], default='present') + )) + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True + ) + client = OVH(module) + + service_name = module.params['service_name'] + user_id = module.params['user_id'] + access = module.params['access'] + state = module.params['state'] + + if state == 'absent': + if access is None: + module.fail_json(msg="access is required with state: absent") + try: + access = client.wrap_call("GET", + f"/cloud/project/{service_name}/user/{user_id}/s3Credentials/{access}") + except OVHResourceNotFound: + module.exit_json(changed=False) + else: + client.wrap_call("DELETE", + f"/cloud/project/{service_name}/user/{user_id}/s3Credentials/{access}") + module.exit_json(changed=True) + else: + credentials = client.wrap_call("POST", + f'/cloud/project/{service_name}/user/{user_id}/s3Credentials') + module.exit_json(changed=True, **credentials) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/public_cloud_user_s3credentials_info.py b/plugins/modules/public_cloud_user_s3credentials_info.py new file mode 100755 index 0000000..04c92a9 --- /dev/null +++ b/plugins/modules/public_cloud_user_s3credentials_info.py @@ -0,0 +1,65 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from __future__ import (absolute_import, division, print_function) + +from ansible.module_utils.basic import AnsibleModule + +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: public_cloud_user_s3credentials_info +short_description: Retrieve s3 credentials info for an OVH public cloud user +description: + - This module retrieves s3 credentials info for an OVH public cloud user +author: Jonathan Piron +requirements: + - ovh >= 0.5.0 +options: + service_name: + required: true + description: The service_name + user_id: + required: true + description: The user_id to retrieve s3 credentials information about +''' + +EXAMPLES = ''' +synthesio.ovh.public_cloud_user_s3credentials_info: + service_name: "{{ service_name }}" + user_id: "{{ user_id }}" +delegate_to: localhost +''' + +RETURN = ''' # ''' + +from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import OVH, ovh_argument_spec + + +def run_module(): + module_args = ovh_argument_spec() + module_args.update(dict( + service_name=dict(required=True), + user_id=dict(required=True), + )) + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True + ) + client = OVH(module) + + service_name = module.params['service_name'] + user_id = module.params['user_id'] + result = client.wrap_call("GET", + f"/cloud/project/{service_name}/user/{user_id}/s3Credentials") + module.exit_json(changed=False, results=result) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/public_cloud_users_info.py b/plugins/modules/public_cloud_users_info.py new file mode 100755 index 0000000..3320ed0 --- /dev/null +++ b/plugins/modules/public_cloud_users_info.py @@ -0,0 +1,60 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from __future__ import (absolute_import, division, print_function) + +from ansible.module_utils.basic import AnsibleModule + +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: public_cloud_users_info +short_description: Retrieve info for all OVH public cloud users +description: + - This module retrieves info from all OVH public cloud users +author: Jonathan Piron +requirements: + - ovh >= 0.5.0 +options: + service_name: + required: true + description: The service_name + +''' + +EXAMPLES = ''' +synthesio.ovh.public_cloud_users_info: + service_name: "{{ service_name }}" +delegate_to: localhost +''' + +RETURN = ''' # ''' + +from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import OVH, ovh_argument_spec + + +def run_module(): + module_args = ovh_argument_spec() + module_args.update(dict( + service_name=dict(required=True), + )) + + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True + ) + client = OVH(module) + + service_name = module.params['service_name'] + result = client.wrap_call("GET", + f"/cloud/project/{service_name}/user") + module.exit_json(changed=False, results=result) + + +def main(): + run_module() + + +if __name__ == '__main__': + main()