mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
cs_portforward: remove CloudStackException dep (#31610)
- fixes pep8 - fixes doc
This commit is contained in:
parent
94a558887a
commit
e872763877
2 changed files with 90 additions and 140 deletions
|
@ -30,20 +30,16 @@ options:
|
|||
description:
|
||||
- Name of virtual machine which we make the port forwarding rule for.
|
||||
- Required if C(state=present).
|
||||
required: false
|
||||
default: null
|
||||
state:
|
||||
description:
|
||||
- State of the port forwarding rule.
|
||||
required: false
|
||||
default: 'present'
|
||||
choices: [ 'present', 'absent' ]
|
||||
default: present
|
||||
choices: [ present, absent ]
|
||||
protocol:
|
||||
description:
|
||||
- Protocol of the port forwarding rule.
|
||||
required: false
|
||||
default: 'tcp'
|
||||
choices: [ 'tcp', 'udp' ]
|
||||
default: tcp
|
||||
choices: [ tcp, udp ]
|
||||
public_port:
|
||||
description:
|
||||
- Start public port for this rule.
|
||||
|
@ -52,8 +48,6 @@ options:
|
|||
description:
|
||||
- End public port for this rule.
|
||||
- If not specified equal C(public_port).
|
||||
required: false
|
||||
default: null
|
||||
private_port:
|
||||
description:
|
||||
- Start private port for this rule.
|
||||
|
@ -62,79 +56,60 @@ options:
|
|||
description:
|
||||
- End private port for this rule.
|
||||
- If not specified equal C(private_port).
|
||||
required: false
|
||||
default: null
|
||||
open_firewall:
|
||||
description:
|
||||
- Whether the firewall rule for public port should be created, while creating the new rule.
|
||||
- Use M(cs_firewall) for managing firewall rules.
|
||||
required: false
|
||||
default: false
|
||||
vm_guest_ip:
|
||||
description:
|
||||
- VM guest NIC secondary IP address for the port forwarding rule.
|
||||
required: false
|
||||
default: false
|
||||
network:
|
||||
description:
|
||||
- Name of the network.
|
||||
required: false
|
||||
default: null
|
||||
version_added: "2.3"
|
||||
vpc:
|
||||
description:
|
||||
- Name of the VPC.
|
||||
required: false
|
||||
default: null
|
||||
version_added: "2.3"
|
||||
domain:
|
||||
description:
|
||||
- Domain the C(vm) is related to.
|
||||
required: false
|
||||
default: null
|
||||
account:
|
||||
description:
|
||||
- Account the C(vm) is related to.
|
||||
required: false
|
||||
default: null
|
||||
project:
|
||||
description:
|
||||
- Name of the project the C(vm) is located in.
|
||||
required: false
|
||||
default: null
|
||||
zone:
|
||||
description:
|
||||
- Name of the zone in which the virtual machine is in.
|
||||
- If not set, default zone is used.
|
||||
required: false
|
||||
default: null
|
||||
poll_async:
|
||||
description:
|
||||
- Poll async jobs until job has finished.
|
||||
required: false
|
||||
default: true
|
||||
tags:
|
||||
description:
|
||||
- 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: [])."
|
||||
required: false
|
||||
default: null
|
||||
aliases: [ 'tag' ]
|
||||
aliases: [ tag ]
|
||||
version_added: "2.4"
|
||||
extends_documentation_fragment: cloudstack
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# 1.2.3.4:80 -> web01:8080
|
||||
- local_action:
|
||||
- name: 1.2.3.4:80 -> web01:8080
|
||||
local_action:
|
||||
module: cs_portforward
|
||||
ip_address: 1.2.3.4
|
||||
vm: web01
|
||||
public_port: 80
|
||||
private_port: 8080
|
||||
|
||||
# forward SSH and open firewall
|
||||
- local_action:
|
||||
- name: forward SSH and open firewall
|
||||
local_action:
|
||||
module: cs_portforward
|
||||
ip_address: '{{ public_ip }}'
|
||||
vm: '{{ inventory_hostname }}'
|
||||
|
@ -142,8 +117,8 @@ EXAMPLES = '''
|
|||
private_port: 22
|
||||
open_firewall: true
|
||||
|
||||
# forward DNS traffic, but do not open firewall
|
||||
- local_action:
|
||||
- name: forward DNS traffic, but do not open firewall
|
||||
local_action:
|
||||
module: cs_portforward
|
||||
ip_address: 1.2.3.4
|
||||
vm: '{{ inventory_hostname }}'
|
||||
|
@ -151,8 +126,8 @@ EXAMPLES = '''
|
|||
private_port: 53
|
||||
protocol: udp
|
||||
|
||||
# remove ssh port forwarding
|
||||
- local_action:
|
||||
- name: remove ssh port forwarding
|
||||
local_action:
|
||||
module: cs_portforward
|
||||
ip_address: 1.2.3.4
|
||||
public_port: 22
|
||||
|
@ -227,10 +202,6 @@ network:
|
|||
type: string
|
||||
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.cloudstack import AnsibleCloudStack, cs_argument_spec, cs_required_together
|
||||
|
@ -257,21 +228,18 @@ class AnsibleCloudStackPortforwarding(AnsibleCloudStack):
|
|||
}
|
||||
self.portforwarding_rule = None
|
||||
|
||||
|
||||
def get_portforwarding_rule(self):
|
||||
if not self.portforwarding_rule:
|
||||
protocol = self.module.params.get('protocol')
|
||||
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['ipaddressid'] = self.get_ip_address(key='id')
|
||||
args['account'] = self.get_account(key='name')
|
||||
args['domainid'] = self.get_domain(key='id')
|
||||
args['projectid'] = self.get_project(key='id')
|
||||
portforwarding_rules = self.cs.listPortForwardingRules(**args)
|
||||
args = {
|
||||
'ipaddressid': self.get_ip_address(key='id'),
|
||||
'account': self.get_account(key='name'),
|
||||
'domainid': self.get_domain(key='id'),
|
||||
'projectid': self.get_project(key='id'),
|
||||
}
|
||||
portforwarding_rules = self.query_api('listPortForwardingRules', **args)
|
||||
|
||||
if portforwarding_rules and 'portforwardingrule' in portforwarding_rules:
|
||||
for rule in portforwarding_rules['portforwardingrule']:
|
||||
|
@ -281,7 +249,6 @@ class AnsibleCloudStackPortforwarding(AnsibleCloudStack):
|
|||
break
|
||||
return self.portforwarding_rule
|
||||
|
||||
|
||||
def present_portforwarding_rule(self):
|
||||
portforwarding_rule = self.get_portforwarding_rule()
|
||||
if portforwarding_rule:
|
||||
|
@ -291,88 +258,77 @@ class AnsibleCloudStackPortforwarding(AnsibleCloudStack):
|
|||
|
||||
if portforwarding_rule:
|
||||
portforwarding_rule = self.ensure_tags(resource=portforwarding_rule, resource_type='PortForwardingRule')
|
||||
self.portforwarding_rule=portforwarding_rule
|
||||
self.portforwarding_rule = portforwarding_rule
|
||||
|
||||
return portforwarding_rule
|
||||
|
||||
|
||||
def create_portforwarding_rule(self):
|
||||
args = {}
|
||||
args['protocol'] = self.module.params.get('protocol')
|
||||
args['publicport'] = self.module.params.get('public_port')
|
||||
args['publicendport'] = self.get_or_fallback('public_end_port', 'public_port')
|
||||
args['privateport'] = self.module.params.get('private_port')
|
||||
args['privateendport'] = self.get_or_fallback('private_end_port', 'private_port')
|
||||
args['openfirewall'] = self.module.params.get('open_firewall')
|
||||
args['vmguestip'] = self.get_vm_guest_ip()
|
||||
args['ipaddressid'] = self.get_ip_address(key='id')
|
||||
args['virtualmachineid'] = self.get_vm(key='id')
|
||||
args['account'] = self.get_account(key='name')
|
||||
args['domainid'] = self.get_domain(key='id')
|
||||
args['networkid'] = self.get_network(key='id')
|
||||
args = {
|
||||
'protocol': self.module.params.get('protocol'),
|
||||
'publicport': self.module.params.get('public_port'),
|
||||
'publicendport': self.get_or_fallback('public_end_port', 'public_port'),
|
||||
'privateport': self.module.params.get('private_port'),
|
||||
'privateendport': self.get_or_fallback('private_end_port', 'private_port'),
|
||||
'openfirewall': self.module.params.get('open_firewall'),
|
||||
'vmguestip': self.get_vm_guest_ip(),
|
||||
'ipaddressid': self.get_ip_address(key='id'),
|
||||
'virtualmachineid': self.get_vm(key='id'),
|
||||
'account': self.get_account(key='name'),
|
||||
'domainid': self.get_domain(key='id'),
|
||||
'networkid': self.get_network(key='id'),
|
||||
}
|
||||
|
||||
portforwarding_rule = None
|
||||
self.result['changed'] = True
|
||||
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')
|
||||
if poll_async:
|
||||
portforwarding_rule = self.poll_job(portforwarding_rule, 'portforwardingrule')
|
||||
return portforwarding_rule
|
||||
|
||||
|
||||
def update_portforwarding_rule(self, portforwarding_rule):
|
||||
args = {}
|
||||
args['protocol'] = self.module.params.get('protocol')
|
||||
args['publicport'] = self.module.params.get('public_port')
|
||||
args['publicendport'] = self.get_or_fallback('public_end_port', 'public_port')
|
||||
args['privateport'] = self.module.params.get('private_port')
|
||||
args['privateendport'] = self.get_or_fallback('private_end_port', 'private_port')
|
||||
args['vmguestip'] = self.get_vm_guest_ip()
|
||||
args['ipaddressid'] = self.get_ip_address(key='id')
|
||||
args['virtualmachineid'] = self.get_vm(key='id')
|
||||
args['networkid'] = self.get_network(key='id')
|
||||
args = {
|
||||
'protocol': self.module.params.get('protocol'),
|
||||
'publicport': self.module.params.get('public_port'),
|
||||
'publicendport': self.get_or_fallback('public_end_port', 'public_port'),
|
||||
'privateport': self.module.params.get('private_port'),
|
||||
'privateendport': self.get_or_fallback('private_end_port', 'private_port'),
|
||||
'vmguestip': self.get_vm_guest_ip(),
|
||||
'ipaddressid': self.get_ip_address(key='id'),
|
||||
'virtualmachineid': self.get_vm(key='id'),
|
||||
'networkid': self.get_network(key='id'),
|
||||
}
|
||||
|
||||
if self.has_changed(args, portforwarding_rule):
|
||||
self.result['changed'] = True
|
||||
if not self.module.check_mode:
|
||||
# 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()
|
||||
portforwarding_rule = self.cs.createPortForwardingRule(**args)
|
||||
portforwarding_rule = self.query_api('createPortForwardingRule', **args)
|
||||
poll_async = self.module.params.get('poll_async')
|
||||
if poll_async:
|
||||
portforwarding_rule = self.poll_job(portforwarding_rule, 'portforwardingrule')
|
||||
return portforwarding_rule
|
||||
|
||||
|
||||
def absent_portforwarding_rule(self):
|
||||
portforwarding_rule = self.get_portforwarding_rule()
|
||||
|
||||
if portforwarding_rule:
|
||||
self.result['changed'] = True
|
||||
args = {}
|
||||
args['id'] = portforwarding_rule['id']
|
||||
|
||||
args = {
|
||||
'id': portforwarding_rule['id'],
|
||||
}
|
||||
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')
|
||||
if poll_async:
|
||||
self.poll_job(res, 'portforwardingrule')
|
||||
return portforwarding_rule
|
||||
|
||||
|
||||
def get_result(self, 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:
|
||||
for search_key, return_key in self.returns_to_int.items():
|
||||
if search_key in portforwarding_rule:
|
||||
|
@ -383,24 +339,24 @@ class AnsibleCloudStackPortforwarding(AnsibleCloudStack):
|
|||
def main():
|
||||
argument_spec = cs_argument_spec()
|
||||
argument_spec.update(dict(
|
||||
ip_address = dict(required=True),
|
||||
protocol= dict(choices=['tcp', 'udp'], default='tcp'),
|
||||
public_port = dict(type='int', required=True),
|
||||
public_end_port = dict(type='int', default=None),
|
||||
private_port = dict(type='int', required=True),
|
||||
private_end_port = dict(type='int', default=None),
|
||||
state = dict(choices=['present', 'absent'], default='present'),
|
||||
open_firewall = dict(type='bool', default=False),
|
||||
vm_guest_ip = dict(default=None),
|
||||
vm = dict(default=None),
|
||||
vpc = dict(default=None),
|
||||
network = dict(default=None),
|
||||
zone = dict(default=None),
|
||||
domain = dict(default=None),
|
||||
account = dict(default=None),
|
||||
project = dict(default=None),
|
||||
poll_async = dict(type='bool', default=True),
|
||||
tags=dict(type='list', aliases=['tag'], default=None),
|
||||
ip_address=dict(required=True),
|
||||
protocol=dict(choices=['tcp', 'udp'], default='tcp'),
|
||||
public_port=dict(type='int', required=True),
|
||||
public_end_port=dict(type='int'),
|
||||
private_port=dict(type='int', required=True),
|
||||
private_end_port=dict(type='int'),
|
||||
state=dict(choices=['present', 'absent'], default='present'),
|
||||
open_firewall=dict(type='bool', default=False),
|
||||
vm_guest_ip=dict(),
|
||||
vm=dict(),
|
||||
vpc=dict(),
|
||||
network=dict(),
|
||||
zone=dict(),
|
||||
domain=dict(),
|
||||
account=dict(),
|
||||
project=dict(),
|
||||
poll_async=dict(type='bool', default=True),
|
||||
tags=dict(type='list', aliases=['tag']),
|
||||
))
|
||||
|
||||
module = AnsibleModule(
|
||||
|
@ -409,7 +365,6 @@ def main():
|
|||
supports_check_mode=True
|
||||
)
|
||||
|
||||
try:
|
||||
acs_pf = AnsibleCloudStackPortforwarding(module)
|
||||
state = module.params.get('state')
|
||||
if state in ['absent']:
|
||||
|
@ -418,10 +373,6 @@ def main():
|
|||
pf_rule = acs_pf.present_portforwarding_rule()
|
||||
|
||||
result = acs_pf.get_result(pf_rule)
|
||||
|
||||
except CloudStackException as e:
|
||||
module.fail_json(msg='CloudStackException: %s' % str(e))
|
||||
|
||||
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_facts.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_container.py
|
||||
lib/ansible/modules/cloud/docker/docker_image.py
|
||||
|
|
Loading…
Reference in a new issue