1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

cloudstack: cs_staticnat: fix VPC support (#22626)

- Fix the VPC support by adding network to create and update methods
- Fix pep8
This commit is contained in:
René Moser 2017-03-15 01:59:20 +01:00 committed by GitHub
parent a227a4ebb4
commit 38b9f055d7

View file

@ -55,7 +55,7 @@ options:
version_added: "2.2" version_added: "2.2"
vpc: vpc:
description: description:
- Name of the VPC. - VPC the network related to.
required: false required: false
default: null default: null
version_added: "2.3" version_added: "2.3"
@ -157,8 +157,13 @@ domain:
sample: example domain sample: example domain
''' '''
# 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 AnsibleCloudStackStaticNat(AnsibleCloudStack): class AnsibleCloudStackStaticNat(AnsibleCloudStack):
@ -166,20 +171,20 @@ class AnsibleCloudStackStaticNat(AnsibleCloudStack):
def __init__(self, module): def __init__(self, module):
super(AnsibleCloudStackStaticNat, self).__init__(module) super(AnsibleCloudStackStaticNat, self).__init__(module)
self.returns = { self.returns = {
'virtualmachinedisplayname': 'vm_display_name', 'virtualmachinedisplayname': 'vm_display_name',
'virtualmachinename': 'vm_name', 'virtualmachinename': 'vm_name',
'ipaddress': 'ip_address', 'ipaddress': 'ip_address',
'vmipaddress': 'vm_guest_ip', 'vmipaddress': 'vm_guest_ip',
} }
def create_static_nat(self, ip_address): def create_static_nat(self, ip_address):
self.result['changed'] = True self.result['changed'] = True
args = {} args = {
args['virtualmachineid'] = self.get_vm(key='id') 'virtualmachineid': self.get_vm(key='id'),
args['ipaddressid'] = ip_address['id'] 'ipaddressid': ip_address['id'],
args['vmguestip'] = self.get_vm_guest_ip() 'vmguestip': self.get_vm_guest_ip(),
args['networkid'] = self.get_network(key='id') 'networkid': self.get_network(key='id')
}
if not self.module.check_mode: if not self.module.check_mode:
res = self.cs.enableStaticNat(**args) res = self.cs.enableStaticNat(**args)
if 'errortext' in res: if 'errortext' in res:
@ -190,14 +195,14 @@ class AnsibleCloudStackStaticNat(AnsibleCloudStack):
ip_address = self.get_ip_address() ip_address = self.get_ip_address()
return ip_address return ip_address
def update_static_nat(self, ip_address): def update_static_nat(self, ip_address):
args = {} args = {
args['virtualmachineid'] = self.get_vm(key='id') 'virtualmachineid': self.get_vm(key='id'),
args['ipaddressid'] = ip_address['id'] 'ipaddressid': ip_address['id'],
args['vmguestip'] = self.get_vm_guest_ip() 'vmguestip': self.get_vm_guest_ip(),
'networkid': self.get_network(key='id')
# make an alias, so we can use _has_changed() }
# make an alias, so we can use has_changed()
ip_address['vmguestip'] = ip_address['vmipaddress'] ip_address['vmguestip'] = ip_address['vmipaddress']
if self.has_changed(args, ip_address, ['vmguestip', 'virtualmachineid']): if self.has_changed(args, ip_address, ['vmguestip', 'virtualmachineid']):
self.result['changed'] = True self.result['changed'] = True
@ -215,7 +220,6 @@ class AnsibleCloudStackStaticNat(AnsibleCloudStack):
ip_address = self.get_ip_address() ip_address = self.get_ip_address()
return ip_address return ip_address
def present_static_nat(self): def present_static_nat(self):
ip_address = self.get_ip_address() ip_address = self.get_ip_address()
if not ip_address['isstaticnat']: if not ip_address['isstaticnat']:
@ -224,7 +228,6 @@ class AnsibleCloudStackStaticNat(AnsibleCloudStack):
ip_address = self.update_static_nat(ip_address) ip_address = self.update_static_nat(ip_address)
return ip_address return ip_address
def absent_static_nat(self): def absent_static_nat(self):
ip_address = self.get_ip_address() ip_address = self.get_ip_address()
if ip_address['isstaticnat']: if ip_address['isstaticnat']:
@ -242,17 +245,17 @@ class AnsibleCloudStackStaticNat(AnsibleCloudStack):
def main(): def main():
argument_spec = cs_argument_spec() argument_spec = cs_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
ip_address = dict(required=True), ip_address=dict(required=True),
vm = dict(default=None), vm=dict(),
vm_guest_ip = dict(default=None), vm_guest_ip=dict(),
network = dict(default=None), network=dict(),
vpc = dict(default=None), vpc=dict(),
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(),
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),
)) ))
module = AnsibleModule( module = AnsibleModule(
@ -277,7 +280,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()