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
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['stableinterface'],
|
||||
'supported_by': 'community'}
|
||||
|
@ -26,13 +25,9 @@ options:
|
|||
description:
|
||||
- Name of the volume.
|
||||
- Either C(volume) or C(vm) is required.
|
||||
required: false
|
||||
default: null
|
||||
volume_type:
|
||||
description:
|
||||
- Type of the volume.
|
||||
required: false
|
||||
default: null
|
||||
choices:
|
||||
- DATADISK
|
||||
- ROOT
|
||||
|
@ -43,35 +38,27 @@ options:
|
|||
- 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.
|
||||
- Either C(volume) or C(vm) is required.
|
||||
required: false
|
||||
default: null
|
||||
version_added: "2.3"
|
||||
device_id:
|
||||
description:
|
||||
- ID of the device on a VM the volume is attached to.
|
||||
- This will only be considered if VM has multiple DATADISK volumes.
|
||||
required: false
|
||||
default: null
|
||||
version_added: "2.3"
|
||||
vpc:
|
||||
description:
|
||||
- Name of the vpc the instance is deployed in.
|
||||
required: false
|
||||
default: null
|
||||
version_added: "2.3"
|
||||
interval_type:
|
||||
description:
|
||||
- Interval of the snapshot.
|
||||
required: false
|
||||
default: 'daily'
|
||||
choices: [ 'hourly', 'daily', 'weekly', 'monthly' ]
|
||||
aliases: [ 'interval' ]
|
||||
default: daily
|
||||
choices: [ hourly, daily, weekly, monthly ]
|
||||
aliases: [ interval ]
|
||||
max_snaps:
|
||||
description:
|
||||
- Max number of snapshots.
|
||||
required: false
|
||||
default: 8
|
||||
aliases: [ 'max' ]
|
||||
aliases: [ max ]
|
||||
schedule:
|
||||
description:
|
||||
- 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=WEEKLY): C(MM:HH:DD (1-7))'
|
||||
- 'Format for C(interval_type=MONTHLY): C(MM:HH:DD (1-28))'
|
||||
required: false
|
||||
default: null
|
||||
time_zone:
|
||||
description:
|
||||
- Specifies a timezone for this command.
|
||||
required: false
|
||||
default: 'UTC'
|
||||
aliases: [ 'timezone' ]
|
||||
default: UTC
|
||||
aliases: [ timezone ]
|
||||
state:
|
||||
description:
|
||||
- State of the snapshot policy.
|
||||
required: false
|
||||
default: 'present'
|
||||
choices: [ 'present', 'absent' ]
|
||||
default: present
|
||||
choices: [ present, absent ]
|
||||
domain:
|
||||
description:
|
||||
- Domain the volume is related to.
|
||||
required: false
|
||||
default: null
|
||||
account:
|
||||
description:
|
||||
- Account the volume is related to.
|
||||
required: false
|
||||
default: null
|
||||
project:
|
||||
description:
|
||||
- Name of the project the volume is related to.
|
||||
required: false
|
||||
default: null
|
||||
extends_documentation_fragment: cloudstack
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Ensure a snapshot policy daily at 1h00 UTC
|
||||
- local_action:
|
||||
- name: ensure a snapshot policy daily at 1h00 UTC
|
||||
local_action:
|
||||
module: cs_snapshot_policy
|
||||
volume: ROOT-478
|
||||
schedule: '00:1'
|
||||
max_snaps: 3
|
||||
|
||||
# Ensure a snapshot policy daily at 1h00 UTC on the second DATADISK of VM web-01
|
||||
- local_action:
|
||||
- name: ensure a snapshot policy daily at 1h00 UTC on the second DATADISK of VM web-01
|
||||
local_action:
|
||||
module: cs_snapshot_policy
|
||||
vm: web-01
|
||||
volume_type: DATADISK
|
||||
|
@ -128,16 +105,16 @@ EXAMPLES = '''
|
|||
schedule: '00:1'
|
||||
max_snaps: 3
|
||||
|
||||
# Ensure a snapshot policy hourly at minute 5 UTC
|
||||
- local_action:
|
||||
- name: ensure a snapshot policy hourly at minute 5 UTC
|
||||
local_action:
|
||||
module: cs_snapshot_policy
|
||||
volume: ROOT-478
|
||||
schedule: '5'
|
||||
interval_type: hourly
|
||||
max_snaps: 1
|
||||
|
||||
# Ensure a snapshot policy weekly on Sunday at 05h00, TZ Europe/Zurich
|
||||
- local_action:
|
||||
- name: ensure a snapshot policy weekly on Sunday at 05h00, TZ Europe/Zurich
|
||||
local_action:
|
||||
module: cs_snapshot_policy
|
||||
volume: ROOT-478
|
||||
schedule: '00:5:1'
|
||||
|
@ -145,8 +122,8 @@ EXAMPLES = '''
|
|||
max_snaps: 1
|
||||
time_zone: 'Europe/Zurich'
|
||||
|
||||
# Ensure a snapshot policy is absent
|
||||
- local_action:
|
||||
- name: ensure a snapshot policy is absent
|
||||
local_action:
|
||||
module: cs_snapshot_policy
|
||||
volume: ROOT-478
|
||||
interval_type: hourly
|
||||
|
@ -206,13 +183,13 @@ domain:
|
|||
type: string
|
||||
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.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):
|
||||
|
@ -225,10 +202,10 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
|||
'maxsnaps': 'max_snaps',
|
||||
}
|
||||
self.interval_types = {
|
||||
'hourly': 0,
|
||||
'daily': 1,
|
||||
'weekly': 2,
|
||||
'monthly': 3,
|
||||
'hourly': 0,
|
||||
'daily': 1,
|
||||
'weekly': 2,
|
||||
'monthly': 3,
|
||||
}
|
||||
self.volume = None
|
||||
|
||||
|
@ -248,7 +225,7 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
|||
'virtualmachineid': self.get_vm(key='id'),
|
||||
'type': self.module.params.get('volume_type'),
|
||||
}
|
||||
volumes = self.cs.listVolumes(**args)
|
||||
volumes = self.query_api('listVolumes', **args)
|
||||
if volumes:
|
||||
if volumes['count'] > 1:
|
||||
device_id = self.module.params.get('device_id')
|
||||
|
@ -268,7 +245,7 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
|||
args = {
|
||||
'volumeid': self.get_volume(key='id')
|
||||
}
|
||||
policies = self.cs.listSnapshotPolicies(**args)
|
||||
policies = self.query_api('listSnapshotPolicies', **args)
|
||||
if policies:
|
||||
for policy in policies['snapshotpolicy']:
|
||||
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'])):
|
||||
self.result['changed'] = True
|
||||
if not self.module.check_mode:
|
||||
res = self.cs.createSnapshotPolicy(**args)
|
||||
res = self.query_api('createSnapshotPolicy', **args)
|
||||
policy = res['snapshotpolicy']
|
||||
if 'errortext' in policy:
|
||||
self.module.fail_json(msg="Failed: '%s'" % policy['errortext'])
|
||||
return policy
|
||||
|
||||
def absent_snapshot_policy(self):
|
||||
|
@ -307,9 +282,7 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
|||
'id': policy['id']
|
||||
}
|
||||
if not self.module.check_mode:
|
||||
res = self.cs.deleteSnapshotPolicies(**args)
|
||||
if 'errortext' in res:
|
||||
self.module.fail_json(msg="Failed: '%s'" % policy['errortext'])
|
||||
self.query_api('deleteSnapshotPolicies', **args)
|
||||
return policy
|
||||
|
||||
def get_result(self, policy):
|
||||
|
@ -322,11 +295,11 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
|||
volume = self.get_volume()
|
||||
if volume:
|
||||
volume_results = {
|
||||
'volume': volume.get('name'),
|
||||
'zone': volume.get('zonename'),
|
||||
'project': volume.get('project'),
|
||||
'account': volume.get('account'),
|
||||
'domain': volume.get('domain'),
|
||||
'volume': volume.get('name'),
|
||||
'zone': volume.get('zonename'),
|
||||
'project': volume.get('project'),
|
||||
'account': volume.get('account'),
|
||||
'domain': volume.get('domain'),
|
||||
}
|
||||
self.result.update(volume_results)
|
||||
return self.result
|
||||
|
@ -335,44 +308,39 @@ class AnsibleCloudStackSnapshotPolicy(AnsibleCloudStack):
|
|||
def main():
|
||||
argument_spec = cs_argument_spec()
|
||||
argument_spec.update(dict(
|
||||
volume=dict(default=None),
|
||||
volume_type=dict(choices=['DATADISK', 'ROOT'], default=None),
|
||||
vm=dict(default=None),
|
||||
device_id=dict(type='int', default=None),
|
||||
vpc=dict(default=None),
|
||||
volume=dict(),
|
||||
volume_type=dict(choices=['DATADISK', 'ROOT']),
|
||||
vm=dict(),
|
||||
device_id=dict(type='int'),
|
||||
vpc=dict(),
|
||||
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']),
|
||||
max_snaps=dict(type='int', default=8, aliases=['max']),
|
||||
state=dict(choices=['present', 'absent'], default='present'),
|
||||
domain=dict(default=None),
|
||||
account=dict(default=None),
|
||||
project=dict(default=None),
|
||||
domain=dict(),
|
||||
account=dict(),
|
||||
project=dict(),
|
||||
))
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec=argument_spec,
|
||||
required_together=cs_required_together(),
|
||||
required_one_of = (
|
||||
required_one_of=(
|
||||
['vm', 'volume'],
|
||||
),
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
try:
|
||||
acs_snapshot_policy = AnsibleCloudStackSnapshotPolicy(module)
|
||||
acs_snapshot_policy = AnsibleCloudStackSnapshotPolicy(module)
|
||||
|
||||
state = module.params.get('state')
|
||||
if state in ['absent']:
|
||||
policy = acs_snapshot_policy.absent_snapshot_policy()
|
||||
else:
|
||||
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))
|
||||
state = module.params.get('state')
|
||||
if state in ['absent']:
|
||||
policy = acs_snapshot_policy.absent_snapshot_policy()
|
||||
else:
|
||||
policy = acs_snapshot_policy.present_snapshot_policy()
|
||||
|
||||
result = acs_snapshot_policy.get_result(policy)
|
||||
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_securitygroup.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_container.py
|
||||
lib/ansible/modules/cloud/docker/docker_image.py
|
||||
|
|
Loading…
Reference in a new issue