This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
/
cft.go
233 lines (197 loc) · 9.22 KB
/
cft.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// >>>>>>> DO NOT EDIT THIS FILE <<<<<<<<<<
// This file is autogenerated via `aws-operator-codegen process`
// If you'd like the change anything about this file make edits to the .templ
// file in the pkg/codegen/assets directory.
package snssubscription
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
awsV1alpha1 "github.com/awslabs/aws-service-operator/pkg/apis/service-operator.aws/v1alpha1"
"github.com/awslabs/aws-service-operator/pkg/config"
"github.com/awslabs/aws-service-operator/pkg/helpers"
)
// New generates a new object
func New(config config.Config, snssubscription *awsV1alpha1.SNSSubscription, topicARN string) *Cloudformation {
return &Cloudformation{
SNSSubscription: snssubscription,
config: config,
topicARN: topicARN,
}
}
// Cloudformation defines the snssubscription cfts
type Cloudformation struct {
config config.Config
SNSSubscription *awsV1alpha1.SNSSubscription
topicARN string
}
// StackName returns the name of the stack based on the aws-operator-config
func (s *Cloudformation) StackName() string {
return helpers.StackName(s.config.ClusterName, "snssubscription", s.SNSSubscription.Name, s.SNSSubscription.Namespace)
}
// GetOutputs return the stack outputs from the DescribeStacks call
func (s *Cloudformation) GetOutputs() (map[string]string, error) {
outputs := map[string]string{}
sess := s.config.AWSSession
svc := cloudformation.New(sess)
stackInputs := cloudformation.DescribeStacksInput{
StackName: aws.String(s.StackName()),
}
output, err := svc.DescribeStacks(&stackInputs)
if err != nil {
return nil, err
}
// Not sure if this is even possible
if len(output.Stacks) != 1 {
return nil, errors.New("no stacks returned with that stack name")
}
for _, out := range output.Stacks[0].Outputs {
outputs[*out.OutputKey] = *out.OutputValue
}
return outputs, err
}
// CreateStack will create the stack with the supplied params
func (s *Cloudformation) CreateStack() (output *cloudformation.CreateStackOutput, err error) {
sess := s.config.AWSSession
svc := cloudformation.New(sess)
cftemplate := helpers.GetCloudFormationTemplate(s.config, "snssubscription", s.SNSSubscription.Spec.CloudFormationTemplateName, s.SNSSubscription.Spec.CloudFormationTemplateNamespace)
stackInputs := cloudformation.CreateStackInput{
StackName: aws.String(s.StackName()),
TemplateURL: aws.String(cftemplate),
NotificationARNs: []*string{
aws.String(s.topicARN),
},
}
resourceName := helpers.CreateParam("ResourceName", s.SNSSubscription.Name)
resourceVersion := helpers.CreateParam("ResourceVersion", s.SNSSubscription.ResourceVersion)
namespace := helpers.CreateParam("Namespace", s.SNSSubscription.Namespace)
clusterName := helpers.CreateParam("ClusterName", s.config.ClusterName)
topicNameTemp := "{{(call .Helpers.GetSNSTopicByName .Config .Obj.Spec.TopicName .Obj.Namespace).Output.TopicARN}}"
topicNameValue, err := helpers.Templatize(topicNameTemp, helpers.Data{Obj: s.SNSSubscription, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
topicName := helpers.CreateParam("TopicARN", helpers.Stringify(topicNameValue))
protocolTemp := "{{.Obj.Spec.Protocol}}"
protocolValue, err := helpers.Templatize(protocolTemp, helpers.Data{Obj: s.SNSSubscription, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
protocol := helpers.CreateParam("Protocol", helpers.Stringify(protocolValue))
endpointTemp := "{{if (eq .Obj.Spec.Protocol \"sqs\")}}{{(call .Helpers.GetSQSQueueByName .Config .Obj.Spec.Endpoint .Obj.Namespace).Output.QueueARN }}{{else}}{{.Obj.Spec.Endpoint}}{{end}}"
endpointValue, err := helpers.Templatize(endpointTemp, helpers.Data{Obj: s.SNSSubscription, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
endpoint := helpers.CreateParam("Endpoint", helpers.Stringify(endpointValue))
queueURLTemp := "{{(call .Helpers.GetSQSQueueByName .Config .Obj.Spec.Endpoint .Obj.Namespace).Output.QueueURL }}"
queueURLValue, err := helpers.Templatize(queueURLTemp, helpers.Data{Obj: s.SNSSubscription, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
queueURL := helpers.CreateParam("QueueURL", helpers.Stringify(queueURLValue))
parameters := []*cloudformation.Parameter{}
parameters = append(parameters, resourceName)
parameters = append(parameters, resourceVersion)
parameters = append(parameters, namespace)
parameters = append(parameters, clusterName)
parameters = append(parameters, topicName)
parameters = append(parameters, protocol)
parameters = append(parameters, endpoint)
parameters = append(parameters, queueURL)
stackInputs.SetParameters(parameters)
resourceNameTag := helpers.CreateTag("ResourceName", s.SNSSubscription.Name)
resourceVersionTag := helpers.CreateTag("ResourceVersion", s.SNSSubscription.ResourceVersion)
namespaceTag := helpers.CreateTag("Namespace", s.SNSSubscription.Namespace)
clusterNameTag := helpers.CreateTag("ClusterName", s.config.ClusterName)
tags := []*cloudformation.Tag{}
tags = append(tags, resourceNameTag)
tags = append(tags, resourceVersionTag)
tags = append(tags, namespaceTag)
tags = append(tags, clusterNameTag)
stackInputs.SetTags(tags)
output, err = svc.CreateStack(&stackInputs)
return
}
// UpdateStack will update the existing stack
func (s *Cloudformation) UpdateStack(updated *awsV1alpha1.SNSSubscription) (output *cloudformation.UpdateStackOutput, err error) {
sess := s.config.AWSSession
svc := cloudformation.New(sess)
cftemplate := helpers.GetCloudFormationTemplate(s.config, "snssubscription", updated.Spec.CloudFormationTemplateName, updated.Spec.CloudFormationTemplateNamespace)
stackInputs := cloudformation.UpdateStackInput{
StackName: aws.String(s.StackName()),
TemplateURL: aws.String(cftemplate),
NotificationARNs: []*string{
aws.String(s.topicARN),
},
}
resourceName := helpers.CreateParam("ResourceName", s.SNSSubscription.Name)
resourceVersion := helpers.CreateParam("ResourceVersion", s.SNSSubscription.ResourceVersion)
namespace := helpers.CreateParam("Namespace", s.SNSSubscription.Namespace)
clusterName := helpers.CreateParam("ClusterName", s.config.ClusterName)
topicNameTemp := "{{(call .Helpers.GetSNSTopicByName .Config .Obj.Spec.TopicName .Obj.Namespace).Output.TopicARN}}"
topicNameValue, err := helpers.Templatize(topicNameTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
topicName := helpers.CreateParam("TopicARN", helpers.Stringify(topicNameValue))
protocolTemp := "{{.Obj.Spec.Protocol}}"
protocolValue, err := helpers.Templatize(protocolTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
protocol := helpers.CreateParam("Protocol", helpers.Stringify(protocolValue))
endpointTemp := "{{if (eq .Obj.Spec.Protocol \"sqs\")}}{{(call .Helpers.GetSQSQueueByName .Config .Obj.Spec.Endpoint .Obj.Namespace).Output.QueueARN }}{{else}}{{.Obj.Spec.Endpoint}}{{end}}"
endpointValue, err := helpers.Templatize(endpointTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
endpoint := helpers.CreateParam("Endpoint", helpers.Stringify(endpointValue))
queueURLTemp := "{{(call .Helpers.GetSQSQueueByName .Config .Obj.Spec.Endpoint .Obj.Namespace).Output.QueueURL }}"
queueURLValue, err := helpers.Templatize(queueURLTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
queueURL := helpers.CreateParam("QueueURL", helpers.Stringify(queueURLValue))
parameters := []*cloudformation.Parameter{}
parameters = append(parameters, resourceName)
parameters = append(parameters, resourceVersion)
parameters = append(parameters, namespace)
parameters = append(parameters, clusterName)
parameters = append(parameters, topicName)
parameters = append(parameters, protocol)
parameters = append(parameters, endpoint)
parameters = append(parameters, queueURL)
stackInputs.SetParameters(parameters)
resourceNameTag := helpers.CreateTag("ResourceName", s.SNSSubscription.Name)
resourceVersionTag := helpers.CreateTag("ResourceVersion", s.SNSSubscription.ResourceVersion)
namespaceTag := helpers.CreateTag("Namespace", s.SNSSubscription.Namespace)
clusterNameTag := helpers.CreateTag("ClusterName", s.config.ClusterName)
tags := []*cloudformation.Tag{}
tags = append(tags, resourceNameTag)
tags = append(tags, resourceVersionTag)
tags = append(tags, namespaceTag)
tags = append(tags, clusterNameTag)
stackInputs.SetTags(tags)
output, err = svc.UpdateStack(&stackInputs)
return
}
// DeleteStack will delete the stack
func (s *Cloudformation) DeleteStack() (err error) {
sess := s.config.AWSSession
svc := cloudformation.New(sess)
stackInputs := cloudformation.DeleteStackInput{}
stackInputs.SetStackName(s.StackName())
_, err = svc.DeleteStack(&stackInputs)
return
}
// WaitUntilStackDeleted will delete the stack
func (s *Cloudformation) WaitUntilStackDeleted() (err error) {
sess := s.config.AWSSession
svc := cloudformation.New(sess)
stackInputs := cloudformation.DescribeStacksInput{
StackName: aws.String(s.StackName()),
}
err = svc.WaitUntilStackDeleteComplete(&stackInputs)
return
}