|
| 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 pipe |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" |
| 20 | + svcsdk "github.com/aws/aws-sdk-go/service/pipes" |
| 21 | +) |
| 22 | + |
| 23 | +// updatePipeTags uses TagResource and UntagResource to add, remove and update |
| 24 | +// a pipe tags. |
| 25 | +func (rm *resourceManager) updatePipeTags( |
| 26 | + ctx context.Context, |
| 27 | + latest *resource, |
| 28 | + desired *resource, |
| 29 | +) error { |
| 30 | + var err error |
| 31 | + rlog := ackrtlog.FromContext(ctx) |
| 32 | + exit := rlog.Trace("rm.updatePipeTags") |
| 33 | + defer exit(err) |
| 34 | + |
| 35 | + addedOrUpdated, removed := compareMaps(latest.ko.Spec.Tags, desired.ko.Spec.Tags) |
| 36 | + |
| 37 | + if len(removed) > 0 { |
| 38 | + input := &svcsdk.UntagResourceInput{ |
| 39 | + ResourceArn: (*string)(desired.ko.Status.ACKResourceMetadata.ARN), |
| 40 | + TagKeys: removed, |
| 41 | + } |
| 42 | + _, err = rm.sdkapi.UntagResourceWithContext(ctx, input) |
| 43 | + rm.metrics.RecordAPICall("UPDATE", "UntagResource", err) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if len(addedOrUpdated) > 0 { |
| 50 | + input := &svcsdk.TagResourceInput{ |
| 51 | + ResourceArn: (*string)(desired.ko.Status.ACKResourceMetadata.ARN), |
| 52 | + Tags: addedOrUpdated, |
| 53 | + } |
| 54 | + _, err = rm.sdkapi.TagResourceWithContext(ctx, input) |
| 55 | + rm.metrics.RecordAPICall("UPDATE", "TagResource", err) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// compareMaps compares two string to string maps and returns three outputs: a |
| 65 | +// map of the new key/values observed, a list of the keys of the removed values |
| 66 | +// and a map containing the updated keys and their new values. |
| 67 | +func compareMaps( |
| 68 | + a map[string]*string, |
| 69 | + b map[string]*string, |
| 70 | +) (addedOrUpdated map[string]*string, removed []*string) { |
| 71 | + addedOrUpdated = make(map[string]*string) |
| 72 | + visited := make(map[string]bool, len(a)) |
| 73 | + for keyA, valueA := range a { |
| 74 | + valueB, found := b[keyA] |
| 75 | + if !found { |
| 76 | + removed = append(removed, &keyA) |
| 77 | + continue |
| 78 | + } |
| 79 | + if *valueA != *valueB { |
| 80 | + addedOrUpdated[keyA] = valueB |
| 81 | + } |
| 82 | + visited[keyA] = true |
| 83 | + } |
| 84 | + for keyB, valueB := range b { |
| 85 | + _, found := a[keyB] |
| 86 | + if !found { |
| 87 | + addedOrUpdated[keyB] = valueB |
| 88 | + } |
| 89 | + } |
| 90 | + return |
| 91 | +} |
0 commit comments