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