12
12
from hcloud .actions .client import BoundAction
13
13
from hcloud .load_balancers .domain import LoadBalancer , IPv4Address , IPv6Network , PublicNetwork , PrivateNet , \
14
14
CreateLoadBalancerResponse , LoadBalancerTarget , LoadBalancerService , LoadBalancerServiceHttp , \
15
- LoadBalancerHealthCheck , LoadBalancerHealtCheckHttp , LoadBalancerAlgorithm
15
+ LoadBalancerHealthCheck , LoadBalancerHealtCheckHttp , LoadBalancerAlgorithm , LoadBalancerTargetLabelSelector , \
16
+ LoadBalancerTargetIP
16
17
17
18
18
19
class BoundLoadBalancer (BoundModelBase ):
@@ -43,6 +44,11 @@ def __init__(self, client, data, complete=True):
43
44
tmp_target = LoadBalancerTarget (type = target ["type" ], use_private_ip = target ["use_private_ip" ])
44
45
if target ["type" ] == "server" :
45
46
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' ])
46
52
tmp_targets .append (tmp_target )
47
53
data ['targets' ] = tmp_targets
48
54
@@ -186,7 +192,7 @@ def remove_target(self, target):
186
192
return self ._client .remove_target (self , target )
187
193
188
194
def change_algorithm (self , algorithm ):
189
- # type: (LoadBalancerService ) -> List[BoundAction]
195
+ # type: (LoadBalancerAlgorithm ) -> List[BoundAction]
190
196
"""Changes the algorithm used by the Load Balancer
191
197
192
198
:param algorithm: :class:`LoadBalancerAlgorithm <hcloud.load_balancers.domain.LoadBalancerAlgorithm>`
@@ -386,9 +392,14 @@ def create(
386
392
for target in targets :
387
393
target_data = {
388
394
"type" : target .type ,
389
- "server" : target .server ,
390
395
"use_private_ip" : target .use_private_ip
391
396
}
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 }
392
403
target_list .append (target_data )
393
404
394
405
data ["targets" ] = target_list
@@ -609,9 +620,14 @@ def add_target(self, load_balancer, target):
609
620
"""
610
621
data = {
611
622
"type" : target .type ,
612
- "server" : {"id" : target .server .id },
613
623
"use_private_ip" : target .use_private_ip
614
624
}
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 }
615
631
616
632
response = self ._client .request (
617
633
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):
629
645
"""
630
646
data = {
631
647
"type" : target .type ,
632
- "server" : {"id" : target .server .id },
633
648
}
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 }
634
655
635
656
response = self ._client .request (
636
657
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):
754
775
data = {
755
776
"load_balancer_type" : load_balancer_type .id_or_name ,
756
777
}
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 )
759
781
return BoundAction (self ._client .actions , response ['action' ])
0 commit comments