Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default region in ssmstore lookup. #595

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions stacker/lookups/handlers/ssmstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def handler(value, **kwargs):

[<region>@]ssmkey

Note: The region is optional, and defaults to us-east-1 if not given.
Note: The region is optional, we'll try to get it from the current
session, if that failed, it would fall back to us-east-1

For example:

Expand All @@ -36,7 +37,7 @@ def handler(value, **kwargs):
"""
value = read_value_from_path(value)

region = "us-east-1"
region = getattr(kwargs.get('provider'), 'region', 'us-east-1')
if "@" in value:
region, value = value.split("@", 1)

Expand Down
47 changes: 38 additions & 9 deletions stacker/tests/lookups/handlers/test_ssmstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

class TestSSMStoreHandler(unittest.TestCase):
client = boto3.client('ssm', region_name='us-east-1')
au_client = boto3.client('ssm', region_name='ap-southeast-2')

def setUp(self):
self.stubber = Stubber(self.client)
self.au_stubber = Stubber(self.au_client)
self.get_parameters_response = {
'Parameters': [
{
Expand All @@ -23,6 +25,18 @@ def setUp(self):
'invalidssmparam'
]
}
self.au_get_parameters_response = {
'Parameters': [
{
'Name': 'ssmkey',
'Type': 'String',
'Value': 'au_ssmvalue'
}
],
'InvalidParameters': [
'invalidssmparam'
]
}
self.invalid_get_parameters_response = {
'InvalidParameters': [
'ssmkey'
Expand All @@ -34,6 +48,7 @@ def setUp(self):
}
self.ssmkey = "ssmkey"
self.ssmvalue = "ssmvalue"
self.au_ssmvalue = "au_ssmvalue"

@mock.patch('stacker.lookups.handlers.ssmstore.get_session',
return_value=SessionStub(client))
Expand All @@ -58,13 +73,27 @@ def test_ssmstore_invalid_value_handler(self, mock_client):
assert True

@mock.patch('stacker.lookups.handlers.ssmstore.get_session',
return_value=SessionStub(client))
def test_ssmstore_handler_with_region(self, mock_client):
self.stubber.add_response('get_parameters',
self.get_parameters_response,
self.expected_params)
region = "us-east-1"
temp_value = "%s@%s" % (region, self.ssmkey)
with self.stubber:
return_value=SessionStub(au_client))
def test_ssmstore_handler_with_implicit_region(self, mock_client):
self.au_stubber.add_response(
'get_parameters',
self.au_get_parameters_response,
self.expected_params,
)
with self.au_stubber:
value = handler(self.ssmkey)
self.assertEqual(value, self.au_ssmvalue)

@mock.patch('stacker.lookups.handlers.ssmstore.get_session',
return_value=SessionStub(au_client))
def test_ssmstore_handler_with_explicit_region(self, mock_client):
self.au_stubber.add_response(
'get_parameters',
self.au_get_parameters_response,
self.expected_params,
)
region = "ap-southeast-2"
temp_value = "{}@{}".format(region, self.ssmkey)
with self.au_stubber:
value = handler(temp_value)
self.assertEqual(value, self.ssmvalue)
self.assertEqual(value, self.au_ssmvalue)