@@ -14,7 +14,7 @@ def CheckAWSStatus(self, experimentsDetails):
1414 self .clients = client .AWSClient ().clientElb
1515
1616 if experimentsDetails .LoadBalancerName == "" or experimentsDetails .LoadBalancerZones == "" :
17- return ValueError ("Provided LoadBalancer Name or LoadBalanerZoner are empty" )
17+ return ValueError ("Provided LoadBalancer Name or LoadBalancerZoner are empty" )
1818
1919 try :
2020 self .clients .describe_load_balancers ()['LoadBalancerDescriptions' ]
@@ -23,36 +23,65 @@ def CheckAWSStatus(self, experimentsDetails):
2323 logging .info ("[Info]: LoadBalancer and Availablity of zone has been checked" )
2424
2525 def getSubnetFromVPC (self , experimentsDetails ):
26- client = boto3 .client ('elb' )
27- try :
28- response = client .describe_load_balancers (
29- LoadBalancerNames = [
30- experimentsDetails .LoadBalancerName ,
31- ]
32- )
33- return (response ['LoadBalancerDescriptions' ][0 ]['Subnets' ])
34- except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
35- return ValueError (exp )
36-
37- def getTargetSubnet (self , experimentsDetails , zone ):
38- client = boto3 .client ('ec2' )
39- try :
40- lst = self .getSubnetFromVPC (experimentsDetails )
41- i = 0
42- for i in range (len (lst )):
43- response = client .describe_subnets (
44- SubnetIds = [
45- lst [i ],
46- ],
26+
27+ if experimentsDetails .LoadBalancerVersion == "elb" :
28+
29+ client = boto3 .client ('elb' , region_name = experimentsDetails .AWSRegion )
30+ try :
31+ response = client .describe_load_balancers (
32+ LoadBalancerNames = [
33+ experimentsDetails .LoadBalancerName ,
34+ ]
4735 )
48- if (response ['Subnets' ][0 ]['AvailabilityZone' ]) == zone :
49- return lst [i ], None
50- except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
51- return lst [i ], ValueError (exp )
36+ return (response ['LoadBalancerDescriptions' ][0 ]['Subnets' ])
37+ except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
38+ return ValueError (exp )
39+
40+ # Use dedicated client for elv2 when env var has been set
41+ elif experimentsDetails .LoadBalancerVersion == "elbv2" :
5242
43+ client = boto3 .client ('elbv2' , region_name = experimentsDetails .AWSRegion )
44+ try :
45+ response = client .describe_load_balancers (
46+ Names = [
47+ experimentsDetails .LoadBalancerName ,
48+ ]
49+ )
50+ return (response ['LoadBalancers' ][0 ]['AvailabilityZones' ])
51+ except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
52+ return ValueError (exp )
5353
54+ def getTargetSubnet (self , experimentsDetails , zone ):
55+ client = boto3 .client ('ec2' , region_name = experimentsDetails .AWSRegion )
56+ if experimentsDetails .LoadBalancerVersion == "elb" :
57+
58+ try :
59+ lst = self .getSubnetFromVPC (experimentsDetails )
60+ i = 0
61+ for i in range (len (lst )):
62+ response = client .describe_subnets (
63+ SubnetIds = [
64+ lst [i ],
65+ ],
66+ )
67+ if (response ['Subnets' ][0 ]['AvailabilityZone' ]) == zone :
68+ return lst [i ], None
69+ except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
70+ return lst [i ], ValueError (exp )
71+
72+ # Specific section to deal with the output of the elv2 client
73+ elif experimentsDetails .LoadBalancerVersion == "elbv2" :
74+ try :
75+ lst = self .getSubnetFromVPC (experimentsDetails )
76+ i = 0
77+ for i in range (len (lst )):
78+ if (lst [i ]['ZoneName' ]) == zone :
79+ return lst [i ]['SubnetId' ], None
80+ except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
81+ return lst [i ], ValueError (exp )
82+
5483 def detachSubnet (self , experimentsDetails , subnet ):
55- client = boto3 .client ('elb' )
84+ client = boto3 .client ('elb' , region_name = experimentsDetails . AWSRegion )
5685 try :
5786 response = client .detach_load_balancer_from_subnets (
5887 LoadBalancerName = experimentsDetails .LoadBalancerName ,
@@ -63,8 +92,33 @@ def detachSubnet(self, experimentsDetails, subnet):
6392 except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
6493 return ValueError (exp )
6594
95+ # Specific method to detach the subnet with elbv2 client
96+ def detachSubnetv2 (self , experimentsDetails , subnet ):
97+ client = boto3 .client ('elbv2' , region_name = experimentsDetails .AWSRegion )
98+ try :
99+
100+ subnetsToKeep = []
101+ response = client .describe_load_balancers (
102+ Names = [
103+ experimentsDetails .LoadBalancerName ,
104+ ]
105+ )
106+
107+ # List subnet to keep that are currently attached to the LB
108+ for az in response ['LoadBalancers' ][0 ]['AvailabilityZones' ]:
109+ if az ['SubnetId' ] not in subnet :
110+ subnetsToKeep .append (az ['SubnetId' ])
111+
112+ response = client .set_subnets (
113+ LoadBalancerArn = response ['LoadBalancers' ][0 ]['LoadBalancerArn' ],
114+ Subnets = subnetsToKeep
115+ )
116+
117+ except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
118+ return ValueError (exp )
119+
66120 def attachSubnet (self , experimentsDetails , subnet ):
67- client = boto3 .client ('elb' )
121+ client = boto3 .client ('elb' , region_name = experimentsDetails . AWSRegion )
68122 try :
69123 response = client .attach_load_balancer_to_subnets (
70124 LoadBalancerName = experimentsDetails .LoadBalancerName ,
@@ -74,4 +128,27 @@ def attachSubnet(self, experimentsDetails, subnet):
74128 ValueError ("[Error]: Fail to attach the target subnet %s" , subnet )
75129 except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
76130 return ValueError (exp )
77-
131+
132+ # Specific method to attach the removed subnet with elbv2 client after Chaos
133+ def attachSubnetv2 (self , experimentsDetails , subnet ):
134+ client = boto3 .client ('elbv2' , region_name = experimentsDetails .AWSRegion )
135+ try :
136+
137+ subnetsToKeep = [subnet ]
138+ response = client .describe_load_balancers (
139+ Names = [
140+ experimentsDetails .LoadBalancerName ,
141+ ]
142+ )
143+
144+ for az in response ['LoadBalancers' ][0 ]['AvailabilityZones' ]:
145+ if az ['SubnetId' ] not in subnet :
146+ subnetsToKeep .append (az ['SubnetId' ])
147+
148+ response = client .set_subnets (
149+ LoadBalancerArn = response ['LoadBalancers' ][0 ]['LoadBalancerArn' ],
150+ Subnets = subnetsToKeep
151+ )
152+
153+ except (self .clients .exceptions .AccessPointNotFoundException , self .clients .exceptions .InvalidConfigurationRequestException ) as exp :
154+ return ValueError (exp )
0 commit comments