|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package alias |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" |
| 20 | + ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" |
| 21 | + "github.com/aws/aws-sdk-go/aws" |
| 22 | + svcsdk "github.com/aws/aws-sdk-go/service/lambda" |
| 23 | + |
| 24 | + svcapitypes "github.com/aws-controllers-k8s/lambda-controller/apis/v1alpha1" |
| 25 | +) |
| 26 | + |
| 27 | +func (rm *resourceManager) customUpdateEventInvokeConfig( |
| 28 | + ctx context.Context, |
| 29 | + desired *resource, |
| 30 | + delta *ackcompare.Delta, |
| 31 | +) (*resource, error) { |
| 32 | + var err error |
| 33 | + rlog := ackrtlog.FromContext(ctx) |
| 34 | + exit := rlog.Trace("rm.customUpdateEventInvokeConfig") |
| 35 | + defer exit(err) |
| 36 | + |
| 37 | + if delta.DifferentAt("Spec.FunctionEventInvokeConfig") { |
| 38 | + _, err = rm.syncEventInvokeConfig(ctx, desired) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + readOneLatest, err := rm.ReadOne(ctx, desired) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + return rm.concreteResource(readOneLatest), nil |
| 49 | +} |
| 50 | + |
| 51 | +func (rm *resourceManager) customCreateEventInvokeConfig( |
| 52 | + ctx context.Context, |
| 53 | + desired *resource, |
| 54 | +) (created *resource, err error) { |
| 55 | + rlog := ackrtlog.FromContext(ctx) |
| 56 | + exit := rlog.Trace("rm.customCreateEventInvokeConfig") |
| 57 | + defer exit(err) |
| 58 | + |
| 59 | + input, err := rm.syncEventInvokeConfig(ctx, desired) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + return input, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (rm *resourceManager) syncEventInvokeConfig( |
| 68 | + ctx context.Context, |
| 69 | + r *resource, |
| 70 | +) (created *resource, err error) { |
| 71 | + rlog := ackrtlog.FromContext(ctx) |
| 72 | + exit := rlog.Trace("rm.syncEventInvokeConfig") |
| 73 | + defer exit(err) |
| 74 | + |
| 75 | + dspec := r.ko.Spec |
| 76 | + input := &svcsdk.PutFunctionEventInvokeConfigInput{ |
| 77 | + FunctionName: aws.String(*dspec.FunctionName), |
| 78 | + Qualifier: aws.String(*dspec.Name), |
| 79 | + } |
| 80 | + |
| 81 | + if r.ko.Spec.FunctionEventInvokeConfig != nil { |
| 82 | + if r.ko.Spec.FunctionEventInvokeConfig.DestinationConfig != nil { |
| 83 | + destinations := &svcsdk.DestinationConfig{} |
| 84 | + if r.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure != nil { |
| 85 | + destinations.OnFailure = &svcsdk.OnFailure{} |
| 86 | + if r.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination != nil { |
| 87 | + destinations.OnFailure.Destination = aws.String(*r.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination) |
| 88 | + } |
| 89 | + } |
| 90 | + if r.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess != nil { |
| 91 | + destinations.OnSuccess = &svcsdk.OnSuccess{} |
| 92 | + if r.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination != nil { |
| 93 | + destinations.OnSuccess.Destination = aws.String(*r.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination) |
| 94 | + } |
| 95 | + } |
| 96 | + input.DestinationConfig = destinations |
| 97 | + } |
| 98 | + if r.ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds != nil { |
| 99 | + input.MaximumEventAgeInSeconds = aws.Int64(*r.ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds) |
| 100 | + } |
| 101 | + if r.ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts != nil { |
| 102 | + input.MaximumRetryAttempts = aws.Int64(*r.ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts) |
| 103 | + } |
| 104 | + } |
| 105 | + _, err = rm.sdkapi.PutFunctionEventInvokeConfigWithContext(ctx, input) |
| 106 | + rm.metrics.RecordAPICall("SYNC", "SyncEventInvokeConfig", err) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + return r, nil |
| 112 | +} |
| 113 | + |
| 114 | +func (rm *resourceManager) setResourceAdditionalFields( |
| 115 | + ctx context.Context, |
| 116 | + ko *svcapitypes.Alias, |
| 117 | +) (err error) { |
| 118 | + rlog := ackrtlog.FromContext(ctx) |
| 119 | + exit := rlog.Trace("rm.setResourceAdditionalFields") |
| 120 | + defer exit(err) |
| 121 | + |
| 122 | + var getFunctionEventInvokeConfigOutput *svcsdk.GetFunctionEventInvokeConfigOutput |
| 123 | + getFunctionEventInvokeConfigOutput, err = rm.sdkapi.GetFunctionEventInvokeConfigWithContext( |
| 124 | + ctx, |
| 125 | + &svcsdk.GetFunctionEventInvokeConfigInput{ |
| 126 | + FunctionName: ko.Spec.FunctionName, |
| 127 | + Qualifier: ko.Spec.Name, |
| 128 | + }, |
| 129 | + ) |
| 130 | + |
| 131 | + rm.metrics.RecordAPICall("GET", "GetFunctionEventInvokeConfig", err) |
| 132 | + if err != nil { |
| 133 | + ko.Spec.FunctionEventInvokeConfig = nil |
| 134 | + } else { |
| 135 | + if getFunctionEventInvokeConfigOutput.DestinationConfig != nil { |
| 136 | + if getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure != nil { |
| 137 | + if getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure.Destination != nil { |
| 138 | + ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination = getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure.Destination |
| 139 | + } |
| 140 | + } |
| 141 | + if getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess != nil { |
| 142 | + if getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess.Destination != nil { |
| 143 | + ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination = getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess.Destination |
| 144 | + } |
| 145 | + } |
| 146 | + } else { |
| 147 | + ko.Spec.FunctionEventInvokeConfig.DestinationConfig = nil |
| 148 | + } |
| 149 | + if getFunctionEventInvokeConfigOutput.MaximumEventAgeInSeconds != nil { |
| 150 | + ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds = getFunctionEventInvokeConfigOutput.MaximumEventAgeInSeconds |
| 151 | + } else { |
| 152 | + ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds = nil |
| 153 | + } |
| 154 | + if getFunctionEventInvokeConfigOutput.DestinationConfig != nil { |
| 155 | + ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts = getFunctionEventInvokeConfigOutput.MaximumRetryAttempts |
| 156 | + } else { |
| 157 | + ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts = nil |
| 158 | + } |
| 159 | + } |
| 160 | + return nil |
| 161 | +} |
0 commit comments