2015-08-03 22:00:04 +00:00
|
|
|
#!/usr/bin/python
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
# Copyright: Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-08-03 22:00:04 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2015-08-03 22:00:04 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: profitbricks_volume_attachments
|
|
|
|
short_description: Attach or detach a volume.
|
|
|
|
description:
|
|
|
|
- Allows you to attach or detach a volume from a ProfitBricks server. This module has a dependency on profitbricks >= 1.0.0
|
|
|
|
version_added: "2.0"
|
|
|
|
options:
|
|
|
|
datacenter:
|
|
|
|
description:
|
|
|
|
- The datacenter in which to operate.
|
|
|
|
required: true
|
|
|
|
server:
|
|
|
|
description:
|
|
|
|
- The name of the server you wish to detach or attach the volume.
|
|
|
|
required: true
|
|
|
|
volume:
|
|
|
|
description:
|
|
|
|
- The volume name or ID.
|
|
|
|
required: true
|
|
|
|
subscription_user:
|
|
|
|
description:
|
2016-12-11 02:50:09 +00:00
|
|
|
- The ProfitBricks username. Overrides the PB_SUBSCRIPTION_ID environment variable.
|
2015-08-03 22:00:04 +00:00
|
|
|
required: false
|
|
|
|
subscription_password:
|
|
|
|
description:
|
2016-12-11 02:50:09 +00:00
|
|
|
- THe ProfitBricks password. Overrides the PB_PASSWORD environment variable.
|
2015-08-03 22:00:04 +00:00
|
|
|
required: false
|
|
|
|
wait:
|
|
|
|
description:
|
|
|
|
- wait for the operation to complete before returning
|
|
|
|
required: false
|
|
|
|
default: "yes"
|
2018-04-24 17:05:50 +00:00
|
|
|
type: bool
|
2015-08-03 22:00:04 +00:00
|
|
|
wait_timeout:
|
|
|
|
description:
|
|
|
|
- how long before wait gives up, in seconds
|
|
|
|
default: 600
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate desired state of the resource
|
|
|
|
required: false
|
|
|
|
default: 'present'
|
|
|
|
choices: ["present", "absent"]
|
|
|
|
|
|
|
|
requirements: [ "profitbricks" ]
|
|
|
|
author: Matt Baldwin (baldwin@stackpointcloud.com)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
|
|
|
|
# Attach a Volume
|
|
|
|
|
|
|
|
- profitbricks_volume_attachments:
|
|
|
|
datacenter: Tardis One
|
|
|
|
server: node002
|
|
|
|
volume: vol01
|
|
|
|
wait_timeout: 500
|
|
|
|
state: present
|
|
|
|
|
|
|
|
# Detach a Volume
|
|
|
|
|
|
|
|
- profitbricks_volume_attachments:
|
|
|
|
datacenter: Tardis One
|
|
|
|
server: node002
|
|
|
|
volume: vol01
|
|
|
|
wait_timeout: 500
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
|
|
|
HAS_PB_SDK = True
|
|
|
|
try:
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
from profitbricks.client import ProfitBricksService
|
2015-08-03 22:00:04 +00:00
|
|
|
except ImportError:
|
|
|
|
HAS_PB_SDK = False
|
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
2015-08-03 22:00:04 +00:00
|
|
|
uuid_match = re.compile(
|
2017-11-21 18:24:37 +00:00
|
|
|
r'[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}', re.I)
|
2015-08-03 22:00:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _wait_for_completion(profitbricks, promise, wait_timeout, msg):
|
2017-01-28 08:12:11 +00:00
|
|
|
if not promise:
|
|
|
|
return
|
2015-08-03 22:00:04 +00:00
|
|
|
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']
|
2017-12-07 16:27:06 +00:00
|
|
|
) + '" to complete.')
|
|
|
|
|
2015-08-03 22:00:04 +00:00
|
|
|
|
|
|
|
def attach_volume(module, profitbricks):
|
|
|
|
"""
|
|
|
|
Attaches a volume.
|
|
|
|
|
|
|
|
This will attach a volume to the server.
|
|
|
|
|
|
|
|
module : AnsibleModule object
|
|
|
|
profitbricks: authenticated profitbricks object.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the volume was attached, false otherwise
|
|
|
|
"""
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
server = module.params.get('server')
|
|
|
|
volume = module.params.get('volume')
|
|
|
|
|
|
|
|
# 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']:
|
2017-12-07 16:27:06 +00:00
|
|
|
server = s['id']
|
2015-08-03 22:00:04 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# Locate UUID for Volume
|
|
|
|
if not (uuid_match.match(volume)):
|
|
|
|
volume_list = profitbricks.list_volumes(datacenter)
|
|
|
|
for v in volume_list['items']:
|
|
|
|
if volume == v['properties']['name']:
|
|
|
|
volume = v['id']
|
|
|
|
break
|
|
|
|
|
|
|
|
return profitbricks.attach_volume(datacenter, server, volume)
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2015-08-03 22:00:04 +00:00
|
|
|
def detach_volume(module, profitbricks):
|
|
|
|
"""
|
|
|
|
Detaches a volume.
|
|
|
|
|
2017-01-27 23:20:31 +00:00
|
|
|
This will remove a volume from the server.
|
2015-08-03 22:00:04 +00:00
|
|
|
|
|
|
|
module : AnsibleModule object
|
|
|
|
profitbricks: authenticated profitbricks object.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the volume was detached, false otherwise
|
|
|
|
"""
|
|
|
|
datacenter = module.params.get('datacenter')
|
|
|
|
server = module.params.get('server')
|
|
|
|
volume = module.params.get('volume')
|
|
|
|
|
|
|
|
# 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']:
|
2017-12-07 16:27:06 +00:00
|
|
|
server = s['id']
|
2015-08-03 22:00:04 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# Locate UUID for Volume
|
|
|
|
if not (uuid_match.match(volume)):
|
|
|
|
volume_list = profitbricks.list_volumes(datacenter)
|
|
|
|
for v in volume_list['items']:
|
|
|
|
if volume == v['properties']['name']:
|
|
|
|
volume = v['id']
|
|
|
|
break
|
|
|
|
|
|
|
|
return profitbricks.detach_volume(datacenter, server, volume)
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2015-08-03 22:00:04 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
datacenter=dict(),
|
|
|
|
server=dict(),
|
|
|
|
volume=dict(),
|
|
|
|
subscription_user=dict(),
|
2017-02-10 20:13:59 +00:00
|
|
|
subscription_password=dict(no_log=True),
|
2015-08-03 22:00:04 +00:00
|
|
|
wait=dict(type='bool', default=True),
|
|
|
|
wait_timeout=dict(type='int', default=600),
|
|
|
|
state=dict(default='present'),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not HAS_PB_SDK:
|
|
|
|
module.fail_json(msg='profitbricks required for this module')
|
|
|
|
|
|
|
|
if not module.params.get('subscription_user'):
|
|
|
|
module.fail_json(msg='subscription_user parameter is required')
|
|
|
|
if not module.params.get('subscription_password'):
|
|
|
|
module.fail_json(msg='subscription_password parameter is required')
|
|
|
|
if not module.params.get('datacenter'):
|
|
|
|
module.fail_json(msg='datacenter parameter is required')
|
|
|
|
if not module.params.get('server'):
|
|
|
|
module.fail_json(msg='server parameter is required')
|
|
|
|
if not module.params.get('volume'):
|
|
|
|
module.fail_json(msg='volume parameter is required')
|
|
|
|
|
|
|
|
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) = detach_volume(module, profitbricks)
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg='failed to set volume_attach state: %s' % str(e))
|
|
|
|
elif state == 'present':
|
|
|
|
try:
|
|
|
|
attach_volume(module, profitbricks)
|
|
|
|
module.exit_json()
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg='failed to set volume_attach state: %s' % str(e))
|
|
|
|
|
|
|
|
|
2016-12-05 16:20:51 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|