|
| 1 | +package cache_subnet_group |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + svcapitypes "github.com/aws-controllers-k8s/elasticache-controller/apis/v1alpha1" |
| 7 | + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" |
| 8 | + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" |
| 9 | + ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" |
| 10 | + "github.com/aws/aws-sdk-go/aws/awserr" |
| 11 | + svcsdk "github.com/aws/aws-sdk-go/service/elasticache" |
| 12 | +) |
| 13 | + |
| 14 | +func (rm *resourceManager) customUpdateCacheSubnetGroup( |
| 15 | + ctx context.Context, |
| 16 | + desired *resource, |
| 17 | + latest *resource, |
| 18 | + delta *ackcompare.Delta, |
| 19 | +) (updated *resource, err error) { |
| 20 | + rlog := ackrtlog.FromContext(ctx) |
| 21 | + exit := rlog.Trace("rm.sdkUpdate") |
| 22 | + defer func() { |
| 23 | + exit(err) |
| 24 | + }() |
| 25 | + input, err := rm.newUpdateRequestPayload(ctx, desired, delta) |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + |
| 30 | + var resp *svcsdk.ModifyCacheSubnetGroupOutput |
| 31 | + _ = resp |
| 32 | + resp, err = rm.sdkapi.ModifyCacheSubnetGroupWithContext(ctx, input) |
| 33 | + rm.metrics.RecordAPICall("UPDATE", "ModifyCacheSubnetGroup", err) |
| 34 | + if err != nil { |
| 35 | + if aerr, ok := err.(awserr.Error); ok { |
| 36 | + switch aerr.Message() { |
| 37 | + case "No modifications were requested.": |
| 38 | + return desired, nil |
| 39 | + } |
| 40 | + } |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + // Merge in the information we read from the API call above to the copy of |
| 44 | + // the original Kubernetes object we passed to the function |
| 45 | + ko := desired.ko.DeepCopy() |
| 46 | + |
| 47 | + if ko.Status.ACKResourceMetadata == nil { |
| 48 | + ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{} |
| 49 | + } |
| 50 | + if resp.CacheSubnetGroup.ARN != nil { |
| 51 | + arn := ackv1alpha1.AWSResourceName(*resp.CacheSubnetGroup.ARN) |
| 52 | + ko.Status.ACKResourceMetadata.ARN = &arn |
| 53 | + } |
| 54 | + if resp.CacheSubnetGroup.CacheSubnetGroupDescription != nil { |
| 55 | + ko.Spec.CacheSubnetGroupDescription = resp.CacheSubnetGroup.CacheSubnetGroupDescription |
| 56 | + } else { |
| 57 | + ko.Spec.CacheSubnetGroupDescription = nil |
| 58 | + } |
| 59 | + if resp.CacheSubnetGroup.CacheSubnetGroupName != nil { |
| 60 | + ko.Spec.CacheSubnetGroupName = resp.CacheSubnetGroup.CacheSubnetGroupName |
| 61 | + } else { |
| 62 | + ko.Spec.CacheSubnetGroupName = nil |
| 63 | + } |
| 64 | + if resp.CacheSubnetGroup.Subnets != nil { |
| 65 | + f3 := []*svcapitypes.Subnet{} |
| 66 | + for _, f3iter := range resp.CacheSubnetGroup.Subnets { |
| 67 | + f3elem := &svcapitypes.Subnet{} |
| 68 | + if f3iter.SubnetAvailabilityZone != nil { |
| 69 | + f3elemf0 := &svcapitypes.AvailabilityZone{} |
| 70 | + if f3iter.SubnetAvailabilityZone.Name != nil { |
| 71 | + f3elemf0.Name = f3iter.SubnetAvailabilityZone.Name |
| 72 | + } |
| 73 | + f3elem.SubnetAvailabilityZone = f3elemf0 |
| 74 | + } |
| 75 | + if f3iter.SubnetIdentifier != nil { |
| 76 | + f3elem.SubnetIdentifier = f3iter.SubnetIdentifier |
| 77 | + } |
| 78 | + if f3iter.SubnetOutpost != nil { |
| 79 | + f3elemf2 := &svcapitypes.SubnetOutpost{} |
| 80 | + if f3iter.SubnetOutpost.SubnetOutpostArn != nil { |
| 81 | + f3elemf2.SubnetOutpostARN = f3iter.SubnetOutpost.SubnetOutpostArn |
| 82 | + } |
| 83 | + f3elem.SubnetOutpost = f3elemf2 |
| 84 | + } |
| 85 | + f3 = append(f3, f3elem) |
| 86 | + } |
| 87 | + ko.Status.Subnets = f3 |
| 88 | + } else { |
| 89 | + ko.Status.Subnets = nil |
| 90 | + } |
| 91 | + if resp.CacheSubnetGroup.VpcId != nil { |
| 92 | + ko.Status.VPCID = resp.CacheSubnetGroup.VpcId |
| 93 | + } else { |
| 94 | + ko.Status.VPCID = nil |
| 95 | + } |
| 96 | + |
| 97 | + rm.setStatusDefaults(ko) |
| 98 | + return &resource{ko}, nil |
| 99 | +} |
| 100 | + |
| 101 | +// newUpdateRequestPayload returns an SDK-specific struct for the HTTP request |
| 102 | +// payload of the Update API call for the resource |
| 103 | +func (rm *resourceManager) newUpdateRequestPayload( |
| 104 | + ctx context.Context, |
| 105 | + r *resource, |
| 106 | + delta *ackcompare.Delta, |
| 107 | +) (*svcsdk.ModifyCacheSubnetGroupInput, error) { |
| 108 | + res := &svcsdk.ModifyCacheSubnetGroupInput{} |
| 109 | + |
| 110 | + if r.ko.Spec.CacheSubnetGroupDescription != nil { |
| 111 | + res.SetCacheSubnetGroupDescription(*r.ko.Spec.CacheSubnetGroupDescription) |
| 112 | + } |
| 113 | + if r.ko.Spec.CacheSubnetGroupName != nil { |
| 114 | + res.SetCacheSubnetGroupName(*r.ko.Spec.CacheSubnetGroupName) |
| 115 | + } |
| 116 | + if r.ko.Spec.SubnetIDs != nil { |
| 117 | + f2 := []*string{} |
| 118 | + for _, f2iter := range r.ko.Spec.SubnetIDs { |
| 119 | + var f2elem string |
| 120 | + f2elem = *f2iter |
| 121 | + f2 = append(f2, &f2elem) |
| 122 | + } |
| 123 | + res.SetSubnetIds(f2) |
| 124 | + } |
| 125 | + |
| 126 | + return res, nil |
| 127 | +} |
0 commit comments