Skip to content

feat: add public cloud user modules #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
119 changes: 119 additions & 0 deletions plugins/modules/public_cloud_user.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
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 = ''' # '''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module returns a json


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']

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing a check mode here

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception should be catched in the OVH class.

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()
65 changes: 65 additions & 0 deletions plugins/modules/public_cloud_user_info.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check mode is also missing here

f"/cloud/project/{service_name}/user/{user_id}")
module.exit_json(changed=False, **result)


def main():
run_module()


if __name__ == '__main__':
main()
93 changes: 93 additions & 0 deletions plugins/modules/public_cloud_user_s3credentials.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
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 }}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As credentials are returned you could maybe add a register in your example.
This raise an issue with ansible logging those credentials 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an exemple of the access that has to be added?

delegate_to: localhost
'''

RETURN = ''' # '''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module returns credentials


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']

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing check mode also

if state == 'absent':
if access is None:
module.fail_json(msg="access is required with state: absent")
try:
access = client.wrap_call("GET",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this, you define access using it in the call?

f"/cloud/project/{service_name}/user/{user_id}/s3Credentials/{access}")
except OVHResourceNotFound:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception should be raised by the OVH class

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()
65 changes: 65 additions & 0 deletions plugins/modules/public_cloud_user_s3credentials_info.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module returns credentials, examples should mention a register

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']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check mode is missing

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()
Loading
Loading