Skip to content

Commit acc5fa8

Browse files
committed
add type hints and function descriptions
1 parent 7862e9e commit acc5fa8

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

plugins/modules/elb_classic_lb_info.py

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@
125125
vpc_id: vpc-c248fda4
126126
"""
127127

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+
128134
try:
129135
import botocore
130136
except ImportError:
@@ -136,13 +142,20 @@
136142
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
137143
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
138144

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+
140147

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.
143151
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.
144155
145-
def list_elbs(connection, load_balancer_names):
156+
Returns:
157+
A list of dictionaries where each dictionary contains informtion about one ELB.
158+
"""
146159
results = []
147160

148161
if not load_balancer_names:
@@ -157,7 +170,17 @@ def list_elbs(connection, load_balancer_names):
157170
return results
158171

159172

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+
"""
161184
description = camel_dict_to_snake_dict(lb)
162185
name = lb["LoadBalancerName"]
163186
instances = lb.get("Instances", [])
@@ -177,11 +200,30 @@ def describe_elb(connection, lb):
177200

178201
@AWSRetry.jittered_backoff()
179202
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+
"""
180212
paginator = connection.get_paginator("describe_load_balancers")
181213
return paginator.paginate().build_full_result()["LoadBalancerDescriptions"]
182214

183215

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+
"""
185227
try:
186228
return connection.describe_load_balancers(aws_retry=True, LoadBalancerNames=[load_balancer_name])[
187229
"LoadBalancerDescriptions"
@@ -190,21 +232,53 @@ def get_lb(connection, load_balancer_name):
190232
return []
191233

192234

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+
"""
194246
attributes = connection.describe_load_balancer_attributes(aws_retry=True, LoadBalancerName=load_balancer_name).get(
195247
"LoadBalancerAttributes", {}
196248
)
197249
return camel_dict_to_snake_dict(attributes)
198250

199251

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+
"""
201263
tags = connection.describe_tags(aws_retry=True, LoadBalancerNames=[load_balancer_name])["TagDescriptions"]
202264
if not tags:
203265
return {}
204266
return boto3_tag_list_to_ansible_dict(tags[0]["Tags"])
205267

206268

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+
"""
208282
instance_states = connection.describe_instance_health(LoadBalancerName=load_balancer_name, Instances=instances).get(
209283
"InstanceStates", []
210284
)
@@ -222,7 +296,7 @@ def main():
222296
)
223297

224298
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)
226300
)
227301

228302
try:

0 commit comments

Comments
 (0)