Skip to content

Commit

Permalink
Support SSM Parameter tags (#3739)
Browse files Browse the repository at this point in the history
  • Loading branch information
jocke-l authored Mar 2, 2021
1 parent 3c810ad commit 457402f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 10 additions & 1 deletion moto/ssm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(
keyid,
last_modified_date,
version,
tags=None,
):
self.name = name
self.type = type
Expand All @@ -54,6 +55,7 @@ def __init__(
self.keyid = keyid
self.last_modified_date = last_modified_date
self.version = version
self.tags = tags or []
self.labels = []

if self.type == "SecureString":
Expand Down Expand Up @@ -1151,6 +1153,12 @@ def _match_filters(self, parameter, filters=None):
values = ["/" + value.strip("/") for value in values]
elif key == "Type":
what = parameter.type
elif key.startswith("tag:"):
what = key[4:] or None
for tag in parameter.tags:
if tag["Key"] == what and tag["Value"] in values:
return True
return False

if what is None:
return False
Expand Down Expand Up @@ -1274,7 +1282,7 @@ def label_parameter_version(self, name, version, labels):
return [invalid_labels, version]

def put_parameter(
self, name, description, value, type, allowed_pattern, keyid, overwrite
self, name, description, value, type, allowed_pattern, keyid, overwrite, tags,
):
if name.lower().lstrip("/").startswith("aws") or name.lower().lstrip(
"/"
Expand Down Expand Up @@ -1315,6 +1323,7 @@ def put_parameter(
keyid,
last_modified_date,
version,
tags or [],
)
)
return version
Expand Down
3 changes: 2 additions & 1 deletion moto/ssm/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,10 @@ def put_parameter(self):
allowed_pattern = self._get_param("AllowedPattern")
keyid = self._get_param("KeyId")
overwrite = self._get_param("Overwrite", False)
tags = self._get_param("Tags", [])

result = self.ssm_backend.put_parameter(
name, description, value, type_, allowed_pattern, keyid, overwrite
name, description, value, type_, allowed_pattern, keyid, overwrite, tags
)

if result is None:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_ssm/test_ssm_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,28 @@ def test_describe_parameters_attributes():
parameters[1]["Version"].should.equal(1)


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

client.put_parameter(Name="/foo/bar", Value="spam", Type="String")
client.put_parameter(
Name="/spam/eggs",
Value="eggs",
Type="String",
Tags=[{"Key": "spam", "Value": "eggs"}],
)

response = client.describe_parameters(
ParameterFilters=[{"Key": "tag:spam", "Values": ["eggs"]}]
)

parameters = response["Parameters"]
parameters.should.have.length_of(1)

parameters[0]["Name"].should.equal("/spam/eggs")


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

0 comments on commit 457402f

Please sign in to comment.