From 8fb57aca1cd7a355bbdf1b8a5c5fc590b40649f5 Mon Sep 17 00:00:00 2001 From: Alexandra French Date: Thu, 29 Jul 2021 14:42:55 -0400 Subject: [PATCH] chore: adds queue uri json generation (#2667) Adds generation of COPILOT_QUEUE_URIS with worker services. Addresses #2550 --- internal/pkg/template/template_functions.go | 30 ++++++++++++ .../pkg/template/template_functions_test.go | 46 +++++++++++++++++++ internal/pkg/template/workload.go | 1 + templates/workloads/partials/cf/envvars.yml | 9 ++++ 4 files changed, 86 insertions(+) diff --git a/internal/pkg/template/template_functions.go b/internal/pkg/template/template_functions.go index 95fc60eb963..d8ea5b8e4b4 100644 --- a/internal/pkg/template/template_functions.go +++ b/internal/pkg/template/template_functions.go @@ -154,6 +154,36 @@ func generateSNSJSON(topics []*Topic) string { return string(out) } +// generateQueueURIJSON turns a list of Topic Subscription objects into a JSON string of their corresponding queues: +// `{"eventsQueue": "${mainURL}", "svcTopicEventsQueue": "${svctopicURL}"}` +// This function must be called on an array of correctly constructed Topic objects. +func generateQueueURIJSON(ts []*TopicSubscription) string { + if ts == nil { + return "" + } + urlMap := make(map[string]string) + urlMap["eventsQueue"] = "${mainURL}" + + for _, sub := range ts { + // TopicSubscriptions with no name, service, or queue will not be included in the json + if sub.Name == nil || sub.Service == nil || sub.Queue == nil { + continue + } + svc := StripNonAlphaNumFunc(aws.StringValue(sub.Service)) + topicName := StripNonAlphaNumFunc(aws.StringValue(sub.Name)) + subName := fmt.Sprintf("%s%sEventsQueue", svc, strings.Title(topicName)) + + urlMap[subName] = fmt.Sprintf("${%s%sURL}", svc, topicName) + } + + out, ok := getJSONMap(urlMap) + if !ok { + return "{}" + } + + return string(out) +} + func getJSONMap(inMap map[string]string) ([]byte, bool) { // Check for empty maps if len(inMap) == 0 { diff --git a/internal/pkg/template/template_functions_test.go b/internal/pkg/template/template_functions_test.go index 91469d73d66..46931039b11 100644 --- a/internal/pkg/template/template_functions_test.go +++ b/internal/pkg/template/template_functions_test.go @@ -282,3 +282,49 @@ func TestGenerateSNSJSON(t *testing.T) { }) } } + +func TestGenerateQueueURIJSON(t *testing.T) { + testCases := map[string]struct { + in []*TopicSubscription + wanted string + wantedSubstring string + wantedSubstring2 string + }{ + "JSON should render correctly": { + in: []*TopicSubscription{ + { + Name: aws.String("tests"), + Service: aws.String("bestsvc"), + Queue: &SQSQueue{ + Delay: aws.Int64(5), + }, + }, + }, + wantedSubstring: `"eventsQueue":"${mainURL}"`, + wantedSubstring2: `"bestsvcTestsEventsQueue":"${bestsvctestsURL}"`, + }, + "Topics with no names show empty but main queue still populates": { + in: []*TopicSubscription{ + { + Service: aws.String("bestSvc"), + }, + }, + wanted: `{"eventsQueue":"${mainURL}"}`, + }, + "nil list of arguments should render with main queue": { + in: []*TopicSubscription{}, + wanted: `{"eventsQueue":"${mainURL}"}`, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + if tc.wanted != "" { + require.Equal(t, generateQueueURIJSON(tc.in), tc.wanted) + } else { + require.Contains(t, generateQueueURIJSON(tc.in), tc.wantedSubstring) + require.Contains(t, generateQueueURIJSON(tc.in), tc.wantedSubstring2) + } + }) + } +} diff --git a/internal/pkg/template/workload.go b/internal/pkg/template/workload.go index a4c30ce135c..158778e90b8 100644 --- a/internal/pkg/template/workload.go +++ b/internal/pkg/template/workload.go @@ -421,6 +421,7 @@ func withSvcParsingFuncs() ParseOption { "randomUUID": randomUUIDFunc, "jsonMountPoints": generateMountPointJSON, "jsonSNSTopics": generateSNSJSON, + "jsonQueueURIs": generateQueueURIJSON, "envControllerParams": envControllerParameters, "logicalIDSafe": StripNonAlphaNumFunc, }) diff --git a/templates/workloads/partials/cf/envvars.yml b/templates/workloads/partials/cf/envvars.yml index 121b5539c77..09e96535c6e 100644 --- a/templates/workloads/partials/cf/envvars.yml +++ b/templates/workloads/partials/cf/envvars.yml @@ -23,6 +23,15 @@ Environment: - Name: COPILOT_SNS_TOPIC_ARNS Value: '{{jsonSNSTopics .Publish.Topics}}' {{- end}}{{- end}} +{{- if .Subscribe}}{{- if .Subscribe.Topics}} +- Name: COPILOT_QUEUE_URIS + Value: !Sub + - '{{jsonQueueURIs .Subscribe.Topics}}' + - mainURL: !Ref EventsQueue + {{- range $topic := .Subscribe.Topics}}{{- if and $topic.Queue $topic.Service $topic.Name}} + {{logicalIDSafe $topic.Service}}{{logicalIDSafe $topic.Name}}URL: !Ref {{logicalIDSafe $topic.Service}}{{logicalIDSafe $topic.Name}}EventsQueue + {{- end}}{{- end}} +{{- end}}{{- end}} {{- if eq .WorkloadType "Load Balanced Web Service"}} - Name: COPILOT_LB_DNS Value: !GetAtt EnvControllerAction.PublicLoadBalancerDNSName