2021-08-16 10:31:02 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright: (c) 2020, Hetzner Cloud GmbH <info@hetzner-cloud.de>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
|
2023-11-23 13:53:10 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-08-16 10:31:02 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
2023-11-20 12:21:23 +00:00
|
|
|
module: placement_group
|
2021-08-16 10:31:02 +00:00
|
|
|
|
|
|
|
short_description: Create and manage placement groups on the Hetzner Cloud.
|
|
|
|
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Create, update and manage placement groups on the Hetzner Cloud.
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Adrian Huber (@Adi146)
|
|
|
|
|
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- The ID of the Hetzner Cloud placement group to manage.
|
|
|
|
- Only required if no placement group I(name) is given
|
|
|
|
type: int
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The Name of the Hetzner Cloud placement group to manage.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Only required if no placement group I(id) is given, or a placement group does not exist.
|
2021-08-16 10:31:02 +00:00
|
|
|
type: str
|
|
|
|
labels:
|
|
|
|
description:
|
|
|
|
- User-defined labels (key-value pairs)
|
|
|
|
type: dict
|
|
|
|
type:
|
|
|
|
description:
|
|
|
|
- The Type of the Hetzner Cloud placement group.
|
|
|
|
type: str
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the placement group.
|
|
|
|
default: present
|
|
|
|
choices: [ absent, present ]
|
|
|
|
type: str
|
|
|
|
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- hetzner.hcloud.hcloud
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Create a basic placement group
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.placement_group:
|
2021-08-16 10:31:02 +00:00
|
|
|
name: my-placement-group
|
|
|
|
state: present
|
|
|
|
type: spread
|
|
|
|
|
|
|
|
- name: Create a placement group with labels
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.placement_group:
|
2021-08-16 10:31:02 +00:00
|
|
|
name: my-placement-group
|
|
|
|
type: spread
|
|
|
|
labels:
|
2023-10-19 10:41:44 +00:00
|
|
|
key: value
|
|
|
|
mylabel: 123
|
2021-08-16 10:31:02 +00:00
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Ensure the placement group is absent (remove if needed)
|
2023-11-21 08:40:11 +00:00
|
|
|
hetzner.hcloud.placement_group:
|
2021-08-16 10:31:02 +00:00
|
|
|
name: my-placement-group
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
hcloud_placement_group:
|
|
|
|
description: The placement group instance
|
|
|
|
returned: Always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: Numeric identifier of the placement group
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
sample: 1937415
|
|
|
|
name:
|
|
|
|
description: Name of the placement group
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: my placement group
|
|
|
|
labels:
|
|
|
|
description: User-defined labels (key-value pairs)
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
type:
|
|
|
|
description: Type of the placement group
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: spread
|
|
|
|
servers:
|
|
|
|
description: Server IDs of the placement group
|
|
|
|
returned: always
|
|
|
|
type: list
|
|
|
|
elements: int
|
|
|
|
sample:
|
|
|
|
- 4711
|
|
|
|
- 4712
|
|
|
|
"""
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2023-07-31 08:12:55 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
from ..module_utils.hcloud import AnsibleHCloud
|
2023-07-31 08:12:55 +00:00
|
|
|
from ..module_utils.vendor.hcloud import HCloudException
|
2023-09-26 07:41:01 +00:00
|
|
|
from ..module_utils.vendor.hcloud.placement_groups import BoundPlacementGroup
|
2021-08-16 10:31:02 +00:00
|
|
|
|
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
class AnsibleHCloudPlacementGroup(AnsibleHCloud):
|
2023-09-26 07:41:01 +00:00
|
|
|
represent = "hcloud_placement_group"
|
|
|
|
|
2023-11-23 13:53:10 +00:00
|
|
|
hcloud_placement_group: BoundPlacementGroup | None = None
|
2021-08-16 10:31:02 +00:00
|
|
|
|
|
|
|
def _prepare_result(self):
|
|
|
|
return {
|
2024-03-27 13:11:30 +00:00
|
|
|
"id": str(self.hcloud_placement_group.id),
|
|
|
|
"name": self.hcloud_placement_group.name,
|
2021-08-16 10:31:02 +00:00
|
|
|
"labels": self.hcloud_placement_group.labels,
|
2024-03-27 13:11:30 +00:00
|
|
|
"type": self.hcloud_placement_group.type,
|
2021-08-16 10:31:02 +00:00
|
|
|
"servers": self.hcloud_placement_group.servers,
|
|
|
|
}
|
|
|
|
|
|
|
|
def _get_placement_group(self):
|
|
|
|
try:
|
|
|
|
if self.module.params.get("id") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_placement_group = self.client.placement_groups.get_by_id(self.module.params.get("id"))
|
2021-08-16 10:31:02 +00:00
|
|
|
elif self.module.params.get("name") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_placement_group = self.client.placement_groups.get_by_name(self.module.params.get("name"))
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception)
|
2021-08-16 10:31:02 +00:00
|
|
|
|
|
|
|
def _create_placement_group(self):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["name"])
|
2021-08-16 10:31:02 +00:00
|
|
|
params = {
|
|
|
|
"name": self.module.params.get("name"),
|
|
|
|
"type": self.module.params.get("type"),
|
|
|
|
"labels": self.module.params.get("labels"),
|
|
|
|
}
|
|
|
|
if not self.module.check_mode:
|
|
|
|
try:
|
|
|
|
self.client.placement_groups.create(**params)
|
2023-08-25 14:19:15 +00:00
|
|
|
except HCloudException as exception:
|
|
|
|
self.fail_json_hcloud(exception, params=params)
|
2021-08-16 10:31:02 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_placement_group()
|
|
|
|
|
|
|
|
def _update_placement_group(self):
|
|
|
|
name = self.module.params.get("name")
|
|
|
|
if name is not None and self.hcloud_placement_group.name != name:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["id"])
|
2021-08-16 10:31:02 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_placement_group.update(name=name)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
labels = self.module.params.get("labels")
|
|
|
|
if labels is not None and self.hcloud_placement_group.labels != labels:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_placement_group.update(labels=labels)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
self._get_placement_group()
|
|
|
|
|
|
|
|
def present_placement_group(self):
|
|
|
|
self._get_placement_group()
|
|
|
|
if self.hcloud_placement_group is None:
|
|
|
|
self._create_placement_group()
|
|
|
|
else:
|
|
|
|
self._update_placement_group()
|
|
|
|
|
|
|
|
def delete_placement_group(self):
|
|
|
|
self._get_placement_group()
|
|
|
|
if self.hcloud_placement_group is not None:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.client.placement_groups.delete(self.hcloud_placement_group)
|
|
|
|
self._mark_as_changed()
|
|
|
|
self.hcloud_placement_group = None
|
|
|
|
|
2023-08-02 10:05:00 +00:00
|
|
|
@classmethod
|
|
|
|
def define_module(cls):
|
2021-08-16 10:31:02 +00:00
|
|
|
return AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id={"type": "int"},
|
|
|
|
name={"type": "str"},
|
|
|
|
labels={"type": "dict"},
|
|
|
|
type={"type": "str"},
|
|
|
|
state={
|
|
|
|
"choices": ["absent", "present"],
|
|
|
|
"default": "present",
|
|
|
|
},
|
2023-11-23 13:53:10 +00:00
|
|
|
**super().base_module_arguments(),
|
2021-08-16 10:31:02 +00:00
|
|
|
),
|
2023-06-27 09:50:13 +00:00
|
|
|
required_one_of=[["id", "name"]],
|
|
|
|
required_if=[["state", "present", ["name"]]],
|
2021-08-16 10:31:02 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-08-04 07:24:14 +00:00
|
|
|
module = AnsibleHCloudPlacementGroup.define_module()
|
2021-08-16 10:31:02 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
hcloud = AnsibleHCloudPlacementGroup(module)
|
2021-08-16 10:31:02 +00:00
|
|
|
state = module.params.get("state")
|
|
|
|
if state == "absent":
|
|
|
|
hcloud.delete_placement_group()
|
|
|
|
elif state == "present":
|
|
|
|
hcloud.present_placement_group()
|
|
|
|
|
|
|
|
module.exit_json(**hcloud.get_result())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|