2020-03-09 13:36:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright: (c) 2019, 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-06-27 09:50:13 +00:00
|
|
|
DOCUMENTATION = """
|
2020-03-09 13:36:01 +00:00
|
|
|
---
|
|
|
|
module: hcloud_network
|
|
|
|
|
|
|
|
short_description: Create and manage cloud Networks on the Hetzner Cloud.
|
|
|
|
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Create, update and manage cloud Networks on the Hetzner Cloud.
|
|
|
|
- You need at least hcloud-python 1.3.0.
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@lkaemmerling)
|
|
|
|
|
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- The ID of the Hetzner Cloud Networks to manage.
|
|
|
|
- Only required if no Network I(name) is given.
|
|
|
|
type: int
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The Name of the Hetzner Cloud Network to manage.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Only required if no Network I(id) is given or a Network does not exist.
|
2020-03-09 13:36:01 +00:00
|
|
|
type: str
|
|
|
|
ip_range:
|
|
|
|
description:
|
|
|
|
- IP range of the Network.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Required if Network does not exist.
|
2020-03-09 13:36:01 +00:00
|
|
|
type: str
|
2023-06-22 11:09:10 +00:00
|
|
|
expose_routes_to_vswitch:
|
|
|
|
description:
|
|
|
|
- Indicates if the routes from this network should be exposed to the vSwitch connection.
|
|
|
|
- The exposing only takes effect if a vSwitch connection is active.
|
|
|
|
type: bool
|
2020-03-09 13:36:01 +00:00
|
|
|
labels:
|
|
|
|
description:
|
|
|
|
- User-defined labels (key-value pairs).
|
|
|
|
type: dict
|
|
|
|
delete_protection:
|
|
|
|
description:
|
|
|
|
- Protect the Network for deletion.
|
|
|
|
type: bool
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the Network.
|
|
|
|
default: present
|
|
|
|
choices: [ absent, present ]
|
|
|
|
type: str
|
|
|
|
|
|
|
|
requirements:
|
|
|
|
- hcloud-python >= 1.3.0
|
|
|
|
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- hetzner.hcloud.hcloud
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
"""
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Create a basic network
|
|
|
|
hcloud_network:
|
|
|
|
name: my-network
|
|
|
|
ip_range: 10.0.0.0/8
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Ensure the Network is absent (remove if needed)
|
|
|
|
hcloud_network:
|
|
|
|
name: my-network
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
hcloud_network:
|
|
|
|
description: The Network
|
|
|
|
returned: always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: ID of the Network
|
|
|
|
type: int
|
|
|
|
returned: always
|
|
|
|
sample: 12345
|
|
|
|
name:
|
|
|
|
description: Name of the Network
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: my-volume
|
|
|
|
ip_range:
|
|
|
|
description: IP range of the Network
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: 10.0.0.0/8
|
2023-06-22 11:09:10 +00:00
|
|
|
expose_routes_to_vswitch:
|
|
|
|
description: Indicates if the routes from this network should be exposed to the vSwitch connection.
|
|
|
|
type: bool
|
|
|
|
returned: always
|
|
|
|
sample: false
|
2020-03-09 13:36:01 +00:00
|
|
|
delete_protection:
|
|
|
|
description: True if Network is protected for deletion
|
|
|
|
type: bool
|
|
|
|
returned: always
|
|
|
|
sample: false
|
2020-06-29 13:41:30 +00:00
|
|
|
version_added: "0.1.0"
|
2020-03-09 13:36:01 +00:00
|
|
|
labels:
|
|
|
|
description: User-defined labels (key-value pairs)
|
|
|
|
type: dict
|
|
|
|
returned: always
|
|
|
|
sample:
|
|
|
|
key: value
|
|
|
|
mylabel: 123
|
|
|
|
"""
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2023-06-27 11:17:21 +00:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 13:36:01 +00:00
|
|
|
from ansible_collections.hetzner.hcloud.plugins.module_utils.hcloud import Hcloud
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleHcloudNetwork(Hcloud):
|
|
|
|
def __init__(self, module):
|
2020-11-27 11:25:06 +00:00
|
|
|
Hcloud.__init__(self, module, "hcloud_network")
|
2020-03-09 13:36:01 +00:00
|
|
|
self.hcloud_network = None
|
|
|
|
|
|
|
|
def _prepare_result(self):
|
|
|
|
return {
|
|
|
|
"id": to_native(self.hcloud_network.id),
|
|
|
|
"name": to_native(self.hcloud_network.name),
|
|
|
|
"ip_range": to_native(self.hcloud_network.ip_range),
|
2023-06-22 11:09:10 +00:00
|
|
|
"expose_routes_to_vswitch": self.hcloud_network.expose_routes_to_vswitch,
|
2020-03-09 13:36:01 +00:00
|
|
|
"delete_protection": self.hcloud_network.protection["delete"],
|
|
|
|
"labels": self.hcloud_network.labels,
|
|
|
|
}
|
|
|
|
|
|
|
|
def _get_network(self):
|
|
|
|
try:
|
|
|
|
if self.module.params.get("id") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_network = self.client.networks.get_by_id(self.module.params.get("id"))
|
2020-03-09 13:36:01 +00:00
|
|
|
else:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_network = self.client.networks.get_by_name(self.module.params.get("name"))
|
2021-02-24 12:37:58 +00:00
|
|
|
except Exception as e:
|
2020-03-09 13:36:01 +00:00
|
|
|
self.module.fail_json(msg=e.message)
|
|
|
|
|
|
|
|
def _create_network(self):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["name", "ip_range"])
|
2020-03-09 13:36:01 +00:00
|
|
|
params = {
|
|
|
|
"name": self.module.params.get("name"),
|
|
|
|
"ip_range": self.module.params.get("ip_range"),
|
|
|
|
"labels": self.module.params.get("labels"),
|
|
|
|
}
|
2023-06-22 11:09:10 +00:00
|
|
|
|
|
|
|
expose_routes_to_vswitch = self.module.params.get("expose_routes_to_vswitch")
|
|
|
|
if expose_routes_to_vswitch is not None:
|
|
|
|
params["expose_routes_to_vswitch"] = expose_routes_to_vswitch
|
|
|
|
|
2021-02-24 12:37:58 +00:00
|
|
|
try:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.client.networks.create(**params)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2021-02-24 12:37:58 +00:00
|
|
|
delete_protection = self.module.params.get("delete_protection")
|
|
|
|
if delete_protection is not None:
|
|
|
|
self._get_network()
|
|
|
|
self.hcloud_network.change_protection(delete=delete_protection).wait_until_finished()
|
|
|
|
except Exception as e:
|
|
|
|
self.module.fail_json(msg=e.message)
|
2020-03-09 13:36:01 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_network()
|
|
|
|
|
|
|
|
def _update_network(self):
|
|
|
|
try:
|
|
|
|
labels = self.module.params.get("labels")
|
|
|
|
if labels is not None and labels != self.hcloud_network.labels:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_network.update(labels=labels)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
ip_range = self.module.params.get("ip_range")
|
|
|
|
if ip_range is not None and ip_range != self.hcloud_network.ip_range:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_network.change_ip_range(ip_range=ip_range).wait_until_finished()
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
2023-06-22 11:09:10 +00:00
|
|
|
expose_routes_to_vswitch = self.module.params.get("expose_routes_to_vswitch")
|
2023-06-27 09:50:13 +00:00
|
|
|
if (
|
|
|
|
expose_routes_to_vswitch is not None
|
|
|
|
and expose_routes_to_vswitch != self.hcloud_network.expose_routes_to_vswitch
|
|
|
|
):
|
2023-06-22 11:09:10 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_network.update(expose_routes_to_vswitch=expose_routes_to_vswitch)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
delete_protection = self.module.params.get("delete_protection")
|
|
|
|
if delete_protection is not None and delete_protection != self.hcloud_network.protection["delete"]:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_network.change_protection(delete=delete_protection).wait_until_finished()
|
|
|
|
self._mark_as_changed()
|
2021-02-24 12:37:58 +00:00
|
|
|
except Exception as e:
|
2020-03-09 13:36:01 +00:00
|
|
|
self.module.fail_json(msg=e.message)
|
|
|
|
self._get_network()
|
|
|
|
|
|
|
|
def present_network(self):
|
|
|
|
self._get_network()
|
|
|
|
if self.hcloud_network is None:
|
|
|
|
self._create_network()
|
|
|
|
else:
|
|
|
|
self._update_network()
|
|
|
|
|
|
|
|
def delete_network(self):
|
|
|
|
try:
|
|
|
|
self._get_network()
|
|
|
|
if self.hcloud_network is not None:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.client.networks.delete(self.hcloud_network)
|
|
|
|
self._mark_as_changed()
|
2021-02-24 12:37:58 +00:00
|
|
|
except Exception as e:
|
2020-03-09 13:36:01 +00:00
|
|
|
self.module.fail_json(msg=e.message)
|
|
|
|
self.hcloud_network = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def define_module():
|
|
|
|
return AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id={"type": "int"},
|
|
|
|
name={"type": "str"},
|
|
|
|
ip_range={"type": "str"},
|
2023-06-22 11:09:10 +00:00
|
|
|
expose_routes_to_vswitch={"type": "bool"},
|
2020-03-09 13:36:01 +00:00
|
|
|
labels={"type": "dict"},
|
|
|
|
delete_protection={"type": "bool"},
|
|
|
|
state={
|
|
|
|
"choices": ["absent", "present"],
|
|
|
|
"default": "present",
|
|
|
|
},
|
|
|
|
**Hcloud.base_module_arguments()
|
|
|
|
),
|
2023-06-27 09:50:13 +00:00
|
|
|
required_one_of=[["id", "name"]],
|
2020-03-09 13:36:01 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleHcloudNetwork.define_module()
|
|
|
|
|
|
|
|
hcloud = AnsibleHcloudNetwork(module)
|
|
|
|
state = module.params["state"]
|
|
|
|
if state == "absent":
|
|
|
|
hcloud.delete_network()
|
|
|
|
elif state == "present":
|
|
|
|
hcloud.present_network()
|
|
|
|
|
|
|
|
module.exit_json(**hcloud.get_result())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|