Enhance function get_parameter by parameter name, version or labels (#3191)

Co-authored-by: Ninh Khong <ninh@1on1english.vn>
This commit is contained in:
Ninh Khong 2020-07-28 22:59:22 +07:00 committed by GitHub
parent 28d1d762af
commit ff1f565142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 2 deletions

View File

@ -1189,8 +1189,33 @@ class SimpleSystemManagerBackend(BaseBackend):
return True return True
def get_parameter(self, name, with_decryption): def get_parameter(self, name, with_decryption):
if name in self._parameters: name_parts = name.split(":")
return self._parameters[name][-1] name_prefix = name_parts[0]
if len(name_parts) > 2:
return None
if name_prefix in self._parameters:
if len(name_parts) == 1:
return self._parameters[name][-1]
if len(name_parts) == 2:
version_or_label = name_parts[1]
parameters = self._parameters[name_prefix]
if version_or_label.isdigit():
result = list(
filter(lambda x: str(x.version) == version_or_label, parameters)
)
if len(result) > 0:
return result[-1]
result = list(
filter(lambda x: version_or_label in x.labels, parameters)
)
if len(result) > 0:
return result[-1]
return None return None
def label_parameter_version(self, name, version, labels): def label_parameter_version(self, name, version, labels):

View File

@ -396,6 +396,64 @@ def test_get_parameter():
) )
@mock_ssm
def test_get_parameter_with_version_and_labels():
client = boto3.client("ssm", region_name="us-east-1")
client.put_parameter(
Name="test-1", Description="A test parameter", Value="value", Type="String"
)
client.put_parameter(
Name="test-2", Description="A test parameter", Value="value", Type="String"
)
client.label_parameter_version(
Name="test-2", ParameterVersion=1, Labels=["test-label"]
)
response = client.get_parameter(Name="test-1:1", WithDecryption=False)
response["Parameter"]["Name"].should.equal("test-1")
response["Parameter"]["Value"].should.equal("value")
response["Parameter"]["Type"].should.equal("String")
response["Parameter"]["LastModifiedDate"].should.be.a(datetime.datetime)
response["Parameter"]["ARN"].should.equal(
"arn:aws:ssm:us-east-1:1234567890:parameter/test-1"
)
response = client.get_parameter(Name="test-2:1", WithDecryption=False)
response["Parameter"]["Name"].should.equal("test-2")
response["Parameter"]["Value"].should.equal("value")
response["Parameter"]["Type"].should.equal("String")
response["Parameter"]["LastModifiedDate"].should.be.a(datetime.datetime)
response["Parameter"]["ARN"].should.equal(
"arn:aws:ssm:us-east-1:1234567890:parameter/test-2"
)
response = client.get_parameter(Name="test-2:test-label", WithDecryption=False)
response["Parameter"]["Name"].should.equal("test-2")
response["Parameter"]["Value"].should.equal("value")
response["Parameter"]["Type"].should.equal("String")
response["Parameter"]["LastModifiedDate"].should.be.a(datetime.datetime)
response["Parameter"]["ARN"].should.equal(
"arn:aws:ssm:us-east-1:1234567890:parameter/test-2"
)
with assert_raises(ClientError) as ex:
client.get_parameter(Name="test-2:2:3", WithDecryption=False)
ex.exception.response["Error"]["Code"].should.equal("ParameterNotFound")
ex.exception.response["Error"]["Message"].should.equal(
"Parameter test-2:2:3 not found."
)
with assert_raises(ClientError) as ex:
client.get_parameter(Name="test-2:2", WithDecryption=False)
ex.exception.response["Error"]["Code"].should.equal("ParameterNotFound")
ex.exception.response["Error"]["Message"].should.equal(
"Parameter test-2:2 not found."
)
@mock_ssm @mock_ssm
def test_get_parameters_errors(): def test_get_parameters_errors():
client = boto3.client("ssm", region_name="us-east-1") client = boto3.client("ssm", region_name="us-east-1")