From 30ad30c47041461c702bc739330bb117fcda6641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Sun, 16 Jul 2017 22:05:14 +0200 Subject: [PATCH] cloudstack: remove CloudStackException dep for several modules (#26874) * cloudstack: cs_affinitygroup: remove CloudStackException dependency * cloudstack: cs_domain: remove CloudStackException dependency * cloudstack: cs_firewall: remove CloudStackException dependency * cloudstack: cs_host: remove CloudStackException dependency * cloudstack: cs_instancegroup: remove CloudStackException dependency * cloudstack: cs_pod: remove CloudStackException dependency * cloudstack: cs_configuration: remove CloudStackException dependency, fix pep8 * cloudstack: cs_cluster: remove CloudStackException dependency * cloudstack: cs_network_acl: remove CloudStackException dependency * cloudstack: cs_network_acl_rule: remove CloudStackException dependency * cloudstack: cs_zone_facts: remove CloudStackException dependency * cloudstack: cs_zone: remove CloudStackException dependency * cloudstack: cs_vpn_gateway: remove CloudStackException dependency * cloudstack: cs_vpc: remove CloudStackException dependency * cloudstack: cs_sshkeypair: remove CloudStackException dependency * cloudstack: cs_role: remove CloudStackException dependency * cloudstack: cs_ip_address: remove CloudStackException dependency * cloudstack: cs_ip_staticnat: remove CloudStackException dependency * cloudstack: cs_resourcelimit: remove CloudStackException dependency * cloudstack: cs_region: remove CloudStackException dependency * cloudstack: cs_project: remove CloudStackException dependency * cloudstack: cs_network: remove CloudStackException dependency * cloudstack: cs_loadbalancer_rule_member: remove CloudStackException dependency * cloudstack: cs_loadbalancer_rule: remove CloudStackException dependency * cloudstack: cs_iso: remove CloudStackException dependency --- .../cloud/cloudstack/cs_affinitygroup.py | 33 +++----- .../modules/cloud/cloudstack/cs_cluster.py | 38 ++++----- .../cloud/cloudstack/cs_configuration.py | 83 +++++++++---------- .../modules/cloud/cloudstack/cs_domain.py | 34 +++----- .../modules/cloud/cloudstack/cs_firewall.py | 37 +++------ .../modules/cloud/cloudstack/cs_host.py | 45 ++++------ .../cloud/cloudstack/cs_instancegroup.py | 29 +++---- .../modules/cloud/cloudstack/cs_ip_address.py | 25 ++---- .../modules/cloud/cloudstack/cs_iso.py | 34 +++----- .../cloud/cloudstack/cs_loadbalancer_rule.py | 47 ++++------- .../cloudstack/cs_loadbalancer_rule_member.py | 31 +++---- .../modules/cloud/cloudstack/cs_network.py | 50 ++++------- .../cloud/cloudstack/cs_network_acl.py | 29 +++---- .../cloud/cloudstack/cs_network_acl_rule.py | 33 +++----- .../modules/cloud/cloudstack/cs_pod.py | 35 +++----- .../modules/cloud/cloudstack/cs_project.py | 48 ++++------- .../modules/cloud/cloudstack/cs_region.py | 34 +++----- .../cloud/cloudstack/cs_resourcelimit.py | 20 ++--- .../modules/cloud/cloudstack/cs_role.py | 45 +++++----- .../modules/cloud/cloudstack/cs_sshkeypair.py | 44 +++++----- .../modules/cloud/cloudstack/cs_staticnat.py | 37 +++------ .../modules/cloud/cloudstack/cs_vpc.py | 43 ++++------ .../cloud/cloudstack/cs_vpn_gateway.py | 29 +++---- .../modules/cloud/cloudstack/cs_zone.py | 36 +++----- .../modules/cloud/cloudstack/cs_zone_facts.py | 8 +- test/sanity/pep8/legacy-files.txt | 1 - 26 files changed, 333 insertions(+), 595 deletions(-) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_affinitygroup.py b/lib/ansible/modules/cloud/cloudstack/cs_affinitygroup.py index d5fc273647..92e3007cd1 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_affinitygroup.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_affinitygroup.py @@ -131,7 +131,6 @@ account: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -155,7 +154,7 @@ class AnsibleCloudStackAffinityGroup(AnsibleCloudStack): 'domainid': self.get_domain(key='id'), 'name': self.module.params.get('name'), } - affinity_groups = self.cs.listAffinityGroups(**args) + affinity_groups = self.query_api('listAffinityGroups', **args) if affinity_groups: self.affinity_group = affinity_groups['affinitygroup'][0] return self.affinity_group @@ -163,7 +162,7 @@ class AnsibleCloudStackAffinityGroup(AnsibleCloudStack): def get_affinity_type(self): affinity_type = self.module.params.get('affinty_type') - affinity_types = self.cs.listAffinityGroupTypes() + affinity_types = self.query_api('listAffinityGroupTypes', ) if affinity_types: if not affinity_type: return affinity_types['affinityGroupType'][0]['type'] @@ -187,10 +186,7 @@ class AnsibleCloudStackAffinityGroup(AnsibleCloudStack): 'domainid': self.get_domain(key='id'), } if not self.module.check_mode: - res = self.cs.createAffinityGroup(**args) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createAffinityGroup', **args) poll_async = self.module.params.get('poll_async') if res and poll_async: @@ -209,10 +205,7 @@ class AnsibleCloudStackAffinityGroup(AnsibleCloudStack): 'domainid': self.get_domain(key='id'), } if not self.module.check_mode: - res = self.cs.deleteAffinityGroup(**args) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteAffinityGroup', **args) poll_async = self.module.params.get('poll_async') if res and poll_async: @@ -239,19 +232,15 @@ def main(): supports_check_mode=True ) - try: - acs_ag = AnsibleCloudStackAffinityGroup(module) + acs_ag = AnsibleCloudStackAffinityGroup(module) - state = module.params.get('state') - if state in ['absent']: - affinity_group = acs_ag.remove_affinity_group() - else: - affinity_group = acs_ag.create_affinity_group() + state = module.params.get('state') + if state in ['absent']: + affinity_group = acs_ag.remove_affinity_group() + else: + affinity_group = acs_ag.create_affinity_group() - result = acs_ag.get_result(affinity_group) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_ag.get_result(affinity_group) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_cluster.py b/lib/ansible/modules/cloud/cloudstack/cs_cluster.py index 5645ebf9b7..790d26e387 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_cluster.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_cluster.py @@ -234,7 +234,6 @@ pod: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, CS_HYPERVISORS @@ -273,7 +272,7 @@ class AnsibleCloudStackCluster(AnsibleCloudStack): 'name': self.module.params.get('pod'), 'zoneid': self.get_zone(key='id'), } - pods = self.cs.listPods(**args) + pods = self.query_api('listPods', **args) if pods: return self._get_by_key(key, pods['pod'][0]) self.module.fail_json(msg="Pod %s not found in zone %s" % (self.module.params.get('pod'), self.get_zone(key='name'))) @@ -285,13 +284,13 @@ class AnsibleCloudStackCluster(AnsibleCloudStack): uuid = self.module.params.get('id') if uuid: args['id'] = uuid - clusters = self.cs.listClusters(**args) + clusters = self.query_api('listClusters', **args) if clusters: self.cluster = clusters['cluster'][0] return self.cluster args['name'] = self.module.params.get('name') - clusters = self.cs.listClusters(**args) + clusters = self.query_api('listClusters', **args) if clusters: self.cluster = clusters['cluster'][0] # fix different return from API then request argument given @@ -335,7 +334,7 @@ class AnsibleCloudStackCluster(AnsibleCloudStack): cluster = None if not self.module.check_mode: - res = self.cs.addCluster(**args) + res = self.query_api('addCluster', **args) if 'errortext' in res: self.module.fail_json(msg="Failed: '%s'" % res['errortext']) # API returns a list as result CLOUDSTACK-9205 @@ -355,10 +354,9 @@ class AnsibleCloudStackCluster(AnsibleCloudStack): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateCluster(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateCluster', **args) cluster = res['cluster'] + return cluster def absent_cluster(self): @@ -369,10 +367,10 @@ class AnsibleCloudStackCluster(AnsibleCloudStack): args = { 'id': cluster['id'], } + if not self.module.check_mode: - res = self.cs.deleteCluster(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + self.query_api('deleteCluster', **args) + return cluster @@ -406,19 +404,15 @@ def main(): supports_check_mode=True ) - try: - acs_cluster = AnsibleCloudStackCluster(module) + acs_cluster = AnsibleCloudStackCluster(module) - state = module.params.get('state') - if state in ['absent']: - cluster = acs_cluster.absent_cluster() - else: - cluster = acs_cluster.present_cluster() + state = module.params.get('state') + if state in ['absent']: + cluster = acs_cluster.absent_cluster() + else: + cluster = acs_cluster.present_cluster() - result = acs_cluster.get_result(cluster) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_cluster.get_result(cluster) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_configuration.py b/lib/ansible/modules/cloud/cloudstack/cs_configuration.py index 8ac97903ed..c4450dedba 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_configuration.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_configuration.py @@ -153,8 +153,13 @@ storage: sample: storage01 ''' -# import cloudstack common -from ansible.module_utils.cloudstack import * +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.cloudstack import ( + AnsibleCloudStack, + cs_argument_spec, + cs_required_together +) + class AnsibleCloudStackConfiguration(AnsibleCloudStack): @@ -162,39 +167,38 @@ class AnsibleCloudStackConfiguration(AnsibleCloudStack): super(AnsibleCloudStackConfiguration, self).__init__(module) self.returns = { 'category': 'category', - 'scope': 'scope', - 'value': 'value', + 'scope': 'scope', + 'value': 'value', } self.storage = None self.account = None self.cluster = None - def _get_common_configuration_args(self): - args = {} - args['name'] = self.module.params.get('name') - args['accountid'] = self.get_account(key='id') - args['storageid'] = self.get_storage(key='id') - args['zoneid'] = self.get_zone(key='id') - args['clusterid'] = self.get_cluster(key='id') + args = { + 'name': self.module.params.get('name'), + 'accountid': self.get_account(key='id'), + 'storageid': self.get_storage(key='id'), + 'zoneid': self.get_zone(key='id'), + 'clusterid': self.get_cluster(key='id'), + } return args - def get_zone(self, key=None): # make sure we do net use the default zone zone = self.module.params.get('zone') if zone: return super(AnsibleCloudStackConfiguration, self).get_zone(key=key) - def get_cluster(self, key=None): if not self.cluster: cluster_name = self.module.params.get('cluster') if not cluster_name: return None - args = {} - args['name'] = cluster_name - clusters = self.cs.listClusters(**args) + args = { + 'name': cluster_name, + } + clusters = self.query_api('listClusters', **args) if clusters: self.cluster = clusters['cluster'][0] self.result['cluster'] = self.cluster['name'] @@ -202,15 +206,15 @@ class AnsibleCloudStackConfiguration(AnsibleCloudStack): self.module.fail_json(msg="Cluster %s not found." % cluster_name) return self._get_by_key(key=key, my_dict=self.cluster) - def get_storage(self, key=None): if not self.storage: storage_pool_name = self.module.params.get('storage') if not storage_pool_name: return None - args = {} - args['name'] = storage_pool_name - storage_pools = self.cs.listStoragePools(**args) + args = { + 'name': storage_pool_name, + } + storage_pools = self.query_api('listStoragePools', **args) if storage_pools: self.storage = storage_pools['storagepool'][0] self.result['storage'] = self.storage['name'] @@ -218,24 +222,21 @@ class AnsibleCloudStackConfiguration(AnsibleCloudStack): self.module.fail_json(msg="Storage pool %s not found." % storage_pool_name) return self._get_by_key(key=key, my_dict=self.storage) - def get_configuration(self): configuration = None args = self._get_common_configuration_args() - configurations = self.cs.listConfigurations(**args) + configurations = self.query_api('listConfigurations', **args) if not configurations: self.module.fail_json(msg="Configuration %s not found." % args['name']) configuration = configurations['configuration'][0] return configuration - def get_value(self): value = str(self.module.params.get('value')) if value in ('True', 'False'): value = value.lower() return value - def present_configuration(self): configuration = self.get_configuration() args = self._get_common_configuration_args() @@ -243,13 +244,10 @@ class AnsibleCloudStackConfiguration(AnsibleCloudStack): if self.has_changed(args, configuration, ['value']): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateConfiguration(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateConfiguration', **args) configuration = res['configuration'] return configuration - def get_result(self, configuration): self.result = super(AnsibleCloudStackConfiguration, self).get_result(configuration) if self.account: @@ -259,16 +257,17 @@ class AnsibleCloudStackConfiguration(AnsibleCloudStack): self.result['zone'] = self.zone['name'] return self.result + def main(): argument_spec = cs_argument_spec() argument_spec.update(dict( - name = dict(required=True), - value = dict(type='str', required=True), - zone = dict(default=None), - storage = dict(default=None), - cluster = dict(default=None), - account = dict(default=None), - domain = dict(default='ROOT') + name=dict(required=True), + value=dict(type='str', required=True), + zone=dict(), + storage=dict(), + cluster=dict(), + account=dict(), + domain=dict(default='ROOT') )) module = AnsibleModule( @@ -277,17 +276,11 @@ def main(): supports_check_mode=True ) - try: - acs_configuration = AnsibleCloudStackConfiguration(module) - configuration = acs_configuration.present_configuration() - result = acs_configuration.get_result(configuration) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) - + acs_configuration = AnsibleCloudStackConfiguration(module) + configuration = acs_configuration.present_configuration() + result = acs_configuration.get_result(configuration) module.exit_json(**result) -# import module snippets -from ansible.module_utils.basic import * + if __name__ == '__main__': main() diff --git a/lib/ansible/modules/cloud/cloudstack/cs_domain.py b/lib/ansible/modules/cloud/cloudstack/cs_domain.py index 8fe6e42273..1cacd6a0c7 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_domain.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_domain.py @@ -114,7 +114,6 @@ network_domain: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -149,7 +148,7 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): 'listall': True } - domains = self.cs.listDomains(**args) + domains = self.query_api('listDomains', **args) if domains: for d in domains['domain']: if path == d['path'].lower(): @@ -194,9 +193,7 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): 'networkdomain': self.module.params.get('network_domain') } if not self.module.check_mode: - res = self.cs.createDomain(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createDomain', **args) domain = res['domain'] return domain @@ -208,9 +205,7 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): if self.has_changed(args, domain): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateDomain(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateDomain', **args) domain = res['domain'] return domain @@ -224,10 +219,7 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): 'id': domain['id'], 'cleanup': self.module.params.get('clean_up') } - res = self.cs.deleteDomain(**args) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteDomain', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -251,19 +243,15 @@ def main(): supports_check_mode=True ) - try: - acs_dom = AnsibleCloudStackDomain(module) + acs_dom = AnsibleCloudStackDomain(module) - state = module.params.get('state') - if state in ['absent']: - domain = acs_dom.absent_domain() - else: - domain = acs_dom.present_domain() + state = module.params.get('state') + if state in ['absent']: + domain = acs_dom.absent_domain() + else: + domain = acs_dom.present_domain() - result = acs_dom.get_result(domain) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_dom.get_result(domain) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_firewall.py b/lib/ansible/modules/cloud/cloudstack/cs_firewall.py index 721ae32793..f0c6f00ae1 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_firewall.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_firewall.py @@ -226,7 +226,6 @@ network: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -276,12 +275,12 @@ class AnsibleCloudStackFirewall(AnsibleCloudStack): args['networkid'] = self.get_network(key='id') if not args['networkid']: self.module.fail_json(msg="missing required argument for type egress: network") - firewall_rules = self.cs.listEgressFirewallRules(**args) + firewall_rules = self.query_api('listEgressFirewallRules', **args) else: args['ipaddressid'] = self.get_ip_address('id') if not args['ipaddressid']: self.module.fail_json(msg="missing required argument for type ingress: ip_address") - firewall_rules = self.cs.listFirewallRules(**args) + firewall_rules = self.query_api('listFirewallRules', **args) if firewall_rules and 'firewallrule' in firewall_rules: for rule in firewall_rules['firewallrule']: @@ -342,13 +341,10 @@ class AnsibleCloudStackFirewall(AnsibleCloudStack): if not self.module.check_mode: if fw_type == 'egress': args['networkid'] = self.get_network(key='id') - res = self.cs.createEgressFirewallRule(**args) + res = self.query_api('createEgressFirewallRule', **args) else: args['ipaddressid'] = self.get_ip_address('id') - res = self.cs.createFirewallRule(**args) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createFirewallRule', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -372,12 +368,9 @@ class AnsibleCloudStackFirewall(AnsibleCloudStack): fw_type = self.module.params.get('type') if not self.module.check_mode: if fw_type == 'egress': - res = self.cs.deleteEgressFirewallRule(**args) + res = self.query_api('deleteEgressFirewallRule', **args) else: - res = self.cs.deleteFirewallRule(**args) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteFirewallRule', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -433,19 +426,15 @@ def main(): supports_check_mode=True ) - try: - acs_fw = AnsibleCloudStackFirewall(module) + acs_fw = AnsibleCloudStackFirewall(module) - state = module.params.get('state') - if state in ['absent']: - fw_rule = acs_fw.remove_firewall_rule() - else: - fw_rule = acs_fw.create_firewall_rule() + state = module.params.get('state') + if state in ['absent']: + fw_rule = acs_fw.remove_firewall_rule() + else: + fw_rule = acs_fw.create_firewall_rule() - result = acs_fw.get_result(fw_rule) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_fw.get_result(fw_rule) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_host.py b/lib/ansible/modules/cloud/cloudstack/cs_host.py index ceed04a223..3b6b58cc7e 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_host.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_host.py @@ -349,7 +349,6 @@ zone: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, CS_HYPERVISORS @@ -415,7 +414,7 @@ class AnsibleCloudStackHost(AnsibleCloudStack): 'name': pod_name, 'zoneid': self.get_zone(key='id'), } - pods = self.cs.listPods(**args) + pods = self.query_api('listPods', **args) if pods: return self._get_by_key(key, pods['pod'][0]) self.module.fail_json(msg="Pod %s not found" % pod_name) @@ -428,7 +427,7 @@ class AnsibleCloudStackHost(AnsibleCloudStack): 'name': cluster_name, 'zoneid': self.get_zone(key='id'), } - clusters = self.cs.listClusters(**args) + clusters = self.query_api('listClusters', **args) if clusters: return self._get_by_key(key, clusters['cluster'][0]) self.module.fail_json(msg="Cluster %s not found" % cluster_name) @@ -447,7 +446,7 @@ class AnsibleCloudStackHost(AnsibleCloudStack): args = { 'zoneid': self.get_zone(key='id'), } - res = self.cs.listHosts(**args) + res = self.query_api('listHosts', **args) if res: for h in res['host']: if name in [h['ipaddress'], h['name']]: @@ -530,9 +529,7 @@ class AnsibleCloudStackHost(AnsibleCloudStack): 'hosttags': self.get_host_tags(), } if not self.module.check_mode: - host = self.cs.addHost(**args) - if 'errortext' in host: - self.module.fail_json(msg="Failed: '%s'" % host['errortext']) + host = self.query_api('addHost', **args) host = host['host'][0] return host @@ -549,9 +546,7 @@ class AnsibleCloudStackHost(AnsibleCloudStack): if self.has_changed(args, host): self.result['changed'] = True if not self.module.check_mode: - host = self.cs.updateHost(**args) - if 'errortext' in host: - self.module.fail_json(msg="Failed: '%s'" % host['errortext']) + host = self.query_api('updateHost', **args) host = host['host'] return host @@ -566,9 +561,7 @@ class AnsibleCloudStackHost(AnsibleCloudStack): if not self.module.check_mode: res = self.enable_maintenance(host) if res: - res = self.cs.deleteHost(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteHost', **args) return host def enable_maintenance(self, host): @@ -578,9 +571,7 @@ class AnsibleCloudStackHost(AnsibleCloudStack): 'id': host['id'], } if not self.module.check_mode: - res = self.cs.prepareHostForMaintenance(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('prepareHostForMaintenance', **args) self.poll_job(res, 'host') host = self._poll_for_maintenance() return host @@ -592,9 +583,7 @@ class AnsibleCloudStackHost(AnsibleCloudStack): 'id': host['id'], } if not self.module.check_mode: - res = self.cs.cancelHostMaintenance(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('cancelHostMaintenance', **args) host = self.poll_job(res, 'host') return host @@ -638,19 +627,15 @@ def main(): supports_check_mode=True ) - try: - acs_host = AnsibleCloudStackHost(module) + acs_host = AnsibleCloudStackHost(module) - state = module.params.get('state') - if state == 'absent': - host = acs_host.absent_host() - else: - host = acs_host.present_host() + state = module.params.get('state') + if state == 'absent': + host = acs_host.absent_host() + else: + host = acs_host.present_host() - result = acs_host.get_result(host) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_host.get_result(host) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instancegroup.py b/lib/ansible/modules/cloud/cloudstack/cs_instancegroup.py index fb470d7450..7ef2dd1ba2 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instancegroup.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instancegroup.py @@ -110,7 +110,6 @@ project: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -133,7 +132,7 @@ class AnsibleCloudStackInstanceGroup(AnsibleCloudStack): 'domainid': self.get_domain('id'), 'projectid': self.get_project('id'), } - instance_groups = self.cs.listInstanceGroups(**args) + instance_groups = self.query_api('listInstanceGroups', **args) if instance_groups: for g in instance_groups['instancegroup']: if name in [g['name'], g['id']]: @@ -153,9 +152,7 @@ class AnsibleCloudStackInstanceGroup(AnsibleCloudStack): 'projectid': self.get_project('id'), } if not self.module.check_mode: - res = self.cs.createInstanceGroup(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createInstanceGroup', **args) instance_group = res['instancegroup'] return instance_group @@ -164,9 +161,7 @@ class AnsibleCloudStackInstanceGroup(AnsibleCloudStack): if instance_group: self.result['changed'] = True if not self.module.check_mode: - res = self.cs.deleteInstanceGroup(id=instance_group['id']) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + self.query_api('deleteInstanceGroup', id=instance_group['id']) return instance_group @@ -186,19 +181,15 @@ def main(): supports_check_mode=True ) - try: - acs_ig = AnsibleCloudStackInstanceGroup(module) + acs_ig = AnsibleCloudStackInstanceGroup(module) - state = module.params.get('state') - if state in ['absent']: - instance_group = acs_ig.absent_instance_group() - else: - instance_group = acs_ig.present_instance_group() + state = module.params.get('state') + if state in ['absent']: + instance_group = acs_ig.absent_instance_group() + else: + instance_group = acs_ig.present_instance_group() - result = acs_ig.get_result(instance_group) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_ig.get_result(instance_group) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_ip_address.py b/lib/ansible/modules/cloud/cloudstack/cs_ip_address.py index 32e26d6bdd..3d67313c93 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_ip_address.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_ip_address.py @@ -135,7 +135,6 @@ domain: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, ) @@ -178,8 +177,6 @@ class AnsibleCloudStackIPAddress(AnsibleCloudStack): ip_address = None if not self.module.check_mode: res = self.cs.associateIpAddress(**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: @@ -196,8 +193,7 @@ class AnsibleCloudStackIPAddress(AnsibleCloudStack): self.result['changed'] = True if not self.module.check_mode: res = self.cs.disassociateIpAddress(id=ip_address['id']) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + poll_async = self.module.params.get('poll_async') if poll_async: self.poll_job(res, 'ipaddress') @@ -227,20 +223,15 @@ def main(): supports_check_mode=True ) - try: - acs_ip_address = AnsibleCloudStackIPAddress(module) + acs_ip_address = AnsibleCloudStackIPAddress(module) - state = module.params.get('state') - if state in ['absent']: - ip_address = acs_ip_address.disassociate_ip_address() - else: - ip_address = acs_ip_address.associate_ip_address() - - result = acs_ip_address.get_result(ip_address) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + state = module.params.get('state') + if state in ['absent']: + ip_address = acs_ip_address.disassociate_ip_address() + else: + ip_address = acs_ip_address.associate_ip_address() + result = acs_ip_address.get_result(ip_address) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_iso.py b/lib/ansible/modules/cloud/cloudstack/cs_iso.py index 20a1b713d2..9fb1face45 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_iso.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_iso.py @@ -275,7 +275,6 @@ tags: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -333,9 +332,7 @@ class AnsibleCloudStackIso(AnsibleCloudStack): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.registerIso(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('registerIso', **args) self.iso = res['iso'][0] return self.iso @@ -367,9 +364,7 @@ class AnsibleCloudStackIso(AnsibleCloudStack): args['zoneid'] = -1 if not self.module.check_mode: - res = self.cs.updateIso(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateIso', **args) self.iso = res['iso'] return self.iso @@ -391,7 +386,7 @@ class AnsibleCloudStackIso(AnsibleCloudStack): if not checksum: args['name'] = self.module.params.get('name') - isos = self.cs.listIsos(**args) + isos = self.query_api('listIsos', **args) if isos: if not checksum: self.iso = isos['iso'][0] @@ -416,9 +411,7 @@ class AnsibleCloudStackIso(AnsibleCloudStack): args['zoneid'] = self.get_zone(key='id') if not self.module.check_mode: - res = self.cs.deleteIso(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteIso', **args) poll_async = self.module.params.get('poll_async') if poll_async: self.poll_job(res, 'iso') @@ -466,20 +459,15 @@ def main(): supports_check_mode=True ) - try: - acs_iso = AnsibleCloudStackIso(module) + acs_iso = AnsibleCloudStackIso(module) - state = module.params.get('state') - if state in ['absent']: - iso = acs_iso.absent_iso() - else: - iso = acs_iso.present_iso() - - result = acs_iso.get_result(iso) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + state = module.params.get('state') + if state in ['absent']: + iso = acs_iso.absent_iso() + else: + iso = acs_iso.present_iso() + result = acs_iso.get_result(iso) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule.py b/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule.py index 60a21f305a..f2cf61ece7 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule.py @@ -225,7 +225,6 @@ state: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, ) @@ -248,7 +247,7 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack): } def get_rule(self, **kwargs): - rules = self.cs.listLoadBalancerRules(**kwargs) + rules = self.query_api('listLoadBalancerRules', **kwargs) if rules: return rules['loadbalancerrule'][0] @@ -263,16 +262,12 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack): } def present_lb_rule(self): - missing_params = [] - for required_params in [ + required_params = [ 'algorithm', 'private_port', 'public_port', - ]: - if not self.module.params.get(required_params): - missing_params.append(required_params) - if missing_params: - self.module.fail_json(msg="missing required arguments: %s" % ','.join(missing_params)) + ] + self.module.fail_on_missing_params(required_params=required_params) args = self._get_common_args() rule = self.get_rule(**args) @@ -297,9 +292,7 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack): 'description': self.module.params.get('description'), 'protocol': self.module.params.get('protocol'), }) - res = self.cs.createLoadBalancerRule(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createLoadBalancerRule', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -315,9 +308,7 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack): if self.has_changed(args, rule): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateLoadBalancerRule(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateLoadBalancerRule', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -330,12 +321,11 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack): if rule: self.result['changed'] = True if rule and not self.module.check_mode: - res = self.cs.deleteLoadBalancerRule(id=rule['id']) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteLoadBalancerRule', id=rule['id']) + poll_async = self.module.params.get('poll_async') if poll_async: - res = self.poll_job(res, 'loadbalancer') + self.poll_job(res, 'loadbalancer') return rule @@ -366,20 +356,15 @@ def main(): supports_check_mode=True ) - try: - acs_lb_rule = AnsibleCloudStackLBRule(module) + acs_lb_rule = AnsibleCloudStackLBRule(module) - state = module.params.get('state') - if state in ['absent']: - rule = acs_lb_rule.absent_lb_rule() - else: - rule = acs_lb_rule.present_lb_rule() - - result = acs_lb_rule.get_result(rule) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + state = module.params.get('state') + if state in ['absent']: + rule = acs_lb_rule.absent_lb_rule() + else: + rule = acs_lb_rule.present_lb_rule() + result = acs_lb_rule.get_result(rule) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule_member.py b/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule_member.py index cea0c3b324..6e6d7f94da 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule_member.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_loadbalancer_rule_member.py @@ -207,7 +207,6 @@ state: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, ) @@ -238,7 +237,7 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack): if self.module.params.get('ip_address'): args['publicipid'] = self.get_ip_address(key='id') - rules = self.cs.listLoadBalancerRules(**args) + rules = self.query_api('listLoadBalancerRules', **args) if rules: if len(rules['loadbalancerrule']) > 1: self.module.fail_json(msg="More than one rule having name %s. Please pass 'ip_address' as well." % args['name']) @@ -253,9 +252,7 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack): } def _get_members_of_rule(self, rule): - res = self.cs.listLoadBalancerRuleInstances(id=rule['id']) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('listLoadBalancerRuleInstances', id=rule['id']) return res.get('loadbalancerruleinstance', []) def _ensure_members(self, operation): @@ -283,7 +280,7 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack): return rule args = self._get_common_args() - vms = self.cs.listVirtualMachines(**args) + vms = self.query_api('listVirtualMachines', **args) to_change_ids = [] for name in to_change: for vm in vms.get('virtualmachine', []): @@ -301,8 +298,7 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack): id=rule['id'], virtualmachineids=to_change_ids, ) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + poll_async = self.module.params.get('poll_async') if poll_async: self.poll_job(res) @@ -344,20 +340,15 @@ def main(): supports_check_mode=True ) - try: - acs_lb_rule_member = AnsibleCloudStackLBRuleMember(module) + acs_lb_rule_member = AnsibleCloudStackLBRuleMember(module) - state = module.params.get('state') - if state in ['absent']: - rule = acs_lb_rule_member.remove_members() - else: - rule = acs_lb_rule_member.add_members() - - result = acs_lb_rule_member.get_result(rule) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + state = module.params.get('state') + if state in ['absent']: + rule = acs_lb_rule_member.remove_members() + else: + rule = acs_lb_rule_member.add_members() + result = acs_lb_rule_member.get_result(rule) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_network.py b/lib/ansible/modules/cloud/cloudstack/cs_network.py index 3f37ab0315..2c8519b661 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_network.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_network.py @@ -326,7 +326,6 @@ network_offering: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, ) @@ -363,7 +362,7 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): 'zoneid': self.get_zone(key='id') } - network_offerings = self.cs.listNetworkOfferings(**args) + network_offerings = self.query_api('listNetworkOfferings', **args) if network_offerings: for no in network_offerings['networkoffering']: if network_offering in [no['name'], no['displaytext'], no['id']]: @@ -388,7 +387,7 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id') } - networks = self.cs.listNetworks(**args) + networks = self.query_api('listNetworks', **args) if networks: for n in networks['network']: if network in [n['name'], n['displaytext'], n['id']]: @@ -411,10 +410,7 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): if self.has_changed(args, network): self.result['changed'] = True if not self.module.check_mode: - network = self.cs.updateNetwork(**args) - - if 'errortext' in network: - self.module.fail_json(msg="Failed: '%s'" % network['errortext']) + network = self.query_api('updateNetwork', **args) poll_async = self.module.params.get('poll_async') if network and poll_async: @@ -446,10 +442,7 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): }) if not self.module.check_mode: - res = self.cs.createNetwork(**args) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createNetwork', **args) network = res['network'] return network @@ -470,10 +463,7 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): } if not self.module.check_mode: - network = self.cs.restartNetwork(**args) - - if 'errortext' in network: - self.module.fail_json(msg="Failed: '%s'" % network['errortext']) + network = self.query_api('restartNetwork', **args) poll_async = self.module.params.get('poll_async') if network and poll_async: @@ -490,14 +480,11 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): } if not self.module.check_mode: - res = self.cs.deleteNetwork(**args) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteNetwork', **args) poll_async = self.module.params.get('poll_async') if res and poll_async: - res = self.poll_job(res, 'network') + self.poll_job(res, 'network') return network @@ -539,24 +526,19 @@ def main(): supports_check_mode=True ) - try: - acs_network = AnsibleCloudStackNetwork(module) + acs_network = AnsibleCloudStackNetwork(module) - state = module.params.get('state') - if state in ['absent']: - network = acs_network.absent_network() + state = module.params.get('state') + if state in ['absent']: + network = acs_network.absent_network() - elif state in ['restarted']: - network = acs_network.restart_network() + elif state in ['restarted']: + network = acs_network.restart_network() - else: - network = acs_network.present_network() - - result = acs_network.get_result(network) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + else: + network = acs_network.present_network() + result = acs_network.get_result(network) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_network_acl.py b/lib/ansible/modules/cloud/cloudstack/cs_network_acl.py index 52e643dcf8..16ffb6ffd7 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_network_acl.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_network_acl.py @@ -123,7 +123,6 @@ zone: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -139,7 +138,7 @@ class AnsibleCloudStackNetworkAcl(AnsibleCloudStack): 'name': self.module.params.get('name'), 'vpcid': self.get_vpc(key='id'), } - network_acls = self.cs.listNetworkACLLists(**args) + network_acls = self.query_api('listNetworkACLLists', **args) if network_acls: return network_acls['networkacllist'][0] return None @@ -154,9 +153,7 @@ class AnsibleCloudStackNetworkAcl(AnsibleCloudStack): 'vpcid': self.get_vpc(key='id') } if not self.module.check_mode: - res = self.cs.createNetworkACLList(**args) - if 'errortext' in res: - self.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createNetworkACLList', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -172,9 +169,7 @@ class AnsibleCloudStackNetworkAcl(AnsibleCloudStack): 'id': network_acl['id'], } if not self.module.check_mode: - res = self.cs.deleteNetworkACLList(**args) - if 'errortext' in res: - self.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteNetworkACLList', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -203,19 +198,15 @@ def main(): supports_check_mode=True ) - try: - acs_network_acl = AnsibleCloudStackNetworkAcl(module) + acs_network_acl = AnsibleCloudStackNetworkAcl(module) - state = module.params.get('state') - if state == 'absent': - network_acl = acs_network_acl.absent_network_acl() - else: - network_acl = acs_network_acl.present_network_acl() + state = module.params.get('state') + if state == 'absent': + network_acl = acs_network_acl.absent_network_acl() + else: + network_acl = acs_network_acl.present_network_acl() - result = acs_network_acl.get_result(network_acl) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_network_acl.get_result(network_acl) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_network_acl_rule.py b/lib/ansible/modules/cloud/cloudstack/cs_network_acl_rule.py index 50dd29d478..b581eafe43 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_network_acl_rule.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_network_acl_rule.py @@ -285,7 +285,6 @@ zone: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -317,7 +316,7 @@ class AnsibleCloudStackNetworkAclRule(AnsibleCloudStack): 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id'), } - network_acl_rules = self.cs.listNetworkACLs(**args) + network_acl_rules = self.query_api('listNetworkACLs', **args) for acl_rule in network_acl_rules.get('networkacl', []): if acl_rule['number'] == self.module.params.get('rule_position'): return acl_rule @@ -358,9 +357,7 @@ class AnsibleCloudStackNetworkAclRule(AnsibleCloudStack): 'id': network_acl_rule['id'], } if not self.module.check_mode: - res = self.cs.deleteNetworkACL(**args) - if 'errortext' in res: - self.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteNetworkACL', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -384,9 +381,7 @@ class AnsibleCloudStackNetworkAclRule(AnsibleCloudStack): 'cidrlist': self.module.params.get('cidr'), } if not self.module.check_mode: - res = self.cs.createNetworkACL(**args) - if 'errortext' in res: - self.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createNetworkACL', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -410,9 +405,7 @@ class AnsibleCloudStackNetworkAclRule(AnsibleCloudStack): if self.has_changed(args, network_acl_rule): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateNetworkACLItem(**args) - if 'errortext' in res: - self.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateNetworkACLItem', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -470,19 +463,15 @@ def main(): supports_check_mode=True ) - try: - acs_network_acl_rule = AnsibleCloudStackNetworkAclRule(module) + acs_network_acl_rule = AnsibleCloudStackNetworkAclRule(module) - state = module.params.get('state') - if state == 'absent': - network_acl_rule = acs_network_acl_rule.absent_network_acl_rule() - else: - network_acl_rule = acs_network_acl_rule.present_network_acl_rule() + state = module.params.get('state') + if state == 'absent': + network_acl_rule = acs_network_acl_rule.absent_network_acl_rule() + else: + network_acl_rule = acs_network_acl_rule.present_network_acl_rule() - result = acs_network_acl_rule.get_result(network_acl_rule) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_network_acl_rule.get_result(network_acl_rule) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_pod.py b/lib/ansible/modules/cloud/cloudstack/cs_pod.py index a408baf9f4..6eb58fa74a 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_pod.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_pod.py @@ -158,7 +158,6 @@ zone: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -199,14 +198,14 @@ class AnsibleCloudStackPod(AnsibleCloudStack): if uuid: args['id'] = uuid args['zoneid'] = self.get_zone(key='id') - pods = self.cs.listPods(**args) + pods = self.query_api('listPods', **args) if pods: self.pod = pods['pod'][0] return self.pod args['name'] = self.module.params.get('name') args['zoneid'] = self.get_zone(key='id') - pods = self.cs.listPods(**args) + pods = self.query_api('listPods', **args) if pods: self.pod = pods['pod'][0] return self.pod @@ -231,9 +230,7 @@ class AnsibleCloudStackPod(AnsibleCloudStack): self.result['changed'] = True args = self._get_common_pod_args() if not self.module.check_mode: - res = self.cs.createPod(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createPod', **args) pod = res['pod'] return pod @@ -246,9 +243,7 @@ class AnsibleCloudStackPod(AnsibleCloudStack): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updatePod(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updatePod', **args) pod = res['pod'] return pod @@ -261,9 +256,7 @@ class AnsibleCloudStackPod(AnsibleCloudStack): 'id': pod['id'] } if not self.module.check_mode: - res = self.cs.deletePod(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + self.query_api('deletePod', **args) return pod @@ -286,18 +279,14 @@ def main(): supports_check_mode=True ) - try: - acs_pod = AnsibleCloudStackPod(module) - state = module.params.get('state') - if state in ['absent']: - pod = acs_pod.absent_pod() - else: - pod = acs_pod.present_pod() + acs_pod = AnsibleCloudStackPod(module) + state = module.params.get('state') + if state in ['absent']: + pod = acs_pod.absent_pod() + else: + pod = acs_pod.present_pod() - result = acs_pod.get_result(pod) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_pod.get_result(pod) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_project.py b/lib/ansible/modules/cloud/cloudstack/cs_project.py index ae00e4ffbb..bd387685a2 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_project.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_project.py @@ -149,7 +149,6 @@ tags: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -165,7 +164,7 @@ class AnsibleCloudStackProject(AnsibleCloudStack): 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id') } - projects = self.cs.listProjects(**args) + projects = self.query_api('listProjects', **args) if projects: for p in projects['project']: if project.lower() in [p['name'].lower(), p['id']]: @@ -193,10 +192,7 @@ class AnsibleCloudStackProject(AnsibleCloudStack): if self.has_changed(args, project): self.result['changed'] = True if not self.module.check_mode: - project = self.cs.updateProject(**args) - - if 'errortext' in project: - self.module.fail_json(msg="Failed: '%s'" % project['errortext']) + project = self.query_api('updateProject', **args) poll_async = self.module.params.get('poll_async') if project and poll_async: @@ -213,10 +209,7 @@ class AnsibleCloudStackProject(AnsibleCloudStack): 'domainid': self.get_domain('id') } if not self.module.check_mode: - project = self.cs.createProject(**args) - - if 'errortext' in project: - self.module.fail_json(msg="Failed: '%s'" % project['errortext']) + project = self.query_api('createProject', **args) poll_async = self.module.params.get('poll_async') if project and poll_async: @@ -234,12 +227,9 @@ class AnsibleCloudStackProject(AnsibleCloudStack): } if not self.module.check_mode: if state == 'suspended': - project = self.cs.suspendProject(**args) + project = self.query_api('suspendProject', **args) else: - project = self.cs.activateProject(**args) - - if 'errortext' in project: - self.module.fail_json(msg="Failed: '%s'" % project['errortext']) + project = self.query_api('activateProject', **args) poll_async = self.module.params.get('poll_async') if project and poll_async: @@ -255,10 +245,7 @@ class AnsibleCloudStackProject(AnsibleCloudStack): 'id': project['id'] } if not self.module.check_mode: - res = self.cs.deleteProject(**args) - - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteProject', **args) poll_async = self.module.params.get('poll_async') if res and poll_async: @@ -284,24 +271,19 @@ def main(): supports_check_mode=True ) - try: - acs_project = AnsibleCloudStackProject(module) + acs_project = AnsibleCloudStackProject(module) - state = module.params.get('state') - if state in ['absent']: - project = acs_project.absent_project() + state = module.params.get('state') + if state in ['absent']: + project = acs_project.absent_project() - elif state in ['active', 'suspended']: - project = acs_project.state_project(state=state) + elif state in ['active', 'suspended']: + project = acs_project.state_project(state=state) - else: - project = acs_project.present_project() - - result = acs_project.get_result(project) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + else: + project = acs_project.present_project() + result = acs_project.get_result(project) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_region.py b/lib/ansible/modules/cloud/cloudstack/cs_region.py index f47b221134..ec0de6da4d 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_region.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_region.py @@ -106,7 +106,6 @@ portable_ip_service_enabled: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -124,7 +123,7 @@ class AnsibleCloudStackRegion(AnsibleCloudStack): def get_region(self): id = self.module.params.get('id') - regions = self.cs.listRegions(id=id) + regions = self.query_api('listRegions', id=id) if regions: return regions['region'][0] return None @@ -145,9 +144,7 @@ class AnsibleCloudStackRegion(AnsibleCloudStack): 'endpoint': self.module.params.get('endpoint') } if not self.module.check_mode: - res = self.cs.addRegion(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('addRegion', **args) region = res['region'] return region @@ -160,9 +157,7 @@ class AnsibleCloudStackRegion(AnsibleCloudStack): if self.has_changed(args, region): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateRegion(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateRegion', **args) region = res['region'] return region @@ -171,9 +166,7 @@ class AnsibleCloudStackRegion(AnsibleCloudStack): if region: self.result['changed'] = True if not self.module.check_mode: - res = self.cs.removeRegion(id=region['id']) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + self.query_api('removeRegion', id=region['id']) return region @@ -195,20 +188,15 @@ def main(): supports_check_mode=True ) - try: - acs_region = AnsibleCloudStackRegion(module) + acs_region = AnsibleCloudStackRegion(module) - state = module.params.get('state') - if state == 'absent': - region = acs_region.absent_region() - else: - region = acs_region.present_region() - - result = acs_region.get_result(region) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + state = module.params.get('state') + if state == 'absent': + region = acs_region.absent_region() + else: + region = acs_region.present_region() + result = acs_region.get_result(region) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_resourcelimit.py b/lib/ansible/modules/cloud/cloudstack/cs_resourcelimit.py index 6c36eca183..b2f72b2abc 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_resourcelimit.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_resourcelimit.py @@ -124,7 +124,6 @@ project: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_required_together, cs_argument_spec ) @@ -186,8 +185,6 @@ class AnsibleCloudStackResourceLimit(AnsibleCloudStack): self.result['changed'] = True if not self.module.check_mode: res = self.cs.updateResourceLimit(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) resource_limit = res['resourcelimit'] return resource_limit @@ -202,9 +199,9 @@ def main(): argument_spec.update(dict( resource_type=dict(required=True, choices=RESOURCE_TYPES.keys(), aliases=['type']), limit=dict(default=-1, aliases=['max'], type='int'), - domain=dict(default=None), - account=dict(default=None), - project=dict(default=None), + domain=dict(), + account=dict(), + project=dict(), )) module = AnsibleModule( @@ -213,14 +210,9 @@ def main(): supports_check_mode=True ) - try: - acs_resource_limit = AnsibleCloudStackResourceLimit(module) - resource_limit = acs_resource_limit.update_resource_limit() - result = acs_resource_limit.get_result(resource_limit) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) - + acs_resource_limit = AnsibleCloudStackResourceLimit(module) + resource_limit = acs_resource_limit.update_resource_limit() + result = acs_resource_limit.get_result(resource_limit) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_role.py b/lib/ansible/modules/cloud/cloudstack/cs_role.py index 4971555019..eceba9723c 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_role.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_role.py @@ -108,7 +108,11 @@ role_type: ''' from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.cloudstack import AnsibleCloudStack, CloudStackException, cs_argument_spec, cs_required_together +from ansible.module_utils.cloudstack import ( + AnsibleCloudStack, + cs_argument_spec, + cs_required_together, +) class AnsibleCloudStackRole(AnsibleCloudStack): @@ -125,14 +129,14 @@ class AnsibleCloudStackRole(AnsibleCloudStack): args = { 'id': uuid, } - roles = self.cs.listRoles(**args) + roles = self.query_api('listRoles', **args) if roles: return roles['role'][0] else: args = { 'name': self.module.params.get('name'), } - roles = self.cs.listRoles(**args) + roles = self.query_api('listRoles', **args) if roles: return roles['role'][0] return None @@ -153,9 +157,7 @@ class AnsibleCloudStackRole(AnsibleCloudStack): 'description': self.module.params.get('description'), } if not self.module.check_mode: - res = self.cs.createRole(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createRole', **args) role = res['role'] return role @@ -168,9 +170,8 @@ class AnsibleCloudStackRole(AnsibleCloudStack): if self.has_changed(args, role): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateRole(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateRole', **args) + # The API as in 4.9 does not return an updated role yet if 'role' not in res: role = self.get_role() @@ -186,18 +187,16 @@ class AnsibleCloudStackRole(AnsibleCloudStack): 'id': role['id'], } if not self.module.check_mode: - res = self.cs.deleteRole(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + self.query_api('deleteRole', **args) return role def main(): argument_spec = cs_argument_spec() argument_spec.update(dict( - uuid=dict(default=None, aliases=['id']), + uuid=dict(aliases=['id']), name=dict(required=True), - description=dict(default=None), + description=dict(), role_type=dict(choices=['User', 'DomainAdmin', 'ResourceAdmin', 'Admin'], default='User'), state=dict(choices=['present', 'absent'], default='present'), )) @@ -208,18 +207,14 @@ def main(): supports_check_mode=True ) - try: - acs_role = AnsibleCloudStackRole(module) - state = module.params.get('state') - if state == 'absent': - role = acs_role.absent_role() - else: - role = acs_role.present_role() + acs_role = AnsibleCloudStackRole(module) + state = module.params.get('state') + if state == 'absent': + role = acs_role.absent_role() + else: + role = acs_role.present_role() - result = acs_role.get_result(role) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_role.get_result(role) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_sshkeypair.py b/lib/ansible/modules/cloud/cloudstack/cs_sshkeypair.py index fa4cb13ffb..65e0e72f03 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_sshkeypair.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_sshkeypair.py @@ -123,7 +123,6 @@ from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_native from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_required_together, cs_argument_spec ) @@ -150,7 +149,7 @@ class AnsibleCloudStackSshKey(AnsibleCloudStack): args['publickey'] = public_key if not self.module.check_mode: args['name'] = name - res = self.cs.registerSSHKeyPair(**args) + res = self.query_api('registerSSHKeyPair', **args) else: fingerprint = self._get_ssh_fingerprint(public_key) if ssh_key['fingerprint'] != fingerprint: @@ -158,26 +157,26 @@ class AnsibleCloudStackSshKey(AnsibleCloudStack): if not self.module.check_mode: # delete the ssh key with matching name but wrong fingerprint args['name'] = name - self.cs.deleteSSHKeyPair(**args) + self.query_api('deleteSSHKeyPair', **args) elif ssh_key['name'].lower() != name.lower(): self.result['changed'] = True if not self.module.check_mode: # delete the ssh key with matching fingerprint but wrong name args['name'] = ssh_key['name'] - self.cs.deleteSSHKeyPair(**args) + self.query_api('deleteSSHKeyPair', **args) # First match for key retrievment will be the fingerprint. # We need to make another lookup if there is a key with identical name. self.ssh_key = None ssh_key = self.get_ssh_key() if ssh_key['fingerprint'] != fingerprint: args['name'] = name - self.cs.deleteSSHKeyPair(**args) + self.query_api('deleteSSHKeyPair', **args) if not self.module.check_mode and self.result['changed']: args['publickey'] = public_key args['name'] = name - res = self.cs.registerSSHKeyPair(**args) + res = self.query_api('registerSSHKeyPair', **args) if res and 'keypair' in res: ssh_key = res['keypair'] @@ -191,7 +190,7 @@ class AnsibleCloudStackSshKey(AnsibleCloudStack): args = self._get_common_args() args['name'] = self.module.params.get('name') if not self.module.check_mode: - res = self.cs.createSSHKeyPair(**args) + res = self.query_api('createSSHKeyPair', **args) ssh_key = res['keypair'] return ssh_key @@ -202,7 +201,7 @@ class AnsibleCloudStackSshKey(AnsibleCloudStack): args = self._get_common_args() args['name'] = name or self.module.params.get('name') if not self.module.check_mode: - self.cs.deleteSSHKeyPair(**args) + self.query_api('deleteSSHKeyPair', **args) return ssh_key def _get_common_args(self): @@ -219,14 +218,14 @@ class AnsibleCloudStackSshKey(AnsibleCloudStack): # Query by fingerprint of the public key args_fingerprint = self._get_common_args() args_fingerprint['fingerprint'] = self._get_ssh_fingerprint(public_key) - ssh_keys = self.cs.listSSHKeyPairs(**args_fingerprint) + ssh_keys = self.query_api('listSSHKeyPairs', **args_fingerprint) if ssh_keys and 'sshkeypair' in ssh_keys: self.ssh_key = ssh_keys['sshkeypair'][0] # When key has not been found by fingerprint, use the name if not self.ssh_key: args_name = self._get_common_args() args_name['name'] = self.module.params.get('name') - ssh_keys = self.cs.listSSHKeyPairs(**args_name) + ssh_keys = self.query_api('listSSHKeyPairs', **args_name) if ssh_keys and 'sshkeypair' in ssh_keys: self.ssh_key = ssh_keys['sshkeypair'][0] return self.ssh_key @@ -258,23 +257,18 @@ def main(): if not HAS_LIB_SSHPUBKEYS: module.fail_json(msg="python library sshpubkeys required: pip install sshpubkeys") - try: - acs_sshkey = AnsibleCloudStackSshKey(module) - state = module.params.get('state') - if state in ['absent']: - ssh_key = acs_sshkey.remove_ssh_key() + acs_sshkey = AnsibleCloudStackSshKey(module) + state = module.params.get('state') + if state in ['absent']: + ssh_key = acs_sshkey.remove_ssh_key() + else: + public_key = module.params.get('public_key') + if public_key: + ssh_key = acs_sshkey.register_ssh_key(public_key) else: - public_key = module.params.get('public_key') - if public_key: - ssh_key = acs_sshkey.register_ssh_key(public_key) - else: - ssh_key = acs_sshkey.create_ssh_key() - - result = acs_sshkey.get_result(ssh_key) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + ssh_key = acs_sshkey.create_ssh_key() + result = acs_sshkey.get_result(ssh_key) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_staticnat.py b/lib/ansible/modules/cloud/cloudstack/cs_staticnat.py index 23229f1450..34138871ab 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_staticnat.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_staticnat.py @@ -160,7 +160,6 @@ domain: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, ) @@ -186,9 +185,7 @@ class AnsibleCloudStackStaticNat(AnsibleCloudStack): 'networkid': self.get_network(key='id') } if not self.module.check_mode: - res = self.cs.enableStaticNat(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + self.query_api('enableStaticNat', **args) # reset ip address and query new values self.ip_address = None @@ -207,13 +204,10 @@ class AnsibleCloudStackStaticNat(AnsibleCloudStack): if self.has_changed(args, ip_address, ['vmguestip', 'virtualmachineid']): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.disableStaticNat(ipaddressid=ip_address['id']) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('disableStaticNat', ipaddressid=ip_address['id']) self.poll_job(res, 'staticnat') - res = self.cs.enableStaticNat(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + + self.query_api('enableStaticNat', **args) # reset ip address and query new values self.ip_address = None @@ -233,9 +227,8 @@ class AnsibleCloudStackStaticNat(AnsibleCloudStack): if ip_address['isstaticnat']: self.result['changed'] = True if not self.module.check_mode: - res = self.cs.disableStaticNat(ipaddressid=ip_address['id']) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('disableStaticNat', ipaddressid=ip_address['id']) + poll_async = self.module.params.get('poll_async') if poll_async: self.poll_job(res, 'staticnat') @@ -264,19 +257,15 @@ def main(): supports_check_mode=True ) - try: - acs_static_nat = AnsibleCloudStackStaticNat(module) + acs_static_nat = AnsibleCloudStackStaticNat(module) - state = module.params.get('state') - if state in ['absent']: - ip_address = acs_static_nat.absent_static_nat() - else: - ip_address = acs_static_nat.present_static_nat() + state = module.params.get('state') + if state in ['absent']: + ip_address = acs_static_nat.absent_static_nat() + else: + ip_address = acs_static_nat.present_static_nat() - result = acs_static_nat.get_result(ip_address) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_static_nat.get_result(ip_address) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_vpc.py b/lib/ansible/modules/cloud/cloudstack/cs_vpc.py index fe361b2ef5..2129277d2e 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_vpc.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_vpc.py @@ -210,7 +210,6 @@ tags: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, ) @@ -242,7 +241,7 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): else: args['isdefault'] = True - vpc_offerings = self.cs.listVPCOfferings(**args) + vpc_offerings = self.query_api('listVPCOfferings', **args) if vpc_offerings: self.vpc_offering = vpc_offerings['vpcoffering'][0] return self._get_by_key(key, self.vpc_offering) @@ -257,7 +256,7 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): 'projectid': self.get_project(key='id'), 'zoneid': self.get_zone(key='id'), } - vpcs = self.cs.listVPCs(**args) + vpcs = self.query_api('listVPCs', **args) if vpcs: vpc_name = self.module.params.get('name') for v in vpcs['vpc']: @@ -276,9 +275,7 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): args = { 'id': vpc['id'], } - res = self.cs.restartVPC(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('restartVPC', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -310,9 +307,7 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): } self.result['diff']['after'] = args if not self.module.check_mode: - res = self.cs.createVPC(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createVPC', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -327,9 +322,7 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): if self.has_changed(args, vpc): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateVPC(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateVPC', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -342,9 +335,7 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): self.result['changed'] = True self.result['diff']['before'] = vpc if not self.module.check_mode: - res = self.cs.deleteVPC(id=vpc['id']) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteVPC', id=vpc['id']) poll_async = self.module.params.get('poll_async') if poll_async: @@ -378,21 +369,17 @@ def main(): supports_check_mode=True, ) - try: - acs_vpc = AnsibleCloudStackVpc(module) + acs_vpc = AnsibleCloudStackVpc(module) - state = module.params.get('state') - if state == 'absent': - vpc = acs_vpc.absent_vpc() - elif state == 'restarted': - vpc = acs_vpc.restart_vpc() - else: - vpc = acs_vpc.present_vpc() + state = module.params.get('state') + if state == 'absent': + vpc = acs_vpc.absent_vpc() + elif state == 'restarted': + vpc = acs_vpc.restart_vpc() + else: + vpc = acs_vpc.present_vpc() - result = acs_vpc.get_result(vpc) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_vpc.get_result(vpc) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_vpn_gateway.py b/lib/ansible/modules/cloud/cloudstack/cs_vpn_gateway.py index 0694b82fd9..4cfe54f444 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_vpn_gateway.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_vpn_gateway.py @@ -121,7 +121,6 @@ project: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together ) @@ -142,7 +141,7 @@ class AnsibleCloudStackVpnGateway(AnsibleCloudStack): 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id') } - vpn_gateways = self.cs.listVpnGateways(**args) + vpn_gateways = self.query_api('listVpnGateways', **args) if vpn_gateways: return vpn_gateways['vpngateway'][0] return None @@ -158,9 +157,7 @@ class AnsibleCloudStackVpnGateway(AnsibleCloudStack): 'projectid': self.get_project(key='id') } if not self.module.check_mode: - res = self.cs.createVpnGateway(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createVpnGateway', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -176,9 +173,7 @@ class AnsibleCloudStackVpnGateway(AnsibleCloudStack): 'id': vpn_gateway['id'] } if not self.module.check_mode: - res = self.cs.deleteVpnGateway(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('deleteVpnGateway', **args) poll_async = self.module.params.get('poll_async') if poll_async: @@ -211,19 +206,15 @@ def main(): supports_check_mode=True ) - try: - acs_vpn_gw = AnsibleCloudStackVpnGateway(module) + acs_vpn_gw = AnsibleCloudStackVpnGateway(module) - state = module.params.get('state') - if state == "absent": - vpn_gateway = acs_vpn_gw.absent_vpn_gateway() - else: - vpn_gateway = acs_vpn_gw.present_vpn_gateway() + state = module.params.get('state') + if state == "absent": + vpn_gateway = acs_vpn_gw.absent_vpn_gateway() + else: + vpn_gateway = acs_vpn_gw.present_vpn_gateway() - result = acs_vpn_gw.get_result(vpn_gateway) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_vpn_gw.get_result(vpn_gateway) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_zone.py b/lib/ansible/modules/cloud/cloudstack/cs_zone.py index f5731ff3d8..827ce0abd9 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_zone.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_zone.py @@ -234,7 +234,6 @@ tags: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, cs_required_together, ) @@ -289,13 +288,13 @@ class AnsibleCloudStackZone(AnsibleCloudStack): uuid = self.module.params.get('id') if uuid: args['id'] = uuid - zones = self.cs.listZones(**args) + zones = self.query_api('listZones', **args) if zones: self.zone = zones['zone'][0] return self.zone args['name'] = self.module.params.get('name') - zones = self.cs.listZones(**args) + zones = self.query_api('listZones', **args) if zones: self.zone = zones['zone'][0] return self.zone @@ -322,9 +321,7 @@ class AnsibleCloudStackZone(AnsibleCloudStack): zone = None if not self.module.check_mode: - res = self.cs.createZone(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('createZone', **args) zone = res['zone'] return zone @@ -338,9 +335,7 @@ class AnsibleCloudStackZone(AnsibleCloudStack): self.result['changed'] = True if not self.module.check_mode: - res = self.cs.updateZone(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + res = self.query_api('updateZone', **args) zone = res['zone'] return zone @@ -353,9 +348,8 @@ class AnsibleCloudStackZone(AnsibleCloudStack): 'id': zone['id'] } if not self.module.check_mode: - res = self.cs.deleteZone(**args) - if 'errortext' in res: - self.module.fail_json(msg="Failed: '%s'" % res['errortext']) + self.query_api('deleteZone', **args) + return zone @@ -386,19 +380,15 @@ def main(): supports_check_mode=True ) - try: - acs_zone = AnsibleCloudStackZone(module) + acs_zone = AnsibleCloudStackZone(module) - state = module.params.get('state') - if state in ['absent']: - zone = acs_zone.absent_zone() - else: - zone = acs_zone.present_zone() + state = module.params.get('state') + if state in ['absent']: + zone = acs_zone.absent_zone() + else: + zone = acs_zone.present_zone() - result = acs_zone.get_result(zone) - - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + result = acs_zone.get_result(zone) module.exit_json(**result) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py b/lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py index 094bfd7811..989daca4f9 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_zone_facts.py @@ -146,7 +146,6 @@ cloudstack_zone.tags: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.cloudstack import ( AnsibleCloudStack, - CloudStackException, cs_argument_spec, ) @@ -197,11 +196,8 @@ def main(): supports_check_mode=False, ) - try: - cs_zone_facts = AnsibleCloudStackZoneFacts(module=module).run() - cs_facts_result = dict(changed=False, ansible_facts=cs_zone_facts) - except CloudStackException as e: - module.fail_json(msg='CloudStackException: %s' % str(e)) + cs_zone_facts = AnsibleCloudStackZoneFacts(module=module).run() + cs_facts_result = dict(changed=False, ansible_facts=cs_zone_facts) module.exit_json(**cs_facts_result) diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 6e6bf3003c..4d91b444ba 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -83,7 +83,6 @@ lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork.py lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_facts.py lib/ansible/modules/cloud/centurylink/clc_loadbalancer.py lib/ansible/modules/cloud/cloudscale/cloudscale_server.py -lib/ansible/modules/cloud/cloudstack/cs_configuration.py lib/ansible/modules/cloud/cloudstack/cs_instance.py lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py lib/ansible/modules/cloud/cloudstack/cs_nic.py