mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
cs_snapshot_policy: remove CloudStackException dependency
This commit is contained in:
parent
97d5e0d027
commit
d6e1f02990
2 changed files with 53 additions and 86 deletions
|
@ -7,7 +7,6 @@
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
'status': ['stableinterface'],
|
'status': ['stableinterface'],
|
||||||
'supported_by': 'community'}
|
'supported_by': 'community'}
|
||||||
|
@ -26,13 +25,9 @@ options:
|
||||||
description:
|
description:
|
||||||
- Name of the volume.
|
- Name of the volume.
|
||||||
- Either C(volume) or C(vm) is required.
|
- Either C(volume) or C(vm) is required.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
volume_type:
|
volume_type:
|
||||||
description:
|
description:
|
||||||
- Type of the volume.
|
- Type of the volume.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
choices:
|
choices:
|
||||||
- DATADISK
|
- DATADISK
|
||||||
- ROOT
|
- ROOT
|
||||||
|
@ -43,35 +38,27 @@ options:
|
||||||
- Use C(volume_type) if VM has a DATADISK and ROOT volume.
|
- Use C(volume_type) if VM has a DATADISK and ROOT volume.
|
||||||
- In case of C(volume_type=DATADISK), additionally use C(device_id) if VM has more than one DATADISK volume.
|
- In case of C(volume_type=DATADISK), additionally use C(device_id) if VM has more than one DATADISK volume.
|
||||||
- Either C(volume) or C(vm) is required.
|
- Either C(volume) or C(vm) is required.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
version_added: "2.3"
|
version_added: "2.3"
|
||||||
device_id:
|
device_id:
|
||||||
description:
|
description:
|
||||||
- ID of the device on a VM the volume is attached to.
|
- ID of the device on a VM the volume is attached to.
|
||||||
- This will only be considered if VM has multiple DATADISK volumes.
|
- This will only be considered if VM has multiple DATADISK volumes.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
version_added: "2.3"
|
version_added: "2.3"
|
||||||
vpc:
|
vpc:
|
||||||
description:
|
description:
|
||||||
- Name of the vpc the instance is deployed in.
|
- Name of the vpc the instance is deployed in.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
version_added: "2.3"
|
version_added: "2.3"
|
||||||
interval_type:
|
interval_type:
|
||||||
description:
|
description:
|
||||||
- Interval of the snapshot.
|
- Interval of the snapshot.
|
||||||
required: false
|
default: daily
|
||||||
default: 'daily'
|
choices: [ hourly, daily, weekly, monthly ]
|
||||||
choices: [ 'hourly', 'daily', 'weekly', 'monthly' ]
|
aliases: [ interval ]
|
||||||
aliases: [ 'interval' ]
|
|
||||||
max_snaps:
|
max_snaps:
|
||||||
description:
|
description:
|
||||||
- Max number of snapshots.
|
- Max number of snapshots.
|
||||||
required: false
|
|
||||||
default: 8
|
default: 8
|
||||||
aliases: [ 'max' ]
|
aliases: [ max ]
|
||||||
schedule:
|
schedule:
|
||||||
description:
|
description:
|
||||||
- Time the snapshot is scheduled. Required if C(state=present).
|
- Time the snapshot is scheduled. Required if C(state=present).
|
||||||
|
@ -79,48 +66,38 @@ options:
|
||||||
- 'Format for C(interval_type=DAILY): C(MM:HH)'
|
- 'Format for C(interval_type=DAILY): C(MM:HH)'
|
||||||
- 'Format for C(interval_type=WEEKLY): C(MM:HH:DD (1-7))'
|
- 'Format for C(interval_type=WEEKLY): C(MM:HH:DD (1-7))'
|
||||||
- 'Format for C(interval_type=MONTHLY): C(MM:HH:DD (1-28))'
|
- 'Format for C(interval_type=MONTHLY): C(MM:HH:DD (1-28))'
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
time_zone:
|
time_zone:
|
||||||
description:
|
description:
|
||||||
- Specifies a timezone for this command.
|
- Specifies a timezone for this command.
|
||||||
required: false
|
default: UTC
|
||||||
default: 'UTC'
|
aliases: [ timezone ]
|
||||||
aliases: [ 'timezone' ]
|
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- State of the snapshot policy.
|
- State of the snapshot policy.
|
||||||
required: false
|
default: present
|
||||||
default: 'present'
|
choices: [ present, absent ]
|
||||||
choices: [ 'present', 'absent' ]
|
|
||||||
domain:
|
domain:
|
||||||
description:
|
description:
|
||||||
- Domain the volume is related to.
|
- Domain the volume is related to.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
account:
|
account:
|
||||||
description:
|
description:
|
||||||
- Account the volume is related to.
|
- Account the volume is related to.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
project:
|
project:
|
||||||
description:
|
description:
|
||||||
- Name of the project the volume is related to.
|
- Name of the project the volume is related to.
|
||||||
required: false
|
|
||||||
default: null
|
|
||||||
extends_documentation_fragment: cloudstack
|
extends_documentation_fragment: cloudstack
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
# Ensure a snapshot policy daily at 1h00 UTC
|
- name: ensure a snapshot policy daily at 1h00 UTC
|
||||||
- local_action:
|
local_action:
|
||||||
module: cs_snapshot_policy
|
module: cs_snapshot_policy
|
||||||
volume: ROOT-478
|
volume: ROOT-478
|
||||||
schedule: '00:1'
|
schedule: '00:1'
|
||||||
max_snaps: 3
|
max_snaps: 3
|
||||||
|
|
||||||
# Ensure a snapshot policy daily at 1h00 UTC on the second DATADISK of VM web-01
|
- name: ensure a snapshot policy daily at 1h00 UTC on the second DATADISK of VM web-01
|
||||||
- local_action:
|
local_action:
|
||||||
module: cs_snapshot_policy
|
module: cs_snapshot_policy
|
||||||
vm: web-01
|
vm: web-01
|
||||||
volume_type: DATADISK
|
volume_type: DATADISK
|
||||||
|
@ -128,16 +105,16 @@ EXAMPLES = '''
|
||||||
schedule: '00:1'
|
schedule: '00:1'
|
||||||
max_snaps: 3
|
max_snaps: 3
|
||||||
|
|
||||||
# Ensure a snapshot policy hourly at minute 5 UTC
|
- name: ensure a snapshot policy hourly at minute 5 UTC
|
||||||
- local_action:
|
local_action:
|
||||||
module: cs_snapshot_policy
|
module: cs_snapshot_policy
|
||||||
volume: ROOT-478
|
volume: ROOT-478
|
||||||
schedule: '5'
|
schedule: '5'
|
||||||
interval_type: hourly
|
interval_type: hourly
|
||||||
max_snaps: 1
|
max_snaps: 1
|
||||||
|
|
||||||
# Ensure a snapshot policy weekly on Sunday at 05h00, TZ Europe/Zurich
|
- name: ensure a snapshot policy weekly on Sunday at 05h00, TZ Europe/Zurich
|
||||||
- local_action:
|
local_action:
|
||||||
module: cs_snapshot_policy
|
module: cs_snapshot_policy
|
||||||
volume: ROOT-478
|
volume: ROOT-478
|
||||||
schedule: '00:5:1'
|
schedule: '00:5:1'
|
||||||
|
@ -145,8 +122,8 @@ EXAMPLES = '''
|
||||||
max_snaps: 1
|
max_snaps: 1
|
||||||
time_zone: 'Europe/Zurich'
|
time_zone: 'Europe/Zurich'
|
||||||
|
|
||||||
# Ensure a snapshot policy is absent
|
- name: ensure a snapshot policy is absent
|
||||||
- local_action:
|
local_action:
|
||||||
module: cs_snapshot_policy
|
module: cs_snapshot_policy
|
||||||
volume: ROOT-478
|
volume: ROOT-478
|
||||||
interval_type: hourly
|
interval_type: hourly
|
||||||
|
@ -206,13 +183,13 @@ domain:
|
||||||
type: string
|
type: string
|
||||||
sample: example domain
|
sample: example domain
|
||||||
'''
|
'''
|
||||||
try:
|
|
||||||
from cs import CloudStackException
|
|
||||||
except ImportError:
|
|
||||||
pass # Handled in AnsibleCloudStack.__init__
|
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.cloudstack import AnsibleCloudStack, cs_argument_spec, cs_required_together
|
from ansible.module_utils.cloudstack import (
|
||||||
|
AnsibleCloudStack,
|
||||||
|
cs_argument_spec,
|
||||||
|
cs_required_together
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
||||||
|
@ -225,10 +202,10 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
||||||
'maxsnaps': 'max_snaps',
|
'maxsnaps': 'max_snaps',
|
||||||
}
|
}
|
||||||
self.interval_types = {
|
self.interval_types = {
|
||||||
'hourly': 0,
|
'hourly': 0,
|
||||||
'daily': 1,
|
'daily': 1,
|
||||||
'weekly': 2,
|
'weekly': 2,
|
||||||
'monthly': 3,
|
'monthly': 3,
|
||||||
}
|
}
|
||||||
self.volume = None
|
self.volume = None
|
||||||
|
|
||||||
|
@ -248,7 +225,7 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
||||||
'virtualmachineid': self.get_vm(key='id'),
|
'virtualmachineid': self.get_vm(key='id'),
|
||||||
'type': self.module.params.get('volume_type'),
|
'type': self.module.params.get('volume_type'),
|
||||||
}
|
}
|
||||||
volumes = self.cs.listVolumes(**args)
|
volumes = self.query_api('listVolumes', **args)
|
||||||
if volumes:
|
if volumes:
|
||||||
if volumes['count'] > 1:
|
if volumes['count'] > 1:
|
||||||
device_id = self.module.params.get('device_id')
|
device_id = self.module.params.get('device_id')
|
||||||
|
@ -268,7 +245,7 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
||||||
args = {
|
args = {
|
||||||
'volumeid': self.get_volume(key='id')
|
'volumeid': self.get_volume(key='id')
|
||||||
}
|
}
|
||||||
policies = self.cs.listSnapshotPolicies(**args)
|
policies = self.query_api('listSnapshotPolicies', **args)
|
||||||
if policies:
|
if policies:
|
||||||
for policy in policies['snapshotpolicy']:
|
for policy in policies['snapshotpolicy']:
|
||||||
if policy['intervaltype'] == self.get_interval_type():
|
if policy['intervaltype'] == self.get_interval_type():
|
||||||
|
@ -293,10 +270,8 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
||||||
if not policy or (policy and self.has_changed(policy, args, only_keys=['schedule', 'maxsnaps', 'timezone'])):
|
if not policy or (policy and self.has_changed(policy, args, only_keys=['schedule', 'maxsnaps', 'timezone'])):
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
res = self.cs.createSnapshotPolicy(**args)
|
res = self.query_api('createSnapshotPolicy', **args)
|
||||||
policy = res['snapshotpolicy']
|
policy = res['snapshotpolicy']
|
||||||
if 'errortext' in policy:
|
|
||||||
self.module.fail_json(msg="Failed: '%s'" % policy['errortext'])
|
|
||||||
return policy
|
return policy
|
||||||
|
|
||||||
def absent_snapshot_policy(self):
|
def absent_snapshot_policy(self):
|
||||||
|
@ -307,9 +282,7 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
||||||
'id': policy['id']
|
'id': policy['id']
|
||||||
}
|
}
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
res = self.cs.deleteSnapshotPolicies(**args)
|
self.query_api('deleteSnapshotPolicies', **args)
|
||||||
if 'errortext' in res:
|
|
||||||
self.module.fail_json(msg="Failed: '%s'" % policy['errortext'])
|
|
||||||
return policy
|
return policy
|
||||||
|
|
||||||
def get_result(self, policy):
|
def get_result(self, policy):
|
||||||
|
@ -322,11 +295,11 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
||||||
volume = self.get_volume()
|
volume = self.get_volume()
|
||||||
if volume:
|
if volume:
|
||||||
volume_results = {
|
volume_results = {
|
||||||
'volume': volume.get('name'),
|
'volume': volume.get('name'),
|
||||||
'zone': volume.get('zonename'),
|
'zone': volume.get('zonename'),
|
||||||
'project': volume.get('project'),
|
'project': volume.get('project'),
|
||||||
'account': volume.get('account'),
|
'account': volume.get('account'),
|
||||||
'domain': volume.get('domain'),
|
'domain': volume.get('domain'),
|
||||||
}
|
}
|
||||||
self.result.update(volume_results)
|
self.result.update(volume_results)
|
||||||
return self.result
|
return self.result
|
||||||
|
@ -335,44 +308,39 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
||||||
def main():
|
def main():
|
||||||
argument_spec = cs_argument_spec()
|
argument_spec = cs_argument_spec()
|
||||||
argument_spec.update(dict(
|
argument_spec.update(dict(
|
||||||
volume=dict(default=None),
|
volume=dict(),
|
||||||
volume_type=dict(choices=['DATADISK', 'ROOT'], default=None),
|
volume_type=dict(choices=['DATADISK', 'ROOT']),
|
||||||
vm=dict(default=None),
|
vm=dict(),
|
||||||
device_id=dict(type='int', default=None),
|
device_id=dict(type='int'),
|
||||||
vpc=dict(default=None),
|
vpc=dict(),
|
||||||
interval_type=dict(default='daily', choices=['hourly', 'daily', 'weekly', 'monthly'], aliases=['interval']),
|
interval_type=dict(default='daily', choices=['hourly', 'daily', 'weekly', 'monthly'], aliases=['interval']),
|
||||||
schedule=dict(default=None),
|
schedule=dict(),
|
||||||
time_zone=dict(default='UTC', aliases=['timezone']),
|
time_zone=dict(default='UTC', aliases=['timezone']),
|
||||||
max_snaps=dict(type='int', default=8, aliases=['max']),
|
max_snaps=dict(type='int', default=8, aliases=['max']),
|
||||||
state=dict(choices=['present', 'absent'], default='present'),
|
state=dict(choices=['present', 'absent'], default='present'),
|
||||||
domain=dict(default=None),
|
domain=dict(),
|
||||||
account=dict(default=None),
|
account=dict(),
|
||||||
project=dict(default=None),
|
project=dict(),
|
||||||
))
|
))
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=argument_spec,
|
argument_spec=argument_spec,
|
||||||
required_together=cs_required_together(),
|
required_together=cs_required_together(),
|
||||||
required_one_of = (
|
required_one_of=(
|
||||||
['vm', 'volume'],
|
['vm', 'volume'],
|
||||||
),
|
),
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
acs_snapshot_policy = AnsibleCloudStackSnapshotPolicy(module)
|
||||||
acs_snapshot_policy = AnsibleCloudStackSnapshotPolicy(module)
|
|
||||||
|
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
if state in ['absent']:
|
if state in ['absent']:
|
||||||
policy = acs_snapshot_policy.absent_snapshot_policy()
|
policy = acs_snapshot_policy.absent_snapshot_policy()
|
||||||
else:
|
else:
|
||||||
policy = acs_snapshot_policy.present_snapshot_policy()
|
policy = acs_snapshot_policy.present_snapshot_policy()
|
||||||
|
|
||||||
result = acs_snapshot_policy.get_result(policy)
|
|
||||||
|
|
||||||
except CloudStackException as e:
|
|
||||||
module.fail_json(msg='CloudStackException: %s' % str(e))
|
|
||||||
|
|
||||||
|
result = acs_snapshot_policy.get_result(policy)
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,6 @@ lib/ansible/modules/cloud/cloudstack/_cs_nic.py
|
||||||
lib/ansible/modules/cloud/cloudstack/cs_portforward.py
|
lib/ansible/modules/cloud/cloudstack/cs_portforward.py
|
||||||
lib/ansible/modules/cloud/cloudstack/cs_securitygroup.py
|
lib/ansible/modules/cloud/cloudstack/cs_securitygroup.py
|
||||||
lib/ansible/modules/cloud/cloudstack/cs_securitygroup_rule.py
|
lib/ansible/modules/cloud/cloudstack/cs_securitygroup_rule.py
|
||||||
lib/ansible/modules/cloud/cloudstack/cs_snapshot_policy.py
|
|
||||||
lib/ansible/modules/cloud/docker/_docker.py
|
lib/ansible/modules/cloud/docker/_docker.py
|
||||||
lib/ansible/modules/cloud/docker/docker_container.py
|
lib/ansible/modules/cloud/docker/docker_container.py
|
||||||
lib/ansible/modules/cloud/docker/docker_image.py
|
lib/ansible/modules/cloud/docker/docker_image.py
|
||||||
|
|
Loading…
Reference in a new issue