cloudstack: fix pep8 for loadbalancer modules (#22822)

pull/23030/head
René Moser 8 years ago committed by GitHub
parent 45951a69a6
commit e3525d8df9

@ -222,8 +222,14 @@ state:
sample: "Add" sample: "Add"
''' '''
# import cloudstack common from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.cloudstack import * from ansible.module_utils.cloudstack import (
AnsibleCloudStack,
CloudStackException,
cs_argument_spec,
cs_required_together,
)
class AnsibleCloudStackLBRule(AnsibleCloudStack): class AnsibleCloudStackLBRule(AnsibleCloudStack):
@ -241,13 +247,11 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack):
'privateport': 'private_port', 'privateport': 'private_port',
} }
def get_rule(self, **kwargs): def get_rule(self, **kwargs):
rules = self.cs.listLoadBalancerRules(**kwargs) rules = self.cs.listLoadBalancerRules(**kwargs)
if rules: if rules:
return rules['loadbalancerrule'][0] return rules['loadbalancerrule'][0]
def _get_common_args(self): def _get_common_args(self):
return { return {
'account': self.get_account(key='name'), 'account': self.get_account(key='name'),
@ -258,7 +262,6 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack):
'name': self.module.params.get('name'), 'name': self.module.params.get('name'),
} }
def present_lb_rule(self): def present_lb_rule(self):
missing_params = [] missing_params = []
for required_params in [ for required_params in [
@ -282,17 +285,18 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack):
rule = self.ensure_tags(resource=rule, resource_type='LoadBalancer') rule = self.ensure_tags(resource=rule, resource_type='LoadBalancer')
return rule return rule
def _create_lb_rule(self, rule): def _create_lb_rule(self, rule):
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
args = self._get_common_args() args = self._get_common_args()
args['algorithm'] = self.module.params.get('algorithm') args.update({
args['privateport'] = self.module.params.get('private_port') 'algorithm': self.module.params.get('algorithm'),
args['publicport'] = self.module.params.get('public_port') 'privateport': self.module.params.get('private_port'),
args['cidrlist'] = self.module.params.get('cidr') 'publicport': self.module.params.get('public_port'),
args['description'] = self.module.params.get('description') 'cidrlist': self.module.params.get('cidr'),
args['protocol'] = self.module.params.get('protocol') 'description': self.module.params.get('description'),
'protocol': self.module.params.get('protocol'),
})
res = self.cs.createLoadBalancerRule(**args) res = self.cs.createLoadBalancerRule(**args)
if 'errortext' in res: if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext']) self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
@ -302,12 +306,12 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack):
rule = self.poll_job(res, 'loadbalancer') rule = self.poll_job(res, 'loadbalancer')
return rule return rule
def _update_lb_rule(self, rule): def _update_lb_rule(self, rule):
args = {} args = {
args['id'] = rule['id'] 'id': rule['id'],
args['algorithm'] = self.module.params.get('algorithm') 'algorithm': self.module.params.get('algorithm'),
args['description'] = self.module.params.get('description') 'description': self.module.params.get('description'),
}
if self.has_changed(args, rule): if self.has_changed(args, rule):
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
@ -320,7 +324,6 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack):
rule = self.poll_job(res, 'loadbalancer') rule = self.poll_job(res, 'loadbalancer')
return rule return rule
def absent_lb_rule(self): def absent_lb_rule(self):
args = self._get_common_args() args = self._get_common_args()
rule = self.get_rule(**args) rule = self.get_rule(**args)
@ -339,22 +342,22 @@ class AnsibleCloudStackLBRule(AnsibleCloudStack):
def main(): def main():
argument_spec = cs_argument_spec() argument_spec = cs_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
name = dict(required=True), name=dict(required=True),
description = dict(default=None), description=dict(),
algorithm = dict(choices=['source', 'roundrobin', 'leastconn'], default='source'), algorithm=dict(choices=['source', 'roundrobin', 'leastconn'], default='source'),
private_port = dict(type='int', default=None), private_port=dict(type='int'),
public_port = dict(type='int', default=None), public_port=dict(type='int'),
protocol = dict(default=None), protocol=dict(),
state = dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
ip_address = dict(required=True, aliases=['public_ip']), ip_address=dict(required=True, aliases=['public_ip']),
cidr = dict(default=None), cidr=dict(),
project = dict(default=None), project=dict(),
open_firewall = dict(type='bool', default=False), open_firewall=dict(type='bool', default=False),
tags = dict(type='list', aliases=['tag'], default=None), tags=dict(type='list', aliases=['tag']),
zone = dict(default=None), zone=dict(),
domain = dict(default=None), domain=dict(),
account = dict(default=None), account=dict(),
poll_async = dict(type='bool', default=True), poll_async=dict(type='bool', default=True),
)) ))
module = AnsibleModule( module = AnsibleModule(
@ -379,7 +382,6 @@ def main():
module.exit_json(**result) module.exit_json(**result)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -204,8 +204,14 @@ state:
sample: "Add" sample: "Add"
''' '''
# import cloudstack common from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.cloudstack import * from ansible.module_utils.cloudstack import (
AnsibleCloudStack,
CloudStackException,
cs_argument_spec,
cs_required_together,
)
class AnsibleCloudStackLBRuleMember(AnsibleCloudStack): class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
@ -223,13 +229,15 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
'privateport': 'private_port', 'privateport': 'private_port',
} }
def get_rule(self): def get_rule(self):
args = self._get_common_args() args = self._get_common_args()
args['name'] = self.module.params.get('name') args.update({
args['zoneid'] = self.get_zone(key='id') if self.module.params.get('zone') else None 'name': self.module.params.get('name'),
'zoneid': self.get_zone(key='id') if self.module.params.get('zone') else None,
})
if self.module.params.get('ip_address'): if self.module.params.get('ip_address'):
args['publicipid'] = self.get_ip_address(key='id') args['publicipid'] = self.get_ip_address(key='id')
rules = self.cs.listLoadBalancerRules(**args) rules = self.cs.listLoadBalancerRules(**args)
if rules: if rules:
if len(rules['loadbalancerrule']) > 1: if len(rules['loadbalancerrule']) > 1:
@ -237,7 +245,6 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
return rules['loadbalancerrule'][0] return rules['loadbalancerrule'][0]
return None return None
def _get_common_args(self): def _get_common_args(self):
return { return {
'account': self.get_account(key='name'), 'account': self.get_account(key='name'),
@ -245,14 +252,12 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
'projectid': self.get_project(key='id'), 'projectid': self.get_project(key='id'),
} }
def _get_members_of_rule(self, rule): def _get_members_of_rule(self, rule):
res = self.cs.listLoadBalancerRuleInstances(id=rule['id']) res = self.cs.listLoadBalancerRuleInstances(id=rule['id'])
if 'errortext' in res: if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext']) self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
return res.get('loadbalancerruleinstance', []) return res.get('loadbalancerruleinstance', [])
def _ensure_members(self, operation): def _ensure_members(self, operation):
if operation not in ['add', 'remove']: if operation not in ['add', 'remove']:
self.module.fail_json(msg="Bad operation: %s" % operation) self.module.fail_json(msg="Bad operation: %s" % operation)
@ -267,7 +272,7 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
wanted_names = self.module.params.get('vms') wanted_names = self.module.params.get('vms')
if operation =='add': if operation == 'add':
cs_func = self.cs.assignToLoadBalancerRule cs_func = self.cs.assignToLoadBalancerRule
to_change = set(wanted_names) - set(existing.keys()) to_change = set(wanted_names) - set(existing.keys())
else: else:
@ -293,8 +298,8 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
if to_change_ids and not self.module.check_mode: if to_change_ids and not self.module.check_mode:
res = cs_func( res = cs_func(
id = rule['id'], id=rule['id'],
virtualmachineids = to_change_ids, virtualmachineids=to_change_ids,
) )
if 'errortext' in res: if 'errortext' in res:
self.module.fail_json(msg="Failed: '%s'" % res['errortext']) self.module.fail_json(msg="Failed: '%s'" % res['errortext'])
@ -304,15 +309,12 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
rule = self.get_rule() rule = self.get_rule()
return rule return rule
def add_members(self): def add_members(self):
return self._ensure_members('add') return self._ensure_members('add')
def remove_members(self): def remove_members(self):
return self._ensure_members('remove') return self._ensure_members('remove')
def get_result(self, rule): def get_result(self, rule):
super(AnsibleCloudStackLBRuleMember, self).get_result(rule) super(AnsibleCloudStackLBRuleMember, self).get_result(rule)
if rule: if rule:
@ -325,15 +327,15 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
def main(): def main():
argument_spec = cs_argument_spec() argument_spec = cs_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
name = dict(required=True), name=dict(required=True),
ip_address = dict(default=None, aliases=['public_ip']), ip_address=dict(aliases=['public_ip']),
vms = dict(required=True, aliases=['vm'], type='list'), vms=dict(required=True, aliases=['vm'], type='list'),
state = dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
zone = dict(default=None), zone=dict(),
domain = dict(default=None), domain=dict(),
project = dict(default=None), project=dict(),
account = dict(default=None), account=dict(),
poll_async = dict(type='bool', default=True), poll_async=dict(type='bool', default=True),
)) ))
module = AnsibleModule( module = AnsibleModule(
@ -358,7 +360,6 @@ def main():
module.exit_json(**result) module.exit_json(**result)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save