Skip to content

Commit f020bf1

Browse files
committed
@GitHK review: refactor
1 parent 1a187fb commit f020bf1

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

packages/aws-library/src/aws_library/ec2/_client.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from types_aiobotocore_ec2.literals import InstanceStateNameType, InstanceTypeType
1616
from types_aiobotocore_ec2.type_defs import (
1717
FilterTypeDef,
18-
SubnetTypeDef,
1918
TagTypeDef,
2019
)
2120

@@ -37,6 +36,7 @@
3736
check_max_number_of_instances_not_exceeded,
3837
compose_user_data,
3938
ec2_instance_data_from_aws_instance,
39+
get_subnet_capacity,
4040
)
4141

4242
_logger = logging.getLogger(__name__)
@@ -168,24 +168,9 @@ async def launch_instances(
168168
# and avoid trying to launch instances in subnets that are already full
169169
# and also allows to circumvent a moto bug that does not raise
170170
# InsufficientInstanceCapacity when a subnet is full
171-
subnets = await self.client.describe_subnets(
172-
SubnetIds=instance_config.subnet_ids
171+
subnet_id_to_available_ips = await get_subnet_capacity(
172+
self, subnet_ids=instance_config.subnet_ids
173173
)
174-
assert "Subnets" in subnets # nosec
175-
subnet_id_to_subnet_map: dict[str, SubnetTypeDef] = {
176-
subnet[
177-
"SubnetId"
178-
]: subnet # pyright: ignore[reportTypedDictNotRequiredAccess]
179-
for subnet in subnets["Subnets"]
180-
}
181-
# preserve the order of instance_config.subnet_ids
182-
183-
subnet_id_to_available_ips: dict[str, int] = {
184-
subnet_id: subnet_id_to_subnet_map[subnet_id][
185-
"AvailableIpAddressCount"
186-
] # pyright: ignore[reportTypedDictNotRequiredAccess]
187-
for subnet_id in instance_config.subnet_ids
188-
}
189174

190175
total_available_ips = sum(subnet_id_to_available_ips.values())
191176
if total_available_ips < min_number_of_instances:

packages/aws-library/src/aws_library/ec2/_utils.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from textwrap import dedent
22
from typing import TYPE_CHECKING, cast
33

4-
from types_aiobotocore_ec2.type_defs import InstanceTypeDef
4+
from types_aiobotocore_ec2.type_defs import (
5+
InstanceTypeDef,
6+
SubnetTypeDef,
7+
)
58

69
from ._errors import EC2TooManyInstancesError
710
from ._models import EC2InstanceConfig, EC2InstanceData, EC2Tags
@@ -61,3 +64,24 @@ async def check_max_number_of_instances_not_exceeded(
6164
> max_total_number_of_instances
6265
):
6366
raise EC2TooManyInstancesError(num_instances=max_total_number_of_instances)
67+
68+
69+
async def get_subnet_capacity(
70+
ec2_client: "SimcoreEC2API", *, subnet_ids: list[str]
71+
) -> dict[str, int]:
72+
73+
subnets = await ec2_client.client.describe_subnets(SubnetIds=subnet_ids)
74+
assert "Subnets" in subnets # nosec
75+
subnet_id_to_subnet_map: dict[str, SubnetTypeDef] = {
76+
subnet["SubnetId"]: subnet # pyright: ignore[reportTypedDictNotRequiredAccess]
77+
for subnet in subnets["Subnets"]
78+
}
79+
# preserve the order of instance_config.subnet_ids
80+
81+
subnet_id_to_available_ips: dict[str, int] = {
82+
subnet_id: subnet_id_to_subnet_map[subnet_id][
83+
"AvailableIpAddressCount"
84+
] # pyright: ignore[reportTypedDictNotRequiredAccess]
85+
for subnet_id in subnet_ids
86+
}
87+
return subnet_id_to_available_ips

0 commit comments

Comments
 (0)