2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 13:17:19 +02:00
|
|
|
# Copyright Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: profitbricks_nic
|
2022-11-09 14:08:55 +01:00
|
|
|
short_description: Create or Remove a NIC
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- This module allows you to create or restore a volume snapshot. This module has a dependency on profitbricks >= 1.0.0
|
|
|
|
options:
|
|
|
|
datacenter:
|
|
|
|
description:
|
|
|
|
- The datacenter in which to operate.
|
2020-11-25 12:03:12 +01:00
|
|
|
type: str
|
2020-12-01 21:28:40 +01:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
server:
|
|
|
|
description:
|
|
|
|
- The server name or ID.
|
2020-11-25 12:03:12 +01:00
|
|
|
type: str
|
2020-12-01 21:28:40 +01:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name or ID of the NIC. This is only required on deletes, but not on create.
|
2020-12-01 21:28:40 +01:00
|
|
|
- If not specified, it defaults to a value based on UUID4.
|
2020-11-25 12:03:12 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
lan:
|
|
|
|
description:
|
|
|
|
- The LAN to place the NIC on. You can pass a LAN that doesn't exist and it will be created. Required on create.
|
2020-11-25 12:03:12 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
subscription_user:
|
|
|
|
description:
|
|
|
|
- The ProfitBricks username. Overrides the PB_SUBSCRIPTION_ID environment variable.
|
2020-11-25 12:03:12 +01:00
|
|
|
type: str
|
2020-12-01 21:28:40 +01:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
subscription_password:
|
|
|
|
description:
|
|
|
|
- THe ProfitBricks password. Overrides the PB_PASSWORD environment variable.
|
2020-11-25 12:03:12 +01:00
|
|
|
type: str
|
2020-12-01 21:28:40 +01:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
wait:
|
|
|
|
description:
|
|
|
|
- wait for the operation to complete before returning
|
|
|
|
required: false
|
2022-08-24 20:22:10 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
|
|
|
wait_timeout:
|
|
|
|
description:
|
|
|
|
- how long before wait gives up, in seconds
|
2020-11-25 12:03:12 +01:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 600
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate desired state of the resource
|
2020-11-25 12:03:12 +01:00
|
|
|
- "The available choices are: C(present), C(absent)."
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
default: 'present'
|
|
|
|
|
|
|
|
requirements: [ "profitbricks" ]
|
|
|
|
author: Matt Baldwin (@baldwinSPC) <baldwin@stackpointcloud.com>
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Create a NIC
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.profitbricks_nic:
|
2020-03-09 10:11:07 +01:00
|
|
|
datacenter: Tardis One
|
|
|
|
server: node002
|
|
|
|
lan: 2
|
|
|
|
wait_timeout: 500
|
|
|
|
state: present
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Remove a NIC
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.profitbricks_nic:
|
2020-03-09 10:11:07 +01:00
|
|
|
datacenter: Tardis One
|
|
|
|
server: node002
|
|
|
|
name: 7341c2454f
|
|
|
|
wait_timeout: 500
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
import re
|
|
|
|
import uuid
|
|
|
|
import time
|
|
|
|
|
|
|
|
HAS_PB_SDK = True
|
|
|
|
try:
|
|
|
|
from profitbricks.client import ProfitBricksService, NIC
|
|
|
|
except ImportError:
|
|
|
|
HAS_PB_SDK = False
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
uuid_match = re.compile(
|
|
|
|
r'[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}', re.I)
|
|
|
|
|
|
|
|
|
2020-12-01 21:28:40 +01:00
|
|
|
def _make_default_name():
|
|
|
|
return str(uuid.uuid4()).replace('-', '')[:10]
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def _wait_for_completion(profitbricks, promise, wait_timeout, msg):
|
|
|
|
if not promise:
|
|
|
|
return
|
|
|
|
wait_timeout = time.time() + wait_timeout
|
|
|
|
while wait_timeout > time.time():
|
|
|
|
time.sleep(5)
|
|
|
|
operation_result = profitbricks.get_request(
|
|
|
|
request_id=promise['requestId'],
|
|
|
|
status=True)
|
|
|
|
|
|
|
|
if operation_result['metadata']['status'] == "DONE":
|
|
|
|
return
|
|
|
|
elif operation_result['metadata']['status'] == "FAILED":
|
|
|
|
raise Exception(
|
|
|
|
'Request failed to complete ' + msg + ' "' + str(
|
|
|
|
promise['requestId']) + '" to complete.')
|
|
|
|
|
|
|
|
raise Exception(
|
|
|
|
'Timed out waiting for async operation ' + msg + ' "' + str(
|
|
|
|
promise['requestId']
|
|
|
|
) + '" to complete.')
|
|
|
|
|
|
|
|
|
|
|
|
def create_nic(module, profitbricks):
|
|
|
|
"""
|
|
|
|
Creates a NIC.
|
|
|
|
|
|
|
|
module : AnsibleModule object
|
|
|
|
profitbricks: authenticated profitbricks object.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the nic creates, false otherwise
|
|
|
|
"""
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
server = module.params.get('server')
|
|
|
|
lan = module.params.get('lan')
|
|
|
|
name = module.params.get('name')
|
2020-12-01 21:28:40 +01:00
|
|
|
if name is None:
|
|
|
|
name = _make_default_name()
|
2020-03-09 10:11:07 +01:00
|
|
|
wait = module.params.get('wait')
|
|
|
|
wait_timeout = module.params.get('wait_timeout')
|
|
|
|
|
|
|
|
# Locate UUID for Datacenter
|
|
|
|
if not (uuid_match.match(datacenter)):
|
|
|
|
datacenter_list = profitbricks.list_datacenters()
|
|
|
|
for d in datacenter_list['items']:
|
|
|
|
dc = profitbricks.get_datacenter(d['id'])
|
|
|
|
if datacenter == dc['properties']['name']:
|
|
|
|
datacenter = d['id']
|
|
|
|
break
|
|
|
|
|
|
|
|
# Locate UUID for Server
|
|
|
|
if not (uuid_match.match(server)):
|
|
|
|
server_list = profitbricks.list_servers(datacenter)
|
|
|
|
for s in server_list['items']:
|
|
|
|
if server == s['properties']['name']:
|
|
|
|
server = s['id']
|
|
|
|
break
|
|
|
|
try:
|
|
|
|
n = NIC(
|
|
|
|
name=name,
|
|
|
|
lan=lan
|
|
|
|
)
|
|
|
|
|
|
|
|
nic_response = profitbricks.create_nic(datacenter, server, n)
|
|
|
|
|
|
|
|
if wait:
|
|
|
|
_wait_for_completion(profitbricks, nic_response,
|
|
|
|
wait_timeout, "create_nic")
|
|
|
|
|
|
|
|
return nic_response
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="failed to create the NIC: %s" % str(e))
|
|
|
|
|
|
|
|
|
|
|
|
def delete_nic(module, profitbricks):
|
|
|
|
"""
|
|
|
|
Removes a NIC
|
|
|
|
|
|
|
|
module : AnsibleModule object
|
|
|
|
profitbricks: authenticated profitbricks object.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the NIC was removed, false otherwise
|
|
|
|
"""
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
server = module.params.get('server')
|
|
|
|
name = module.params.get('name')
|
2020-12-01 21:28:40 +01:00
|
|
|
if name is None:
|
|
|
|
name = _make_default_name()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Locate UUID for Datacenter
|
|
|
|
if not (uuid_match.match(datacenter)):
|
|
|
|
datacenter_list = profitbricks.list_datacenters()
|
|
|
|
for d in datacenter_list['items']:
|
|
|
|
dc = profitbricks.get_datacenter(d['id'])
|
|
|
|
if datacenter == dc['properties']['name']:
|
|
|
|
datacenter = d['id']
|
|
|
|
break
|
|
|
|
|
|
|
|
# Locate UUID for Server
|
|
|
|
server_found = False
|
|
|
|
if not (uuid_match.match(server)):
|
|
|
|
server_list = profitbricks.list_servers(datacenter)
|
|
|
|
for s in server_list['items']:
|
|
|
|
if server == s['properties']['name']:
|
|
|
|
server_found = True
|
|
|
|
server = s['id']
|
|
|
|
break
|
|
|
|
|
|
|
|
if not server_found:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Locate UUID for NIC
|
|
|
|
nic_found = False
|
|
|
|
if not (uuid_match.match(name)):
|
|
|
|
nic_list = profitbricks.list_nics(datacenter, server)
|
|
|
|
for n in nic_list['items']:
|
|
|
|
if name == n['properties']['name']:
|
|
|
|
nic_found = True
|
|
|
|
name = n['id']
|
|
|
|
break
|
|
|
|
|
|
|
|
if not nic_found:
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
nic_response = profitbricks.delete_nic(datacenter, server, name)
|
|
|
|
return nic_response
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="failed to remove the NIC: %s" % str(e))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2020-12-01 21:28:40 +01:00
|
|
|
datacenter=dict(required=True),
|
|
|
|
server=dict(required=True),
|
|
|
|
name=dict(),
|
2020-03-09 10:11:07 +01:00
|
|
|
lan=dict(),
|
2020-12-01 21:28:40 +01:00
|
|
|
subscription_user=dict(required=True),
|
|
|
|
subscription_password=dict(required=True, no_log=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
wait=dict(type='bool', default=True),
|
|
|
|
wait_timeout=dict(type='int', default=600),
|
|
|
|
state=dict(default='present'),
|
2020-12-01 21:28:40 +01:00
|
|
|
),
|
|
|
|
required_if=(
|
|
|
|
('state', 'absent', ['name']),
|
|
|
|
('state', 'present', ['lan']),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not HAS_PB_SDK:
|
|
|
|
module.fail_json(msg='profitbricks required for this module')
|
|
|
|
|
|
|
|
subscription_user = module.params.get('subscription_user')
|
|
|
|
subscription_password = module.params.get('subscription_password')
|
|
|
|
|
|
|
|
profitbricks = ProfitBricksService(
|
|
|
|
username=subscription_user,
|
|
|
|
password=subscription_password)
|
|
|
|
|
|
|
|
state = module.params.get('state')
|
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
try:
|
|
|
|
(changed) = delete_nic(module, profitbricks)
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg='failed to set nic state: %s' % str(e))
|
|
|
|
|
|
|
|
elif state == 'present':
|
|
|
|
try:
|
|
|
|
(nic_dict) = create_nic(module, profitbricks)
|
2020-11-25 12:03:12 +01:00
|
|
|
module.exit_json(nics=nic_dict) # @FIXME changed not calculated?
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg='failed to set nic state: %s' % str(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|