Skip to content

Commit 558e882

Browse files
authored
chore: adds publish data structures (#2564)
Adds data structures for new publish manifest change. Addresses #2550 By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent bb68802 commit 558e882

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

internal/pkg/manifest/workload.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,17 @@ type TaskConfig struct {
450450
Storage *Storage `yaml:"storage"`
451451
}
452452

453+
// PublishConfig represents the configurable options for setting up publishers.
454+
type PublishConfig struct {
455+
Topics []Topic `yaml:"topics"`
456+
}
457+
458+
// Topic represents the configurable options for setting up a SNS Topic.
459+
type Topic struct {
460+
Name *string `yaml:"name"`
461+
AllowedWorkers []string `yaml:"allowed_workers"`
462+
}
463+
453464
// NetworkConfig represents options for network connection to AWS resources within a VPC.
454465
type NetworkConfig struct {
455466
VPC *vpcConfig `yaml:"vpc"`

internal/pkg/manifest/workload_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,63 @@ func TestDependency_UnmarshalYAML(t *testing.T) {
635635
})
636636
}
637637
}
638+
639+
func TestUnmarshalPublish(t *testing.T) {
640+
testCases := map[string]struct {
641+
inContent string
642+
wantedPublish PublishConfig
643+
wantedErr error
644+
}{
645+
"Valid publish yaml": {
646+
inContent: `topics:
647+
- name: tests
648+
allowed_workers:
649+
- hello
650+
`,
651+
wantedPublish: PublishConfig{
652+
Topics: []Topic{
653+
{
654+
Name: aws.String("tests"),
655+
AllowedWorkers: []string{"hello"},
656+
},
657+
},
658+
},
659+
},
660+
"Empty workers don't appear in topic": {
661+
inContent: `topics:
662+
- name: tests
663+
`,
664+
wantedPublish: PublishConfig{
665+
Topics: []Topic{
666+
{
667+
Name: aws.String("tests"),
668+
},
669+
},
670+
},
671+
},
672+
"Error when unmarshalable": {
673+
inContent: `topics:
674+
- name: tests
675+
allowed_workers:
676+
- hello
677+
- name: orders
678+
`,
679+
wantedErr: errors.New("yaml: line 1: did not find expected '-' indicator"),
680+
},
681+
}
682+
683+
for name, tc := range testCases {
684+
t.Run(name, func(t *testing.T) {
685+
p := PublishConfig{}
686+
687+
err := yaml.Unmarshal([]byte(tc.inContent), &p)
688+
689+
if tc.wantedErr != nil {
690+
require.EqualError(t, err, tc.wantedErr.Error())
691+
} else {
692+
require.NoError(t, err)
693+
require.Equal(t, tc.wantedPublish, p)
694+
}
695+
})
696+
}
697+
}

internal/pkg/template/template_functions.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,38 @@ func generateMountPointJSON(mountPoints []*MountPoint) string {
131131
return string(out)
132132

133133
}
134+
135+
// generatePublisherJSON turns a list of Topics objects into a JSON string:
136+
// `{"myTopic": ["hello"], "mySecondTopic": ["hello","goodbye"]}`
137+
// This function must be called on an array of correctly constructed Topic objects.
138+
func generatePublishJSON(topics []*Topics) string {
139+
publisherMap := make(map[string][]string)
140+
141+
for _, pb := range topics {
142+
if aws.StringValue(pb.Name) == "" {
143+
continue
144+
}
145+
publisherMap[aws.StringValue(pb.Name)] = aws.StringValueSlice(pb.AllowedWorkers)
146+
}
147+
148+
out, ok := getJSONMap(publisherMap)
149+
if !ok {
150+
return "{}"
151+
}
152+
153+
return string(out)
154+
}
155+
156+
func getJSONMap(inMap map[string][]string) ([]byte, bool) {
157+
// Check for empty maps
158+
if len(inMap) == 0 {
159+
return nil, false
160+
}
161+
162+
out, err := json.Marshal(inMap)
163+
if err != nil {
164+
return nil, false
165+
}
166+
167+
return out, true
168+
}

internal/pkg/template/template_functions_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,18 @@ func TestGenerateMountPointJSON(t *testing.T) {
237237
require.Equal(t, "{}", generateMountPointJSON([]*MountPoint{}), "nil list of arguments should render ")
238238
require.Equal(t, "{}", generateMountPointJSON([]*MountPoint{{SourceVolume: aws.String("fromEFS")}}), "empty paths should not get injected")
239239
}
240+
241+
func TestGeneratePublishJSON(t *testing.T) {
242+
require.Equal(t, `{"tests":["testsWorker1","testsWorker2"]}`,
243+
generatePublishJSON(
244+
[]*Topics{
245+
{
246+
Name: aws.String("tests"),
247+
AllowedWorkers: aws.StringSlice([]string{"testsWorker1", "testsWorker2"}),
248+
},
249+
},
250+
), "JSON should render correctly")
251+
252+
require.Equal(t, `{"tests":[]}`, generatePublishJSON([]*Topics{{Name: aws.String("tests")}}), "Topics with no workers show empty list")
253+
require.Equal(t, "{}", generatePublishJSON([]*Topics{}), "nil list of arguments should render")
254+
}

internal/pkg/template/workload.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ type StateMachineOpts struct {
202202
Retries *int
203203
}
204204

205+
// PublishOpts holds configuration needed if the service has publishers.
206+
type PublishOpts struct {
207+
Topics []*Topics
208+
}
209+
210+
// Topics holds information needed to render a SNSTopic in a container definition.
211+
type Topics struct {
212+
Name *string
213+
AllowedWorkers []*string
214+
}
215+
205216
// NetworkOpts holds AWS networking configuration for the workloads.
206217
type NetworkOpts struct {
207218
AssignPublicIP string
@@ -237,6 +248,7 @@ type WorkloadOpts struct {
237248
DomainAlias string
238249
DockerLabels map[string]string
239250
DependsOn map[string]string
251+
Publish *PublishOpts
240252

241253
// Additional options for service templates.
242254
WorkloadType string
@@ -333,6 +345,7 @@ func withSvcParsingFuncs() ParseOption {
333345
"quoteSlice": QuotePSliceFunc,
334346
"randomUUID": randomUUIDFunc,
335347
"jsonMountPoints": generateMountPointJSON,
348+
"jsonPublishers": generatePublishJSON,
336349
"envControllerParams": envControllerParameters,
337350
})
338351
}

0 commit comments

Comments
 (0)