ansible-collection-hetzner-.../plugins/modules/hcloud_route.py

192 lines
5.2 KiB
Python
Raw Normal View History

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)
DOCUMENTATION = """
2020-03-09 13:36:01 +00:00
---
module: hcloud_route
short_description: Create and delete cloud routes on the Hetzner Cloud.
description:
- Create, update and delete cloud routes on the Hetzner Cloud.
author:
- Lukas Kaemmerling (@lkaemmerling)
options:
network:
description:
- The name of the Hetzner Cloud Network.
type: str
required: true
destination:
description:
- Destination network or host of this route.
type: str
required: true
gateway:
description:
- Gateway for the route.
type: str
required: true
state:
description:
- State of the route.
default: present
choices: [ absent, present ]
type: str
requirements:
- hcloud-python >= 1.3.0
extends_documentation_fragment:
- hetzner.hcloud.hcloud
"""
2020-03-09 13:36:01 +00:00
EXAMPLES = """
- name: Create a basic route
2023-07-20 10:14:57 +00:00
hetzner.hcloud.hcloud_route:
2020-03-09 13:36:01 +00:00
network: my-network
destination: 10.100.1.0/24
gateway: 10.0.1.1
state: present
- name: Ensure the route is absent
2023-07-20 10:14:57 +00:00
hetzner.hcloud.hcloud_route:
2020-03-09 13:36:01 +00:00
network: my-network
destination: 10.100.1.0/24
gateway: 10.0.1.1
state: absent
"""
RETURN = """
hcloud_route:
description: One Route of a Network
returned: always
type: complex
contains:
network:
description: Name of the Network
type: str
returned: always
sample: my-network
destination:
description: Destination network or host of this route
type: str
returned: always
sample: 10.0.0.0/8
gateway:
description: Gateway of the route
type: str
returned: always
sample: 10.0.0.1
"""
from typing import Optional
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException
from ..module_utils.vendor.hcloud.networks import BoundNetwork, NetworkRoute
2020-03-09 13:36:01 +00:00
class AnsibleHCloudRoute(AnsibleHCloud):
represent = "hcloud_route"
hcloud_network: Optional[BoundNetwork] = None
hcloud_route: Optional[NetworkRoute] = None
2020-03-09 13:36:01 +00:00
def _prepare_result(self):
return {
"network": to_native(self.hcloud_network.name),
"destination": to_native(self.hcloud_route.destination),
"gateway": self.hcloud_route.gateway,
}
def _get_network(self):
try:
self.hcloud_network = self.client.networks.get_by_name(self.module.params.get("network"))
self.hcloud_route = None
except HCloudException as exception:
self.fail_json_hcloud(exception)
2020-03-09 13:36:01 +00:00
def _get_route(self):
destination = self.module.params.get("destination")
gateway = self.module.params.get("gateway")
for route in self.hcloud_network.routes:
if route.destination == destination and route.gateway == gateway:
self.hcloud_route = route
def _create_route(self):
route = NetworkRoute(
destination=self.module.params.get("destination"), gateway=self.module.params.get("gateway")
2020-03-09 13:36:01 +00:00
)
if not self.module.check_mode:
try:
self.hcloud_network.add_route(route=route).wait_until_finished()
except HCloudException as exception:
self.fail_json_hcloud(exception)
2020-03-09 13:36:01 +00:00
self._mark_as_changed()
self._get_network()
self._get_route()
def present_route(self):
self._get_network()
self._get_route()
if self.hcloud_route is None:
self._create_route()
def delete_route(self):
self._get_network()
self._get_route()
if self.hcloud_route is not None and self.hcloud_network is not None:
if not self.module.check_mode:
try:
self.hcloud_network.delete_route(self.hcloud_route).wait_until_finished()
except HCloudException as exception:
self.fail_json_hcloud(exception)
2020-03-09 13:36:01 +00:00
self._mark_as_changed()
self.hcloud_route = None
@classmethod
def define_module(cls):
2020-03-09 13:36:01 +00:00
return AnsibleModule(
argument_spec=dict(
network={"type": "str", "required": True},
gateway={"type": "str", "required": True},
2020-04-14 06:34:22 +00:00
destination={"type": "str", "required": True},
2020-03-09 13:36:01 +00:00
state={
"choices": ["absent", "present"],
"default": "present",
},
**super().base_module_arguments()
2020-03-09 13:36:01 +00:00
),
supports_check_mode=True,
)
def main():
module = AnsibleHCloudRoute.define_module()
2020-03-09 13:36:01 +00:00
hcloud = AnsibleHCloudRoute(module)
2020-03-09 13:36:01 +00:00
state = module.params["state"]
if state == "absent":
hcloud.delete_route()
elif state == "present":
hcloud.present_route()
module.exit_json(**hcloud.get_result())
if __name__ == "__main__":
main()