community.general/lib/ansible/modules/cloud/cloudstack/cs_ip_address.py

240 lines
6.6 KiB
Python
Raw Normal View History

2015-08-10 14:53:46 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (c) 2015, Darren Worrall <darren@iweb.co.uk>
# (c) 2015, René Moser <mail@renemoser.net>
2015-08-10 14:53:46 +00:00
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
2017-08-16 03:16:38 +00:00
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['stableinterface'],
'supported_by': 'community'}
2016-12-06 10:35:25 +00:00
2015-08-10 14:53:46 +00:00
DOCUMENTATION = '''
---
module: cs_ip_address
short_description: Manages public IP address associations on Apache CloudStack based clouds.
2015-08-10 14:53:46 +00:00
description:
2015-08-10 15:42:44 +00:00
- Acquires and associates a public IP to an account or project. Due to API
limitations this is not an idempotent call, so be sure to only
conditionally call this when C(state=present)
2015-08-10 14:53:46 +00:00
version_added: '2.0'
author:
- "Darren Worrall (@dazworrall)"
- "René Moser (@resmo)"
2015-08-10 14:53:46 +00:00
options:
ip_address:
description:
- Public IP address.
- Required if C(state=absent)
required: false
default: null
2015-08-10 14:53:46 +00:00
domain:
description:
- Domain the IP address is related to.
required: false
default: null
network:
description:
- Network the IP address is related to.
required: false
default: null
vpc:
description:
- VPC the IP address is related to.
required: false
default: null
version_added: "2.2"
2015-08-10 14:53:46 +00:00
account:
description:
- Account the IP address is related to.
required: false
default: null
project:
description:
- Name of the project the IP address is related to.
required: false
default: null
zone:
description:
- Name of the zone in which the IP address is in.
2015-08-10 14:53:46 +00:00
- 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
extends_documentation_fragment: cloudstack
'''
EXAMPLES = '''
# Associate an IP address conditonally
2015-08-10 14:53:46 +00:00
- local_action:
module: cs_ip_address
2015-08-10 15:07:04 +00:00
network: My Network
2015-08-10 14:53:46 +00:00
register: ip_address
when: instance.public_ip is undefined
2015-08-10 14:53:46 +00:00
# Disassociate an IP address
- local_action:
module: cs_ip_address
ip_address: 1.2.3.4
state: absent
'''
RETURN = '''
---
id:
description: UUID of the Public IP address.
returned: success
type: string
sample: a6f7a5fc-43f8-11e5-a151-feff819cdc9f
2015-08-10 14:53:46 +00:00
ip_address:
description: Public IP address.
returned: success
type: string
sample: 1.2.3.4
zone:
description: Name of zone the IP address is related to.
returned: success
type: string
sample: ch-gva-2
project:
description: Name of project the IP address is related to.
returned: success
type: string
sample: Production
account:
description: Account the IP address is related to.
returned: success
type: string
sample: example account
domain:
description: Domain the IP address is related to.
returned: success
type: string
sample: example domain
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.cloudstack import (
AnsibleCloudStack,
cs_argument_spec,
cs_required_together,
)
2015-08-10 14:53:46 +00:00
class AnsibleCloudStackIPAddress(AnsibleCloudStack):
2015-08-10 14:53:46 +00:00
def __init__(self, module):
super(AnsibleCloudStackIPAddress, self).__init__(module)
self.returns = {
'ipaddress': 'ip_address',
}
2015-08-10 14:53:46 +00:00
def get_ip_address(self, key=None):
if self.ip_address:
return self._get_by_key(key, self.ip_address)
args = {
'ipaddress': self.module.params.get('ip_address'),
'account': self.get_account(key='name'),
'domainid': self.get_domain(key='id'),
'projectid': self.get_project(key='id'),
'vpcid': self.get_vpc(key='id'),
}
2015-08-10 14:53:46 +00:00
ip_addresses = self.cs.listPublicIpAddresses(**args)
if ip_addresses:
self.ip_address = ip_addresses['publicipaddress'][0]
return self._get_by_key(key, self.ip_address)
def associate_ip_address(self):
self.result['changed'] = True
args = {
'account': self.get_account(key='name'),
'domainid': self.get_domain(key='id'),
'projectid': self.get_project(key='id'),
'networkid': self.get_network(key='id'),
'zoneid': self.get_zone(key='id'),
'vpcid': self.get_vpc(key='id'),
}
ip_address = None
2015-08-10 14:53:46 +00:00
if not self.module.check_mode:
res = self.cs.associateIpAddress(**args)
poll_async = self.module.params.get('poll_async')
if poll_async:
ip_address = self.poll_job(res, 'ipaddress')
2015-08-10 14:53:46 +00:00
return ip_address
def disassociate_ip_address(self):
ip_address = self.get_ip_address()
if not ip_address:
return None
2015-08-10 14:53:46 +00:00
if ip_address['isstaticnat']:
self.module.fail_json(msg="IP address is allocated via static nat")
self.result['changed'] = True
if not self.module.check_mode:
res = self.cs.disassociateIpAddress(id=ip_address['id'])
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
2017-07-16 20:05:14 +00:00
2015-08-10 14:53:46 +00:00
poll_async = self.module.params.get('poll_async')
if poll_async:
2016-06-09 20:39:03 +00:00
self.poll_job(res, 'ipaddress')
2015-08-10 14:53:46 +00:00
return ip_address
2015-08-10 14:53:46 +00:00
def main():
argument_spec = cs_argument_spec()
argument_spec.update(dict(
ip_address=dict(required=False),
state=dict(choices=['present', 'absent'], default='present'),
vpc=dict(),
network=dict(),
zone=dict(),
domain=dict(),
account=dict(),
project=dict(),
poll_async=dict(type='bool', default=True),
))
2015-08-10 14:53:46 +00:00
module = AnsibleModule(
argument_spec=argument_spec,
required_together=cs_required_together(),
required_if=[
('state', 'absent', ['ip_address']),
],
2015-08-10 14:53:46 +00:00
supports_check_mode=True
)
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
2017-07-16 20:05:14 +00:00
acs_ip_address = AnsibleCloudStackIPAddress(module)
2015-08-10 14:53:46 +00:00
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
2017-07-16 20:05:14 +00:00
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()
2015-08-10 14:53:46 +00:00
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
2017-07-16 20:05:14 +00:00
result = acs_ip_address.get_result(ip_address)
2015-08-10 14:53:46 +00:00
module.exit_json(**result)
2015-08-10 14:53:46 +00:00
if __name__ == '__main__':
main()