forked from aws-controllers-k8s/dynamodb-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk.go
381 lines (347 loc) · 12.3 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
// 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 backup
import (
"context"
"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"
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/dynamodb"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
svcapitypes "github.com/aws-controllers-k8s/dynamodb-controller/apis/v1alpha1"
)
// Hack to avoid import errors during build...
var (
_ = &metav1.Time{}
_ = strings.ToLower("")
_ = &aws.JSONValue{}
_ = &svcsdk.DynamoDB{}
_ = &svcapitypes.Backup{}
_ = ackv1alpha1.AWSAccountID("")
_ = &ackerr.NotFound
_ = &ackcondition.NotManagedMessage
_ = &reflect.Value{}
)
// 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 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.requiredFieldsMissingFromReadOneInput(r) {
return nil, ackerr.NotFound
}
input, err := rm.newDescribeRequestPayload(r)
if err != nil {
return nil, err
}
var resp *svcsdk.DescribeBackupOutput
resp, err = rm.sdkapi.DescribeBackupWithContext(ctx, input)
rm.metrics.RecordAPICall("READ_ONE", "DescribeBackup", err)
if err != nil {
if awsErr, ok := ackerr.AWSError(err); ok && awsErr.Code() == "BackupNotFoundException" {
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()
if ko.Status.ACKResourceMetadata == nil {
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
}
if resp.BackupDescription.BackupDetails.BackupArn != nil {
arn := ackv1alpha1.AWSResourceName(*resp.BackupDescription.BackupDetails.BackupArn)
ko.Status.ACKResourceMetadata.ARN = &arn
}
if resp.BackupDescription.BackupDetails.BackupCreationDateTime != nil {
ko.Status.BackupCreationDateTime = &metav1.Time{*resp.BackupDescription.BackupDetails.BackupCreationDateTime}
} else {
ko.Status.BackupCreationDateTime = nil
}
if resp.BackupDescription.BackupDetails.BackupExpiryDateTime != nil {
ko.Status.BackupExpiryDateTime = &metav1.Time{*resp.BackupDescription.BackupDetails.BackupExpiryDateTime}
} else {
ko.Status.BackupExpiryDateTime = nil
}
if resp.BackupDescription.BackupDetails.BackupName != nil {
ko.Spec.BackupName = resp.BackupDescription.BackupDetails.BackupName
} else {
ko.Spec.BackupName = nil
}
if resp.BackupDescription.BackupDetails.BackupSizeBytes != nil {
ko.Status.BackupSizeBytes = resp.BackupDescription.BackupDetails.BackupSizeBytes
} else {
ko.Status.BackupSizeBytes = nil
}
if resp.BackupDescription.BackupDetails.BackupStatus != nil {
ko.Status.BackupStatus = resp.BackupDescription.BackupDetails.BackupStatus
} else {
ko.Status.BackupStatus = nil
}
if resp.BackupDescription.BackupDetails.BackupType != nil {
ko.Status.BackupType = resp.BackupDescription.BackupDetails.BackupType
} else {
ko.Status.BackupType = nil
}
rm.setStatusDefaults(ko)
if isBackupCreating(&resource{ko}) {
return &resource{ko}, requeueWaitWhileCreating
}
return &resource{ko}, nil
}
// requiredFieldsMissingFromReadOneInput returns true if there are any fields
// for the ReadOne Input shape that are required but not present in the
// resource's Spec or Status
func (rm *resourceManager) requiredFieldsMissingFromReadOneInput(
r *resource,
) bool {
return (r.ko.Status.ACKResourceMetadata == nil || r.ko.Status.ACKResourceMetadata.ARN == nil)
}
// newDescribeRequestPayload returns SDK-specific struct for the HTTP request
// payload of the Describe API call for the resource
func (rm *resourceManager) newDescribeRequestPayload(
r *resource,
) (*svcsdk.DescribeBackupInput, error) {
res := &svcsdk.DescribeBackupInput{}
if r.ko.Status.ACKResourceMetadata != nil && r.ko.Status.ACKResourceMetadata.ARN != nil {
res.SetBackupArn(string(*r.ko.Status.ACKResourceMetadata.ARN))
}
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 exit(err)
input, err := rm.newCreateRequestPayload(ctx, desired)
if err != nil {
return nil, err
}
var resp *svcsdk.CreateBackupOutput
_ = resp
resp, err = rm.sdkapi.CreateBackupWithContext(ctx, input)
rm.metrics.RecordAPICall("CREATE", "CreateBackup", 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 ko.Status.ACKResourceMetadata == nil {
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
}
if resp.BackupDetails.BackupArn != nil {
arn := ackv1alpha1.AWSResourceName(*resp.BackupDetails.BackupArn)
ko.Status.ACKResourceMetadata.ARN = &arn
}
if resp.BackupDetails.BackupCreationDateTime != nil {
ko.Status.BackupCreationDateTime = &metav1.Time{*resp.BackupDetails.BackupCreationDateTime}
} else {
ko.Status.BackupCreationDateTime = nil
}
if resp.BackupDetails.BackupExpiryDateTime != nil {
ko.Status.BackupExpiryDateTime = &metav1.Time{*resp.BackupDetails.BackupExpiryDateTime}
} else {
ko.Status.BackupExpiryDateTime = nil
}
if resp.BackupDetails.BackupName != nil {
ko.Spec.BackupName = resp.BackupDetails.BackupName
} else {
ko.Spec.BackupName = nil
}
if resp.BackupDetails.BackupSizeBytes != nil {
ko.Status.BackupSizeBytes = resp.BackupDetails.BackupSizeBytes
} else {
ko.Status.BackupSizeBytes = nil
}
if resp.BackupDetails.BackupStatus != nil {
ko.Status.BackupStatus = resp.BackupDetails.BackupStatus
} else {
ko.Status.BackupStatus = nil
}
if resp.BackupDetails.BackupType != nil {
ko.Status.BackupType = resp.BackupDetails.BackupType
} else {
ko.Status.BackupType = 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.CreateBackupInput, error) {
res := &svcsdk.CreateBackupInput{}
if r.ko.Spec.BackupName != nil {
res.SetBackupName(*r.ko.Spec.BackupName)
}
if r.ko.Spec.TableName != nil {
res.SetTableName(*r.ko.Spec.TableName)
}
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) {
// TODO(jaypipes): Figure this out...
return nil, ackerr.NotImplemented
}
// 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 exit(err)
input, err := rm.newDeleteRequestPayload(r)
if err != nil {
return nil, err
}
var resp *svcsdk.DeleteBackupOutput
_ = resp
resp, err = rm.sdkapi.DeleteBackupWithContext(ctx, input)
rm.metrics.RecordAPICall("DELETE", "DeleteBackup", err)
return nil, err
}
// newDeleteRequestPayload returns an SDK-specific struct for the HTTP request
// payload of the Delete API call for the resource
func (rm *resourceManager) newDeleteRequestPayload(
r *resource,
) (*svcsdk.DeleteBackupInput, error) {
res := &svcsdk.DeleteBackupInput{}
if r.ko.Status.ACKResourceMetadata != nil && r.ko.Status.ACKResourceMetadata.ARN != nil {
res.SetBackupArn(string(*r.ko.Status.ACKResourceMetadata.ARN))
}
return res, nil
}
// setStatusDefaults sets default properties into supplied custom resource
func (rm *resourceManager) setStatusDefaults(
ko *svcapitypes.Backup,
) {
if ko.Status.ACKResourceMetadata == nil {
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
}
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
}
}
if rm.terminalAWSError(err) || err == ackerr.SecretTypeNotSupported || err == ackerr.SecretNotFound {
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 {
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
}