Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
Include ability to resize, attach, and detach volumes (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalvarezguillen authored Jan 22, 2021
1 parent 17f1d2c commit dcb398f
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 11 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/14-attach-detach-and-resize-volumes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- vultr_block_storage - Included ability to resize, attach and detach Block Storage Volumes.
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ tags:
- cloud
- vultr
- ngine_io
version: 1.0.0
version: 1.1.0
146 changes: 141 additions & 5 deletions plugins/modules/vultr_block_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
description:
- Size of the block storage volume in GB.
- Required if I(state) is present.
- If it's larger than the volume's current size, the volume will be resized.
type: int
region:
description:
Expand All @@ -37,8 +38,19 @@
description:
- State of the block storage volume.
default: present
choices: [ present, absent ]
choices: [ present, absent, attached, detached ]
type: str
attached_to_SUBID:
description:
- The ID of the server the volume is attached to.
- Required if I(state) is attached.
aliases: [ attached_to_id ]
type: int
live_attachment:
description:
- Whether the volume should be attached/detached, even if the server not stopped.
type: bool
default: True
extends_documentation_fragment:
- ngine_io.vultr.vultr
Expand All @@ -55,6 +67,19 @@
ngine_io.vultr.vultr_block_storage:
name: myvolume
state: absent
- name: Ensure a block storage volume exists and is attached to server 114
ngine_io.vultr.vultr_block_storage:
name: myvolume
state: attached
attached_to_id: 114
size: 10
- name: Ensure a block storage volume exists and is not attached to any server
ngine_io.vultr.vultr_block_storage:
name: myvolume
state: detached
size: 10
'''

RETURN = '''
Expand Down Expand Up @@ -136,7 +161,6 @@
sample: "active"
'''

from ansible.module_utils.basic import AnsibleModule
from ..module_utils.vultr import (
Vultr,
Expand Down Expand Up @@ -216,28 +240,140 @@ def absent_block_storage_volume(self):
)
return volume

def detached_block_storage_volume(self):
volume = self.present_block_storage_volume()
if volume.get('attached_to_SUBID') is None:
return volume

self.result['changed'] = True

if not self.module.check_mode:
data = {
'SUBID': volume['SUBID'],
'live': self.get_yes_or_no('live_attachment')
}
self.api_query(
path='/v1/block/detach',
method='POST',
data=data
)

volume = self.get_block_storage_volumes()
else:
volume['attached_to_SUBID'] = None

self.result['diff']['after'] = volume

return volume

def attached_block_storage_volume(self):
expected_server = self.module.params.get('attached_to_SUBID')
volume = self.present_block_storage_volume()
server = volume.get('attached_to_SUBID')
if server == expected_server:
return volume

if server is not None:
self.module.fail_json(
msg='Volume already attached to server %s' % server
)

self.result['changed'] = True

if not self.module.check_mode:
data = {
'SUBID': volume['SUBID'],
# This API call expects a param called attach_to_SUBID,
# but all the BlockStorage API response payloads call
# this parameter attached_to_SUBID. So we'll standardize
# to the latter and attached_to_id, but we'll pass the
# expected attach_to_SUBID to this API call.
'attach_to_SUBID': expected_server,
'live': self.get_yes_or_no('live_attachment'),
}
self.api_query(
path='/v1/block/attach',
method='POST',
data=data
)
volume = self.get_block_storage_volumes()
else:
volume['attached_to_SUBID'] = expected_server

self.result['diff']['after'] = volume

return volume

def ensure_volume_size(self, volume, expected_size):
curr_size = volume.get('size_gb')
# When creating, attaching, or detaching a volume in check_mode,
# sadly, size_gb doesn't exist, because those methods return the
# result of get_block_storage_volumes, which is {} on check_mode.
if curr_size is None or curr_size >= expected_size:
# we only resize volumes that are smaller than
# expected. There's no shrinking operation.
return volume

self.result['changed'] = True

volume['size_gb'] = expected_size
self.result['diff']['after'] = volume

if not self.module.check_mode:
data = {'SUBID': volume['SUBID'], 'size_gb': expected_size}
self.api_query(
path='/v1/block/resize',
method='POST',
data=data,
)

return volume


def main():
argument_spec = vultr_argument_spec()
argument_spec.update(dict(
name=dict(type='str', required=True, aliases=['description', 'label']),
size=dict(type='int'),
region=dict(type='str'),
state=dict(type='str', choices=['present', 'absent'], default='present'),
state=dict(
type='str',
choices=['present', 'absent', 'attached', 'detached'],
default='present'
),
attached_to_SUBID=dict(type='int', aliases=['attached_to_id']),
live_attachment=dict(type='bool', default=True)
))

module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
required_if=[['state', 'present', ['size', 'region']]]
required_if=[
['state', 'present', ['size', 'region']],
['state', 'detached', ['size', 'region']],
['state', 'attached', ['size', 'region', 'attached_to_SUBID']],
]
)

vultr_block_storage = AnsibleVultrBlockStorage(module)
if module.params.get('state') == "absent":

desired_state = module.params.get('state')
if desired_state == "absent":
volume = vultr_block_storage.absent_block_storage_volume()
elif desired_state == 'attached':
volume = vultr_block_storage.attached_block_storage_volume()
elif desired_state == 'detached':
volume = vultr_block_storage.detached_block_storage_volume()
else:
volume = vultr_block_storage.present_block_storage_volume()

expected_size = module.params.get('size')
if expected_size and desired_state != 'absent':
volume = vultr_block_storage.ensure_volume_size(
volume,
expected_size
)

result = vultr_block_storage.get_result(volume)
module.exit_json(**result)

Expand Down
1 change: 1 addition & 0 deletions plugins/modules/vultr_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
- Whether to send an activation email when the server is ready or not.
- Only considered on creation.
type: bool
default: false
private_network_enabled:
description:
- Whether to enable private networking or not.
Expand Down
1 change: 1 addition & 0 deletions plugins/modules/vultr_server_baremetal.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Whether to send an activation email when the server is ready or not.
- Only considered on creation.
type: bool
default: false
ipv6_enabled:
description:
- Whether to enable IPv6 or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@
vultr_resource_prefix: "vultr-test-prefix"
vultr_block_storage_name: "{{ vultr_resource_prefix }}-volume"
vultr_block_storage_size: 10
vultr_block_storage_size_2: 12
vultr_block_storage_size_3: 14
vultr_block_storage_region: New Jersey

vultr_server_name: "{{ vultr_resource_prefix }}_vm_for_attachment"
vultr_server_ssh_keys:
- name: key1
key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAgEAyWYItY+3w5b8PdGRoz0oY5mufqydW96naE+VM3JSvJFAUS08rAjQQpQ03ymoALeHQy6JVZbcgecxn6p0pAOINQdqufn4udPtOPCtMjNiPGpkSM9ah/6X5+kvyWMNrvlf+Ld4OOoszP5sAkgQzIbrFQAm41XknBUha0zkewZwfrVhain4pnDjV7wCcChId/Q/Gbi4xMtXkisznWcAJcueBs3EEZDKhJ5q0VeWSJEhYJDLFN1sOxF0AIUnMrOhfKQ/LjgREXPB6uCl899INUTXRNNjRpeMXyJ2wMMmOAbua2qEd1r13Bu1n+6A823Hzb33fyMXuqWnJwBJ4DCvMlGuEsfuOK+xk7DaBfLHbcM6fsPk0/4psTE6YLgC41remr6+u5ZWsY/faMtSnNPie8Z8Ov0DIYGdhbJjUXk1HomxRV9+ZfZ2Ob8iCwlaAQAyEUM6fs3Kxt8pBD8dx1HOkhsfBWPvuDr5y+kqE7H8/MuPDTc0QgH2pjUMpmw/XBwNDHshVEjrZvtICOjOLUJxcowLO1ivNYwPwowQxfisMy56LfYdjsOslBiqsrkAqvNGm1zu8wKHeqVN9w5l3yUELpvubfm9NKIvYcl6yWF36T0c5vE+g0DU/Jy4XpTj0hZG9QV2mRQcLJnd2pxQtJT7cPFtrn/+tgRxzjEtbDXummDV4sE= [email protected]"

vultr_server_plan_1: 1024 MB RAM,25 GB SSD,1.00 TB BW
Loading

0 comments on commit dcb398f

Please sign in to comment.