Skip to content

Commit 8cd2e51

Browse files
committed
added cancel command
1 parent 5ca1ec6 commit 8cd2e51

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ async def send_command(
107107
@log_decorator(_logger, logging.DEBUG)
108108
@ssm_exception_handler(_logger)
109109
async def get_command(self, instance_id: str, *, command_id: str) -> SSMCommand:
110-
111110
response = await self._client.get_command_invocation(
112111
CommandId=command_id, InstanceId=instance_id
113112
)
@@ -130,6 +129,13 @@ async def get_command(self, instance_id: str, *, command_id: str) -> SSMCommand:
130129
),
131130
)
132131

132+
@log_decorator(_logger, logging.DEBUG)
133+
@ssm_exception_handler(_logger)
134+
async def cancel_command(self, instance_id: str, *, command_id: str) -> None:
135+
await self._client.cancel_command(
136+
CommandId=command_id, InstanceIds=[instance_id]
137+
)
138+
133139
@log_decorator(_logger, logging.DEBUG)
134140
@ssm_exception_handler(_logger)
135141
async def is_instance_connected_to_ssm_server(self, instance_id: str) -> bool:

packages/aws-library/tests/test_ssm_client.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ async def simcore_ssm_api(
3737
await ec2.close()
3838

3939

40-
async def test_ssm_client_lifespan(simcore_ssm_api: SimcoreSSMAPI):
41-
...
40+
async def test_ssm_client_lifespan(simcore_ssm_api: SimcoreSSMAPI): ...
4241

4342

4443
async def test_aiobotocore_ssm_client_when_ssm_server_goes_up_and_down(
@@ -125,6 +124,30 @@ async def test_send_command(
125124
)
126125

127126

127+
async def test_cancel_command(
128+
mocked_aws_server: ThreadedMotoServer,
129+
simcore_ssm_api: SimcoreSSMAPI,
130+
faker: Faker,
131+
):
132+
command_name = faker.word()
133+
target_instance_id = faker.pystr()
134+
sent_command = await simcore_ssm_api.send_command(
135+
instance_ids=[target_instance_id],
136+
command=faker.text(),
137+
command_name=command_name,
138+
)
139+
assert sent_command
140+
assert sent_command.command_id
141+
assert sent_command.name == command_name
142+
assert sent_command.instance_ids == [target_instance_id]
143+
assert sent_command.status == "Success"
144+
145+
# cancelling a finished command is a no-op but is a bit of a joke as moto does not implement cancel command yet
146+
await simcore_ssm_api.cancel_command(
147+
instance_id=target_instance_id, command_id=sent_command.command_id
148+
)
149+
150+
128151
async def test_is_instance_connected_to_ssm_server(
129152
mocked_aws_server: ThreadedMotoServer,
130153
simcore_ssm_api: SimcoreSSMAPI,

packages/pytest-simcore/src/pytest_simcore/helpers/moto.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ def _patch_describe_instance_information(
5555
return {"InstanceInformationList": [{"PingStatus": "Online"}]}
5656

5757

58+
def _patch_cancel_command(self, operation_name, api_params) -> dict[str, Any]:
59+
warnings.warn(
60+
"moto is missing the cancel_command function, therefore it is manually mocked."
61+
"TIP: periodically check if it gets updated https://docs.getmoto.org/en/latest/docs/services/ssm.html#ssm",
62+
UserWarning,
63+
stacklevel=1,
64+
)
65+
return {}
66+
67+
5868
# Mocked aiobotocore _make_api_call function
5969
async def patched_aiobotocore_make_api_call(self, operation_name, api_params):
6070
# For example for the Access Analyzer service
@@ -63,6 +73,8 @@ async def patched_aiobotocore_make_api_call(self, operation_name, api_params):
6373
# Rationale -> https://github.com/boto/botocore/blob/develop/botocore/client.py#L810:L816
6474
if operation_name == "SendCommand":
6575
return await _patch_send_command(self, operation_name, api_params)
76+
if operation_name == "CancelCommand":
77+
return _patch_cancel_command(self, operation_name, api_params)
6678
if operation_name == "DescribeInstanceInformation":
6779
return _patch_describe_instance_information(self, operation_name, api_params)
6880

0 commit comments

Comments
 (0)