Skip to content

Commit

Permalink
chore: adds queue uri json generation (#2667)
Browse files Browse the repository at this point in the history
Adds generation of COPILOT_QUEUE_URIS with worker services.

Addresses #2550
  • Loading branch information
xar-tol authored Jul 29, 2021
1 parent e0a86c3 commit 8fb57ac
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/pkg/template/template_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
46 changes: 46 additions & 0 deletions internal/pkg/template/template_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
1 change: 1 addition & 0 deletions internal/pkg/template/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ func withSvcParsingFuncs() ParseOption {
"randomUUID": randomUUIDFunc,
"jsonMountPoints": generateMountPointJSON,
"jsonSNSTopics": generateSNSJSON,
"jsonQueueURIs": generateQueueURIJSON,
"envControllerParams": envControllerParameters,
"logicalIDSafe": StripNonAlphaNumFunc,
})
Expand Down
9 changes: 9 additions & 0 deletions templates/workloads/partials/cf/envvars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8fb57ac

Please sign in to comment.