cs_portforward: remove CloudStackException dep (#31610)

- fixes pep8
- fixes doc
pull/31616/head
René Moser 7 years ago committed by GitHub
parent 94a558887a
commit e872763877

@ -30,20 +30,16 @@ options:
description: description:
- Name of virtual machine which we make the port forwarding rule for. - Name of virtual machine which we make the port forwarding rule for.
- Required if C(state=present). - Required if C(state=present).
required: false
default: null
state: state:
description: description:
- State of the port forwarding rule. - State of the port forwarding rule.
required: false default: present
default: 'present' choices: [ present, absent ]
choices: [ 'present', 'absent' ]
protocol: protocol:
description: description:
- Protocol of the port forwarding rule. - Protocol of the port forwarding rule.
required: false default: tcp
default: 'tcp' choices: [ tcp, udp ]
choices: [ 'tcp', 'udp' ]
public_port: public_port:
description: description:
- Start public port for this rule. - Start public port for this rule.
@ -52,8 +48,6 @@ options:
description: description:
- End public port for this rule. - End public port for this rule.
- If not specified equal C(public_port). - If not specified equal C(public_port).
required: false
default: null
private_port: private_port:
description: description:
- Start private port for this rule. - Start private port for this rule.
@ -62,79 +56,60 @@ options:
description: description:
- End private port for this rule. - End private port for this rule.
- If not specified equal C(private_port). - If not specified equal C(private_port).
required: false
default: null
open_firewall: open_firewall:
description: description:
- Whether the firewall rule for public port should be created, while creating the new rule. - Whether the firewall rule for public port should be created, while creating the new rule.
- Use M(cs_firewall) for managing firewall rules. - Use M(cs_firewall) for managing firewall rules.
required: false
default: false default: false
vm_guest_ip: vm_guest_ip:
description: description:
- VM guest NIC secondary IP address for the port forwarding rule. - VM guest NIC secondary IP address for the port forwarding rule.
required: false
default: false default: false
network: network:
description: description:
- Name of the network. - Name of the network.
required: false
default: null
version_added: "2.3" version_added: "2.3"
vpc: vpc:
description: description:
- Name of the VPC. - Name of the VPC.
required: false
default: null
version_added: "2.3" version_added: "2.3"
domain: domain:
description: description:
- Domain the C(vm) is related to. - Domain the C(vm) is related to.
required: false
default: null
account: account:
description: description:
- Account the C(vm) is related to. - Account the C(vm) is related to.
required: false
default: null
project: project:
description: description:
- Name of the project the C(vm) is located in. - Name of the project the C(vm) is located in.
required: false
default: null
zone: zone:
description: description:
- Name of the zone in which the virtual machine is in. - Name of the zone in which the virtual machine is in.
- If not set, default zone is used. - If not set, default zone is used.
required: false
default: null
poll_async: poll_async:
description: description:
- Poll async jobs until job has finished. - Poll async jobs until job has finished.
required: false
default: true default: true
tags: tags:
description: description:
- List of tags. Tags are a list of dictionaries having keys C(key) and C(value). - List of tags. Tags are a list of dictionaries having keys C(key) and C(value).
- "To delete all tags, set a empty list e.g. C(tags: [])." - "To delete all tags, set a empty list e.g. C(tags: [])."
required: false aliases: [ tag ]
default: null
aliases: [ 'tag' ]
version_added: "2.4" version_added: "2.4"
extends_documentation_fragment: cloudstack extends_documentation_fragment: cloudstack
''' '''
EXAMPLES = ''' EXAMPLES = '''
# 1.2.3.4:80 -> web01:8080 - name: 1.2.3.4:80 -> web01:8080
- local_action: local_action:
module: cs_portforward module: cs_portforward
ip_address: 1.2.3.4 ip_address: 1.2.3.4
vm: web01 vm: web01
public_port: 80 public_port: 80
private_port: 8080 private_port: 8080
# forward SSH and open firewall - name: forward SSH and open firewall
- local_action: local_action:
module: cs_portforward module: cs_portforward
ip_address: '{{ public_ip }}' ip_address: '{{ public_ip }}'
vm: '{{ inventory_hostname }}' vm: '{{ inventory_hostname }}'
@ -142,8 +117,8 @@ EXAMPLES = '''
private_port: 22 private_port: 22
open_firewall: true open_firewall: true
# forward DNS traffic, but do not open firewall - name: forward DNS traffic, but do not open firewall
- local_action: local_action:
module: cs_portforward module: cs_portforward
ip_address: 1.2.3.4 ip_address: 1.2.3.4
vm: '{{ inventory_hostname }}' vm: '{{ inventory_hostname }}'
@ -151,8 +126,8 @@ EXAMPLES = '''
private_port: 53 private_port: 53
protocol: udp protocol: udp
# remove ssh port forwarding - name: remove ssh port forwarding
- local_action: local_action:
module: cs_portforward module: cs_portforward
ip_address: 1.2.3.4 ip_address: 1.2.3.4
public_port: 22 public_port: 22
@ -227,10 +202,6 @@ network:
type: string type: string
sample: dmz sample: dmz
''' '''
try:
from cs import CloudStackException
except ImportError:
pass # Handled in AnsibleCloudStack.__init__
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.cloudstack import AnsibleCloudStack, cs_argument_spec, cs_required_together from ansible.module_utils.cloudstack import AnsibleCloudStack, cs_argument_spec, cs_required_together
@ -257,21 +228,18 @@ class AnsibleCloudStackPortforwarding(AnsibleCloudStack):
} }
self.portforwarding_rule = None self.portforwarding_rule = None
def get_portforwarding_rule(self): def get_portforwarding_rule(self):
if not self.portforwarding_rule: if not self.portforwarding_rule:
protocol = self.module.params.get('protocol') protocol = self.module.params.get('protocol')
public_port = self.module.params.get('public_port') public_port = self.module.params.get('public_port')
public_end_port = self.get_or_fallback('public_end_port', 'public_port')
private_port = self.module.params.get('private_port')
private_end_port = self.get_or_fallback('private_end_port', 'private_port')
args = {} args = {
args['ipaddressid'] = self.get_ip_address(key='id') 'ipaddressid': self.get_ip_address(key='id'),
args['account'] = self.get_account(key='name') 'account': self.get_account(key='name'),
args['domainid'] = self.get_domain(key='id') 'domainid': self.get_domain(key='id'),
args['projectid'] = self.get_project(key='id') 'projectid': self.get_project(key='id'),
portforwarding_rules = self.cs.listPortForwardingRules(**args) }
portforwarding_rules = self.query_api('listPortForwardingRules', **args)
if portforwarding_rules and 'portforwardingrule' in portforwarding_rules: if portforwarding_rules and 'portforwardingrule' in portforwarding_rules:
for rule in portforwarding_rules['portforwardingrule']: for rule in portforwarding_rules['portforwardingrule']:
@ -281,7 +249,6 @@ class AnsibleCloudStackPortforwarding(AnsibleCloudStack):
break break
return self.portforwarding_rule return self.portforwarding_rule
def present_portforwarding_rule(self): def present_portforwarding_rule(self):
portforwarding_rule = self.get_portforwarding_rule() portforwarding_rule = self.get_portforwarding_rule()
if portforwarding_rule: if portforwarding_rule:
@ -295,84 +262,73 @@ class AnsibleCloudStackPortforwarding(AnsibleCloudStack):
return portforwarding_rule return portforwarding_rule
def create_portforwarding_rule(self): def create_portforwarding_rule(self):
args = {} args = {
args['protocol'] = self.module.params.get('protocol') 'protocol': self.module.params.get('protocol'),
args['publicport'] = self.module.params.get('public_port') 'publicport': self.module.params.get('public_port'),
args['publicendport'] = self.get_or_fallback('public_end_port', 'public_port') 'publicendport': self.get_or_fallback('public_end_port', 'public_port'),
args['privateport'] = self.module.params.get('private_port') 'privateport': self.module.params.get('private_port'),
args['privateendport'] = self.get_or_fallback('private_end_port', 'private_port') 'privateendport': self.get_or_fallback('private_end_port', 'private_port'),
args['openfirewall'] = self.module.params.get('open_firewall') 'openfirewall': self.module.params.get('open_firewall'),
args['vmguestip'] = self.get_vm_guest_ip() 'vmguestip': self.get_vm_guest_ip(),
args['ipaddressid'] = self.get_ip_address(key='id') 'ipaddressid': self.get_ip_address(key='id'),
args['virtualmachineid'] = self.get_vm(key='id') 'virtualmachineid': self.get_vm(key='id'),
args['account'] = self.get_account(key='name') 'account': self.get_account(key='name'),
args['domainid'] = self.get_domain(key='id') 'domainid': self.get_domain(key='id'),
args['networkid'] = self.get_network(key='id') 'networkid': self.get_network(key='id'),
}
portforwarding_rule = None portforwarding_rule = None
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
portforwarding_rule = self.cs.createPortForwardingRule(**args) portforwarding_rule = self.query_api('createPortForwardingRule', **args)
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
portforwarding_rule = self.poll_job(portforwarding_rule, 'portforwardingrule') portforwarding_rule = self.poll_job(portforwarding_rule, 'portforwardingrule')
return portforwarding_rule return portforwarding_rule
def update_portforwarding_rule(self, portforwarding_rule): def update_portforwarding_rule(self, portforwarding_rule):
args = {} args = {
args['protocol'] = self.module.params.get('protocol') 'protocol': self.module.params.get('protocol'),
args['publicport'] = self.module.params.get('public_port') 'publicport': self.module.params.get('public_port'),
args['publicendport'] = self.get_or_fallback('public_end_port', 'public_port') 'publicendport': self.get_or_fallback('public_end_port', 'public_port'),
args['privateport'] = self.module.params.get('private_port') 'privateport': self.module.params.get('private_port'),
args['privateendport'] = self.get_or_fallback('private_end_port', 'private_port') 'privateendport': self.get_or_fallback('private_end_port', 'private_port'),
args['vmguestip'] = self.get_vm_guest_ip() 'vmguestip': self.get_vm_guest_ip(),
args['ipaddressid'] = self.get_ip_address(key='id') 'ipaddressid': self.get_ip_address(key='id'),
args['virtualmachineid'] = self.get_vm(key='id') 'virtualmachineid': self.get_vm(key='id'),
args['networkid'] = self.get_network(key='id') 'networkid': self.get_network(key='id'),
}
if self.has_changed(args, portforwarding_rule): if self.has_changed(args, portforwarding_rule):
self.result['changed'] = True self.result['changed'] = True
if not self.module.check_mode: if not self.module.check_mode:
# API broken in 4.2.1?, workaround using remove/create instead of update # API broken in 4.2.1?, workaround using remove/create instead of update
# portforwarding_rule = self.cs.updatePortForwardingRule(**args) # portforwarding_rule = self.query_api('updatePortForwardingRule', **args)
self.absent_portforwarding_rule() self.absent_portforwarding_rule()
portforwarding_rule = self.cs.createPortForwardingRule(**args) portforwarding_rule = self.query_api('createPortForwardingRule', **args)
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
portforwarding_rule = self.poll_job(portforwarding_rule, 'portforwardingrule') portforwarding_rule = self.poll_job(portforwarding_rule, 'portforwardingrule')
return portforwarding_rule return portforwarding_rule
def absent_portforwarding_rule(self): def absent_portforwarding_rule(self):
portforwarding_rule = self.get_portforwarding_rule() portforwarding_rule = self.get_portforwarding_rule()
if portforwarding_rule: if portforwarding_rule:
self.result['changed'] = True self.result['changed'] = True
args = {} args = {
args['id'] = portforwarding_rule['id'] 'id': portforwarding_rule['id'],
}
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.deletePortForwardingRule(**args) res = self.query_api('deletePortForwardingRule', **args)
poll_async = self.module.params.get('poll_async') poll_async = self.module.params.get('poll_async')
if poll_async: if poll_async:
self.poll_job(res, 'portforwardingrule') self.poll_job(res, 'portforwardingrule')
return portforwarding_rule return portforwarding_rule
def get_result(self, portforwarding_rule): def get_result(self, portforwarding_rule):
super(AnsibleCloudStackPortforwarding, self).get_result(portforwarding_rule) super(AnsibleCloudStackPortforwarding, self).get_result(portforwarding_rule)
network_name = self.get_network(key='name')
if network_name:
self.result['network'] = network_name
vpc_name = self.get_vpc(key='name')
if vpc_name:
self.result['vpc'] = vpc_name
if portforwarding_rule: if portforwarding_rule:
for search_key, return_key in self.returns_to_int.items(): for search_key, return_key in self.returns_to_int.items():
if search_key in portforwarding_rule: if search_key in portforwarding_rule:
@ -386,21 +342,21 @@ def main():
ip_address=dict(required=True), ip_address=dict(required=True),
protocol=dict(choices=['tcp', 'udp'], default='tcp'), protocol=dict(choices=['tcp', 'udp'], default='tcp'),
public_port=dict(type='int', required=True), public_port=dict(type='int', required=True),
public_end_port = dict(type='int', default=None), public_end_port=dict(type='int'),
private_port=dict(type='int', required=True), private_port=dict(type='int', required=True),
private_end_port = dict(type='int', default=None), private_end_port=dict(type='int'),
state=dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
open_firewall=dict(type='bool', default=False), open_firewall=dict(type='bool', default=False),
vm_guest_ip = dict(default=None), vm_guest_ip=dict(),
vm = dict(default=None), vm=dict(),
vpc = dict(default=None), vpc=dict(),
network = dict(default=None), network=dict(),
zone = dict(default=None), zone=dict(),
domain = dict(default=None), domain=dict(),
account = dict(default=None), account=dict(),
project = dict(default=None), project=dict(),
poll_async=dict(type='bool', default=True), poll_async=dict(type='bool', default=True),
tags=dict(type='list', aliases=['tag'], default=None), tags=dict(type='list', aliases=['tag']),
)) ))
module = AnsibleModule( module = AnsibleModule(
@ -409,7 +365,6 @@ def main():
supports_check_mode=True supports_check_mode=True
) )
try:
acs_pf = AnsibleCloudStackPortforwarding(module) acs_pf = AnsibleCloudStackPortforwarding(module)
state = module.params.get('state') state = module.params.get('state')
if state in ['absent']: if state in ['absent']:
@ -418,10 +373,6 @@ def main():
pf_rule = acs_pf.present_portforwarding_rule() pf_rule = acs_pf.present_portforwarding_rule()
result = acs_pf.get_result(pf_rule) result = acs_pf.get_result(pf_rule)
except CloudStackException as e:
module.fail_json(msg='CloudStackException: %s' % str(e))
module.exit_json(**result) module.exit_json(**result)

@ -67,7 +67,6 @@ lib/ansible/modules/cloud/centurylink/clc_loadbalancer.py
lib/ansible/modules/cloud/cloudstack/cs_instance.py lib/ansible/modules/cloud/cloudstack/cs_instance.py
lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py
lib/ansible/modules/cloud/cloudstack/_cs_nic.py lib/ansible/modules/cloud/cloudstack/_cs_nic.py
lib/ansible/modules/cloud/cloudstack/cs_portforward.py
lib/ansible/modules/cloud/docker/_docker.py lib/ansible/modules/cloud/docker/_docker.py
lib/ansible/modules/cloud/docker/docker_container.py lib/ansible/modules/cloud/docker/docker_container.py
lib/ansible/modules/cloud/docker/docker_image.py lib/ansible/modules/cloud/docker/docker_image.py

Loading…
Cancel
Save