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

fix(storage): Use template functions for LogicalID and Env Var names #1035

Merged
merged 10 commits into from
Jun 20, 2020

Conversation

bvtujo
Copy link
Contributor

@bvtujo bvtujo commented Jun 19, 2020

This PR adds template functions to the storage resource CF templates. We now
only need to specify a resource name to the DynamoDB and S3 types, and
template functions StorageLogicalIDSafe and EnvVarName will properly construct
CF resource names and Env Vars.

This PR also reorganizes and renames some common go template functions that
previously lived in the "deploy/cloudformation/stack" package, moving them to the
"template" package.

Finally, it changes the implementation of ToSnakeCase to better match StringsWithUPPERCase
(now renders as "STRINGS_WITH_UPPER_CASE" instead of "STRINGS_WITH_U_P_P_E_R_CASE")

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

bvtujo added 2 commits June 18, 2020 15:41
* Move all functions used in go templates to the template package.
* Move functions only used in one file from deploy/cloudformation/
  stack/template_functions.go to deploy/cloudformation/stack/svc.go
* Rename LogicalIDSafe used in mft/cf to ReplaceDashes (as the behavior
  is quite different from the LogicalIDSafe function used for storage
  environment variables)
	modified:   internal/pkg/deploy/cloudformation/stack/app.go
@bvtujo bvtujo requested a review from efekarakus June 19, 2020 00:21
@bvtujo bvtujo marked this pull request as ready for review June 19, 2020 17:41
@bvtujo bvtujo requested a review from a team as a code owner June 19, 2020 17:41
Copy link
Contributor

@efekarakus efekarakus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! It makes a lot of sense to move all these functions to the template pkg thank you so much, should we add tests for them now that they're exported?

@@ -106,3 +104,8 @@ func NewS3(input *S3Props) *S3 {
parser: template.New(),
}
}

var storageTemplateFunctions = map[string]interface{}{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would you mind moving the global variable to the top of the file? I don't know if there is official guidance on it but I think they tend to be written that way

// sanitizes it by removing "-" characters (not allowed)
// and replacing them with "DASH" (allowed by CloudFormation but
// not permitted in ecs-cli generated resource names).
func ReplaceDashes(logicalID string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of ending all of these function names with Func
so that it reads template.ReplaceDashesFunc to denote that they are template functions vs. regular functions?

var nonAlphaNum = regexp.MustCompile("[^a-zA-Z0-9]+")

// StorageLogicalIDSafe strips non-alphanumeric characters from an input string.
func StorageLogicalIDSafe(s string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we name this just:

Suggested change
func StorageLogicalIDSafe(s string) string {
func LogicalIDSafe(s string) string {


// EnvVarName converts an input resource name to LogicalIDSafe, then appends
// "Name" to the end. When generating environment variables, this string
// is then passed through the "toSnakeCase" method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove that last sentence? Technically users of this function don't have to pass it to ToSnakeCase

When generating environment variables, this string is then passed through the "toSnakeCase" method

bvtujo added 2 commits June 19, 2020 14:51
Also addresses feedback: moves templateFuncs to top of file, renames
StorageLogicalIDSafe to LogicalIDSafe
@bvtujo bvtujo requested review from iamhopaul123 and seongm-1 June 19, 2020 23:01
Copy link
Contributor

@efekarakus efekarakus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thank you so much!


// LogicalIDSafeFunc strips non-alphanumeric characters from an input string.
func LogicalIDSafeFunc(s string) string {
return nonAlphaNum.ReplaceAllString(s, "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to call ReplaceDashesFunc to make a string logical id safe?

Copy link
Contributor Author

@bvtujo bvtujo Jun 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReplaceDashesFunc is used when writing the service cloudformation and has slightly different behavior than the LogicalIDSafeFunc, which is used when writing addons templates.

Originally, ReplaceDashesFunc was the only LogicalIDSafe method we needed, as we only needed to turn service names into CF logical IDs. Service names can only include lowercase letters, numbers, and dashes, so for example it would turn my-cool-frontend into myDASHcoolDASHfrontend for the service CF logical IDs which related to that service.

We also needed to be able to recover the name of the service from the logical ID in methods in internal/pkg/deploy/cloudformation/stack used in calls to pipeline update (this method is buried deep inside a call to GetRegionalAppResources and part of that is extracting original IDs from logical IDs for ECR repo URL generation).

With the addition of storage init, though, we needed to be able to sanitize more complex strings into logical IDs, as DDB table names can include uppercase, lowercase, numbers, and ._- characters. This is why I added LogicalIDSafeFunc, to handle this more complex parsing, as it simply removes non-alphanumeric characters from the string and results in simpler env var names injected into the container.

Does that make sense?

Copy link
Contributor

@iamhopaul123 iamhopaul123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome PR :shipit:

@efekarakus efekarakus added the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Jun 19, 2020
@bvtujo bvtujo removed the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Jun 20, 2020
@mergify mergify bot merged commit 7cb56c0 into aws:master Jun 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants