Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Delete Protection to hcloud_volume and hcloud_volume_info #63665

Merged
merged 2 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 70 additions & 45 deletions lib/ansible/modules/cloud/hcloud/hcloud_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
---
module: hcloud_volume

short_description: Create and manage block volumes on the Hetzner Cloud.
short_description: Create and manage block Volume on the Hetzner Cloud.

version_added: "2.8"

description:
- Create, update and attach/detach block volumes on the Hetzner Cloud.
- Create, update and attach/detach block Volume on the Hetzner Cloud.

author:
- Christopher Schmitt (@cschmitt-hcloud)
Expand Down Expand Up @@ -64,80 +64,85 @@
- Server Name the Volume should be assigned to.
- Required if no I(location) is given and Volume does not exists.
type: str
delete_protection:
description:
- Protect the Volume for deletion.
type: bool
version_added: "2.10"
labels:
description:
- User-defined key-value pairs.
type: dict
state:
description:
- State of the volume.
- State of the Volume.
default: present
choices: [absent, present]
type: str
extends_documentation_fragment: hcloud
"""

EXAMPLES = """
- name: Create a volume
- name: Create a Volume
hcloud_volume:
name: my-volume
location: fsn1
size: 100
state: present
- name: Create a volume and format it with ext4
- name: Create a Volume and format it with ext4
hcloud_volume:
name: my-volume
location: fsn
format: ext4
size: 100
state: present
- name: Mount a existing volume and automount
- name: Mount a existing Volume and automount
hcloud_volume:
name: my-volume
server: my-server
automount: yes
state: present
- name: Mount a existing volume and automount
- name: Mount a existing Volume and automount
hcloud_volume:
name: my-volume
server: my-server
automount: yes
state: present
- name: Ensure the volume is absent (remove if needed)
- name: Ensure the Volume is absent (remove if needed)
hcloud_volume:
name: my-volume
state: absent
"""

RETURN = """
hcloud_volume:
description: The block volume
description: The block Volume
returned: Always
type: complex
contains:
id:
description: ID of the volume
description: ID of the Volume
type: int
returned: Always
sample: 12345
name:
description: Name of the volume
description: Name of the Volume
type: str
returned: Always
sample: my-volume
size:
description: Size in GB of the volume
description: Size in GB of the Volume
type: int
returned: Always
sample: 1337
linux_device:
description: Path to the device that contains the volume.
description: Path to the device that contains the Volume.
returned: always
type: str
sample: /dev/disk/by-id/scsi-0HC_Volume_12345
version_added: "2.10"
location:
description: Location name where the volume is located at
description: Location name where the Volume is located at
type: str
returned: Always
sample: "fsn1"
Expand All @@ -149,10 +154,16 @@
key: value
mylabel: 123
server:
description: Server name where the volume is attached to
description: Server name where the Volume is attached to
type: str
returned: Always
sample: "my-server"
delete_protection:
description: True if Volume is protected for deletion
type: bool
returned: always
sample: false
LKaemmerling marked this conversation as resolved.
Show resolved Hide resolved
version_added: "2.10"
"""

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -185,6 +196,7 @@ def _prepare_result(self):
"labels": self.hcloud_volume.labels,
"server": to_native(server_name),
"linux_device": to_native(self.hcloud_volume.linux_device),
"delete_protection": self.hcloud_volume.protection["delete"],
}

def _get_volume(self):
Expand Down Expand Up @@ -227,36 +239,45 @@ def _create_volume(self):
self._get_volume()

def _update_volume(self):
size = self.module.params.get("size")
if size:
if self.hcloud_volume.size < size:
if not self.module.check_mode:
self.hcloud_volume.resize(size).wait_until_finished()
self._mark_as_changed()
elif self.hcloud_volume.size > size:
self.module.warn("Shrinking of volumes is not supported")
try:
size = self.module.params.get("size")
if size:
if self.hcloud_volume.size < size:
if not self.module.check_mode:
self.hcloud_volume.resize(size).wait_until_finished()
self._mark_as_changed()
elif self.hcloud_volume.size > size:
self.module.warn("Shrinking of volumes is not supported")

server_name = self.module.params.get("server")
if server_name:
server = self.client.servers.get_by_name(server_name)
if self.hcloud_volume.server is None or self.hcloud_volume.server.name != server.name:
if not self.module.check_mode:
automount = self.module.params.get("automount", False)
self.hcloud_volume.attach(server, automount=automount).wait_until_finished()
self._mark_as_changed()
else:
if self.hcloud_volume.server is not None:
if not self.module.check_mode:
self.hcloud_volume.detach().wait_until_finished()
self._mark_as_changed()

server_name = self.module.params.get("server")
if server_name:
server = self.client.servers.get_by_name(server_name)
if self.hcloud_volume.server is None or self.hcloud_volume.server.name != server.name:
labels = self.module.params.get("labels")
if labels is not None and labels != self.hcloud_volume.labels:
if not self.module.check_mode:
automount = self.module.params.get("automount", False)
self.hcloud_volume.attach(server, automount=automount).wait_until_finished()
self.hcloud_volume.update(labels=labels)
self._mark_as_changed()
else:
if self.hcloud_volume.server is not None:

delete_protection = self.module.params.get("delete_protection")
if delete_protection is not None and delete_protection != self.hcloud_volume.protection["delete"]:
if not self.module.check_mode:
self.hcloud_volume.detach().wait_until_finished()
self.hcloud_volume.change_protection(delete=delete_protection).wait_until_finished()
self._mark_as_changed()

labels = self.module.params.get("labels")
if labels is not None and labels != self.hcloud_volume.labels:
if not self.module.check_mode:
self.hcloud_volume.update(labels=labels)
self._mark_as_changed()

self._get_volume()
self._get_volume()
except hcloud.APIException as e:
self.module.fail_json(msg=e.message)

def present_volume(self):
self._get_volume()
Expand All @@ -266,12 +287,15 @@ def present_volume(self):
self._update_volume()

def delete_volume(self):
self._get_volume()
if self.hcloud_volume is not None:
if not self.module.check_mode:
self.client.volumes.delete(self.hcloud_volume)
self._mark_as_changed()
self.hcloud_volume = None
try:
self._get_volume()
if self.hcloud_volume is not None:
if not self.module.check_mode:
self.client.volumes.delete(self.hcloud_volume)
self._mark_as_changed()
self.hcloud_volume = None
except hcloud.APIException as e:
self.module.fail_json(msg=e.message)

@staticmethod
def define_module():
Expand All @@ -287,6 +311,7 @@ def define_module():
format={"type": "str",
"choices": ['xfs', 'ext4'],
},
delete_protection={"type": "bool"},
state={
"choices": ["absent", "present"],
"default": "present",
Expand Down
32 changes: 19 additions & 13 deletions lib/ansible/modules/cloud/hcloud/hcloud_volume_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@
---
module: hcloud_volume_info

short_description: Gather infos about your Hetzner Cloud volumes.
short_description: Gather infos about your Hetzner Cloud Volumes.

version_added: "2.8"
description:
- Gather infos about your Hetzner Cloud volumes.
- Gather infos about your Hetzner Cloud Volumes.

author:
- Lukas Kaemmerling (@LKaemmerling)

options:
id:
description:
- The ID of the volume you want to get.
- The ID of the Volume you want to get.
type: int
name:
description:
- The name of the volume you want to get.
- The name of the Volume you want to get.
type: str
label_selector:
description:
- The label selector for the volume you want to get.
- The label selector for the Volume you want to get.
type: str
extends_documentation_fragment: hcloud
"""

