From 99385de340043ab89a0db41a2864312424e696f8 Mon Sep 17 00:00:00 2001 From: Rene Moser Date: Wed, 16 Dec 2015 11:00:17 +0100 Subject: [PATCH] cloudstack: fixes and improvements cloudstack: fix python3 support and use type='bool' cloudstack: cs_instance: update vm on state=restored --- cloud/cloudstack/cs_account.py | 4 +-- cloud/cloudstack/cs_affinitygroup.py | 4 +-- cloud/cloudstack/cs_domain.py | 6 ++-- cloud/cloudstack/cs_firewall.py | 4 +-- cloud/cloudstack/cs_instance.py | 34 ++++++++----------- cloud/cloudstack/cs_instancegroup.py | 2 +- cloud/cloudstack/cs_ip_address.py | 4 +-- cloud/cloudstack/cs_iso.py | 10 +++--- cloud/cloudstack/cs_loadbalancer_rule.py | 6 ++-- .../cloudstack/cs_loadbalancer_rule_member.py | 4 +-- cloud/cloudstack/cs_network.py | 6 ++-- cloud/cloudstack/cs_portforward.py | 6 ++-- cloud/cloudstack/cs_project.py | 4 +-- cloud/cloudstack/cs_securitygroup.py | 2 +- cloud/cloudstack/cs_securitygroup_rule.py | 4 +-- cloud/cloudstack/cs_sshkeypair.py | 2 +- cloud/cloudstack/cs_staticnat.py | 4 +-- cloud/cloudstack/cs_template.py | 24 ++++++------- cloud/cloudstack/cs_user.py | 4 +-- cloud/cloudstack/cs_vmsnapshot.py | 6 ++-- 20 files changed, 68 insertions(+), 72 deletions(-) diff --git a/cloud/cloudstack/cs_account.py b/cloud/cloudstack/cs_account.py index 839f6e53281..80ab6748bb0 100644 --- a/cloud/cloudstack/cs_account.py +++ b/cloud/cloudstack/cs_account.py @@ -356,7 +356,7 @@ def main(): username = dict(default=None), password = dict(default=None), timezone = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -390,7 +390,7 @@ def main(): result = acs_acc.get_result(account) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_affinitygroup.py b/cloud/cloudstack/cs_affinitygroup.py index b1dc075c434..77323315d59 100644 --- a/cloud/cloudstack/cs_affinitygroup.py +++ b/cloud/cloudstack/cs_affinitygroup.py @@ -233,7 +233,7 @@ def main(): domain = dict(default=None), account = dict(default=None), project = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -256,7 +256,7 @@ def main(): result = acs_ag.get_result(affinity_group) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_domain.py b/cloud/cloudstack/cs_domain.py index 0d8b7deaab4..0d041d73dd0 100644 --- a/cloud/cloudstack/cs_domain.py +++ b/cloud/cloudstack/cs_domain.py @@ -244,8 +244,8 @@ def main(): path = dict(required=True), state = dict(choices=['present', 'absent'], default='present'), network_domain = dict(default=None), - clean_up = dict(choices=BOOLEANS, default=False), - poll_async = dict(choices=BOOLEANS, default=True), + clean_up = dict(type='bool', default=False), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -268,7 +268,7 @@ def main(): result = acs_dom.get_result(domain) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_firewall.py b/cloud/cloudstack/cs_firewall.py index 9834dd6713a..7a6bfb6c093 100644 --- a/cloud/cloudstack/cs_firewall.py +++ b/cloud/cloudstack/cs_firewall.py @@ -414,7 +414,7 @@ def main(): domain = dict(default=None), account = dict(default=None), project = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) required_together = cs_required_together() @@ -450,7 +450,7 @@ def main(): result = acs_fw.get_result(fw_rule) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_instance.py b/cloud/cloudstack/cs_instance.py index 852be68a347..88076fa1c51 100644 --- a/cloud/cloudstack/cs_instance.py +++ b/cloud/cloudstack/cs_instance.py @@ -44,7 +44,6 @@ options: state: description: - State of the instance. - - C(restored) added in version 2.1. required: false default: 'present' choices: [ 'deployed', 'started', 'stopped', 'restarted', 'restored', 'destroyed', 'expunged', 'present', 'absent' ] @@ -827,23 +826,19 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): def restore_instance(self): instance = self.get_instance() - - if not instance: - instance = self.deploy_instance() - return instance - self.result['changed'] = True + # in check mode intance may not be instanciated + if instance: + 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']) - 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') + poll_async = self.module.params.get('poll_async') + if poll_async: + instance = self._poll_job(res, 'virtualmachine') return instance @@ -897,9 +892,9 @@ def main(): user_data = dict(default=None), zone = dict(default=None), ssh_key = dict(default=None), - force = dict(choices=BOOLEANS, default=False), + force = dict(type='bool', default=False), tags = dict(type='list', aliases=[ 'tag' ], default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) required_together = cs_required_together() @@ -931,6 +926,7 @@ def main(): instance = acs_instance.expunge_instance() elif state in ['restored']: + acs_instance.present_instance() instance = acs_instance.restore_instance() elif state in ['present', 'deployed']: @@ -953,7 +949,7 @@ def main(): result = acs_instance.get_result(instance) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_instancegroup.py b/cloud/cloudstack/cs_instancegroup.py index 4ffda0ede1a..bece79013ee 100644 --- a/cloud/cloudstack/cs_instancegroup.py +++ b/cloud/cloudstack/cs_instancegroup.py @@ -199,7 +199,7 @@ def main(): result = acs_ig.get_result(instance_group) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_ip_address.py b/cloud/cloudstack/cs_ip_address.py index 1be597fd8cb..c5a65d6272c 100644 --- a/cloud/cloudstack/cs_ip_address.py +++ b/cloud/cloudstack/cs_ip_address.py @@ -233,7 +233,7 @@ def main(): account = dict(default=None), network = dict(default=None), project = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -256,7 +256,7 @@ def main(): result = acs_ip_address.get_result(ip_address) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_iso.py b/cloud/cloudstack/cs_iso.py index 98a06f6cd96..5508fdd21fa 100644 --- a/cloud/cloudstack/cs_iso.py +++ b/cloud/cloudstack/cs_iso.py @@ -306,10 +306,10 @@ def main(): account = dict(default=None), project = dict(default=None), checksum = dict(default=None), - is_ready = dict(choices=BOOLEANS, default=False), - bootable = dict(choices=BOOLEANS, default=True), - is_featured = dict(choices=BOOLEANS, default=False), - is_dynamically_scalable = dict(choices=BOOLEANS, default=False), + is_ready = dict(type='bool', default=False), + bootable = dict(type='bool', default=True), + is_featured = dict(type='bool', default=False), + is_dynamically_scalable = dict(type='bool', default=False), state = dict(choices=['present', 'absent'], default='present'), )) @@ -333,7 +333,7 @@ def main(): result = acs_iso.get_result(iso) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_loadbalancer_rule.py b/cloud/cloudstack/cs_loadbalancer_rule.py index 713aaad0d45..aec04cf7eb2 100644 --- a/cloud/cloudstack/cs_loadbalancer_rule.py +++ b/cloud/cloudstack/cs_loadbalancer_rule.py @@ -355,12 +355,12 @@ def main(): ip_address = dict(required=True, aliases=['public_ip']), cidr = dict(default=None), project = dict(default=None), - open_firewall = dict(choices=BOOLEANS, default=False), + open_firewall = dict(type='bool', default=False), tags = dict(type='list', aliases=['tag'], default=None), zone = dict(default=None), domain = dict(default=None), account = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -383,7 +383,7 @@ def main(): result = acs_lb_rule.get_result(rule) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_loadbalancer_rule_member.py b/cloud/cloudstack/cs_loadbalancer_rule_member.py index f0738830855..757c00674ef 100644 --- a/cloud/cloudstack/cs_loadbalancer_rule_member.py +++ b/cloud/cloudstack/cs_loadbalancer_rule_member.py @@ -335,7 +335,7 @@ def main(): domain = dict(default=None), project = dict(default=None), account = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -358,7 +358,7 @@ def main(): result = acs_lb_rule_member.get_result(rule) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_network.py b/cloud/cloudstack/cs_network.py index 6dea3dd3ca6..fa1c7a68870 100644 --- a/cloud/cloudstack/cs_network.py +++ b/cloud/cloudstack/cs_network.py @@ -539,14 +539,14 @@ def main(): vlan = dict(default=None), vpc = dict(default=None), isolated_pvlan = dict(default=None), - clean_up = dict(type='bool', choices=BOOLEANS, default=False), + clean_up = dict(type='bool', default=False), network_domain = dict(default=None), state = dict(choices=['present', 'absent', 'restarted' ], default='present'), acl_type = dict(choices=['account', 'domain'], default='account'), project = dict(default=None), domain = dict(default=None), account = dict(default=None), - poll_async = dict(type='bool', choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) required_together = cs_required_together() required_together.extend([ @@ -578,7 +578,7 @@ def main(): result = acs_network.get_result(network) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_portforward.py b/cloud/cloudstack/cs_portforward.py index 4d091f687d9..9932dcb0d6e 100644 --- a/cloud/cloudstack/cs_portforward.py +++ b/cloud/cloudstack/cs_portforward.py @@ -376,14 +376,14 @@ def main(): private_port = dict(type='int', required=True), private_end_port = dict(type='int', default=None), state = dict(choices=['present', 'absent'], default='present'), - open_firewall = dict(choices=BOOLEANS, default=False), + open_firewall = dict(type='bool', default=False), vm_guest_ip = dict(default=None), vm = dict(default=None), zone = dict(default=None), domain = dict(default=None), account = dict(default=None), project = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -405,7 +405,7 @@ def main(): result = acs_pf.get_result(pf_rule) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_project.py b/cloud/cloudstack/cs_project.py index 504fefc6f0c..6b37923d90d 100644 --- a/cloud/cloudstack/cs_project.py +++ b/cloud/cloudstack/cs_project.py @@ -266,7 +266,7 @@ def main(): state = dict(choices=['present', 'absent', 'active', 'suspended' ], default='present'), domain = dict(default=None), account = dict(default=None), - poll_async = dict(type='bool', choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -293,7 +293,7 @@ def main(): result = acs_project.get_result(project) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_securitygroup.py b/cloud/cloudstack/cs_securitygroup.py index 255d306c789..d0319059793 100644 --- a/cloud/cloudstack/cs_securitygroup.py +++ b/cloud/cloudstack/cs_securitygroup.py @@ -180,7 +180,7 @@ def main(): result = acs_sg.get_result(sg) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_securitygroup_rule.py b/cloud/cloudstack/cs_securitygroup_rule.py index 69e04ee7f92..a088c6c2c1e 100644 --- a/cloud/cloudstack/cs_securitygroup_rule.py +++ b/cloud/cloudstack/cs_securitygroup_rule.py @@ -384,7 +384,7 @@ def main(): end_port = dict(type='int', default=None), state = dict(choices=['present', 'absent'], default='present'), project = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) required_together = cs_required_together() required_together.extend([ @@ -417,7 +417,7 @@ def main(): result = acs_sg_rule.get_result(sg_rule) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_sshkeypair.py b/cloud/cloudstack/cs_sshkeypair.py index 7e665cd62f6..7794303f019 100644 --- a/cloud/cloudstack/cs_sshkeypair.py +++ b/cloud/cloudstack/cs_sshkeypair.py @@ -241,7 +241,7 @@ def main(): result = acs_sshkey.get_result(ssh_key) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_staticnat.py b/cloud/cloudstack/cs_staticnat.py index c8fba54885e..f6b5d3f9bae 100644 --- a/cloud/cloudstack/cs_staticnat.py +++ b/cloud/cloudstack/cs_staticnat.py @@ -269,7 +269,7 @@ def main(): domain = dict(default=None), account = dict(default=None), project = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -292,7 +292,7 @@ def main(): result = acs_static_nat.get_result(ip_address) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_template.py b/cloud/cloudstack/cs_template.py index 94803aeb9eb..dc043425ded 100644 --- a/cloud/cloudstack/cs_template.py +++ b/cloud/cloudstack/cs_template.py @@ -537,29 +537,29 @@ def main(): vm = dict(default=None), snapshot = dict(default=None), os_type = dict(default=None), - is_ready = dict(type='bool', choices=BOOLEANS, default=False), - is_public = dict(type='bool', choices=BOOLEANS, default=True), - is_featured = dict(type='bool', choices=BOOLEANS, default=False), - is_dynamically_scalable = dict(type='bool', choices=BOOLEANS, default=False), - is_extractable = dict(type='bool', choices=BOOLEANS, default=False), - is_routing = dict(type='bool', choices=BOOLEANS, default=False), + is_ready = dict(type='bool', default=False), + is_public = dict(type='bool', default=True), + is_featured = dict(type='bool', default=False), + is_dynamically_scalable = dict(type='bool', default=False), + is_extractable = dict(type='bool', default=False), + is_routing = dict(type='bool', default=False), checksum = dict(default=None), template_filter = dict(default='self', choices=['featured', 'self', 'selfexecutable', 'sharedexecutable', 'executable', 'community']), hypervisor = dict(choices=['KVM', 'VMware', 'BareMetal', 'XenServer', 'LXC', 'HyperV', 'UCS', 'OVM', 'Simulator'], default=None), - requires_hvm = dict(type='bool', choices=BOOLEANS, default=False), - password_enabled = dict(type='bool', choices=BOOLEANS, default=False), + requires_hvm = dict(type='bool', default=False), + password_enabled = dict(type='bool', default=False), template_tag = dict(default=None), - sshkey_enabled = dict(type='bool', choices=BOOLEANS, default=False), + sshkey_enabled = dict(type='bool', default=False), format = dict(choices=['QCOW2', 'RAW', 'VHD', 'OVA'], default=None), details = dict(default=None), bits = dict(type='int', choices=[ 32, 64 ], default=64), state = dict(choices=['present', 'absent'], default='present'), - cross_zones = dict(type='bool', choices=BOOLEANS, default=False), + cross_zones = dict(type='bool', default=False), zone = dict(default=None), domain = dict(default=None), account = dict(default=None), project = dict(default=None), - poll_async = dict(type='bool', choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) required_together = cs_required_together() @@ -595,7 +595,7 @@ def main(): result = acs_tpl.get_result(tpl) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_user.py b/cloud/cloudstack/cs_user.py index e6fe1c1f513..a0be4634904 100644 --- a/cloud/cloudstack/cs_user.py +++ b/cloud/cloudstack/cs_user.py @@ -415,7 +415,7 @@ def main(): last_name = dict(default=None), password = dict(default=None), timezone = dict(default=None), - poll_async = dict(choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) module = AnsibleModule( @@ -449,7 +449,7 @@ def main(): result = acs_acc.get_result(user) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result) diff --git a/cloud/cloudstack/cs_vmsnapshot.py b/cloud/cloudstack/cs_vmsnapshot.py index 43e561bb93a..bec9e5132e3 100644 --- a/cloud/cloudstack/cs_vmsnapshot.py +++ b/cloud/cloudstack/cs_vmsnapshot.py @@ -263,12 +263,12 @@ def main(): vm = dict(required=True), description = dict(default=None), zone = dict(default=None), - snapshot_memory = dict(choices=BOOLEANS, default=False), + snapshot_memory = dict(type='bool', default=False), state = dict(choices=['present', 'absent', 'revert'], default='present'), domain = dict(default=None), account = dict(default=None), project = dict(default=None), - poll_async = dict(type='bool', choices=BOOLEANS, default=True), + poll_async = dict(type='bool', default=True), )) required_together = cs_required_together() @@ -298,7 +298,7 @@ def main(): result = acs_vmsnapshot.get_result(snapshot) - except CloudStackException, e: + except CloudStackException as e: module.fail_json(msg='CloudStackException: %s' % str(e)) module.exit_json(**result)