| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2020, 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: load_balancer_network |
| 12 |
|
| 13 |
short_description: Manage the relationship between Hetzner Cloud Networks and Load Balancers |
| 14 |
|
| 15 |
|
| 16 |
description: |
| 17 |
- Create and delete the relationship Hetzner Cloud Networks and Load Balancers |
| 18 |
|
| 19 |
author: |
| 20 |
- Lukas Kaemmerling (@lkaemmerling) |
| 21 |
version_added: 0.1.0 |
| 22 |
options: |
| 23 |
network: |
| 24 |
description: |
| 25 |
- Name or ID of the Hetzner Cloud Networks. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
load_balancer: |
| 29 |
description: |
| 30 |
- Name or ID of the Hetzner Cloud Load Balancer. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
ip_range: |
| 34 |
description: |
| 35 |
- IP range in CIDR block notation of the subnet to attach to. |
| 36 |
type: str |
| 37 |
ip: |
| 38 |
description: |
| 39 |
- The IP the Load Balancer should have. |
| 40 |
type: str |
| 41 |
state: |
| 42 |
description: |
| 43 |
- State of the load_balancer_network. |
| 44 |
default: present |
| 45 |
choices: [ absent, present ] |
| 46 |
type: str |
| 47 |
|
| 48 |
extends_documentation_fragment: |
| 49 |
- hetzner.hcloud.hcloud |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = """ |
| 53 |
- name: Create a basic Load Balancer network |
| 54 |
hetzner.hcloud.load_balancer_network: |
| 55 |
load_balancer: my-LoadBalancer |
| 56 |
network: my-network |
| 57 |
state: present |
| 58 |
|
| 59 |
- name: Create a Load Balancer network and specify the subnet |
| 60 |
hetzner.hcloud.load_balancer_network: |
| 61 |
load_balancer: my-LoadBalancer |
| 62 |
network: my-network |
| 63 |
ip_range: 10.1.0.0/24 |
| 64 |
state: present |
| 65 |
|
| 66 |
- name: Create a Load Balancer network and specify the ip address |
| 67 |
hetzner.hcloud.load_balancer_network: |
| 68 |
load_balancer: my-LoadBalancer |
| 69 |
network: my-network |
| 70 |
ip: 10.0.0.1 |
| 71 |
state: present |
| 72 |
|
| 73 |
- name: Ensure the Load Balancer network is absent (remove if needed) |
| 74 |
hetzner.hcloud.load_balancer_network: |
| 75 |
load_balancer: my-LoadBalancer |
| 76 |
network: my-network |
| 77 |
state: absent |
| 78 |
""" |
| 79 |
|
| 80 |
RETURN = """ |
| 81 |
hcloud_load_balancer_network: |
| 82 |
description: The relationship between a Load Balancer and a network |
| 83 |
returned: always |
| 84 |
type: dict |
| 85 |
contains: |
| 86 |
network: |
| 87 |
description: Name of the Network |
| 88 |
type: str |
| 89 |
returned: always |
| 90 |
sample: my-network |
| 91 |
load_balancer: |
| 92 |
description: Name of the Load Balancer |
| 93 |
type: str |
| 94 |
returned: always |
| 95 |
sample: my-LoadBalancer |
| 96 |
ip: |
| 97 |
description: IP of the Load Balancer within the Network ip range |
| 98 |
type: str |
| 99 |
returned: always |
| 100 |
sample: 10.0.0.8 |
| 101 |
""" |
| 102 |
|
| 103 |
from ipaddress import ip_address, ip_network |
| 104 |
from time import sleep |
| 105 |
|
| 106 |
from ansible.module_utils.basic import AnsibleModule |
| 107 |
|
| 108 |
from ..module_utils._base import AnsibleHCloud |
| 109 |
from ..module_utils._vendor.hcloud import HCloudException |
| 110 |
from ..module_utils._vendor.hcloud.load_balancers import BoundLoadBalancer, PrivateNet |
| 111 |
from ..module_utils._vendor.hcloud.networks import BoundNetwork |
| 112 |
|
| 113 |
|
| 114 |
class AnsibleHCloudLoadBalancerNetwork(AnsibleHCloud): |
| 115 |
represent = "hcloud_load_balancer_network" |
| 116 |
|
| 117 |
hcloud_network: BoundNetwork | None = None |
| 118 |
hcloud_load_balancer: BoundLoadBalancer | None = None |
| 119 |
hcloud_load_balancer_network: PrivateNet | None = None |
| 120 |
|
| 121 |
def _prepare_result(self): |
| 122 |
return { |
| 123 |
"network": self.hcloud_network.name, |
| 124 |
"load_balancer": self.hcloud_load_balancer.name, |
| 125 |
"ip": self.hcloud_load_balancer_network.ip, |
| 126 |
} |
| 127 |
|
| 128 |
def _get_load_balancer_and_network(self): |
| 129 |
try: |
| 130 |
self.hcloud_load_balancer_network = None |
| 131 |
|
| 132 |
self.hcloud_network = self._client_get_by_name_or_id( |
| 133 |
"networks", |
| 134 |
self.module.params.get("network"), |
| 135 |
) |
| 136 |
self.hcloud_load_balancer = self._client_get_by_name_or_id( |
| 137 |
"load_balancers", |
| 138 |
self.module.params.get("load_balancer"), |
| 139 |
) |
| 140 |
self.hcloud_load_balancer_network = self.hcloud_load_balancer.private_net_for(self.hcloud_network) |
| 141 |
except HCloudException as exception: |
| 142 |
self.fail_json_hcloud(exception) |
| 143 |
|
| 144 |
def _attach(self): |
| 145 |
params = { |
| 146 |
"network": self.hcloud_network, |
| 147 |
} |
| 148 |
|
| 149 |
if self.module.params.get("ip_range") is not None: |
| 150 |
params["ip_range"] = self.module.params.get("ip_range") |
| 151 |
if self.module.params.get("ip") is not None: |
| 152 |
params["ip"] = self.module.params.get("ip") |
| 153 |
|
| 154 |
if not self.module.check_mode: |
| 155 |
try: |
| 156 |
action = self.hcloud_load_balancer.attach_to_network(**params) |
| 157 |
action.wait_until_finished() |
| 158 |
|
| 159 |
# Workaround to handle flakiness from the API |
| 160 |
self._wait_for_attachment(True) |
| 161 |
except HCloudException as exception: |
| 162 |
self.fail_json_hcloud(exception) |
| 163 |
|
| 164 |
self._mark_as_changed() |
| 165 |
|
| 166 |
def _detach(self): |
| 167 |
if not self.module.check_mode: |
| 168 |
try: |
| 169 |
action = self.hcloud_load_balancer.detach_from_network(self.hcloud_load_balancer_network.network) |
| 170 |
action.wait_until_finished() |
| 171 |
|
| 172 |
# Workaround to handle flakiness from the API |
| 173 |
self._wait_for_attachment(False) |
| 174 |
except HCloudException as exception: |
| 175 |
self.fail_json_hcloud(exception) |
| 176 |
|
| 177 |
self._mark_as_changed() |
| 178 |
|
| 179 |
def _create_load_balancer_network(self): |
| 180 |
self._attach() |
| 181 |
|
| 182 |
self._get_load_balancer_and_network() |
| 183 |
|
| 184 |
def _update_load_balancer_network(self): |
| 185 |
ip_range = self.module.params.get("ip_range") |
| 186 |
if ip_range is not None: |
| 187 |
ip_range_network = ip_network(ip_range) |
| 188 |
if ip_range_network not in [ip_network(o.ip_range) for o in self.hcloud_network.subnets]: |
| 189 |
# Validate before "detach" instead of relying on the "attach" API |
| 190 |
# validation, leaving the resource in a half applied state. |
| 191 |
self.module.fail_json(msg=f"ip_range '{ip_range}' was not found in the network subnets") |
| 192 |
|
| 193 |
if ip_address(self.hcloud_load_balancer_network.ip) not in ip_range_network: |
| 194 |
self._detach() |
| 195 |
self._attach() |
| 196 |
|
| 197 |
# No further updates needed, exit |
| 198 |
self._get_load_balancer_and_network() |
| 199 |
return |
| 200 |
|
| 201 |
def present_load_balancer_network(self): |
| 202 |
self._get_load_balancer_and_network() |
| 203 |
if self.hcloud_load_balancer_network is None: |
| 204 |
self._create_load_balancer_network() |
| 205 |
else: |
| 206 |
self._update_load_balancer_network() |
| 207 |
|
| 208 |
def delete_load_balancer_network(self): |
| 209 |
self._get_load_balancer_and_network() |
| 210 |
if self.hcloud_load_balancer_network is not None and self.hcloud_load_balancer is not None: |
| 211 |
self._detach() |
| 212 |
self.hcloud_load_balancer_network = None |
| 213 |
|
| 214 |
# Workaround to handle flakiness from the API |
| 215 |
def _wait_for_attachment(self, present: bool): |
| 216 |
def done(x: PrivateNet | None): |
| 217 |
if present: |
| 218 |
return x is not None |
| 219 |
return x is None |
| 220 |
|
| 221 |
# pylint: disable=disallowed-name |
| 222 |
for _ in range(10): |
| 223 |
self.hcloud_load_balancer.reload() |
| 224 |
self.hcloud_load_balancer_network = self.hcloud_load_balancer.private_net_for(self.hcloud_network) |
| 225 |
|
| 226 |
if done(self.hcloud_load_balancer_network): |
| 227 |
break |
| 228 |
|
| 229 |
sleep(2) |
| 230 |
|
| 231 |
@classmethod |
| 232 |
def define_module(cls): |
| 233 |
return AnsibleModule( |
| 234 |
argument_spec=dict( |
| 235 |
load_balancer={"type": "str", "required": True}, |
| 236 |
network={"type": "str", "required": True}, |
| 237 |
ip_range={"type": "str"}, |
| 238 |
ip={"type": "str"}, |
| 239 |
state={ |
| 240 |
"choices": ["absent", "present"], |
| 241 |
"default": "present", |
| 242 |
}, |
| 243 |
**super().base_module_arguments(), |
| 244 |
), |
| 245 |
supports_check_mode=True, |
| 246 |
) |
| 247 |
|
| 248 |
|
| 249 |
def main(): |
| 250 |
module = AnsibleHCloudLoadBalancerNetwork.define_module() |
| 251 |
|
| 252 |
hcloud = AnsibleHCloudLoadBalancerNetwork(module) |
| 253 |
state = module.params["state"] |
| 254 |
if state == "absent": |
| 255 |
hcloud.delete_load_balancer_network() |
| 256 |
elif state == "present": |
| 257 |
hcloud.present_load_balancer_network() |
| 258 |
|
| 259 |
module.exit_json(**hcloud.get_result()) |
| 260 |
|
| 261 |
|
| 262 |
if __name__ == "__main__": |
| 263 |
main() |