| 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: load_balancer_target |
| 12 |
|
| 13 |
short_description: Manage Hetzner Cloud Load Balancer targets |
| 14 |
|
| 15 |
|
| 16 |
description: |
| 17 |
- Create and delete Hetzner Cloud Load Balancer targets |
| 18 |
|
| 19 |
author: |
| 20 |
- Lukas Kaemmerling (@lkaemmerling) |
| 21 |
version_added: 0.1.0 |
| 22 |
options: |
| 23 |
type: |
| 24 |
description: |
| 25 |
- The type of the target. |
| 26 |
type: str |
| 27 |
choices: [ server, label_selector, ip ] |
| 28 |
required: true |
| 29 |
load_balancer: |
| 30 |
description: |
| 31 |
- Name or ID of the Hetzner Cloud Load Balancer. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
server: |
| 35 |
description: |
| 36 |
- Name or ID of the Hetzner Cloud Server. |
| 37 |
- Required if I(type) is server |
| 38 |
type: str |
| 39 |
label_selector: |
| 40 |
description: |
| 41 |
- A Label Selector that will be used to determine the targets dynamically |
| 42 |
- Required if I(type) is label_selector |
| 43 |
type: str |
| 44 |
ip: |
| 45 |
description: |
| 46 |
- An IP from a Hetzner Dedicated Server, needs to belongs to the same user as the project. |
| 47 |
- Required if I(type) is ip |
| 48 |
type: str |
| 49 |
use_private_ip: |
| 50 |
description: |
| 51 |
- Route the traffic over the private IP of the Load Balancer through a Hetzner Cloud Network. |
| 52 |
- Load Balancer needs to be attached to a network. See M(hetzner.hcloud.load_balancer_network) |
| 53 |
type: bool |
| 54 |
default: False |
| 55 |
state: |
| 56 |
description: |
| 57 |
- State of the load_balancer_network. |
| 58 |
default: present |
| 59 |
choices: [ absent, present ] |
| 60 |
type: str |
| 61 |
|
| 62 |
extends_documentation_fragment: |
| 63 |
- hetzner.hcloud.hcloud |
| 64 |
""" |
| 65 |
|
| 66 |
EXAMPLES = """ |
| 67 |
- name: Create a server Load Balancer target |
| 68 |
hetzner.hcloud.load_balancer_target: |
| 69 |
type: server |
| 70 |
load_balancer: my-LoadBalancer |
| 71 |
server: my-server |
| 72 |
state: present |
| 73 |
|
| 74 |
- name: Create a label_selector Load Balancer target |
| 75 |
hetzner.hcloud.load_balancer_target: |
| 76 |
type: label_selector |
| 77 |
load_balancer: my-LoadBalancer |
| 78 |
label_selector: application=backend |
| 79 |
state: present |
| 80 |
|
| 81 |
- name: Create an IP Load Balancer target |
| 82 |
hetzner.hcloud.load_balancer_target: |
| 83 |
type: ip |
| 84 |
load_balancer: my-LoadBalancer |
| 85 |
ip: 127.0.0.1 |
| 86 |
state: present |
| 87 |
|
| 88 |
- name: Ensure the Load Balancer target is absent (remove if needed) |
| 89 |
hetzner.hcloud.load_balancer_target: |
| 90 |
type: server |
| 91 |
load_balancer: my-LoadBalancer |
| 92 |
server: my-server |
| 93 |
state: absent |
| 94 |
""" |
| 95 |
|
| 96 |
RETURN = """ |
| 97 |
hcloud_load_balancer_target: |
| 98 |
description: The relationship between a Load Balancer and a network |
| 99 |
returned: always |
| 100 |
type: dict |
| 101 |
contains: |
| 102 |
type: |
| 103 |
description: Type of the Load Balancer Target |
| 104 |
type: str |
| 105 |
returned: always |
| 106 |
sample: server |
| 107 |
load_balancer: |
| 108 |
description: Name of the Load Balancer |
| 109 |
type: str |
| 110 |
returned: always |
| 111 |
sample: my-LoadBalancer |
| 112 |
server: |
| 113 |
description: Name of the Server |
| 114 |
type: str |
| 115 |
returned: if I(type) is server |
| 116 |
sample: my-server |
| 117 |
label_selector: |
| 118 |
description: Label Selector |
| 119 |
type: str |
| 120 |
returned: if I(type) is label_selector |
| 121 |
sample: application=backend |
| 122 |
ip: |
| 123 |
description: IP of the dedicated server |
| 124 |
type: str |
| 125 |
returned: if I(type) is ip |
| 126 |
sample: 127.0.0.1 |
| 127 |
use_private_ip: |
| 128 |
description: |
| 129 |
- Route the traffic over the private IP of the Load Balancer through a Hetzner Cloud Network. |
| 130 |
- Load Balancer needs to be attached to a network. See M(hetzner.hcloud.load_balancer_network) |
| 131 |
type: bool |
| 132 |
sample: true |
| 133 |
returned: always |
| 134 |
""" |
| 135 |
|
| 136 |
from ansible.module_utils.basic import AnsibleModule |
| 137 |
|
| 138 |
from ..module_utils._base import AnsibleHCloud |
| 139 |
from ..module_utils._vendor.hcloud import APIException, HCloudException |
| 140 |
from ..module_utils._vendor.hcloud.load_balancers import ( |
| 141 |
BoundLoadBalancer, |
| 142 |
LoadBalancerTarget, |
| 143 |
LoadBalancerTargetIP, |
| 144 |
LoadBalancerTargetLabelSelector, |
| 145 |
) |
| 146 |
from ..module_utils._vendor.hcloud.servers import BoundServer |
| 147 |
|
| 148 |
|
| 149 |
class AnsibleHCloudLoadBalancerTarget(AnsibleHCloud): |
| 150 |
represent = "hcloud_load_balancer_target" |
| 151 |
|
| 152 |
hcloud_load_balancer: BoundLoadBalancer | None = None |
| 153 |
hcloud_load_balancer_target: LoadBalancerTarget | None = None |
| 154 |
hcloud_server: BoundServer | None = None |
| 155 |
|
| 156 |
def _prepare_result(self): |
| 157 |
result = { |
| 158 |
"type": self.hcloud_load_balancer_target.type, |
| 159 |
"load_balancer": self.hcloud_load_balancer.name, |
| 160 |
"use_private_ip": self.hcloud_load_balancer_target.use_private_ip, |
| 161 |
} |
| 162 |
|
| 163 |
if self.hcloud_load_balancer_target.type == "server": |
| 164 |
result["server"] = self.hcloud_load_balancer_target.server.name |
| 165 |
elif self.hcloud_load_balancer_target.type == "label_selector": |
| 166 |
result["label_selector"] = self.hcloud_load_balancer_target.label_selector.selector |
| 167 |
elif self.hcloud_load_balancer_target.type == "ip": |
| 168 |
result["ip"] = self.hcloud_load_balancer_target.ip.ip |
| 169 |
return result |
| 170 |
|
| 171 |
def _get_load_balancer_and_target(self): |
| 172 |
try: |
| 173 |
self.hcloud_load_balancer = self._client_get_by_name_or_id( |
| 174 |
"load_balancers", |
| 175 |
self.module.params.get("load_balancer"), |
| 176 |
) |
| 177 |
|
| 178 |
if self.module.params.get("type") == "server": |
| 179 |
self.hcloud_server = self._client_get_by_name_or_id( |
| 180 |
"servers", |
| 181 |
self.module.params.get("server"), |
| 182 |
) |
| 183 |
|
| 184 |
self.hcloud_load_balancer_target = None |
| 185 |
except HCloudException as exception: |
| 186 |
self.fail_json_hcloud(exception) |
| 187 |
|
| 188 |
def _get_load_balancer_target(self): |
| 189 |
for target in self.hcloud_load_balancer.targets: |
| 190 |
if self.module.params.get("type") == "server" and target.type == "server": |
| 191 |
if target.server.id == self.hcloud_server.id: |
| 192 |
self.hcloud_load_balancer_target = target |
| 193 |
elif self.module.params.get("type") == "label_selector" and target.type == "label_selector": |
| 194 |
if target.label_selector.selector == self.module.params.get("label_selector"): |
| 195 |
self.hcloud_load_balancer_target = target |
| 196 |
elif self.module.params.get("type") == "ip" and target.type == "ip": |
| 197 |
if target.ip.ip == self.module.params.get("ip"): |
| 198 |
self.hcloud_load_balancer_target = target |
| 199 |
|
| 200 |
def _create_load_balancer_target(self): |
| 201 |
params = {"target": None} |
| 202 |
|
| 203 |
if self.module.params.get("type") == "server": |
| 204 |
self.module.fail_on_missing_params(required_params=["server"]) |
| 205 |
params["target"] = LoadBalancerTarget( |
| 206 |
type=self.module.params.get("type"), |
| 207 |
server=self.hcloud_server, |
| 208 |
use_private_ip=self.module.params.get("use_private_ip"), |
| 209 |
) |
| 210 |
elif self.module.params.get("type") == "label_selector": |
| 211 |
self.module.fail_on_missing_params(required_params=["label_selector"]) |
| 212 |
params["target"] = LoadBalancerTarget( |
| 213 |
type=self.module.params.get("type"), |
| 214 |
label_selector=LoadBalancerTargetLabelSelector(selector=self.module.params.get("label_selector")), |
| 215 |
use_private_ip=self.module.params.get("use_private_ip"), |
| 216 |
) |
| 217 |
elif self.module.params.get("type") == "ip": |
| 218 |
self.module.fail_on_missing_params(required_params=["ip"]) |
| 219 |
params["target"] = LoadBalancerTarget( |
| 220 |
type=self.module.params.get("type"), |
| 221 |
ip=LoadBalancerTargetIP(ip=self.module.params.get("ip")), |
| 222 |
use_private_ip=False, |
| 223 |
) |
| 224 |
|
| 225 |
if not self.module.check_mode: |
| 226 |
try: |
| 227 |
action = self.hcloud_load_balancer.add_target(**params) |
| 228 |
action.wait_until_finished() |
| 229 |
except APIException as exception: |
| 230 |
if exception.code == "locked" or exception.code == "conflict": |
| 231 |
self._create_load_balancer_target() |
| 232 |
else: |
| 233 |
self.fail_json_hcloud(exception) |
| 234 |
except HCloudException as exception: |
| 235 |
self.fail_json_hcloud(exception) |
| 236 |
|
| 237 |
self._mark_as_changed() |
| 238 |
self._get_load_balancer_and_target() |
| 239 |
self._get_load_balancer_target() |
| 240 |
|
| 241 |
def present_load_balancer_target(self): |
| 242 |
self._get_load_balancer_and_target() |
| 243 |
self._get_load_balancer_target() |
| 244 |
if self.hcloud_load_balancer_target is None: |
| 245 |
self._create_load_balancer_target() |
| 246 |
|
| 247 |
def delete_load_balancer_target(self): |
| 248 |
self._get_load_balancer_and_target() |
| 249 |
self._get_load_balancer_target() |
| 250 |
if self.hcloud_load_balancer_target is not None and self.hcloud_load_balancer is not None: |
| 251 |
if not self.module.check_mode: |
| 252 |
target = None |
| 253 |
if self.module.params.get("type") == "server": |
| 254 |
self.module.fail_on_missing_params(required_params=["server"]) |
| 255 |
target = LoadBalancerTarget(type=self.module.params.get("type"), server=self.hcloud_server) |
| 256 |
elif self.module.params.get("type") == "label_selector": |
| 257 |
self.module.fail_on_missing_params(required_params=["label_selector"]) |
| 258 |
target = LoadBalancerTarget( |
| 259 |
type=self.module.params.get("type"), |
| 260 |
label_selector=LoadBalancerTargetLabelSelector( |
| 261 |
selector=self.module.params.get("label_selector") |
| 262 |
), |
| 263 |
use_private_ip=self.module.params.get("use_private_ip"), |
| 264 |
) |
| 265 |
elif self.module.params.get("type") == "ip": |
| 266 |
self.module.fail_on_missing_params(required_params=["ip"]) |
| 267 |
target = LoadBalancerTarget( |
| 268 |
type=self.module.params.get("type"), |
| 269 |
ip=LoadBalancerTargetIP(ip=self.module.params.get("ip")), |
| 270 |
use_private_ip=False, |
| 271 |
) |
| 272 |
try: |
| 273 |
action = self.hcloud_load_balancer.remove_target(target) |
| 274 |
action.wait_until_finished() |
| 275 |
except HCloudException as exception: |
| 276 |
self.fail_json_hcloud(exception) |
| 277 |
self._mark_as_changed() |
| 278 |
self.hcloud_load_balancer_target = None |
| 279 |
|
| 280 |
@classmethod |
| 281 |
def define_module(cls): |
| 282 |
return AnsibleModule( |
| 283 |
argument_spec=dict( |
| 284 |
type={"type": "str", "required": True, "choices": ["server", "label_selector", "ip"]}, |
| 285 |
load_balancer={"type": "str", "required": True}, |
| 286 |
server={"type": "str"}, |
| 287 |
label_selector={"type": "str"}, |
| 288 |
ip={"type": "str"}, |
| 289 |
use_private_ip={"type": "bool", "default": False}, |
| 290 |
state={ |
| 291 |
"choices": ["absent", "present"], |
| 292 |
"default": "present", |
| 293 |
}, |
| 294 |
**super().base_module_arguments(), |
| 295 |
), |
| 296 |
supports_check_mode=True, |
| 297 |
) |
| 298 |
|
| 299 |
|
| 300 |
def main(): |
| 301 |
module = AnsibleHCloudLoadBalancerTarget.define_module() |
| 302 |
|
| 303 |
hcloud = AnsibleHCloudLoadBalancerTarget(module) |
| 304 |
state = module.params["state"] |
| 305 |
if state == "absent": |
| 306 |
hcloud.delete_load_balancer_target() |
| 307 |
elif state == "present": |
| 308 |
hcloud.present_load_balancer_target() |
| 309 |
|
| 310 |
module.exit_json(**hcloud.get_result()) |
| 311 |
|
| 312 |
|
| 313 |
if __name__ == "__main__": |
| 314 |
main() |