SSM: Allow filtering by existence of tag-name (#6238)

This commit is contained in:
Bert Blommers 2023-04-20 21:12:43 +00:00 committed by GitHub
parent c2e3d90fc9
commit 28f3f84644
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 11 deletions

View File

@ -1434,7 +1434,7 @@ class SimpleSystemManagerBackend(BaseBackend):
f"The following filter key is not valid: {key}. Valid filter keys include: [Type, KeyId]." f"The following filter key is not valid: {key}. Valid filter keys include: [Type, KeyId]."
) )
if not values: if key in ["Name", "Type", "Path", "Tier", "Keyid"] and not values:
raise InvalidFilterValue( raise InvalidFilterValue(
"The following filter values are missing : null for filter key Name." "The following filter values are missing : null for filter key Name."
) )
@ -1636,7 +1636,7 @@ class SimpleSystemManagerBackend(BaseBackend):
elif key.startswith("tag:"): elif key.startswith("tag:"):
what = [tag["Value"] for tag in parameter.tags if tag["Key"] == key[4:]] what = [tag["Value"] for tag in parameter.tags if tag["Key"] == key[4:]]
if what is None: if what is None or what == []:
return False return False
# 'what' can be a list (of multiple tag-values, for instance) # 'what' can be a list (of multiple tag-values, for instance)
is_list = isinstance(what, list) is_list = isinstance(what, list)
@ -1650,6 +1650,9 @@ class SimpleSystemManagerBackend(BaseBackend):
elif option == "Contains" and not any(value in what for value in values): elif option == "Contains" and not any(value in what for value in values):
return False return False
elif option == "Equals": elif option == "Equals":
if is_list and len(values) == 0:
# User hasn't provided possible tag-values - they just want to know whether the tag exists
return True
if is_list and not any(val in what for val in values): if is_list and not any(val in what for val in values):
return False return False
elif not is_list and not any(what == val for val in values): elif not is_list and not any(what == val for val in values):

View File

@ -2,15 +2,15 @@ import json
from moto.core.responses import BaseResponse from moto.core.responses import BaseResponse
from .exceptions import ValidationException from .exceptions import ValidationException
from .models import ssm_backends from .models import ssm_backends, SimpleSystemManagerBackend
class SimpleSystemManagerResponse(BaseResponse): class SimpleSystemManagerResponse(BaseResponse):
def __init__(self): def __init__(self) -> None:
super().__init__(service_name="ssm") super().__init__(service_name="ssm")
@property @property
def ssm_backend(self): def ssm_backend(self) -> SimpleSystemManagerBackend:
return ssm_backends[self.current_account][self.region] return ssm_backends[self.current_account][self.region]
@property @property

View File

@ -1039,14 +1039,17 @@ def test_describe_parameters_tags():
Tags=[{"Key": "spam", "Value": "eggs"}], Tags=[{"Key": "spam", "Value": "eggs"}],
) )
response = client.describe_parameters( parameters = client.describe_parameters(
ParameterFilters=[{"Key": "tag:spam", "Values": ["eggs"]}] ParameterFilters=[{"Key": "tag:spam", "Values": ["eggs"]}]
) )["Parameters"]
assert len(parameters) == 1
assert parameters[0]["Name"] == "/spam/eggs"
parameters = response["Parameters"] # Verify we can filter by the existence of a tag
parameters.should.have.length_of(1) filters = [{"Key": "tag:spam"}]
response = client.describe_parameters(ParameterFilters=filters)
parameters[0]["Name"].should.equal("/spam/eggs") assert len(response["Parameters"]) == 1
assert {p["Name"] for p in response["Parameters"]} == set(["/spam/eggs"])
@mock_ssm @mock_ssm