Skip to content

Commit

Permalink
SSM: Support tags for Maintenance Windows (#6350)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers authored May 31, 2023
1 parent c1acc10 commit 1a8060c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion moto/s3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2524,7 +2524,7 @@ def __init__(

# Maps bucket names to account IDs. This is used to locate the exact S3Backend
# holding the bucket and to maintain the common bucket namespace.
self.bucket_accounts: dict[str, str] = {}
self.bucket_accounts: Dict[str, str] = {}


s3_backends = S3BackendDict(
Expand Down
9 changes: 9 additions & 0 deletions moto/ssm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,9 @@ def _validate_resource_type_and_id(
raise InvalidResourceId()
else:
return
elif resource_type == "MaintenanceWindow":
if resource_id not in self.windows:
raise InvalidResourceId()
elif resource_type not in (
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ssm.html#SSM.Client.remove_tags_from_resource
"ManagedInstance",
Expand Down Expand Up @@ -2076,6 +2079,7 @@ def create_maintenance_window(
schedule_offset: int,
start_date: str,
end_date: str,
tags: Optional[List[Dict[str, str]]],
) -> str:
"""
Creates a maintenance window. No error handling or input validation has been implemented yet.
Expand All @@ -2092,6 +2096,11 @@ def create_maintenance_window(
end_date,
)
self.windows[window.id] = window

if tags:
window_tags = {t["Key"]: t["Value"] for t in tags}
self.add_tags_to_resource("MaintenanceWindow", window.id, window_tags)

return window.id

def get_maintenance_window(self, window_id: str) -> FakeMaintenanceWindow:
Expand Down
2 changes: 2 additions & 0 deletions moto/ssm/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ def create_maintenance_window(self) -> str:
schedule_offset = self._get_int_param("ScheduleOffset")
start_date = self._get_param("StartDate")
end_date = self._get_param("EndDate")
tags = self._get_param("Tags")
window_id = self.ssm_backend.create_maintenance_window(
name=name,
description=desc,
Expand All @@ -431,6 +432,7 @@ def create_maintenance_window(self) -> str:
schedule_offset=schedule_offset,
start_date=start_date,
end_date=end_date,
tags=tags,
)
return json.dumps({"WindowId": window_id})

Expand Down
59 changes: 59 additions & 0 deletions tests/test_ssm/test_ssm_maintenance_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,62 @@ def test_delete_maintenance_windows():

resp = ssm.describe_maintenance_windows()
resp.should.have.key("WindowIdentities").equals([])


@mock_ssm
def test_tags():
ssm = boto3.client("ssm", region_name="us-east-1")

# create without & list
mw_id = ssm.create_maintenance_window(
Name="simple-window",
Schedule="cron(15 12 * * ? *)",
Duration=2,
Cutoff=1,
AllowUnassociatedTargets=False,
)["WindowId"]

# add & list
ssm.add_tags_to_resource(
ResourceType="MaintenanceWindow",
ResourceId=mw_id,
Tags=[{"Key": "k1", "Value": "v1"}],
)
tags = ssm.list_tags_for_resource(
ResourceType="MaintenanceWindow", ResourceId=mw_id
)["TagList"]
assert tags == [{"Key": "k1", "Value": "v1"}]

# create & list
mw_id = ssm.create_maintenance_window(
Name="simple-window",
Schedule="cron(15 12 * * ? *)",
Duration=2,
Cutoff=1,
AllowUnassociatedTargets=False,
Tags=[{"Key": "k2", "Value": "v2"}],
)["WindowId"]
tags = ssm.list_tags_for_resource(
ResourceType="MaintenanceWindow", ResourceId=mw_id
)["TagList"]
assert tags == [{"Key": "k2", "Value": "v2"}]

# add more & list
ssm.add_tags_to_resource(
ResourceType="MaintenanceWindow",
ResourceId=mw_id,
Tags=[{"Key": "k3", "Value": "v3"}],
)
tags = ssm.list_tags_for_resource(
ResourceType="MaintenanceWindow", ResourceId=mw_id
)["TagList"]
assert tags == [{"Key": "k2", "Value": "v2"}, {"Key": "k3", "Value": "v3"}]

# remove & list
ssm.remove_tags_from_resource(
ResourceType="MaintenanceWindow", ResourceId=mw_id, TagKeys=["k3"]
)
tags = ssm.list_tags_for_resource(
ResourceType="MaintenanceWindow", ResourceId=mw_id
)["TagList"]
assert tags == [{"Key": "k2", "Value": "v2"}]

0 comments on commit 1a8060c

Please sign in to comment.