2015-07-08 18:22:38 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
#
|
2015-07-14 23:36:50 +02:00
|
|
|
# Copyright (c) 2015 CenturyLink
|
2015-07-08 18:22:38 +02:00
|
|
|
#
|
2015-07-14 23:36:50 +02:00
|
|
|
# This file is part of Ansible.
|
2015-07-08 18:22:38 +02:00
|
|
|
#
|
2015-07-14 23:36:50 +02:00
|
|
|
# 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.
|
2015-07-08 18:22:38 +02:00
|
|
|
#
|
2015-07-14 23:36:50 +02:00
|
|
|
# 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.
|
2015-07-08 18:22:38 +02:00
|
|
|
#
|
2015-07-14 23:36:50 +02:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>
|
2015-07-08 18:22:38 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
module: clc_publicip
|
|
|
|
short_description: Add and Delete public ips on servers in CenturyLink Cloud.
|
|
|
|
description:
|
|
|
|
- An Ansible module to add or delete public ip addresses on an existing server or servers in CenturyLink Cloud.
|
2015-07-20 17:31:33 +02:00
|
|
|
version_added: "2.0"
|
2015-07-08 18:22:38 +02:00
|
|
|
options:
|
|
|
|
protocol:
|
2015-10-21 19:36:51 +02:00
|
|
|
description:
|
2015-07-08 18:22:38 +02:00
|
|
|
- The protocol that the public IP will listen for.
|
|
|
|
default: TCP
|
2015-07-13 22:52:00 +02:00
|
|
|
choices: ['TCP', 'UDP', 'ICMP']
|
2015-07-08 18:22:38 +02:00
|
|
|
required: False
|
|
|
|
ports:
|
|
|
|
description:
|
2015-10-21 19:36:51 +02:00
|
|
|
- A list of ports to expose. This is required when state is 'present'
|
|
|
|
required: False
|
|
|
|
default: None
|
2015-07-08 18:22:38 +02:00
|
|
|
server_ids:
|
|
|
|
description:
|
|
|
|
- A list of servers to create public ips on.
|
|
|
|
required: True
|
|
|
|
state:
|
|
|
|
description:
|
2015-10-21 19:36:51 +02:00
|
|
|
- Determine whether to create or delete public IPs. If present module will not create a second public ip if one
|
2015-07-08 18:22:38 +02:00
|
|
|
already exists.
|
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
required: False
|
|
|
|
wait:
|
|
|
|
description:
|
|
|
|
- Whether to wait for the tasks to finish before returning.
|
|
|
|
choices: [ True, False ]
|
|
|
|
default: True
|
|
|
|
required: False
|
2015-07-13 22:52:00 +02:00
|
|
|
requirements:
|
|
|
|
- python = 2.7
|
|
|
|
- requests >= 2.5.0
|
|
|
|
- clc-sdk
|
2015-10-23 16:34:58 +02:00
|
|
|
author: "CLC Runner (@clc-runner)"
|
2015-07-13 22:52:00 +02:00
|
|
|
notes:
|
|
|
|
- To use this module, it is required to set the below environment variables which enables access to the
|
|
|
|
Centurylink Cloud
|
2015-07-20 17:34:57 +02:00
|
|
|
- CLC_V2_API_USERNAME, the account login id for the centurylink cloud
|
2015-10-21 19:36:51 +02:00
|
|
|
- CLC_V2_API_PASSWORD, the account password for the centurylink cloud
|
2015-07-13 22:52:00 +02:00
|
|
|
- Alternatively, the module accepts the API token and account alias. The API token can be generated using the
|
|
|
|
CLC account login and password via the HTTP api call @ https://api.ctl.io/v2/authentication/login
|
2015-07-20 17:34:57 +02:00
|
|
|
- CLC_V2_API_TOKEN, the API token generated from https://api.ctl.io/v2/authentication/login
|
|
|
|
- CLC_ACCT_ALIAS, the account alias associated with the centurylink cloud
|
2015-07-13 22:52:00 +02:00
|
|
|
- Users can set CLC_V2_API_URL to specify an endpoint for pointing to a different CLC environment.
|
2015-07-08 18:22:38 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Note - You must set the CLC_V2_API_USERNAME And CLC_V2_API_PASSWD Environment variables before running these examples
|
|
|
|
|
|
|
|
- name: Add Public IP to Server
|
|
|
|
hosts: localhost
|
|
|
|
gather_facts: False
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Create Public IP For Servers
|
|
|
|
clc_publicip:
|
|
|
|
protocol: 'TCP'
|
|
|
|
ports:
|
|
|
|
- 80
|
|
|
|
server_ids:
|
2015-10-21 19:36:51 +02:00
|
|
|
- UC1TEST-SVR01
|
|
|
|
- UC1TEST-SVR02
|
2015-07-08 18:22:38 +02:00
|
|
|
state: present
|
|
|
|
register: clc
|
|
|
|
|
|
|
|
- name: debug
|
|
|
|
debug: var=clc
|
|
|
|
|
|
|
|
- name: Delete Public IP from Server
|
|
|
|
hosts: localhost
|
|
|
|
gather_facts: False
|
|
|
|
connection: local
|
|
|
|
tasks:
|
|
|
|
- name: Create Public IP For Servers
|
|
|
|
clc_publicip:
|
|
|
|
server_ids:
|
2015-10-21 19:36:51 +02:00
|
|
|
- UC1TEST-SVR01
|
|
|
|
- UC1TEST-SVR02
|
2015-07-08 18:22:38 +02:00
|
|
|
state: absent
|
|
|
|
register: clc
|
|
|
|
|
|
|
|
- name: debug
|
|
|
|
debug: var=clc
|
|
|
|
'''
|
|
|
|
|
2015-10-21 19:36:51 +02:00
|
|
|
RETURN = '''
|
|
|
|
server_ids:
|
|
|
|
description: The list of server ids that are changed
|
|
|
|
returned: success
|
|
|
|
type: list
|
|
|
|
sample:
|
|
|
|
[
|
|
|
|
"UC1TEST-SVR01",
|
|
|
|
"UC1TEST-SVR02"
|
|
|
|
]
|
|
|
|
'''
|
|
|
|
|
2015-07-08 18:22:38 +02:00
|
|
|
__version__ = '${version}'
|
|
|
|
|
2015-07-13 22:52:00 +02:00
|
|
|
from distutils.version import LooseVersion
|
|
|
|
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
except ImportError:
|
|
|
|
REQUESTS_FOUND = False
|
|
|
|
else:
|
|
|
|
REQUESTS_FOUND = True
|
2015-07-08 18:22:38 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Requires the clc-python-sdk.
|
|
|
|
# sudo pip install clc-sdk
|
|
|
|
#
|
|
|
|
try:
|
|
|
|
import clc as clc_sdk
|
|
|
|
from clc import CLCException
|
|
|
|
except ImportError:
|
|
|
|
CLC_FOUND = False
|
|
|
|
clc_sdk = None
|
|
|
|
else:
|
|
|
|
CLC_FOUND = True
|
|
|
|
|
|
|
|
|
|
|
|
class ClcPublicIp(object):
|
|
|
|
clc = clc_sdk
|
|
|
|
module = None
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
"""
|
|
|
|
Construct module
|
|
|
|
"""
|
|
|
|
self.module = module
|
|
|
|
if not CLC_FOUND:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg='clc-python-sdk required for this module')
|
2015-07-13 22:52:00 +02:00
|
|
|
if not REQUESTS_FOUND:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg='requests library is required for this module')
|
|
|
|
if requests.__version__ and LooseVersion(requests.__version__) < LooseVersion('2.5.0'):
|
|
|
|
self.module.fail_json(
|
|
|
|
msg='requests library version should be >= 2.5.0')
|
2015-07-08 18:22:38 +02:00
|
|
|
|
|
|
|
self._set_user_agent(self.clc)
|
|
|
|
|
|
|
|
def process_request(self):
|
|
|
|
"""
|
|
|
|
Process the request - Main Code Path
|
|
|
|
:return: Returns with either an exit_json or fail_json
|
|
|
|
"""
|
|
|
|
self._set_clc_credentials_from_env()
|
|
|
|
params = self.module.params
|
|
|
|
server_ids = params['server_ids']
|
|
|
|
ports = params['ports']
|
|
|
|
protocol = params['protocol']
|
|
|
|
state = params['state']
|
|
|
|
|
|
|
|
if state == 'present':
|
2015-10-21 19:36:51 +02:00
|
|
|
changed, changed_server_ids, requests = self.ensure_public_ip_present(
|
2015-07-08 18:22:38 +02:00
|
|
|
server_ids=server_ids, protocol=protocol, ports=ports)
|
|
|
|
elif state == 'absent':
|
2015-10-21 19:36:51 +02:00
|
|
|
changed, changed_server_ids, requests = self.ensure_public_ip_absent(
|
2015-07-08 18:22:38 +02:00
|
|
|
server_ids=server_ids)
|
|
|
|
else:
|
|
|
|
return self.module.fail_json(msg="Unknown State: " + state)
|
|
|
|
self._wait_for_requests_to_complete(requests)
|
|
|
|
return self.module.exit_json(changed=changed,
|
2015-10-21 19:36:51 +02:00
|
|
|
server_ids=changed_server_ids)
|
2015-07-08 18:22:38 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _define_module_argument_spec():
|
|
|
|
"""
|
|
|
|
Define the argument spec for the ansible module
|
|
|
|
:return: argument spec dictionary
|
|
|
|
"""
|
|
|
|
argument_spec = dict(
|
|
|
|
server_ids=dict(type='list', required=True),
|
2015-07-13 22:52:00 +02:00
|
|
|
protocol=dict(default='TCP', choices=['TCP', 'UDP', 'ICMP']),
|
2015-10-21 19:36:51 +02:00
|
|
|
ports=dict(type='list'),
|
2015-07-08 18:22:38 +02:00
|
|
|
wait=dict(type='bool', default=True),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
)
|
|
|
|
return argument_spec
|
|
|
|
|
|
|
|
def ensure_public_ip_present(self, server_ids, protocol, ports):
|
|
|
|
"""
|
|
|
|
Ensures the given server ids having the public ip available
|
|
|
|
:param server_ids: the list of server ids
|
|
|
|
:param protocol: the ip protocol
|
|
|
|
:param ports: the list of ports to expose
|
|
|
|
:return: (changed, changed_server_ids, results)
|
|
|
|
changed: A flag indicating if there is any change
|
|
|
|
changed_server_ids : the list of server ids that are changed
|
|
|
|
results: The result list from clc public ip call
|
|
|
|
"""
|
|
|
|
changed = False
|
|
|
|
results = []
|
|
|
|
changed_server_ids = []
|
|
|
|
servers = self._get_servers_from_clc(
|
|
|
|
server_ids,
|
|
|
|
'Failed to obtain server list from the CLC API')
|
|
|
|
servers_to_change = [
|
|
|
|
server for server in servers if len(
|
|
|
|
server.PublicIPs().public_ips) == 0]
|
|
|
|
ports_to_expose = [{'protocol': protocol, 'port': port}
|
|
|
|
for port in ports]
|
|
|
|
for server in servers_to_change:
|
|
|
|
if not self.module.check_mode:
|
2015-07-13 22:52:00 +02:00
|
|
|
result = self._add_publicip_to_server(server, ports_to_expose)
|
2015-07-08 18:22:38 +02:00
|
|
|
results.append(result)
|
|
|
|
changed_server_ids.append(server.id)
|
|
|
|
changed = True
|
|
|
|
return changed, changed_server_ids, results
|
|
|
|
|
2015-07-13 22:52:00 +02:00
|
|
|
def _add_publicip_to_server(self, server, ports_to_expose):
|
|
|
|
result = None
|
|
|
|
try:
|
|
|
|
result = server.PublicIPs().Add(ports_to_expose)
|
|
|
|
except CLCException, ex:
|
|
|
|
self.module.fail_json(msg='Failed to add public ip to the server : {0}. {1}'.format(
|
|
|
|
server.id, ex.response_text
|
|
|
|
))
|
|
|
|
return result
|
|
|
|
|
2015-07-08 18:22:38 +02:00
|
|
|
def ensure_public_ip_absent(self, server_ids):
|
|
|
|
"""
|
|
|
|
Ensures the given server ids having the public ip removed if there is any
|
|
|
|
:param server_ids: the list of server ids
|
|
|
|
:return: (changed, changed_server_ids, results)
|
|
|
|
changed: A flag indicating if there is any change
|
|
|
|
changed_server_ids : the list of server ids that are changed
|
|
|
|
results: The result list from clc public ip call
|
|
|
|
"""
|
|
|
|
changed = False
|
|
|
|
results = []
|
|
|
|
changed_server_ids = []
|
|
|
|
servers = self._get_servers_from_clc(
|
|
|
|
server_ids,
|
|
|
|
'Failed to obtain server list from the CLC API')
|
|
|
|
servers_to_change = [
|
|
|
|
server for server in servers if len(
|
|
|
|
server.PublicIPs().public_ips) > 0]
|
|
|
|
for server in servers_to_change:
|
|
|
|
if not self.module.check_mode:
|
2015-07-13 22:52:00 +02:00
|
|
|
result = self._remove_publicip_from_server(server)
|
|
|
|
results.append(result)
|
2015-07-08 18:22:38 +02:00
|
|
|
changed_server_ids.append(server.id)
|
|
|
|
changed = True
|
|
|
|
return changed, changed_server_ids, results
|
|
|
|
|
2015-07-13 22:52:00 +02:00
|
|
|
def _remove_publicip_from_server(self, server):
|
2015-10-21 19:36:51 +02:00
|
|
|
result = None
|
2015-07-13 22:52:00 +02:00
|
|
|
try:
|
|
|
|
for ip_address in server.PublicIPs().public_ips:
|
|
|
|
result = ip_address.Delete()
|
|
|
|
except CLCException, ex:
|
|
|
|
self.module.fail_json(msg='Failed to remove public ip from the server : {0}. {1}'.format(
|
|
|
|
server.id, ex.response_text
|
|
|
|
))
|
|
|
|
return result
|
|
|
|
|
2015-07-08 18:22:38 +02:00
|
|
|
def _wait_for_requests_to_complete(self, requests_lst):
|
|
|
|
"""
|
|
|
|
Waits until the CLC requests are complete if the wait argument is True
|
|
|
|
:param requests_lst: The list of CLC request objects
|
|
|
|
:return: none
|
|
|
|
"""
|
|
|
|
if not self.module.params['wait']:
|
|
|
|
return
|
|
|
|
for request in requests_lst:
|
|
|
|
request.WaitUntilComplete()
|
|
|
|
for request_details in request.requests:
|
|
|
|
if request_details.Status() != 'succeeded':
|
|
|
|
self.module.fail_json(
|
|
|
|
msg='Unable to process public ip request')
|
|
|
|
|
|
|
|
def _set_clc_credentials_from_env(self):
|
|
|
|
"""
|
|
|
|
Set the CLC Credentials on the sdk by reading environment variables
|
|
|
|
:return: none
|
|
|
|
"""
|
|
|
|
env = os.environ
|
|
|
|
v2_api_token = env.get('CLC_V2_API_TOKEN', False)
|
|
|
|
v2_api_username = env.get('CLC_V2_API_USERNAME', False)
|
|
|
|
v2_api_passwd = env.get('CLC_V2_API_PASSWD', False)
|
|
|
|
clc_alias = env.get('CLC_ACCT_ALIAS', False)
|
|
|
|
api_url = env.get('CLC_V2_API_URL', False)
|
|
|
|
|
|
|
|
if api_url:
|
|
|
|
self.clc.defaults.ENDPOINT_URL_V2 = api_url
|
|
|
|
|
|
|
|
if v2_api_token and clc_alias:
|
|
|
|
self.clc._LOGIN_TOKEN_V2 = v2_api_token
|
|
|
|
self.clc._V2_ENABLED = True
|
|
|
|
self.clc.ALIAS = clc_alias
|
|
|
|
elif v2_api_username and v2_api_passwd:
|
|
|
|
self.clc.v2.SetCredentials(
|
|
|
|
api_username=v2_api_username,
|
|
|
|
api_passwd=v2_api_passwd)
|
|
|
|
else:
|
|
|
|
return self.module.fail_json(
|
|
|
|
msg="You must set the CLC_V2_API_USERNAME and CLC_V2_API_PASSWD "
|
|
|
|
"environment variables")
|
|
|
|
|
|
|
|
def _get_servers_from_clc(self, server_ids, message):
|
|
|
|
"""
|
|
|
|
Gets list of servers form CLC api
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.clc.v2.Servers(server_ids).servers
|
|
|
|
except CLCException as exception:
|
|
|
|
self.module.fail_json(msg=message + ': %s' % exception)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _set_user_agent(clc):
|
|
|
|
if hasattr(clc, 'SetRequestsSession'):
|
|
|
|
agent_string = "ClcAnsibleModule/" + __version__
|
|
|
|
ses = requests.Session()
|
|
|
|
ses.headers.update({"Api-Client": agent_string})
|
|
|
|
ses.headers['User-Agent'] += " " + agent_string
|
|
|
|
clc.SetRequestsSession(ses)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
The main function. Instantiates the module and calls process_request.
|
|
|
|
:return: none
|
|
|
|
"""
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=ClcPublicIp._define_module_argument_spec(),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
clc_public_ip = ClcPublicIp(module)
|
|
|
|
clc_public_ip.process_request()
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import * # pylint: disable=W0614
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|