Skip to content

Commit b12deb4

Browse files
committed
updates api
1 parent 4715c33 commit b12deb4

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
_logger = logging.getLogger(__name__)
3131

3232

33+
ALL = None
34+
35+
3336
@dataclass()
3437
class SimcoreEC2API:
3538
client: EC2Client
@@ -66,20 +69,30 @@ async def ping(self) -> bool:
6669
@ec2_exception_handler(_logger)
6770
async def get_ec2_instance_capabilities(
6871
self,
69-
instance_type_names: set[InstanceTypeType],
72+
instance_type_names: set[InstanceTypeType] | None = ALL,
7073
) -> list[EC2InstanceType]:
7174
"""Returns the ec2 instance types from a list of instance type names (sorted by name)
7275
7376
Arguments:
74-
instance_type_names -- the types to filter with. If an empty set, it returns all.
77+
instance_type_names -- the types to filter with
7578
7679
Raises:
7780
Ec2InstanceTypeInvalidError: some invalid types were used as filter
7881
ClustersKeeperRuntimeError: unexpected error communicating with EC2
7982
8083
"""
84+
if instance_type_names is None:
85+
assert ALL is None # nosec
86+
selected_instance_types = []
87+
else:
88+
selected_instance_types = list(instance_type_names)
89+
90+
if len(selected_instance_types) == 0:
91+
msg = "`instance_type_names` cannot be an empty set. Set as None if all"
92+
raise ValueError(msg)
93+
8194
instance_types = await self.client.describe_instance_types(
82-
InstanceTypes=list(instance_type_names)
95+
InstanceTypes=selected_instance_types
8396
)
8497
list_instances: list[EC2InstanceType] = []
8598
for instance in instance_types.get("InstanceTypes", []):

packages/aws-library/tests/test_ec2_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,22 @@ async def test_get_ec2_instance_capabilities(
119119
assert [_.name for _ in instance_types] == sorted(ec2_allowed_instances)
120120

121121

122-
async def test_get_ec2_instance_capabilities_empty_list_returns_all_options(
122+
async def test_get_ec2_instance_capabilities_returns_all_options(
123123
simcore_ec2_api: SimcoreEC2API,
124124
):
125-
instance_types = await simcore_ec2_api.get_ec2_instance_capabilities(set())
125+
instance_types = await simcore_ec2_api.get_ec2_instance_capabilities()
126126
assert instance_types
127127
# NOTE: this might need adaptation when moto is updated
128128
assert 700 < len(instance_types) < 828
129129

130130

131+
async def test_get_ec2_instance_capabilities_raise_with_empty_set(
132+
simcore_ec2_api: SimcoreEC2API,
133+
):
134+
with pytest.raises(ValueError, match="instance_type_names"):
135+
await simcore_ec2_api.get_ec2_instance_capabilities(set())
136+
137+
131138
async def test_get_ec2_instance_capabilities_with_invalid_type_raises(
132139
simcore_ec2_api: SimcoreEC2API,
133140
faker: Faker,

0 commit comments

Comments
 (0)