Пример #1
0
    def __get_service_http(self, http_arg):
        service_http = LoadBalancerServiceHttp(certificates=[])
        if http_arg.get("cookie_name") is not None:
            service_http.cookie_name = http_arg.get("cookie_name")
        if http_arg.get("cookie_lifetime") is not None:
            service_http.cookie_lifetime = http_arg.get("cookie_lifetime")
        if http_arg.get("sticky_sessions") is not None:
            service_http.sticky_sessions = http_arg.get("sticky_sessions")
        if http_arg.get("redirect_http") is not None:
            service_http.redirect_http = http_arg.get("redirect_http")
        if http_arg.get("certificates") is not None:
            certificates = http_arg.get("certificates")
            if certificates is not None:
                for certificate in certificates:
                    hcloud_cert = None
                    try:
                        try:
                            hcloud_cert = self.client.certificates.get_by_name(
                                certificate
                            )
                        except Exception:
                            hcloud_cert = self.client.certificates.get_by_id(
                                certificate
                            )
                    except Exception as e:
                        self.module.fail_json(msg=e.message)
                    service_http.certificates.append(hcloud_cert)

        return service_http
Пример #2
0
    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)