EXAMPLES = """
- name: Gather hcloud volume infos
- name: Gather hcloud Volume infos
hcloud_volume_info:
register: output
- name: Print the gathered infos
Expand All @@ -54,41 +54,46 @@

RETURN = """
hcloud_volume_info:
description: The volume infos as list
description: The Volume infos as list
returned: always
type: complex
contains:
id:
description: Numeric identifier of the volume
description: Numeric identifier of the Volume
returned: always
type: int
sample: 1937415
name:
description: Name of the volume
description: Name of the Volume
returned: always
type: str
sample: my-volume
size:
description: Size of the volume
description: Size of the Volume
returned: always
type: str
sample: 10
linux_device:
description: Path to the device that contains the volume.
description: Path to the device that contains the Volume.
returned: always
type: str
sample: /dev/disk/by-id/scsi-0HC_Volume_12345
version_added: "2.10"
location:
description: Name of the location where the volume resides in
description: Name of the location where the Volume resides in
returned: always
type: str
sample: fsn1
server:
description: Name of the server where the volume is attached to
description: Name of the server where the Volume is attached to
returned: always
type: str
sample: my-server
delete_protection:
description: True if the Volume is protected for deletion
returned: always
type: bool
version_added: "2.10"
labels:
description: User-defined labels (key-value pairs)
returned: always
Expand Down Expand Up @@ -126,6 +131,7 @@ def _prepare_result(self):
"labels": volume.labels,
"server": to_native(server_name),
"linux_device": to_native(volume.linux_device),
"delete_protection": volume.protection["delete"],
})

return tmp
Expand Down