1212from hcloud .actions .client import BoundAction
1313from hcloud .load_balancers .domain import LoadBalancer , IPv4Address , IPv6Network , PublicNetwork , PrivateNet , \
1414 CreateLoadBalancerResponse , LoadBalancerTarget , LoadBalancerService , LoadBalancerServiceHttp , \
15- LoadBalancerHealthCheck , LoadBalancerHealtCheckHttp , LoadBalancerAlgorithm
15+ LoadBalancerHealthCheck , LoadBalancerHealtCheckHttp , LoadBalancerAlgorithm , LoadBalancerTargetLabelSelector , \
16+ LoadBalancerTargetIP
1617
1718
1819class BoundLoadBalancer (BoundModelBase ):
@@ -43,6 +44,11 @@ def __init__(self, client, data, complete=True):
4344 tmp_target = LoadBalancerTarget (type = target ["type" ], use_private_ip = target ["use_private_ip" ])
4445 if target ["type" ] == "server" :
4546 tmp_target .server = BoundServer (client ._client .servers , data = target ['server' ], complete = False )
47+ elif target ["type" ] == "label_selector" :
48+ tmp_target .label_selector = LoadBalancerTargetLabelSelector (
49+ selector = target ['label_selector' ]['selector' ])
50+ elif target ["type" ] == "ip" :
51+ tmp_target .label_selector = LoadBalancerTargetIP (ip = target ['ip' ]['ip' ])
4652 tmp_targets .append (tmp_target )
4753 data ['targets' ] = tmp_targets
4854
@@ -186,7 +192,7 @@ def remove_target(self, target):
186192 return self ._client .remove_target (self , target )
187193
188194 def change_algorithm (self , algorithm ):
189- # type: (LoadBalancerService ) -> List[BoundAction]
195+ # type: (LoadBalancerAlgorithm ) -> List[BoundAction]
190196 """Changes the algorithm used by the Load Balancer
191197
192198 :param algorithm: :class:`LoadBalancerAlgorithm <hcloud.load_balancers.domain.LoadBalancerAlgorithm>`
@@ -386,9 +392,14 @@ def create(
386392 for target in targets :
387393 target_data = {
388394 "type" : target .type ,
389- "server" : target .server ,
390395 "use_private_ip" : target .use_private_ip
391396 }
397+ if target .type == "server" :
398+ target_data ['server' ] = {"id" : target .server .id }
399+ elif target .type == "label_selector" :
400+ target_data ['label_selector' ] = {"selector" : target .label_selector .selector }
401+ elif target .type == "ip" :
402+ target_data ['ip' ] = {"ip" : target .ip .ip }
392403 target_list .append (target_data )
393404
394405 data ["targets" ] = target_list
@@ -609,9 +620,14 @@ def add_target(self, load_balancer, target):
609620 """
610621 data = {
611622 "type" : target .type ,
612- "server" : {"id" : target .server .id },
613623 "use_private_ip" : target .use_private_ip
614624 }
625+ if target .type == "server" :
626+ data ['server' ] = {"id" : target .server .id }
627+ elif target .type == "label_selector" :
628+ data ['label_selector' ] = {"selector" : target .label_selector .selector }
629+ elif target .type == "ip" :
630+ data ['ip' ] = {"ip" : target .ip .ip }
615631
616632 response = self ._client .request (
617633 url = "/load_balancers/{load_balancer_id}/actions/add_target" .format (load_balancer_id = load_balancer .id ),
@@ -629,8 +645,13 @@ def remove_target(self, load_balancer, target):
629645 """
630646 data = {
631647 "type" : target .type ,
632- "server" : {"id" : target .server .id },
633648 }
649+ if target .type == "server" :
650+ data ['server' ] = {"id" : target .server .id }
651+ elif target .type == "label_selector" :
652+ data ['label_selector' ] = {"selector" : target .label_selector .selector }
653+ elif target .type == "ip" :
654+ data ['ip' ] = {"ip" : target .ip .ip }
634655
635656 response = self ._client .request (
636657 url = "/load_balancers/{load_balancer_id}/actions/remove_target" .format (load_balancer_id = load_balancer .id ),
@@ -754,6 +775,7 @@ def change_type(self, load_balancer, load_balancer_type):
754775 data = {
755776 "load_balancer_type" : load_balancer_type .id_or_name ,
756777 }
757- response = self ._client .request (url = "/load_balancers/{load_balancer_id}/actions/change_type" .format (load_balancer_id = load_balancer .id ),
758- method = "POST" , json = data )
778+ response = self ._client .request (
779+ url = "/load_balancers/{load_balancer_id}/actions/change_type" .format (load_balancer_id = load_balancer .id ),
780+ method = "POST" , json = data )
759781 return BoundAction (self ._client .actions , response ['action' ])
0 commit comments