-
Notifications
You must be signed in to change notification settings - Fork 53
Create ovh_block_volume_snapshot.py #123
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #!/usr/bin/python | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
| __metaclass__ = type | ||
|
|
||
| from ansible.module_utils.basic import AnsibleModule | ||
| from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import OVH, ovh_argument_spec | ||
|
|
||
| DOCUMENTATION = ''' | ||
| --- | ||
| module: ovh_block_volume_snapshot | ||
| short_description: Create a snapshot of an OVH Cloud block storage volume | ||
| description: | ||
| - This module creates a snapshot for a given block storage volume on OVH Public Cloud. | ||
| options: | ||
| service_name: | ||
| description: The OVH Cloud project ID | ||
| required: true | ||
| type: str | ||
| volume_id: | ||
| description: The ID of the volume to snapshot | ||
| required: true | ||
| type: str | ||
| snapshot_name: | ||
| description: Name of the snapshot | ||
| required: true | ||
| type: str | ||
| description: | ||
| description: Description of the snapshot | ||
| required: false | ||
| type: str | ||
| author: | ||
| - Your Name | ||
| ''' | ||
|
|
||
| EXAMPLES = ''' | ||
| - name: Create a snapshot of a volume | ||
| ovh_block_volume_snapshot: | ||
| service_name: "project-xyz" | ||
| volume_id: "vol-abc123" | ||
| snapshot_name: "daily-backup" | ||
| description: "Snapshot taken automatically" | ||
| delegate_to: localhost | ||
| ''' | ||
|
|
||
| RETURN = ''' | ||
| msg: | ||
| description: Message about snapshot creation | ||
| returned: always | ||
| type: str | ||
| ''' | ||
|
|
||
| def run_module(): | ||
| argument_spec = ovh_argument_spec() | ||
| argument_spec.update(dict( | ||
| service_name=dict(required=True, type='str'), | ||
| volume_id=dict(required=True, type='str'), | ||
| snapshot_name=dict(required=True, type='str'), | ||
| description=dict(required=False, type='str', default="Snapshot created by Ansible") | ||
| )) | ||
|
|
||
| module = AnsibleModule( | ||
| argument_spec=argument_spec, | ||
| supports_check_mode=False | ||
| ) | ||
|
|
||
| client = OVH(module) | ||
|
|
||
| service_name = module.params['service_name'] | ||
| volume_id = module.params['volume_id'] | ||
| snapshot_name = module.params['snapshot_name'] | ||
| description = module.params['description'] | ||
|
|
||
| try: | ||
| result = client.wrap_call( | ||
| "POST", | ||
| f"/cloud/project/{service_name}/volume/{volume_id}/snapshot", | ||
| name=snapshot_name, | ||
| description=description | ||
| ) | ||
| module.exit_json(changed=True, msg=f"Snapshot '{snapshot_name}' created successfully.", result=result) | ||
| except Exception as e: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exceptions are handled in |
||
| module.fail_json(msg=f"Failed to create snapshot: {str(e)}") | ||
|
Comment on lines
+75
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ansible modules should ideally be idempotent. This means running the module multiple times with the same parameters should result in the same final state and report The current implementation attempts to create the snapshot every time. If a snapshot with the given To make this module idempotent, consider adding logic to check if a snapshot with the specified There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a good point. Did you run the module multiple times with the same name?
Comment on lines
+83
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The It's generally better practice to catch more specific exceptions if possible, or rely on the error handling within |
||
|
|
||
| def main(): | ||
| run_module() | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
authorfield in theDOCUMENTATIONblock is currently set to "Your Name". Please update this field to reflect the actual author(s) or team responsible for this module.