125
125
vpc_id: vpc-c248fda4
126
126
"""
127
127
128
+ from typing import Any
129
+ from typing import List
130
+ from typing import Dict
131
+ from typing import Tuple
132
+ from typing import Union
133
+
128
134
try :
129
135
import botocore
130
136
except ImportError :
136
142
from ansible_collections .amazon .aws .plugins .module_utils .retries import AWSRetry
137
143
from ansible_collections .amazon .aws .plugins .module_utils .tagging import boto3_tag_list_to_ansible_dict
138
144
139
- from ansible_collections .community .aws .plugins .module_utils .modules import AnsibleCommunityAWSModule as AnsibleAWSModule
145
+ from ansible_collections .amazon .aws .plugins .module_utils .modules import AnsibleAWSModule
146
+
140
147
141
- MAX_AWS_RETRIES = 5
142
- MAX_AWS_DELAY = 5
148
+ def list_elbs (connection : Any , load_balancer_names : List [str ]) -> List [Dict ]:
149
+ """
150
+ List Elastic Load Balancers (ELBs) and their detailed information.
143
151
152
+ Parameters:
153
+ connection (boto3.client): The Boto3 ELB client object.
154
+ load_balancer_names (List[str]): List of ELB names to gather information about.
144
155
145
- def list_elbs (connection , load_balancer_names ):
156
+ Returns:
157
+ A list of dictionaries where each dictionary contains informtion about one ELB.
158
+ """
146
159
results = []
147
160
148
161
if not load_balancer_names :
@@ -157,7 +170,17 @@ def list_elbs(connection, load_balancer_names):
157
170
return results
158
171
159
172
160
- def describe_elb (connection , lb ):
173
+ def describe_elb (connection : Any , lb : Dict ) -> Dict :
174
+ """
175
+ Describes an Elastic Load Balancer (ELB).
176
+
177
+ Parameters:
178
+ connection (boto3.client): The Boto3 ELB client object.
179
+ lb (Dict): Dictionary containing ELB .
180
+
181
+ Returns:
182
+ A dictionary with detailed information of the ELB.
183
+ """
161
184
description = camel_dict_to_snake_dict (lb )
162
185
name = lb ["LoadBalancerName" ]
163
186
instances = lb .get ("Instances" , [])
@@ -177,11 +200,30 @@ def describe_elb(connection, lb):
177
200
178
201
@AWSRetry .jittered_backoff ()
179
202
def get_all_lb (connection ):
203
+ """
204
+ Get paginated result for information of all Elastic Load Balancers.
205
+
206
+ Parameters:
207
+ connection (boto3.client): The Boto3 ELB client object.
208
+
209
+ Returns:
210
+ A list of dictionaries containing descriptions of all ELBs.
211
+ """
180
212
paginator = connection .get_paginator ("describe_load_balancers" )
181
213
return paginator .paginate ().build_full_result ()["LoadBalancerDescriptions" ]
182
214
183
215
184
- def get_lb (connection , load_balancer_name ):
216
+ def get_lb (connection : Any , load_balancer_name : str ) -> Union [Dict , List ]:
217
+ """
218
+ Describes a specific Elastic Load Balancer (ELB) by name.
219
+
220
+ Parameters:
221
+ connection (boto3.client): The Boto3 ELB client object.
222
+ load_balancer_name (str): Name of the ELB to gather information about.
223
+
224
+ Returns:
225
+ A dictionary with detailed information of the specified ELB.
226
+ """
185
227
try :
186
228
return connection .describe_load_balancers (aws_retry = True , LoadBalancerNames = [load_balancer_name ])[
187
229
"LoadBalancerDescriptions"
@@ -190,21 +232,53 @@ def get_lb(connection, load_balancer_name):
190
232
return []
191
233
192
234
193
- def get_lb_attributes (connection , load_balancer_name ):
235
+ def get_lb_attributes (connection : Any , load_balancer_name : str ) -> Dict :
236
+ """
237
+ Retrieves attributes of specific Elastic Load Balancer (ELB) by name.
238
+
239
+ Parameters:
240
+ connection (boto3.client): The Boto3 ELB client object.
241
+ load_balancer_name (str): Name of the ELB to gather information about.
242
+
243
+ Returns:
244
+ A dictionary with detailed information of the attributes of specified ELB.
245
+ """
194
246
attributes = connection .describe_load_balancer_attributes (aws_retry = True , LoadBalancerName = load_balancer_name ).get (
195
247
"LoadBalancerAttributes" , {}
196
248
)
197
249
return camel_dict_to_snake_dict (attributes )
198
250
199
251
200
- def get_tags (connection , load_balancer_name ):
252
+ def get_tags (connection : Any , load_balancer_name : str ) -> Dict :
253
+ """
254
+ Retrieves tags of specific Elastic Load Balancer (ELB) by name.
255
+
256
+ Parameters:
257
+ connection (boto3.client): The Boto3 ELB client object.
258
+ load_balancer_name (str): Name of the ELB to gather information about.
259
+
260
+ Returns:
261
+ A dictionary of tags associated with the specified ELB.
262
+ """
201
263
tags = connection .describe_tags (aws_retry = True , LoadBalancerNames = [load_balancer_name ])["TagDescriptions" ]
202
264
if not tags :
203
265
return {}
204
266
return boto3_tag_list_to_ansible_dict (tags [0 ]["Tags" ])
205
267
206
268
207
- def lb_instance_health (connection , load_balancer_name , instances , state ):
269
+ def lb_instance_health (connection : Any , load_balancer_name : str , instances : List [Dict ], state : str ) -> Tuple [List , int ]:
270
+ """
271
+ Describes the health status of instances associated with a specific Elastic Load Balancer (ELB).
272
+
273
+ Parameters:
274
+ connection (Any): The Boto3 client object for ELB.
275
+ load_balancer_name (str): The name of the ELB.
276
+ instances (List[Dict]): List of dictionaries containing instances associated with the ELB.
277
+ state (str): The health state to filter by (e.g., "InService", "OutOfService", "Unknown").
278
+
279
+ Returns:
280
+ Tuple[List, int]: A tuple containing a list of instance IDs matching state and the count of matching instances.
281
+ """
208
282
instance_states = connection .describe_instance_health (LoadBalancerName = load_balancer_name , Instances = instances ).get (
209
283
"InstanceStates" , []
210
284
)
@@ -222,7 +296,7 @@ def main():
222
296
)
223
297
224
298
connection = module .client (
225
- "elb" , retry_decorator = AWSRetry .jittered_backoff (retries = MAX_AWS_RETRIES , delay = MAX_AWS_DELAY )
299
+ "elb" , retry_decorator = AWSRetry .jittered_backoff (retries = 5 , delay = 5 )
226
300
)
227
301
228
302
try :
0 commit comments