-
Notifications
You must be signed in to change notification settings - Fork 18
/
sdk.go
392 lines (354 loc) · 12.4 KB
/
sdk.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// Code generated by ack-generate. DO NOT EDIT.
package platform_endpoint
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
"github.com/aws/aws-sdk-go/aws"
svcsdk "github.com/aws/aws-sdk-go/service/sns"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
svcapitypes "github.com/aws-controllers-k8s/sns-controller/apis/v1alpha1"
)
// Hack to avoid import errors during build...
var (
_ = &metav1.Time{}
_ = strings.ToLower("")
_ = &aws.JSONValue{}
_ = &svcsdk.SNS{}
_ = &svcapitypes.PlatformEndpoint{}
_ = ackv1alpha1.AWSAccountID("")
_ = &ackerr.NotFound
_ = &ackcondition.NotManagedMessage
_ = &reflect.Value{}
_ = fmt.Sprintf("")
_ = &ackrequeue.NoRequeue{}
)
// sdkFind returns SDK-specific information about a supplied resource
func (rm *resourceManager) sdkFind(
ctx context.Context,
r *resource,
) (latest *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.sdkFind")
defer func() {
exit(err)
}()
// If any required fields in the input shape are missing, AWS resource is
// not created yet. Return NotFound here to indicate to callers that the
// resource isn't yet created.
if rm.requiredFieldsMissingFromGetAttributesInput(r) {
return nil, ackerr.NotFound
}
input, err := rm.newGetAttributesRequestPayload(r)
if err != nil {
return nil, err
}
var resp *svcsdk.GetEndpointAttributesOutput
resp, err = rm.sdkapi.GetEndpointAttributesWithContext(ctx, input)
rm.metrics.RecordAPICall("GET_ATTRIBUTES", "GetEndpointAttributes", err)
if err != nil {
if awsErr, ok := ackerr.AWSError(err); ok && awsErr.Code() == "NotFound" {
return nil, ackerr.NotFound
}
return nil, err
}
// Merge in the information we read from the API call above to the copy of
// the original Kubernetes object we passed to the function
ko := r.ko.DeepCopy()
ko.Spec.CustomUserData = resp.Attributes["CustomUserData"]
ko.Spec.Enabled = resp.Attributes["Enabled"]
ko.Spec.Token = resp.Attributes["Token"]
rm.setStatusDefaults(ko)
return &resource{ko}, nil
}
// requiredFieldsMissingFromGetAtttributesInput returns true if there are any
// fields for the GetAttributes Input shape that are required by not present in
// the resource's Spec or Status
func (rm *resourceManager) requiredFieldsMissingFromGetAttributesInput(
r *resource,
) bool {
return r.ko.Status.EndpointARN == nil
}
// newGetAttributesRequestPayload returns SDK-specific struct for the HTTP
// request payload of the GetAttributes API call for the resource
func (rm *resourceManager) newGetAttributesRequestPayload(
r *resource,
) (*svcsdk.GetEndpointAttributesInput, error) {
res := &svcsdk.GetEndpointAttributesInput{}
if r.ko.Status.EndpointARN != nil {
res.SetEndpointArn(*r.ko.Status.EndpointARN)
}
return res, nil
}
// sdkCreate creates the supplied resource in the backend AWS service API and
// returns a copy of the resource with resource fields (in both Spec and
// Status) filled in with values from the CREATE API operation's Output shape.
func (rm *resourceManager) sdkCreate(
ctx context.Context,
desired *resource,
) (created *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.sdkCreate")
defer func() {
exit(err)
}()
input, err := rm.newCreateRequestPayload(ctx, desired)
if err != nil {
return nil, err
}
var resp *svcsdk.CreatePlatformEndpointOutput
_ = resp
resp, err = rm.sdkapi.CreatePlatformEndpointWithContext(ctx, input)
rm.metrics.RecordAPICall("CREATE", "CreatePlatformEndpoint", err)
if err != nil {
return nil, err
}
// Merge in the information we read from the API call above to the copy of
// the original Kubernetes object we passed to the function
ko := desired.ko.DeepCopy()
if resp.EndpointArn != nil {
ko.Status.EndpointARN = resp.EndpointArn
} else {
ko.Status.EndpointARN = nil
}
rm.setStatusDefaults(ko)
return &resource{ko}, nil
}
// newCreateRequestPayload returns an SDK-specific struct for the HTTP request
// payload of the Create API call for the resource
func (rm *resourceManager) newCreateRequestPayload(
ctx context.Context,
r *resource,
) (*svcsdk.CreatePlatformEndpointInput, error) {
res := &svcsdk.CreatePlatformEndpointInput{}
attrMap := map[string]*string{}
if r.ko.Spec.CustomUserData != nil {
attrMap["CustomUserData"] = r.ko.Spec.CustomUserData
}
if r.ko.Spec.Enabled != nil {
attrMap["Enabled"] = r.ko.Spec.Enabled
}
if r.ko.Spec.Token != nil {
attrMap["Token"] = r.ko.Spec.Token
}
if len(attrMap) > 0 {
res.SetAttributes(attrMap)
}
if r.ko.Spec.CustomUserData != nil {
res.SetCustomUserData(*r.ko.Spec.CustomUserData)
}
if r.ko.Spec.PlatformApplicationARN != nil {
res.SetPlatformApplicationArn(*r.ko.Spec.PlatformApplicationARN)
}
if r.ko.Spec.Token != nil {
res.SetToken(*r.ko.Spec.Token)
}
return res, nil
}
// sdkUpdate patches the supplied resource in the backend AWS service API and
// returns a new resource with updated fields.
func (rm *resourceManager) sdkUpdate(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (*resource, error) {
var err error
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.sdkUpdate")
defer func() {
exit(err)
}()
// If any required fields in the input shape are missing, AWS resource is
// not created yet. And sdkUpdate should never be called if this is the
// case, and it's an error in the generated code if it is...
if rm.requiredFieldsMissingFromSetAttributesInput(desired) {
panic("Required field in SetAttributes input shape missing!")
}
input, err := rm.newSetAttributesRequestPayload(desired)
if err != nil {
return nil, err
}
// NOTE(jaypipes): SetAttributes calls return a response but they don't
// contain any useful information. Instead, below, we'll be returning a
// DeepCopy of the supplied desired state, which should be fine because
// that desired state has been constructed from a call to GetAttributes...
_, respErr := rm.sdkapi.SetEndpointAttributesWithContext(ctx, input)
rm.metrics.RecordAPICall("SET_ATTRIBUTES", "SetEndpointAttributes", respErr)
if respErr != nil {
if awsErr, ok := ackerr.AWSError(respErr); ok && awsErr.Code() == "NotFound" {
// Technically, this means someone deleted the backend resource in
// between the time we got a result back from sdkFind() and here...
return nil, ackerr.NotFound
}
return nil, respErr
}
// Merge in the information we read from the API call above to the copy of
// the original Kubernetes object we passed to the function
ko := desired.ko.DeepCopy()
rm.setStatusDefaults(ko)
return &resource{ko}, nil
}
// requiredFieldsMissingFromSetAtttributesInput returns true if there are any
// fields for the SetAttributes Input shape that are required by not present in
// the resource's Spec or Status
func (rm *resourceManager) requiredFieldsMissingFromSetAttributesInput(
r *resource,
) bool {
return r.ko.Status.EndpointARN == nil
}
// newSetAttributesRequestPayload returns SDK-specific struct for the HTTP
// request payload of the SetAttributes API call for the resource
func (rm *resourceManager) newSetAttributesRequestPayload(
r *resource,
) (*svcsdk.SetEndpointAttributesInput, error) {
res := &svcsdk.SetEndpointAttributesInput{}
attrMap := map[string]*string{}
if r.ko.Spec.CustomUserData != nil {
attrMap["CustomUserData"] = r.ko.Spec.CustomUserData
}
if r.ko.Spec.Enabled != nil {
attrMap["Enabled"] = r.ko.Spec.Enabled
}
if r.ko.Spec.Token != nil {
attrMap["Token"] = r.ko.Spec.Token
}
res.SetAttributes(attrMap)
if r.ko.Status.EndpointARN != nil {
res.SetEndpointArn(*r.ko.Status.EndpointARN)
}
return res, nil
}
// sdkDelete deletes the supplied resource in the backend AWS service API
func (rm *resourceManager) sdkDelete(
ctx context.Context,
r *resource,
) (latest *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.sdkDelete")
defer func() {
exit(err)
}()
// TODO(jaypipes): Figure this out...
return nil, nil
}
// setStatusDefaults sets default properties into supplied custom resource
func (rm *resourceManager) setStatusDefaults(
ko *svcapitypes.PlatformEndpoint,
) {
if ko.Status.ACKResourceMetadata == nil {
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
}
if ko.Status.ACKResourceMetadata.Region == nil {
ko.Status.ACKResourceMetadata.Region = &rm.awsRegion
}
if ko.Status.ACKResourceMetadata.OwnerAccountID == nil {
ko.Status.ACKResourceMetadata.OwnerAccountID = &rm.awsAccountID
}
if ko.Status.Conditions == nil {
ko.Status.Conditions = []*ackv1alpha1.Condition{}
}
}
// updateConditions returns updated resource, true; if conditions were updated
// else it returns nil, false
func (rm *resourceManager) updateConditions(
r *resource,
onSuccess bool,
err error,
) (*resource, bool) {
ko := r.ko.DeepCopy()
rm.setStatusDefaults(ko)
// Terminal condition
var terminalCondition *ackv1alpha1.Condition = nil
var recoverableCondition *ackv1alpha1.Condition = nil
var syncCondition *ackv1alpha1.Condition = nil
for _, condition := range ko.Status.Conditions {
if condition.Type == ackv1alpha1.ConditionTypeTerminal {
terminalCondition = condition
}
if condition.Type == ackv1alpha1.ConditionTypeRecoverable {
recoverableCondition = condition
}
if condition.Type == ackv1alpha1.ConditionTypeResourceSynced {
syncCondition = condition
}
}
var termError *ackerr.TerminalError
if rm.terminalAWSError(err) || err == ackerr.SecretTypeNotSupported || err == ackerr.SecretNotFound || errors.As(err, &termError) {
if terminalCondition == nil {
terminalCondition = &ackv1alpha1.Condition{
Type: ackv1alpha1.ConditionTypeTerminal,
}
ko.Status.Conditions = append(ko.Status.Conditions, terminalCondition)
}
var errorMessage = ""
if err == ackerr.SecretTypeNotSupported || err == ackerr.SecretNotFound || errors.As(err, &termError) {
errorMessage = err.Error()
} else {
awsErr, _ := ackerr.AWSError(err)
errorMessage = awsErr.Error()
}
terminalCondition.Status = corev1.ConditionTrue
terminalCondition.Message = &errorMessage
} else {
// Clear the terminal condition if no longer present
if terminalCondition != nil {
terminalCondition.Status = corev1.ConditionFalse
terminalCondition.Message = nil
}
// Handling Recoverable Conditions
if err != nil {
if recoverableCondition == nil {
// Add a new Condition containing a non-terminal error
recoverableCondition = &ackv1alpha1.Condition{
Type: ackv1alpha1.ConditionTypeRecoverable,
}
ko.Status.Conditions = append(ko.Status.Conditions, recoverableCondition)
}
recoverableCondition.Status = corev1.ConditionTrue
awsErr, _ := ackerr.AWSError(err)
errorMessage := err.Error()
if awsErr != nil {
errorMessage = awsErr.Error()
}
recoverableCondition.Message = &errorMessage
} else if recoverableCondition != nil {
recoverableCondition.Status = corev1.ConditionFalse
recoverableCondition.Message = nil
}
}
// Required to avoid the "declared but not used" error in the default case
_ = syncCondition
if terminalCondition != nil || recoverableCondition != nil || syncCondition != nil {
return &resource{ko}, true // updated
}
return nil, false // not updated
}
// terminalAWSError returns awserr, true; if the supplied error is an aws Error type
// and if the exception indicates that it is a Terminal exception
// 'Terminal' exception are specified in generator configuration
func (rm *resourceManager) terminalAWSError(err error) bool {
// No terminal_errors specified for this resource in generator config
return false
}