Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/modules/route.py

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2019, Hetzner Cloud GmbH <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6
7 from __future__ import annotations
8
9 DOCUMENTATION = """
10 ---
11 module: route
12
13 short_description: Create and delete cloud routes on the Hetzner Cloud.
14
15
16 description:
17 - Create, update and delete cloud routes on the Hetzner Cloud.
18
19 author:
20 - Lukas Kaemmerling (@lkaemmerling)
21
22 options:
23 network:
24 description:
25 - Name or ID of the Hetzner Cloud Network.
26 type: str
27 required: true
28 destination:
29 description:
30 - Destination network or host of this route.
31 type: str
32 required: true
33 gateway:
34 description:
35 - Gateway for the route.
36 type: str
37 required: true
38 state:
39 description:
40 - State of the route.
41 default: present
42 choices: [ absent, present ]
43 type: str
44
45 extends_documentation_fragment:
46 - hetzner.hcloud.hcloud
47 """
48
49 EXAMPLES = """
50 - name: Create a basic route
51 hetzner.hcloud.route:
52 network: my-network
53 destination: 10.100.1.0/24
54 gateway: 10.0.1.1
55 state: present
56
57 - name: Ensure the route is absent
58 hetzner.hcloud.route:
59 network: my-network
60 destination: 10.100.1.0/24
61 gateway: 10.0.1.1
62 state: absent
63 """
64
65 RETURN = """
66 hcloud_route:
67 description: One Route of a Network
68 returned: always
69 type: dict
70 contains:
71 network:
72 description: Name of the Network
73 type: str
74 returned: always
75 sample: my-network
76 destination:
77 description: Destination network or host of this route
78 type: str
79 returned: always
80 sample: 10.0.0.0/8
81 gateway:
82 description: Gateway of the route
83 type: str
84 returned: always
85 sample: 10.0.0.1
86 """
87
88 from ansible.module_utils.basic import AnsibleModule
89
90 from ..module_utils._base import AnsibleHCloud
91 from ..module_utils._vendor.hcloud import HCloudException
92 from ..module_utils._vendor.hcloud.networks import BoundNetwork, NetworkRoute
93
94
95 class AnsibleHCloudRoute(AnsibleHCloud):
96 represent = "hcloud_route"
97
98 hcloud_network: BoundNetwork | None = None
99 hcloud_route: NetworkRoute | None = None
100
101 def _prepare_result(self):
102 return {
103 "network": self.hcloud_network.name,
104 "destination": self.hcloud_route.destination,
105 "gateway": self.hcloud_route.gateway,
106 }
107
108 def _get_network(self):
109 try:
110 self.hcloud_network = self._client_get_by_name_or_id(
111 "networks",
112 self.module.params.get("network"),
113 )
114 self.hcloud_route = None
115 except HCloudException as exception:
116 self.fail_json_hcloud(exception)
117
118 def _get_route(self):
119 destination = self.module.params.get("destination")
120 gateway = self.module.params.get("gateway")
121 for route in self.hcloud_network.routes:
122 if route.destination == destination and route.gateway == gateway:
123 self.hcloud_route = route
124
125 def _create_route(self):
126 route = NetworkRoute(
127 destination=self.module.params.get("destination"), gateway=self.module.params.get("gateway")
128 )
129
130 if not self.module.check_mode:
131 try:
132 action = self.hcloud_network.add_route(route=route)
133 action.wait_until_finished()
134 except HCloudException as exception:
135 self.fail_json_hcloud(exception)
136
137 self._mark_as_changed()
138 self._get_network()
139 self._get_route()
140
141 def present_route(self):
142 self._get_network()
143 self._get_route()
144 if self.hcloud_route is None:
145 self._create_route()
146
147 def delete_route(self):
148 self._get_network()
149 self._get_route()
150 if self.hcloud_route is not None and self.hcloud_network is not None:
151 if not self.module.check_mode:
152 try:
153 action = self.hcloud_network.delete_route(self.hcloud_route)
154 action.wait_until_finished()
155 except HCloudException as exception:
156 self.fail_json_hcloud(exception)
157 self._mark_as_changed()
158 self.hcloud_route = None
159
160 @classmethod
161 def define_module(cls):
162 return AnsibleModule(
163 argument_spec=dict(
164 network={"type": "str", "required": True},
165 gateway={"type": "str", "required": True},
166 destination={"type": "str", "required": True},
167 state={
168 "choices": ["absent", "present"],
169 "default": "present",
170 },
171 **super().base_module_arguments(),
172 ),
173 supports_check_mode=True,
174 )
175
176
177 def main():
178 module = AnsibleHCloudRoute.define_module()
179
180 hcloud = AnsibleHCloudRoute(module)
181 state = module.params["state"]
182 if state == "absent":
183 hcloud.delete_route()
184 elif state == "present":
185 hcloud.present_route()
186
187 module.exit_json(**hcloud.get_result())
188
189
190 if __name__ == "__main__":
191 main()