def _create_load_balancer_service(self): self.module.fail_on_missing_params( required_params=["protocol"] ) if self.module.params.get("protocol") == "tcp": self.module.fail_on_missing_params( required_params=["destination_port"] ) params = { "protocol": self.module.params.get("protocol"), "listen_port": self.module.params.get("listen_port"), "proxyprotocol": self.module.params.get("proxyprotocol") } if self.module.params.get("destination_port"): params["destination_port"] = self.module.params.get("destination_port") if self.module.params.get("http"): params["http"] = self.__get_service_http(http_arg=self.module.params.get("http")) if self.module.params.get("health_check"): params["health_check"] = self.__get_service_health_checks( health_check=self.module.params.get("health_check")) if not self.module.check_mode: try: self.hcloud_load_balancer.add_service(LoadBalancerService(**params)).wait_until_finished( max_retries=1000) except Exception as e: self.module.fail_json(msg=e.message) self._mark_as_changed() self._get_load_balancer() self._get_load_balancer_service()
def test_update_service(self, hetzner_client, response_update_service, bound_load_balancer): hetzner_client.request.return_value = response_update_service new_health_check = LoadBalancerHealthCheck(protocol='http', port=13, interval=1, timeout=1, retries=1) service = LoadBalancerService(listen_port=12, health_check=new_health_check) action = bound_load_balancer.update_service(service) hetzner_client.request.assert_called_with( json={ 'listen_port': 12, 'health_check': { 'protocol': 'http', 'port': 13, 'interval': 1, 'timeout': 1, 'retries': 1 } }, url="/load_balancers/14/actions/update_service", method="POST") assert action.id == 13 assert action.progress == 100 assert action.command == "update_service"
def test_delete_service(self, hetzner_client, load_balancer): action = hetzner_client.load_balancers.delete_service(load_balancer, LoadBalancerService(protocol="http", listen_port=123, destination_port=124, proxyprotocol=False)) assert action.id == 13 assert action.command == "delete_service"
def test_update_service(self, hetzner_client, load_balancer): action = hetzner_client.load_balancers.update_service(load_balancer, LoadBalancerService(protocol="http", listen_port=123, destination_port=124, proxyprotocol=False, health_check=LoadBalancerHealthCheck(protocol='http', port=123, interval=1, timeout=1, retries=1))) assert action.id == 13 assert action.command == "update_service"
def test_delete_service(self, hetzner_client, response_delete_service, bound_load_balancer): hetzner_client.request.return_value = response_delete_service service = LoadBalancerService(listen_port=12) action = bound_load_balancer.delete_service(service) hetzner_client.request.assert_called_with( json={'listen_port': 12}, url="/load_balancers/14/actions/delete_service", method="POST") assert action.id == 13 assert action.progress == 100 assert action.command == "delete_service"
def test_add_service(self, hetzner_client, response_add_service, bound_load_balancer): hetzner_client.request.return_value = response_add_service service = LoadBalancerService(listen_port=80, protocol="http") action = bound_load_balancer.add_service(service) hetzner_client.request.assert_called_with( json={ 'protocol': 'http', 'listen_port': 80 }, url="/load_balancers/14/actions/add_service", method="POST") assert action.id == 13 assert action.progress == 100 assert action.command == "add_service"
def _update_load_balancer_service(self): changed = False try: params = { "listen_port": self.module.params.get("listen_port"), } if self.module.params.get("destination_port") is not None: if self.hcloud_load_balancer_service.destination_port != self.module.params.get( "destination_port"): params["destination_port"] = self.module.params.get( "destination_port") changed = True if self.module.params.get("protocol") is not None: if self.hcloud_load_balancer_service.protocol != self.module.params.get( "protocol"): params["protocol"] = self.module.params.get("protocol") changed = True if self.module.params.get("proxyprotocol") is not None: if self.hcloud_load_balancer_service.proxyprotocol != self.module.params.get( "proxyprotocol"): params["proxyprotocol"] = self.module.params.get( "proxyprotocol") changed = True if self.module.params.get("http") is not None: params["http"] = self.__get_service_http( http_arg=self.module.params.get("http")) changed = True if self.module.params.get("health_check") is not None: params["health_check"] = self.__get_service_health_checks( health_check=self.module.params.get("health_check")) changed = True if not self.module.check_mode: self.hcloud_load_balancer.update_service( LoadBalancerService(**params)).wait_until_finished( max_retries=1000) except APIException as e: self.module.fail_json(msg=e.message) self._get_load_balancer() if changed: self._mark_as_changed()
def __init__(self, client, data, complete=True): algorithm = data.get("algorithm") if algorithm: data['algorithm'] = LoadBalancerAlgorithm(type=algorithm['type']) public_net = data.get("public_net") if public_net: ipv4_address = IPv4Address(**public_net['ipv4']) ipv6_network = IPv6Network(**public_net['ipv6']) data['public_net'] = PublicNetwork(ipv4=ipv4_address, ipv6=ipv6_network, enabled=public_net['enabled']) private_nets = data.get("private_net") if private_nets: private_nets = [ PrivateNet(network=BoundNetwork(client._client.networks, {"id": private_net['network']}, complete=False), ip=private_net['ip']) for private_net in private_nets ] data['private_net'] = private_nets targets = data.get("targets") if targets: tmp_targets = [] for target in targets: tmp_target = LoadBalancerTarget(type=target["type"]) if target["type"] == "server": tmp_target.server = BoundServer(client._client.servers, data=target['server'], complete=False) tmp_target.use_private_ip = target["use_private_ip"] elif target["type"] == "label_selector": tmp_target.label_selector = LoadBalancerTargetLabelSelector( selector=target['label_selector']['selector']) tmp_target.use_private_ip = target["use_private_ip"] elif target["type"] == "ip": tmp_target.ip = LoadBalancerTargetIP(ip=target['ip']['ip']) tmp_targets.append(tmp_target) data['targets'] = tmp_targets services = data.get("services") if services: tmp_services = [] for service in services: tmp_service = LoadBalancerService( protocol=service["protocol"], listen_port=service["listen_port"], destination_port=service["destination_port"], proxyprotocol=service["proxyprotocol"]) if service["protocol"] != "tcp": tmp_service.http = LoadBalancerServiceHttp( sticky_sessions=service['http']['sticky_sessions'], redirect_http=service['http']['redirect_http'], cookie_name=service['http']['cookie_name'], cookie_lifetime=service['http']['cookie_lifetime']) tmp_service.http.certificates = [ BoundCertificate(client._client.certificates, {"id": certificate}, complete=False) for certificate in service['http']['certificates'] ] tmp_service.health_check = LoadBalancerHealthCheck( protocol=service['health_check']['protocol'], port=service['health_check']['port'], interval=service['health_check']['interval'], retries=service['health_check']['retries'], timeout=service['health_check']['timeout']) if tmp_service.health_check.protocol != "tcp": tmp_service.health_check.http = LoadBalancerHealtCheckHttp( domain=service['health_check']['http']['domain'], path=service['health_check']['http']['path'], response=service['health_check']['http']['response'], tls=service['health_check']['http']['tls'], status_codes=service['health_check']['http'] ['status_codes']) tmp_services.append(tmp_service) data['services'] = tmp_services load_balancer_type = data.get("load_balancer_type") if load_balancer_type is not None: data['load_balancer_type'] = BoundLoadBalancerType( client._client.load_balancer_types, load_balancer_type) location = data.get("location") if location is not None: data['location'] = BoundLocation(client._client.locations, location) super(BoundLoadBalancer, self).__init__(client, data, complete)