Skip to content

Commit

Permalink
Fix default_scm_codecommit_account_id and put/delete parameter paths
Browse files Browse the repository at this point in the history
  • Loading branch information
sbkok committed Apr 3, 2024
1 parent fd1ecad commit 1e3f81d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def lambda_handler(event, _):
try:
parameter_store = ParameterStore(DEPLOYMENT_ACCOUNT_REGION, boto3)
default_scm_codecommit_account_id = parameter_store.fetch_parameter(
"/adf/scm/default-scm-codecommit-account-id",
"scm/default-scm-codecommit-account-id",
)
except ParameterNotFoundError:
default_scm_codecommit_account_id = deployment_account_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def fetch_required_ssm_params(pipeline_input, regions):
"default_scm_branch",
)
output["default_scm_codecommit_account_id"] = parameter_store.fetch_parameter(
"/adf/scm/default-scm-codecommit-account-id",
"scm/default-scm-codecommit-account-id",
)
codestar_connection_path = (
pipeline_input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def prepare_deployment_account(sts, deployment_account_id, config):
)
)
deployment_account_parameter_store.put_parameter(
'/adf/scm/default-scm-codecommit-account-id',
'scm/default-scm-codecommit-account-id',
(
config.config
.get('scm', {})
Expand Down Expand Up @@ -293,7 +293,7 @@ def worker_thread(

# Ensuring the stage parameter on the target account is up-to-date
parameter_store.put_parameter(
'/adf/org/stage',
'org/stage',
config.config.get('org', {}).get(
'stage',
ADF_DEFAULT_ORG_STAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, **kwargs):
ADF_DEFAULT_SCM_FALLBACK_BRANCH,
)
self.default_scm_codecommit_account_id = self.map_params.get(
"/adf/scm/default_scm_codecommit_account_id",
"scm/default_scm_codecommit_account_id",
ADF_DEFAULT_SCM_CODECOMMIT_ACCOUNT_ID,
)
self.configuration = self._generate_configuration()
Expand Down Expand Up @@ -759,7 +759,7 @@ def __init__(
ADF_DEFAULT_SCM_FALLBACK_BRANCH,
)
self.default_scm_codecommit_account_id = map_params.get(
"/adf/scm/default_scm_codecommit_account_id",
"scm/default_scm_codecommit_account_id",
ADF_DEFAULT_SCM_CODECOMMIT_ACCOUNT_ID,
)
self.cfn = _codepipeline.CfnPipeline(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@ def put_parameter(self, name, value, tier='Standard'):
current_value = self.fetch_parameter(name)
assert current_value == value
LOGGER.debug(
'No need to update parameter %s/%s with value %s since they '
'No need to update parameter %s with value %s since they '
'are the same',
PARAMETER_PREFIX,
name,
ParameterStore._build_param_name(name),
value,
)
except (ParameterNotFoundError, AssertionError):
param_name = ParameterStore._build_param_name(name)
LOGGER.debug(
'Putting SSM Parameter %s/%s with value %s',
PARAMETER_PREFIX,
name,
'Putting SSM Parameter %s with value %s',
param_name,
value,
)
self.client.put_parameter(
Name=f"{PARAMETER_PREFIX}/{name}",
Name=param_name,
Description=PARAMETER_DESCRIPTION,
Value=value,
Type='String',
Expand All @@ -58,45 +57,49 @@ def put_parameter(self, name, value, tier='Standard'):
)

def delete_parameter(self, name):
param_name = ParameterStore._build_param_name(name)
try:
LOGGER.debug('Deleting Parameter %s', name)
LOGGER.debug('Deleting Parameter %s', param_name)
self.client.delete_parameter(
Name=f"{PARAMETER_PREFIX}/{name}",
Name=param_name,
)
except self.client.exceptions.ParameterNotFound:
LOGGER.debug(
'Attempted to delete Parameter %s/%s but it was not found',
PARAMETER_PREFIX,
name,
'Attempted to delete Parameter %s but it was not found',
param_name,
)

def fetch_parameters_by_path(self, path):
"""Gets a Parameter(s) by Path from Parameter Store (Recursively)
"""
param_path = ParameterStore._build_param_name(path)
try:
LOGGER.debug(
'Fetching Parameters from path %s/%s',
PARAMETER_PREFIX,
path,
'Fetching Parameters from path %s',
param_path,
)
return paginator(
self.client.get_parameters_by_path,
Path=f"{PARAMETER_PREFIX}/{path}",
Path=param_path,
Recursive=True,
WithDecryption=False
)
except self.client.exceptions.ParameterNotFound as error:
raise ParameterNotFoundError(
f'Parameter Path {PARAMETER_PREFIX}/{path} Not Found',
f'Parameter Path {param_path} Not Found',
) from error

@staticmethod
def _build_param_name(name, adf_only=True):
param_prefix = PARAMETER_PREFIX if adf_only else '/'
prefix_seperator = '' if name.startswith('/') else '/'
return f"{param_prefix}{prefix_seperator}{name}"

def fetch_parameter(self, name, with_decryption=False, adf_only=True):
"""Gets a Parameter from Parameter Store (Returns the Value)
"""
param_name = ParameterStore._build_param_name(name, adf_only)
try:
param_prefix = PARAMETER_PREFIX if adf_only else '/'
prefix_seperator = '' if name.startswith('/') else '/'
param_name = f"{param_prefix}{prefix_seperator}{name}"
LOGGER.debug(
'Fetching Parameter %s',
param_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_prepare_deployment_account_defaults(param_store_cls, cls, sts):
)
for param_store in parameter_store_list:
assert param_store.put_parameter.call_count == (
13 if param_store == deploy_param_store else 8
14 if param_store == deploy_param_store else 8
)
param_store.put_parameter.assert_has_calls(
[
Expand All @@ -154,6 +154,10 @@ def test_prepare_deployment_account_defaults(param_store_cls, cls, sts):
deploy_param_store.put_parameter.assert_has_calls(
[
call('default_scm_branch', 'main'),
call(
'scm/default-scm-codecommit-account-id',
deployment_account_id,
),
call('deployment_maps/allow_empty_target', 'False'),
call('org/stage', 'none'),
call('notification_type', 'email'),
Expand Down Expand Up @@ -230,7 +234,7 @@ def test_prepare_deployment_account_specific_config(param_store_cls, cls, sts):
)
for param_store in parameter_store_list:
assert param_store.put_parameter.call_count == (
15 if param_store == deploy_param_store else 8
16 if param_store == deploy_param_store else 8
)
param_store.put_parameter.assert_has_calls(
[
Expand All @@ -252,6 +256,10 @@ def test_prepare_deployment_account_specific_config(param_store_cls, cls, sts):
[
call('auto_create_repositories', 'disabled'),
call('default_scm_branch', 'main'),
call(
'scm/default-scm-codecommit-account-id',
deployment_account_id,
),
call('deployment_maps/allow_empty_target', 'False'),
call('org/stage', 'test-stage'),
call('notification_type', 'slack'),
Expand Down

0 comments on commit 1e3f81d

Please sign in to comment.