Implement the ListCommands API endpoint for the SSM client.

Currently only supports getting commands by CommandId and InstanceIds.
This commit is contained in:
Mike Liu 2018-04-25 16:25:44 -04:00
parent a974a3dfe4
commit 1016487c78
2 changed files with 37 additions and 0 deletions

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
from collections import defaultdict
from moto.core import BaseBackend, BaseModel
from moto.core.exceptions import RESTError
from moto.ec2 import ec2_backends
import datetime
@ -229,6 +230,37 @@ class SimpleSystemManagerBackend(BaseBackend):
'Command': command.response_object()
}
def list_commands(self, **kwargs):
"""
https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_ListCommands.html
"""
commands = self._commands
command_id = kwargs.get('CommandId', None)
if command_id:
commands = [self.get_command_by_id(command_id)]
instance_id = kwargs.get('InstanceId', None)
if instance_id:
commands = self.get_commands_by_instance_id(instance_id)
return {
'Commands': [command.response_object() for command in commands]
}
def get_command_by_id(self, id):
command = next(
(command for command in self._commands if command.command_id == id), None)
if command is None:
raise RESTError('InvalidCommandId', 'Invalid command id.')
return command
def get_commands_by_instance_id(self, instance_id):
return [
command for command in self._commands
if instance_id in command.instance_ids]
ssm_backends = {}
for region, ec2_backend in ec2_backends.items():

View File

@ -204,3 +204,8 @@ class SimpleSystemManagerResponse(BaseResponse):
return json.dumps(
self.ssm_backend.send_command(**self.request_params)
)
def list_commands(self):
return json.dumps(
self.ssm_backend.list_commands(**self.request_params)
)