Skip to content

Commit 8fb57ac

Browse files
authored
chore: adds queue uri json generation (aws#2667)
Adds generation of COPILOT_QUEUE_URIS with worker services. Addresses aws#2550
1 parent e0a86c3 commit 8fb57ac

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

internal/pkg/template/template_functions.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,36 @@ func generateSNSJSON(topics []*Topic) string {
154154
return string(out)
155155
}
156156

157+
// generateQueueURIJSON turns a list of Topic Subscription objects into a JSON string of their corresponding queues:
158+
// `{"eventsQueue": "${mainURL}", "svcTopicEventsQueue": "${svctopicURL}"}`
159+
// This function must be called on an array of correctly constructed Topic objects.
160+
func generateQueueURIJSON(ts []*TopicSubscription) string {
161+
if ts == nil {
162+
return ""
163+
}
164+
urlMap := make(map[string]string)
165+
urlMap["eventsQueue"] = "${mainURL}"
166+
167+
for _, sub := range ts {
168+
// TopicSubscriptions with no name, service, or queue will not be included in the json
169+
if sub.Name == nil || sub.Service == nil || sub.Queue == nil {
170+
continue
171+
}
172+
svc := StripNonAlphaNumFunc(aws.StringValue(sub.Service))
173+
topicName := StripNonAlphaNumFunc(aws.StringValue(sub.Name))
174+
subName := fmt.Sprintf("%s%sEventsQueue", svc, strings.Title(topicName))
175+
176+
urlMap[subName] = fmt.Sprintf("${%s%sURL}", svc, topicName)
177+
}
178+
179+
out, ok := getJSONMap(urlMap)
180+
if !ok {
181+
return "{}"
182+
}
183+
184+
return string(out)
185+
}
186+
157187
func getJSONMap(inMap map[string]string) ([]byte, bool) {
158188
// Check for empty maps
159189
if len(inMap) == 0 {

internal/pkg/template/template_functions_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,49 @@ func TestGenerateSNSJSON(t *testing.T) {
282282
})
283283
}
284284
}
285+
286+
func TestGenerateQueueURIJSON(t *testing.T) {
287+
testCases := map[string]struct {
288+
in []*TopicSubscription
289+
wanted string
290+
wantedSubstring string
291+
wantedSubstring2 string
292+
}{
293+
"JSON should render correctly": {
294+
in: []*TopicSubscription{
295+
{
296+
Name: aws.String("tests"),
297+
Service: aws.String("bestsvc"),
298+
Queue: &SQSQueue{
299+
Delay: aws.Int64(5),
300+
},
301+
},
302+
},
303+
wantedSubstring: `"eventsQueue":"${mainURL}"`,
304+
wantedSubstring2: `"bestsvcTestsEventsQueue":"${bestsvctestsURL}"`,
305+
},
306+
"Topics with no names show empty but main queue still populates": {
307+
in: []*TopicSubscription{
308+
{
309+
Service: aws.String("bestSvc"),
310+
},
311+
},
312+
wanted: `{"eventsQueue":"${mainURL}"}`,
313+
},
314+
"nil list of arguments should render with main queue": {
315+
in: []*TopicSubscription{},
316+
wanted: `{"eventsQueue":"${mainURL}"}`,
317+
},
318+
}
319+
320+
for name, tc := range testCases {
321+
t.Run(name, func(t *testing.T) {
322+
if tc.wanted != "" {
323+
require.Equal(t, generateQueueURIJSON(tc.in), tc.wanted)
324+
} else {
325+
require.Contains(t, generateQueueURIJSON(tc.in), tc.wantedSubstring)
326+
require.Contains(t, generateQueueURIJSON(tc.in), tc.wantedSubstring2)
327+
}
328+
})
329+
}
330+
}

internal/pkg/template/workload.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ func withSvcParsingFuncs() ParseOption {
421421
"randomUUID": randomUUIDFunc,
422422
"jsonMountPoints": generateMountPointJSON,
423423
"jsonSNSTopics": generateSNSJSON,
424+
"jsonQueueURIs": generateQueueURIJSON,
424425
"envControllerParams": envControllerParameters,
425426
"logicalIDSafe": StripNonAlphaNumFunc,
426427
})

templates/workloads/partials/cf/envvars.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ Environment:
2323
- Name: COPILOT_SNS_TOPIC_ARNS
2424
Value: '{{jsonSNSTopics .Publish.Topics}}'
2525
{{- end}}{{- end}}
26+
{{- if .Subscribe}}{{- if .Subscribe.Topics}}
27+
- Name: COPILOT_QUEUE_URIS
28+
Value: !Sub
29+
- '{{jsonQueueURIs .Subscribe.Topics}}'
30+
- mainURL: !Ref EventsQueue
31+
{{- range $topic := .Subscribe.Topics}}{{- if and $topic.Queue $topic.Service $topic.Name}}
32+
{{logicalIDSafe $topic.Service}}{{logicalIDSafe $topic.Name}}URL: !Ref {{logicalIDSafe $topic.Service}}{{logicalIDSafe $topic.Name}}EventsQueue
33+
{{- end}}{{- end}}
34+
{{- end}}{{- end}}
2635
{{- if eq .WorkloadType "Load Balanced Web Service"}}
2736
- Name: COPILOT_LB_DNS
2837
Value: !GetAtt EnvControllerAction.PublicLoadBalancerDNSName

0 commit comments

Comments
 (0)