cloudscale migrated to cloudscale_ch.cloud (#517)

This commit is contained in:
René Moser 2020-06-16 17:12:47 +02:00 committed by GitHub
commit e5bb9dab9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 0 additions and 3657 deletions

View file

@ -1,296 +0,0 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, Gaudenz Steinlin <gaudenz.steinlin@cloudscale.ch>
# 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
DOCUMENTATION = '''
---
module: cloudscale_floating_ip
short_description: Manages floating IPs on the cloudscale.ch IaaS service
description:
- Create, assign and delete floating IPs on the cloudscale.ch IaaS service.
notes:
- To create a new floating IP at least the C(ip_version) and C(server) options are required.
- Once a floating_ip is created all parameters except C(server) are read-only.
- It's not possible to request a floating IP without associating it with a server at the same time.
- This module requires the ipaddress python library. This library is included in Python since version 3.3. It is available as a
module on PyPI for earlier versions.
author:
- Gaudenz Steinlin (@gaudenz)
- Denis Krienbühl (@href)
options:
state:
description:
- State of the floating IP.
default: present
choices: [ present, absent ]
type: str
ip:
description:
- Floating IP address to change.
- Required to assign the IP to a different server or if I(state) is absent.
aliases: [ network ]
type: str
ip_version:
description:
- IP protocol version of the floating IP.
choices: [ 4, 6 ]
type: int
server:
description:
- UUID of the server assigned to this floating IP.
- Required unless I(state) is absent.
type: str
type:
description:
- The type of the floating IP.
choices: [ regional, global ]
type: str
default: regional
region:
description:
- Region in which the floating IP resides (e.g. C(lgp) or C(rma)).
If omitted, the region of the project default zone is used.
This parameter must be omitted if I(type) is set to C(global).
type: str
version_added: '0.2.0'
prefix_length:
description:
- Only valid if I(ip_version) is 6.
- Prefix length for the IPv6 network. Currently only a prefix of /56 can be requested. If no I(prefix_length) is present, a
single address is created.
choices: [ 56 ]
type: int
reverse_ptr:
description:
- Reverse PTR entry for this address.
- You cannot set a reverse PTR entry for IPv6 floating networks. Reverse PTR entries are only allowed for single addresses.
type: str
extends_documentation_fragment:
- community.general.cloudscale
'''
EXAMPLES = '''
# Request a new floating IP
- name: Request a floating IP
cloudscale_floating_ip:
ip_version: 4
server: 47cec963-fcd2-482f-bdb6-24461b2d47b1
reverse_ptr: my-server.example.com
api_token: xxxxxx
register: floating_ip
# Assign an existing floating IP to a different server
- name: Move floating IP to backup server
cloudscale_floating_ip:
ip: 192.0.2.123
server: ea3b39a3-77a8-4d0b-881d-0bb00a1e7f48
api_token: xxxxxx
# Request a new floating IPv6 network
- name: Request a floating IP
cloudscale_floating_ip:
ip_version: 6
prefix_length: 56
server: 47cec963-fcd2-482f-bdb6-24461b2d47b1
api_token: xxxxxx
region: lpg1
register: floating_ip
# Assign an existing floating network to a different server
- name: Move floating IP to backup server
cloudscale_floating_ip:
ip: '{{ floating_ip.network | ip }}'
server: ea3b39a3-77a8-4d0b-881d-0bb00a1e7f48
api_token: xxxxxx
# Release a floating IP
- name: Release floating IP
cloudscale_floating_ip:
ip: 192.0.2.123
state: absent
api_token: xxxxxx
'''
RETURN = '''
href:
description: The API URL to get details about this floating IP.
returned: success when state == present
type: str
sample: https://api.cloudscale.ch/v1/floating-ips/2001:db8::cafe
network:
description: The CIDR notation of the network that is routed to your server.
returned: success when state == present
type: str
sample: 2001:db8::cafe/128
next_hop:
description: Your floating IP is routed to this IP address.
returned: success when state == present
type: str
sample: 2001:db8:dead:beef::42
reverse_ptr:
description: The reverse pointer for this floating IP address.
returned: success when state == present
type: str
sample: 185-98-122-176.cust.cloudscale.ch
server:
description: The floating IP is routed to this server.
returned: success when state == present
type: str
sample: 47cec963-fcd2-482f-bdb6-24461b2d47b1
ip:
description: The floating IP address or network. This is always present and used to identify floating IPs after creation.
returned: success
type: str
sample: 185.98.122.176
region:
description: The region of the floating IP.
returned: success when state == present
type: dict
sample: {'slug': 'lpg'}
version_added: '0.2.0'
state:
description: The current status of the floating IP.
returned: success
type: str
sample: present
'''
import traceback
IPADDRESS_IMP_ERR = None
try:
from ipaddress import ip_network
HAS_IPADDRESS = True
except ImportError:
IPADDRESS_IMP_ERR = traceback.format_exc()
HAS_IPADDRESS = False
from ansible.module_utils.basic import AnsibleModule, env_fallback, missing_required_lib
from ansible_collections.community.general.plugins.module_utils.cloudscale import AnsibleCloudscaleBase, cloudscale_argument_spec
class AnsibleCloudscaleFloatingIP(AnsibleCloudscaleBase):
def __init__(self, module):
super(AnsibleCloudscaleFloatingIP, self).__init__(module)
# Initialize info dict
# Set state to absent, will be updated by self.update_info()
self.info = {'state': 'absent'}
if self._module.params['ip']:
self.update_info()
@staticmethod
def _resp2info(resp):
# If the API response has some content, the floating IP must exist
resp['state'] = 'present'
# Add the IP address to the response, otherwise handling get's to complicated as this
# has to be converted from the network all the time.
resp['ip'] = str(ip_network(resp['network']).network_address)
# Replace the server with just the UUID, the href to the server is useless and just makes
# things more complicated
if resp['server'] is not None:
resp['server'] = resp['server']['uuid']
return resp
def update_info(self):
resp = self._get('floating-ips/' + self._module.params['ip'])
if resp:
self.info = self._resp2info(resp)
else:
self.info = {'ip': self._module.params['ip'],
'state': 'absent'}
def request_floating_ip(self):
params = self._module.params
# check for required parameters to request a floating IP
missing_parameters = []
for p in ('ip_version', 'server'):
if p not in params or not params[p]:
missing_parameters.append(p)
if len(missing_parameters) > 0:
self._module.fail_json(msg='Missing required parameter(s) to request a floating IP: %s.' %
' '.join(missing_parameters))
data = {'ip_version': params['ip_version'],
'server': params['server']}
for p in ('prefix_length', 'reverse_ptr', 'type', 'region'):
if params[p]:
data[p] = params[p]
self.info = self._resp2info(self._post('floating-ips', data))
def release_floating_ip(self):
self._delete('floating-ips/%s' % self._module.params['ip'])
self.info = {'ip': self.info['ip'], 'state': 'absent'}
def update_floating_ip(self):
params = self._module.params
if 'server' not in params or not params['server']:
self._module.fail_json(msg='Missing required parameter to update a floating IP: server.')
self.info = self._resp2info(self._post('floating-ips/%s' % params['ip'], {'server': params['server']}))
def main():
argument_spec = cloudscale_argument_spec()
argument_spec.update(dict(
state=dict(default='present', choices=('present', 'absent'), type='str'),
ip=dict(aliases=('network', ), type='str'),
ip_version=dict(choices=(4, 6), type='int'),
server=dict(type='str'),
type=dict(type='str', choices=('regional', 'global'), default='regional'),
region=dict(type='str'),
prefix_length=dict(choices=(56,), type='int'),
reverse_ptr=dict(type='str'),
))
module = AnsibleModule(
argument_spec=argument_spec,
required_one_of=(('ip', 'ip_version'),),
supports_check_mode=True,
)
if not HAS_IPADDRESS:
module.fail_json(msg=missing_required_lib('ipaddress'), exception=IPADDRESS_IMP_ERR)
target_state = module.params['state']
target_server = module.params['server']
floating_ip = AnsibleCloudscaleFloatingIP(module)
current_state = floating_ip.info['state']
current_server = floating_ip.info['server'] if 'server' in floating_ip.info else None
if module.check_mode:
module.exit_json(changed=not target_state == current_state or
(current_state == 'present' and current_server != target_server),
**floating_ip.info)
changed = False
if current_state == 'absent' and target_state == 'present':
floating_ip.request_floating_ip()
changed = True
elif current_state == 'present' and target_state == 'absent':
floating_ip.release_floating_ip()
changed = True
elif current_state == 'present' and current_server != target_server:
floating_ip.update_floating_ip()
changed = True
module.exit_json(changed=changed, **floating_ip.info)
if __name__ == '__main__':
main()

View file

@ -1,549 +0,0 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright: (c) 2017, Gaudenz Steinlin <gaudenz.steinlin@cloudscale.ch>
# Copyright: (c) 2019, René Moser <mail@renemoser.net>
# 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
DOCUMENTATION = '''
---
module: cloudscale_server
short_description: Manages servers on the cloudscale.ch IaaS service
description:
- Create, update, start, stop and delete servers on the cloudscale.ch IaaS service.
notes:
- Since version 2.8, I(uuid) and I(name) or not mutually exclusive anymore.
- If I(uuid) option is provided, it takes precedence over I(name) for server selection. This allows to update the server's name.
- If no I(uuid) option is provided, I(name) is used for server selection. If more than one server with this name exists, execution is aborted.
- Only the I(name) and I(flavor) are evaluated for the update.
- The option I(force=true) must be given to allow the reboot of existing running servers for applying the changes.
author:
- Gaudenz Steinlin (@gaudenz)
- René Moser (@resmo)
- Denis Krienbühl (@href)
options:
state:
description:
- State of the server.
choices: [ running, stopped, absent ]
default: running
type: str
name:
description:
- Name of the Server.
- Either I(name) or I(uuid) are required.
type: str
uuid:
description:
- UUID of the server.
- Either I(name) or I(uuid) are required.
type: str
flavor:
description:
- Flavor of the server.
type: str
image:
description:
- Image used to create the server.
type: str
zone:
description:
- Zone in which the server resides (e.g. C(lgp1) or C(rma1)).
type: str
version_added: '0.2.0'
volume_size_gb:
description:
- Size of the root volume in GB.
default: 10
type: int
bulk_volume_size_gb:
description:
- Size of the bulk storage volume in GB.
- No bulk storage volume if not set.
type: int
ssh_keys:
description:
- List of SSH public keys.
- Use the full content of your .pub file here.
type: list
password:
description:
- Password for the server.
type: str
use_public_network:
description:
- Attach a public network interface to the server.
default: yes
type: bool
use_private_network:
description:
- Attach a private network interface to the server.
default: no
type: bool
use_ipv6:
description:
- Enable IPv6 on the public network interface.
default: yes
type: bool
anti_affinity_with:
description:
- UUID of another server to create an anti-affinity group with.
- Mutually exclusive with I(server_groups).
- Deprecated, removed in community.general 2.0.0.
type: str
server_groups:
description:
- List of UUID or names of server groups.
- Mutually exclusive with I(anti_affinity_with).
type: list
user_data:
description:
- Cloud-init configuration (cloud-config) data to use for the server.
type: str
force:
description:
- Allow to stop the running server for updating if necessary.
default: no
type: bool
tags:
description:
- Tags assosiated with the servers. Set this to C({}) to clear any tags.
type: dict
extends_documentation_fragment:
- community.general.cloudscale
'''
EXAMPLES = '''
# Create and start a server with an existing server group (shiny-group)
- name: Start cloudscale.ch server
cloudscale_server:
name: my-shiny-cloudscale-server
image: debian-8
flavor: flex-4
ssh_keys: ssh-rsa XXXXXXXXXX...XXXX ansible@cloudscale
server_groups: shiny-group
zone: lpg1
use_private_network: True
bulk_volume_size_gb: 100
api_token: xxxxxx
# Start another server in anti-affinity (server group shiny-group)
- name: Start second cloudscale.ch server
cloudscale_server:
name: my-other-shiny-server
image: ubuntu-16.04
flavor: flex-8
ssh_keys: ssh-rsa XXXXXXXXXXX ansible@cloudscale
server_groups: shiny-group
zone: lpg1
api_token: xxxxxx
# Force to update the flavor of a running server
- name: Start cloudscale.ch server
cloudscale_server:
name: my-shiny-cloudscale-server
image: debian-8
flavor: flex-8
force: yes
ssh_keys: ssh-rsa XXXXXXXXXX...XXXX ansible@cloudscale
use_private_network: True
bulk_volume_size_gb: 100
api_token: xxxxxx
register: server1
# Stop the first server
- name: Stop my first server
cloudscale_server:
uuid: '{{ server1.uuid }}'
state: stopped
api_token: xxxxxx
# Delete my second server
- name: Delete my second server
cloudscale_server:
name: my-other-shiny-server
state: absent
api_token: xxxxxx
# Start a server and wait for the SSH host keys to be generated
- name: Start server and wait for SSH host keys
cloudscale_server:
name: my-cloudscale-server-with-ssh-key
image: debian-8
flavor: flex-4
ssh_keys: ssh-rsa XXXXXXXXXXX ansible@cloudscale
api_token: xxxxxx
register: server
until: server.ssh_fingerprints is defined and server.ssh_fingerprints
retries: 60
delay: 2
'''
RETURN = '''
href:
description: API URL to get details about this server
returned: success when not state == absent
type: str
sample: https://api.cloudscale.ch/v1/servers/cfde831a-4e87-4a75-960f-89b0148aa2cc
uuid:
description: The unique identifier for this server
returned: success
type: str
sample: cfde831a-4e87-4a75-960f-89b0148aa2cc
name:
description: The display name of the server
returned: success
type: str
sample: its-a-me-mario.cloudscale.ch
state:
description: The current status of the server
returned: success
type: str
sample: running
flavor:
description: The flavor that has been used for this server
returned: success when not state == absent
type: dict
sample: { "slug": "flex-4", "name": "Flex-4", "vcpu_count": 2, "memory_gb": 4 }
image:
description: The image used for booting this server
returned: success when not state == absent
type: dict
sample: { "default_username": "ubuntu", "name": "Ubuntu 18.04 LTS", "operating_system": "Ubuntu", "slug": "ubuntu-18.04" }
zone:
description: The zone used for booting this server
returned: success when not state == absent
type: dict
sample: { 'slug': 'lpg1' }
version_added: '0.2.0'
volumes:
description: List of volumes attached to the server
returned: success when not state == absent
type: list
sample: [ {"type": "ssd", "device": "/dev/vda", "size_gb": "50"} ]
interfaces:
description: List of network ports attached to the server
returned: success when not state == absent
type: list
sample: [ { "type": "public", "addresses": [ ... ] } ]
ssh_fingerprints:
description: A list of SSH host key fingerprints. Will be null until the host keys could be retrieved from the server.
returned: success when not state == absent
type: list
sample: ["ecdsa-sha2-nistp256 SHA256:XXXX", ... ]
ssh_host_keys:
description: A list of SSH host keys. Will be null until the host keys could be retrieved from the server.
returned: success when not state == absent
type: list
sample: ["ecdsa-sha2-nistp256 XXXXX", ... ]
anti_affinity_with:
description:
- List of servers in the same anti-affinity group
- Deprecated, removed in community.general 2.0.0.
returned: success when not state == absent
type: list
sample: []
server_groups:
description: List of server groups
returned: success when not state == absent
type: list
sample: [ {"href": "https://api.cloudscale.ch/v1/server-groups/...", "uuid": "...", "name": "db-group"} ]
tags:
description: Tags assosiated with the volume.
returned: success
type: dict
sample: { 'project': 'my project' }
'''
from datetime import datetime, timedelta
from time import sleep
from copy import deepcopy
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.cloudscale import AnsibleCloudscaleBase, cloudscale_argument_spec
ALLOWED_STATES = ('running',
'stopped',
'absent',
)
class AnsibleCloudscaleServer(AnsibleCloudscaleBase):
def __init__(self, module):
super(AnsibleCloudscaleServer, self).__init__(module)
# Initialize server dictionary
self._info = {}
def _init_server_container(self):
return {
'uuid': self._module.params.get('uuid') or self._info.get('uuid'),
'name': self._module.params.get('name') or self._info.get('name'),
'state': 'absent',
}
def _get_server_info(self, refresh=False):
if self._info and not refresh:
return self._info
self._info = self._init_server_container()
uuid = self._info.get('uuid')
if uuid is not None:
server_info = self._get('servers/%s' % uuid)
if server_info:
self._info = self._transform_state(server_info)
else:
name = self._info.get('name')
if name is not None:
servers = self._get('servers') or []
matching_server = []
for server in servers:
if server['name'] == name:
matching_server.append(server)
if len(matching_server) == 1:
self._info = self._transform_state(matching_server[0])
elif len(matching_server) > 1:
self._module.fail_json(msg="More than one server with name '%s' exists. "
"Use the 'uuid' parameter to identify the server." % name)
return self._info
@staticmethod
def _transform_state(server):
if 'status' in server:
server['state'] = server['status']
del server['status']
else:
server['state'] = 'absent'
return server
def _wait_for_state(self, states):
start = datetime.now()
timeout = self._module.params['api_timeout'] * 2
while datetime.now() - start < timedelta(seconds=timeout):
server_info = self._get_server_info(refresh=True)
if server_info.get('state') in states:
return server_info
sleep(1)
# Timeout succeeded
if server_info.get('name') is not None:
msg = "Timeout while waiting for a state change on server %s to states %s. " \
"Current state is %s." % (server_info.get('name'), states, server_info.get('state'))
else:
name_uuid = self._module.params.get('name') or self._module.params.get('uuid')
msg = 'Timeout while waiting to find the server %s' % name_uuid
self._module.fail_json(msg=msg)
def _start_stop_server(self, server_info, target_state="running", ignore_diff=False):
actions = {
'stopped': 'stop',
'running': 'start',
}
server_state = server_info.get('state')
if server_state != target_state:
self._result['changed'] = True
if not ignore_diff:
self._result['diff']['before'].update({
'state': server_info.get('state'),
})
self._result['diff']['after'].update({
'state': target_state,
})
if not self._module.check_mode:
self._post('servers/%s/%s' % (server_info['uuid'], actions[target_state]))
server_info = self._wait_for_state((target_state, ))
return server_info
def _update_param(self, param_key, server_info, requires_stop=False):
param_value = self._module.params.get(param_key)
if param_value is None:
return server_info
if 'slug' in server_info[param_key]:
server_v = server_info[param_key]['slug']
else:
server_v = server_info[param_key]
if server_v != param_value:
# Set the diff output
self._result['diff']['before'].update({param_key: server_v})
self._result['diff']['after'].update({param_key: param_value})
if server_info.get('state') == "running":
if requires_stop and not self._module.params.get('force'):
self._module.warn("Some changes won't be applied to running servers. "
"Use force=yes to allow the server '%s' to be stopped/started." % server_info['name'])
return server_info
# Either the server is stopped or change is forced
self._result['changed'] = True
if not self._module.check_mode:
if requires_stop:
self._start_stop_server(server_info, target_state="stopped", ignore_diff=True)
patch_data = {
param_key: param_value,
}
# Response is 204: No Content
self._patch('servers/%s' % server_info['uuid'], patch_data)
# State changes to "changing" after update, waiting for stopped/running
server_info = self._wait_for_state(('stopped', 'running'))
return server_info
def _get_server_group_ids(self):
server_group_params = self._module.params['server_groups']
if not server_group_params:
return None
matching_group_names = []
results = []
server_groups = self._get('server-groups')
for server_group in server_groups:
if server_group['uuid'] in server_group_params:
results.append(server_group['uuid'])
server_group_params.remove(server_group['uuid'])
elif server_group['name'] in server_group_params:
results.append(server_group['uuid'])
server_group_params.remove(server_group['name'])
# Remember the names found
matching_group_names.append(server_group['name'])
# Names are not unique, verify if name already found in previous iterations
elif server_group['name'] in matching_group_names:
self._module.fail_json(msg="More than one server group with name exists: '%s'. "
"Use the 'uuid' parameter to identify the server group." % server_group['name'])
if server_group_params:
self._module.fail_json(msg="Server group name or UUID not found: %s" % ', '.join(server_group_params))
return results
def _create_server(self, server_info):
self._result['changed'] = True
data = deepcopy(self._module.params)
for i in ('uuid', 'state', 'force', 'api_timeout', 'api_token'):
del data[i]
data['server_groups'] = self._get_server_group_ids()
self._result['diff']['before'] = self._init_server_container()
self._result['diff']['after'] = deepcopy(data)
if not self._module.check_mode:
self._post('servers', data)
server_info = self._wait_for_state(('running', ))
return server_info
def _update_server(self, server_info):
previous_state = server_info.get('state')
# The API doesn't support to update server groups.
# Show a warning to the user if the desired state does not match.
desired_server_group_ids = self._get_server_group_ids()
if desired_server_group_ids is not None:
current_server_group_ids = [grp['uuid'] for grp in server_info['server_groups']]
if desired_server_group_ids != current_server_group_ids:
self._module.warn("Server groups can not be mutated, server needs redeployment to change groups.")
server_info = self._update_param('flavor', server_info, requires_stop=True)
server_info = self._update_param('name', server_info)
server_info = self._update_param('tags', server_info)
if previous_state == "running":
server_info = self._start_stop_server(server_info, target_state="running", ignore_diff=True)
return server_info
def present_server(self):
server_info = self._get_server_info()
if server_info.get('state') != "absent":
# If target state is stopped, stop before an potential update and force would not be required
if self._module.params.get('state') == "stopped":
server_info = self._start_stop_server(server_info, target_state="stopped")
server_info = self._update_server(server_info)
if self._module.params.get('state') == "running":
server_info = self._start_stop_server(server_info, target_state="running")
else:
server_info = self._create_server(server_info)
server_info = self._start_stop_server(server_info, target_state=self._module.params.get('state'))
return server_info
def absent_server(self):
server_info = self._get_server_info()
if server_info.get('state') != "absent":
self._result['changed'] = True
self._result['diff']['before'] = deepcopy(server_info)
self._result['diff']['after'] = self._init_server_container()
if not self._module.check_mode:
self._delete('servers/%s' % server_info['uuid'])
server_info = self._wait_for_state(('absent', ))
return server_info
def main():
argument_spec = cloudscale_argument_spec()
argument_spec.update(dict(
state=dict(default='running', choices=ALLOWED_STATES),
name=dict(),
uuid=dict(),
flavor=dict(),
image=dict(),
zone=dict(),
volume_size_gb=dict(type='int', default=10),
bulk_volume_size_gb=dict(type='int'),
ssh_keys=dict(type='list'),
password=dict(no_log=True),
use_public_network=dict(type='bool', default=True),
use_private_network=dict(type='bool', default=False),
use_ipv6=dict(type='bool', default=True),
anti_affinity_with=dict(removed_in_version='2.0.0', removed_from_collection='community.general'), # was Ansible 2.11
server_groups=dict(type='list'),
user_data=dict(),
force=dict(type='bool', default=False),
tags=dict(type='dict'),
))
module = AnsibleModule(
argument_spec=argument_spec,
required_one_of=(('name', 'uuid'),),
mutually_exclusive=(('anti_affinity_with', 'server_groups'),),
supports_check_mode=True,
)
cloudscale_server = AnsibleCloudscaleServer(module)
if module.params['state'] == "absent":
server = cloudscale_server.absent_server()
else:
server = cloudscale_server.present_server()
result = cloudscale_server.get_result(server)
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -1,232 +0,0 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019, René Moser <mail@renemoser.net>
# 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
DOCUMENTATION = '''
---
module: cloudscale_server_group
short_description: Manages server groups on the cloudscale.ch IaaS service
description:
- Create, update and remove server groups.
author:
- René Moser (@resmo)
- Denis Krienbühl (@href)
options:
name:
description:
- Name of the server group.
- Either I(name) or I(uuid) is required. These options are mutually exclusive.
type: str
uuid:
description:
- UUID of the server group.
- Either I(name) or I(uuid) is required. These options are mutually exclusive.
type: str
type:
description:
- Type of the server group.
default: anti-affinity
type: str
zone:
description:
- Zone slug of the server group (e.g. C(lgp1) or C(rma1)).
type: str
version_added: '0.2.0'
state:
description:
- State of the server group.
choices: [ present, absent ]
default: present
type: str
tags:
description:
- Tags assosiated with the server groups. Set this to C({}) to clear any tags.
type: dict
extends_documentation_fragment:
- community.general.cloudscale
'''
EXAMPLES = '''
---
- name: Ensure server group exists
cloudscale_server_group:
name: my-name
type: anti-affinity
api_token: xxxxxx
- name: Ensure server group in a specific zone
cloudscale_server_group:
name: my-rma-group
type: anti-affinity
zone: lpg1
api_token: xxxxxx
- name: Ensure a server group is absent
cloudscale_server_group:
name: my-name
state: absent
api_token: xxxxxx
'''
RETURN = '''
---
href:
description: API URL to get details about this server group
returned: if available
type: str
sample: https://api.cloudscale.ch/v1/server-group/cfde831a-4e87-4a75-960f-89b0148aa2cc
uuid:
description: The unique identifier for this server
returned: always
type: str
sample: cfde831a-4e87-4a75-960f-89b0148aa2cc
name:
description: The display name of the server group
returned: always
type: str
sample: load balancers
type:
description: The type the server group
returned: if available
type: str
sample: anti-affinity
zone:
description: The zone of the server group
returned: success
type: dict
sample: { 'slug': 'rma1' }
version_added: '0.2.0'
servers:
description: A list of servers that are part of the server group.
returned: if available
type: list
sample: []
state:
description: State of the server group.
returned: always
type: str
sample: present
tags:
description: Tags assosiated with the server group.
returned: success
type: dict
sample: { 'project': 'my project' }
'''
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.cloudscale import AnsibleCloudscaleBase, cloudscale_argument_spec
class AnsibleCloudscaleServerGroup(AnsibleCloudscaleBase):
def __init__(self, module, namespace):
super(AnsibleCloudscaleServerGroup, self).__init__(module)
self._info = {}
def _init_container(self):
return {
'uuid': self._module.params.get('uuid') or self._info.get('uuid'),
'name': self._module.params.get('name') or self._info.get('name'),
'state': 'absent',
}
def _create_server_group(self, server_group):
self._module.fail_on_missing_params(['name'])
self._result['changed'] = True
data = {
'name': self._module.params.get('name'),
'type': self._module.params.get('type'),
'zone': self._module.params.get('zone'),
'tags': self._module.params.get('tags'),
}
if not self._module.check_mode:
server_group = self._post('server-groups', data)
return server_group
def _update_server_group(self, server_group):
updated = self._param_updated('name', server_group)
updated = self._param_updated('tags', server_group) or updated
# Refresh if resource was updated in live mode
if updated and not self._module.check_mode:
server_group = self.get_server_group()
return server_group
def get_server_group(self):
self._info = self._init_container()
uuid = self._info.get('uuid')
if uuid is not None:
server_group = self._get('server-groups/%s' % uuid)
if server_group:
self._info.update(server_group)
self._info.update(dict(state='present'))
else:
name = self._info.get('name')
matching_server_groups = []
for server_group in self._get('server-groups'):
if server_group['name'] == name:
matching_server_groups.append(server_group)
if len(matching_server_groups) > 1:
self._module.fail_json(msg="More than one server group with name exists: '%s'. "
"Use the 'uuid' parameter to identify the server group." % name)
elif len(matching_server_groups) == 1:
self._info.update(matching_server_groups[0])
self._info.update(dict(state='present'))
return self._info
def present_group(self):
server_group = self.get_server_group()
if server_group.get('state') == 'absent':
server_group = self._create_server_group(server_group)
else:
server_group = self._update_server_group(server_group)
return server_group
def absent_group(self):
server_group = self.get_server_group()
if server_group.get('state') != 'absent':
self._result['changed'] = True
if not self._module.check_mode:
self._delete('server-groups/%s' % server_group['uuid'])
return server_group
def main():
argument_spec = cloudscale_argument_spec()
argument_spec.update(dict(
name=dict(),
uuid=dict(),
type=dict(default='anti-affinity'),
zone=dict(),
tags=dict(type='dict'),
state=dict(default='present', choices=['absent', 'present']),
))
module = AnsibleModule(
argument_spec=argument_spec,
required_one_of=(('name', 'uuid'),),
supports_check_mode=True,
)
cloudscale_server_group = AnsibleCloudscaleServerGroup(module, 'cloudscale_server_group')
if module.params['state'] == 'absent':
server_group = cloudscale_server_group.absent_group()
else:
server_group = cloudscale_server_group.present_group()
result = cloudscale_server_group.get_result(server_group)
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -1,300 +0,0 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018, Gaudenz Steinlin <gaudenz.steinlin@cloudscale.ch>
# Copyright (c) 2019, René Moser <mail@renemoser.net>
# 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
DOCUMENTATION = '''
---
module: cloudscale_volume
short_description: Manages volumes on the cloudscale.ch IaaS service.
description:
- Create, attach/detach, update and delete volumes on the cloudscale.ch IaaS service.
notes:
- To create a new volume at least the I(name) and I(size_gb) options
are required.
- A volume can be created and attached to a server in the same task.
author:
- Gaudenz Steinlin (@gaudenz)
- René Moser (@resmo)
- Denis Krienbühl (@href)
options:
state:
description:
- State of the volume.
default: present
choices: [ present, absent ]
type: str
name:
description:
- Name of the volume. Either name or UUID must be present to change an
existing volume.
type: str
uuid:
description:
- UUID of the volume. Either name or UUID must be present to change an
existing volume.
type: str
size_gb:
description:
- Size of the volume in GB.
type: int
type:
description:
- Type of the volume. Cannot be changed after creating the volume.
Defaults to C(ssd) on volume creation.
choices: [ ssd, bulk ]
type: str
zone:
description:
- Zone in which the volume resides (e.g. C(lgp1) or C(rma1)). Cannot be
changed after creating the volume. Defaults to the project default zone.
type: str
version_added: '0.2.0'
server_uuids:
description:
- UUIDs of the servers this volume is attached to. Set this to C([]) to
detach the volume. Currently a volume can only be attached to a
single server.
aliases: [ server_uuid ]
type: list
tags:
description:
- Tags associated with the volume. Set this to C({}) to clear any tags.
type: dict
extends_documentation_fragment:
- community.general.cloudscale
'''
EXAMPLES = '''
# Create a new SSD volume
- name: Create an SSD volume
cloudscale_volume:
name: my_ssd_volume
zone: 'lpg1'
size_gb: 50
api_token: xxxxxx
register: my_ssd_volume
# Attach an existing volume to a server
- name: Attach volume to server
cloudscale_volume:
uuid: my_ssd_volume.uuid
server_uuids:
- ea3b39a3-77a8-4d0b-881d-0bb00a1e7f48
api_token: xxxxxx
# Create and attach a volume to a server
- name: Create and attach volume to server
cloudscale_volume:
name: my_ssd_volume
zone: 'lpg1'
size_gb: 50
server_uuids:
- ea3b39a3-77a8-4d0b-881d-0bb00a1e7f48
api_token: xxxxxx
# Detach volume from server
- name: Detach volume from server
cloudscale_volume:
uuid: my_ssd_volume.uuid
server_uuids: []
api_token: xxxxxx
# Delete a volume
- name: Delete volume
cloudscale_volume:
name: my_ssd_volume
state: absent
api_token: xxxxxx
'''
RETURN = '''
href:
description: The API URL to get details about this volume.
returned: state == present
type: str
sample: https://api.cloudscale.ch/v1/volumes/2db69ba3-1864-4608-853a-0771b6885a3a
uuid:
description: The unique identifier for this volume.
returned: state == present
type: str
sample: 2db69ba3-1864-4608-853a-0771b6885a3a
name:
description: The display name of the volume.
returned: state == present
type: str
sample: my_ssd_volume
size_gb:
description: The size of the volume in GB.
returned: state == present
type: str
sample: 50
type:
description: The type of the volume.
returned: state == present
type: str
sample: bulk
zone:
description: The zone of the volume.
returned: state == present
type: dict
sample: {'slug': 'lpg1'}
version_added: '0.2.0'
server_uuids:
description: The UUIDs of the servers this volume is attached to.
returned: state == present
type: list
sample: ['47cec963-fcd2-482f-bdb6-24461b2d47b1']
state:
description: The current status of the volume.
returned: success
type: str
sample: present
tags:
description: Tags associated with the volume.
returned: state == present
type: dict
sample: { 'project': 'my project' }
'''
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.cloudscale import (AnsibleCloudscaleBase,
cloudscale_argument_spec,
)
class AnsibleCloudscaleVolume(AnsibleCloudscaleBase):
def __init__(self, module):
super(AnsibleCloudscaleVolume, self).__init__(module)
self._info = {}
def _init_container(self):
return {
'uuid': self._module.params.get('uuid') or self._info.get('uuid'),
'name': self._module.params.get('name') or self._info.get('name'),
'state': 'absent',
}
def _create(self, volume):
# Fail when missing params for creation
self._module.fail_on_missing_params(['name', 'size_gb'])
# Fail if a user uses a UUID and state=present but the volume was not found.
if self._module.params.get('uuid'):
self._module.fail_json(msg="The volume with UUID '%s' was not found "
"and we would create a new one with different UUID, "
"this is probably not want you have asked for." % self._module.params.get('uuid'))
self._result['changed'] = True
data = {
'name': self._module.params.get('name'),
'type': self._module.params.get('type'),
'zone': self._module.params.get('zone'),
'size_gb': self._module.params.get('size_gb') or 'ssd',
'server_uuids': self._module.params.get('server_uuids') or [],
'tags': self._module.params.get('tags'),
}
if not self._module.check_mode:
volume = self._post('volumes', data)
return volume
def _update(self, volume):
update_params = (
'name',
'size_gb',
'server_uuids',
'tags',
)
updated = False
for param in update_params:
updated = self._param_updated(param, volume) or updated
# Refresh if resource was updated in live mode
if updated and not self._module.check_mode:
volume = self.get_volume()
return volume
def get_volume(self):
self._info = self._init_container()
uuid = self._info.get('uuid')
if uuid is not None:
volume = self._get('volumes/%s' % uuid)
if volume:
self._info.update(volume)
self._info['state'] = 'present'
else:
name = self._info.get('name')
matching_volumes = []
for volume in self._get('volumes'):
if volume['name'] == name:
matching_volumes.append(volume)
if len(matching_volumes) > 1:
self._module.fail_json(msg="More than one volume with name exists: '%s'. "
"Use the 'uuid' parameter to identify the volume." % name)
elif len(matching_volumes) == 1:
self._info.update(matching_volumes[0])
self._info['state'] = 'present'
return self._info
def present(self):
volume = self.get_volume()
if volume.get('state') == 'absent':
volume = self._create(volume)
else:
volume = self._update(volume)
return volume
def absent(self):
volume = self.get_volume()
if volume.get('state') != 'absent':
self._result['changed'] = True
if not self._module.check_mode:
volume['state'] = "absent"
self._delete('volumes/%s' % volume['uuid'])
return volume
def main():
argument_spec = cloudscale_argument_spec()
argument_spec.update(dict(
state=dict(default='present', choices=('present', 'absent')),
name=dict(),
uuid=dict(),
zone=dict(),
size_gb=dict(type='int'),
type=dict(choices=('ssd', 'bulk')),
server_uuids=dict(type='list', aliases=['server_uuid']),
tags=dict(type='dict'),
))
module = AnsibleModule(
argument_spec=argument_spec,
required_one_of=(('name', 'uuid'),),
supports_check_mode=True,
)
cloudscale_volume = AnsibleCloudscaleVolume(module)
if module.params['state'] == 'absent':
server_group = cloudscale_volume.absent()
else:
server_group = cloudscale_volume.present()
result = cloudscale_volume.get_result(server_group)
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -1 +0,0 @@
./cloud/cloudscale/cloudscale_floating_ip.py

View file

@ -1 +0,0 @@
./cloud/cloudscale/cloudscale_server.py

View file

@ -1 +0,0 @@
./cloud/cloudscale/cloudscale_server_group.py

View file

@ -1 +0,0 @@
./cloud/cloudscale/cloudscale_volume.py