Switch Snapshot module to OpenStackModule

Switch snapshot module to the general OpenStackModule.

Change-Id: I0f4281324e5276122f51722c7b8e79ee185fd476
This commit is contained in:
Artem Goncharov 2021-05-20 13:24:27 +02:00
parent 6b3bf3bba0
commit e3c61aeefd
2 changed files with 69 additions and 76 deletions

View File

@ -21,7 +21,7 @@
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
name: ansible_volume_snapshot name: ansible_volume_snapshot
register: snap_info register: snap_info
ignore_errors: sdk_version is version(0.49, '<') ignore_errors: sdk_version is version('0.49', '<')
- name: Create volume backup - name: Create volume backup
openstack.cloud.volume_backup: openstack.cloud.volume_backup:
@ -30,14 +30,14 @@
display_name: ansible_volume_backup display_name: ansible_volume_backup
volume: ansible_volume volume: ansible_volume
register: vol_backup register: vol_backup
ignore_errors: sdk_version is version(0.49, '<') ignore_errors: sdk_version is version('0.49', '<')
- name: Get backup info - name: Get backup info
openstack.cloud.volume_backup_info: openstack.cloud.volume_backup_info:
cloud: "{{ cloud }}" cloud: "{{ cloud }}"
name: ansible_volume_backup name: ansible_volume_backup
register: backup_info register: backup_info
ignore_errors: sdk_version is version(0.49, '<') ignore_errors: sdk_version is version('0.49', '<')
- debug: var=vol - debug: var=vol

View File

@ -83,61 +83,11 @@ snapshot:
display_name: test_snapshot display_name: test_snapshot
''' '''
from ansible.module_utils.basic import AnsibleModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (
openstack_full_argument_spec,
openstack_module_kwargs,
openstack_cloud_from_module,
)
def _present_volume_snapshot(module, cloud): class VolumeSnapshotModule(OpenStackModule):
volume = cloud.get_volume(module.params['volume']) argument_spec = dict(
snapshot = cloud.get_volume_snapshot(module.params['display_name'],
filters={'volume_id': volume.id})
if not snapshot:
snapshot = cloud.create_volume_snapshot(volume.id,
force=module.params['force'],
wait=module.params['wait'],
timeout=module.params[
'timeout'],
name=module.params['display_name'],
description=module.params.get(
'display_description')
)
module.exit_json(changed=True, snapshot=snapshot)
else:
module.exit_json(changed=False, snapshot=snapshot)
def _absent_volume_snapshot(module, cloud):
volume = cloud.get_volume(module.params['volume'])
snapshot = cloud.get_volume_snapshot(module.params['display_name'],
filters={'volume_id': volume.id})
if not snapshot:
module.exit_json(changed=False)
else:
cloud.delete_volume_snapshot(name_or_id=snapshot.id,
wait=module.params['wait'],
timeout=module.params['timeout'],
)
module.exit_json(changed=True, snapshot_id=snapshot.id)
def _system_state_change(module, cloud):
volume = cloud.get_volume(module.params['volume'])
snapshot = cloud.get_volume_snapshot(module.params['display_name'],
filters={'volume_id': volume.id})
state = module.params['state']
if state == 'present':
return snapshot is None
if state == 'absent':
return snapshot is not None
def main():
argument_spec = openstack_full_argument_spec(
display_name=dict(required=True, aliases=['name']), display_name=dict(required=True, aliases=['name']),
display_description=dict(default=None, aliases=['description']), display_description=dict(default=None, aliases=['description']),
volume=dict(required=True), volume=dict(required=True),
@ -145,29 +95,72 @@ def main():
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
) )
module_kwargs = openstack_module_kwargs() module_kwargs = dict(
module = AnsibleModule(argument_spec, supports_check_mode=True
supports_check_mode=True, )
**module_kwargs)
sdk, cloud = openstack_cloud_from_module(module) def _present_volume_snapshot(self):
volume = self.conn.get_volume(self.params['volume'])
state = module.params['state'] snapshot = self.conn.get_volume_snapshot(
self.params['display_name'], filters={'volume_id': volume.id})
try: if not snapshot:
if cloud.volume_exists(module.params['volume']): snapshot = self.conn.create_volume_snapshot(
if module.check_mode: volume.id,
module.exit_json(changed=_system_state_change(module, cloud)) force=self.params['force'],
if state == 'present': wait=self.params['wait'],
_present_volume_snapshot(module, cloud) timeout=self.params['timeout'],
if state == 'absent': name=self.params['display_name'],
_absent_volume_snapshot(module, cloud) description=self.params.get('display_description')
)
self.exit_json(changed=True, snapshot=snapshot)
else: else:
module.fail_json( self.exit_json(changed=False, snapshot=snapshot)
def _absent_volume_snapshot(self):
volume = self.conn.get_volume(self.params['volume'])
snapshot = self.conn.get_volume_snapshot(
self.params['display_name'], filters={'volume_id': volume.id})
if not snapshot:
self.exit_json(changed=False)
else:
self.conn.delete_volume_snapshot(
name_or_id=snapshot.id,
wait=self.params['wait'],
timeout=self.params['timeout'],
)
self.exit_json(changed=True, snapshot_id=snapshot.id)
def _system_state_change(self):
volume = self.conn.get_volume(self.params['volume'])
snapshot = self.conn.get_volume_snapshot(
self.params['display_name'],
filters={'volume_id': volume.id})
state = self.params['state']
if state == 'present':
return snapshot is None
if state == 'absent':
return snapshot is not None
def run(self):
state = self.params['state']
if self.conn.volume_exists(self.params['volume']):
if self.ansible.check_mode:
self.exit_json(changed=self._system_state_change())
if state == 'present':
self._present_volume_snapshot()
if state == 'absent':
self._absent_volume_snapshot()
else:
self.fail_json(
msg="No volume with name or id '{0}' was found.".format( msg="No volume with name or id '{0}' was found.".format(
module.params['volume'])) self.params['volume']))
except (sdk.exceptions.OpenStackCloudException, sdk.exceptions.ResourceTimeout) as e:
module.fail_json(msg=e.message)
def main():
module = VolumeSnapshotModule()
module()
if __name__ == '__main__': if __name__ == '__main__':