cloudstack: cs_instance: implement state=restored

This commit is contained in:
Rene Moser 2015-11-30 17:08:07 +01:00 committed by Matt Clay
parent fa852c14fe
commit 73c6a35cae

View file

@ -23,7 +23,7 @@ DOCUMENTATION = '''
module: cs_instance module: cs_instance
short_description: Manages instances and virtual machines on Apache CloudStack based clouds. short_description: Manages instances and virtual machines on Apache CloudStack based clouds.
description: description:
- Deploy, start, update, scale, restart, stop and destroy instances. - Deploy, start, update, scale, restart, restore, stop and destroy instances.
version_added: '2.0' version_added: '2.0'
author: "René Moser (@resmo)" author: "René Moser (@resmo)"
options: options:
@ -44,9 +44,10 @@ options:
state: state:
description: description:
- State of the instance. - State of the instance.
- C(restored) added in version 2.1.
required: false required: false
default: 'present' default: 'present'
choices: [ 'deployed', 'started', 'stopped', 'restarted', 'destroyed', 'expunged', 'present', 'absent' ] choices: [ 'deployed', 'started', 'stopped', 'restarted', 'restored', 'destroyed', 'expunged', 'present', 'absent' ]
service_offering: service_offering:
description: description:
- Name or id of the service offering of the new instance. - Name or id of the service offering of the new instance.
@ -427,7 +428,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
iso = self.module.params.get('iso') iso = self.module.params.get('iso')
if not template and not iso: if not template and not iso:
self.module.fail_json(msg="Template or ISO is required.") return None
args = {} args = {}
args['account'] = self.get_account(key='name') args['account'] = self.get_account(key='name')
@ -494,6 +495,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
break break
return self.instance return self.instance
def get_iptonetwork_mappings(self): def get_iptonetwork_mappings(self):
network_mappings = self.module.params.get('ip_to_networks') network_mappings = self.module.params.get('ip_to_networks')
if network_mappings is None: if network_mappings is None:
@ -509,6 +511,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
res.append({'networkid': ids[i], 'ip': data['ip']}) res.append({'networkid': ids[i], 'ip': data['ip']})
return res return res
def get_network_ids(self, network_names=None): def get_network_ids(self, network_names=None):
if network_names is None: if network_names is None:
network_names = self.module.params.get('networks') network_names = self.module.params.get('networks')
@ -561,6 +564,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
user_data = base64.b64encode(user_data) user_data = base64.b64encode(user_data)
return user_data return user_data
def get_details(self): def get_details(self):
res = None res = None
cpu = self.module.params.get('cpu') cpu = self.module.params.get('cpu')
@ -574,6 +578,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
}] }]
return res return res
def deploy_instance(self, start_vm=True): def deploy_instance(self, start_vm=True):
self.result['changed'] = True self.result['changed'] = True
networkids = self.get_network_ids() networkids = self.get_network_ids()
@ -582,6 +587,9 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
args = {} args = {}
args['templateid'] = self.get_template_or_iso(key='id') args['templateid'] = self.get_template_or_iso(key='id')
if not args['templateid']:
self.module.fail_json(msg="Template or ISO is required.")
args['zoneid'] = self.get_zone(key='id') args['zoneid'] = self.get_zone(key='id')
args['serviceofferingid'] = self.get_service_offering_id() args['serviceofferingid'] = self.get_service_offering_id()
args['account'] = self.get_account(key='name') args['account'] = self.get_account(key='name')
@ -798,6 +806,28 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
return instance return instance
def restore_instance(self):
instance = self.get_instance()
if not instance:
instance = self.deploy_instance()
return instance
self.result['changed'] = True
args = {}
args['templateid'] = self.get_template_or_iso(key='id')
args['virtualmachineid'] = instance['id']
res = self.cs.restoreVirtualMachine(**args)
if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
poll_async = self.module.params.get('poll_async')
if poll_async:
instance = self._poll_job(res, 'virtualmachine')
return instance
def get_result(self, instance): def get_result(self, instance):
super(AnsibleCloudStackInstance, self).get_result(instance) super(AnsibleCloudStackInstance, self).get_result(instance)
if instance: if instance:
@ -817,13 +847,14 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
self.result['default_ip'] = nic['ipaddress'] self.result['default_ip'] = nic['ipaddress']
return self.result return self.result
def main(): def main():
argument_spec = cs_argument_spec() argument_spec = cs_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
name = dict(required=True), name = dict(required=True),
display_name = dict(default=None), display_name = dict(default=None),
group = dict(default=None), group = dict(default=None),
state = dict(choices=['present', 'deployed', 'started', 'stopped', 'restarted', 'absent', 'destroyed', 'expunged'], default='present'), state = dict(choices=['present', 'deployed', 'started', 'stopped', 'restarted', 'restored', 'absent', 'destroyed', 'expunged'], default='present'),
service_offering = dict(default=None), service_offering = dict(default=None),
cpu = dict(default=None, type='int'), cpu = dict(default=None, type='int'),
cpu_speed = dict(default=None, type='int'), cpu_speed = dict(default=None, type='int'),
@ -880,6 +911,9 @@ def main():
elif state in ['expunged']: elif state in ['expunged']:
instance = acs_instance.expunge_instance() instance = acs_instance.expunge_instance()
elif state in ['restored']:
instance = acs_instance.restore_instance()
elif state in ['present', 'deployed']: elif state in ['present', 'deployed']:
instance = acs_instance.present_instance() instance = acs_instance.present_instance()