Skip to content

feat(public cloud disk utils): Volume extend and info modules #115

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
106 changes: 106 additions & 0 deletions plugins/modules/public_cloud_block_storage_upsize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function)

from ansible.module_utils.basic import AnsibleModule

__metaclass__ = type

DOCUMENTATION = r'''
---
module: public_cloud_block_storage_upsize
short_description: Extend OVH public cloud volume.
description:
- This module extends a volume on OVH public cloud.
author: James Bos (GoDoo Ptd Ltd)

requirements:
- ovh >= 0.5.0

options:
service_name:
required: true
description: Project ID
region:
required: true
description: Region where volume is hosted
volume_id:
required: true
description: The volume id
size:
required: true
description: Volume size (in GB)
type: integer
name:
required: true
description: Volume name
'''

EXAMPLES = r'''
- name: Resize Volume
synthesio.ovh.public_cloud_block_storage_upsize:
volume_id: "{{ volume_metadata.id }}"
service_name: "{{ ovh_project_id }}"
endpoint: "{{ ovh_endpoint }}"
region: "{{ ovh_region }}"
size: "{{ storage_gb }}"
name: "{{ inventory_hostname }}-storage"
application_key: "{{ ovh_application_key }}"
application_secret: "{{ ovh_application_secret }}"
consumer_key: "{{ ovh_consumer_key }}"
delegate_to: localhost
register: volume_metadata
'''

RETURN = r''' # '''

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),
region=dict(required=True),
size=dict(required=True, type="int"),
name=dict(required=True),
volume_id=dict(required=True),
))

module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)
client = OVH(module)

service_name = module.params['service_name']
volume_id = module.params['volume_id']
region = module.params['region']
size = module.params['size']
name = module.params['name']

Copy link
Collaborator

Choose a reason for hiding this comment

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

You should add a check_mode option before running for real

try:
result = client.wrap_call(
"POST",
'/cloud/project/%s/volume/%s/upsize' % (service_name, volume_id),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use fstring

name=name,
region=region,
size=size,
)
module.exit_json(
msg="Volume {} ({}), has been resized on OVH public Cloud".format(
name, result['id']),
changed=True,
**result)

except APIError as api_error:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Exceptions are catched in the OVH class

module.fail_json(msg="Failed to call OVH API: {0}".format(api_error))


def main():
run_module()


if __name__ == '__main__':
main()
89 changes: 89 additions & 0 deletions plugins/modules/public_cloud_block_storage_volume_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function)

from ansible.module_utils.basic import AnsibleModule

__metaclass__ = type

DOCUMENTATION = r'''
---
module: public_cloud_block_storage_volume_info
short_description: Retrieve OVH API public cloud volume info by volume name.
description:
- This module provides volume information for all instances in a Project.
Author: James Bos (GoDoo Pty Ltd)

requirements:
- ovh >= 0.5.0

options:
service_name:
required: true
description: Project ID
region:
required: true
description: Region where volume is hosted
name:
required: true
description: Volume name
'''

EXAMPLES = r'''
- name: Get Volume Info
synthesio.ovh.public_cloud_block_storage_volume_info:
service_name: "{{ ovh_project_id }}"
endpoint: "{{ ovh_endpoint }}"
region: "{{ ovh_region }}"
name: "{{ inventory_hostname }}-storage"
application_key: "{{ ovh_application_key }}"
application_secret: "{{ ovh_application_secret }}"
consumer_key: "{{ ovh_consumer_key }}"
delegate_to: localhost
register: block_storage_metadata
'''

RETURN = r''' # '''

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),
region=dict(required=True),
name=dict(required=True),
))

module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
Copy link
Collaborator

Choose a reason for hiding this comment

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

As this is only returning informations, the check mode could be set to false.
Otherwise you should add a check mode

)
client = OVH(module)

service_name = module.params['service_name']
region = module.params['region']

try:
result = client.wrap_call(
'GET',
'/cloud/project/%s/volume' % service_name,
region=region,
)

# OVH API currently does not allow to filter by volume name
result = [volume for volume in result if volume['name'] == module.params['name']][0]
module.exit_json(changed=False, **result)

except APIError as api_error:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, exceptions are handled in the OVH class

module.fail_json(msg="Failed to call OVH API: {0}".format(api_error))


def main():
run_module()


if __name__ == '__main__':
main()
Loading