Skip to content

Commit d86b927

Browse files
authored
Adds updateTags for subnet group. (#42)
Issue #, if available: Customers cannot update tags for subnet group after creation. Description of changes: Add some functions so that tags of subnet group can be updated now. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent d078b74 commit d86b927

File tree

10 files changed

+280
-29
lines changed

10 files changed

+280
-29
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-12-13T17:51:08Z"
2+
build_date: "2022-12-14T00:10:49Z"
33
build_hash: 16f0e201b37a06b535370cc69e11adb934a22d33
44
go_version: go1.19
55
version: v0.20.1-18-g16f0e20
66
api_directory_checksum: a1e396caca4bdd1612fa7d09f0ee56f3e4976ff7
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: c20e176b6706e0a4c8db79814d1a1672ad760c81
10+
file_checksum: 8aa3940404a0667041e270bce2d86684b9e1df4d
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ resources:
8080
- SubnetInUse
8181
- InvalidParameterValueException
8282
hooks:
83-
sdk_read_many_pre_set_output:
84-
template_path: hooks/subnetgroup/sdk_read_many_pre_set_output.go.tpl
8583
sdk_create_post_set_output:
8684
template_path: hooks/subnetgroup/sdk_create_post_set_output.go.tpl
85+
sdk_read_many_pre_set_output:
86+
template_path: hooks/subnetgroup/sdk_read_many_pre_set_output.go.tpl
87+
sdk_read_many_post_set_output:
88+
template_path: hooks/subnetgroup/sdk_read_many_post_set_output.go.tpl
8789
sdk_update_post_set_output:
8890
template_path: hooks/subnetgroup/sdk_create_post_set_output.go.tpl
91+
sdk_update_pre_build_request:
92+
template_path: hooks/subnetgroup/sdk_update_pre_build_request.go.tpl
8993
renames:
9094
operations:
9195
CreateSubnetGroup:

generator.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ resources:
8080
- SubnetInUse
8181
- InvalidParameterValueException
8282
hooks:
83-
sdk_read_many_pre_set_output:
84-
template_path: hooks/subnetgroup/sdk_read_many_pre_set_output.go.tpl
8583
sdk_create_post_set_output:
8684
template_path: hooks/subnetgroup/sdk_create_post_set_output.go.tpl
85+
sdk_read_many_pre_set_output:
86+
template_path: hooks/subnetgroup/sdk_read_many_pre_set_output.go.tpl
87+
sdk_read_many_post_set_output:
88+
template_path: hooks/subnetgroup/sdk_read_many_post_set_output.go.tpl
8789
sdk_update_post_set_output:
8890
template_path: hooks/subnetgroup/sdk_create_post_set_output.go.tpl
91+
sdk_update_pre_build_request:
92+
template_path: hooks/subnetgroup/sdk_update_pre_build_request.go.tpl
8993
renames:
9094
operations:
9195
CreateSubnetGroup:

pkg/resource/subnet_group/hooks.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 subnet_group
15+
16+
import (
17+
"context"
18+
19+
svcapitypes "github.com/aws-controllers-k8s/memorydb-controller/apis/v1alpha1"
20+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
21+
svcsdk "github.com/aws/aws-sdk-go/service/memorydb"
22+
)
23+
24+
// getTags gets tags from given ParameterGroup.
25+
func (rm *resourceManager) getTags(
26+
ctx context.Context,
27+
resourceARN string,
28+
) ([]*svcapitypes.Tag, error) {
29+
resp, err := rm.sdkapi.ListTagsWithContext(
30+
ctx,
31+
&svcsdk.ListTagsInput{
32+
ResourceArn: &resourceARN,
33+
},
34+
)
35+
rm.metrics.RecordAPICall("GET", "ListTags", err)
36+
if err != nil {
37+
return nil, err
38+
}
39+
tags := make([]*svcapitypes.Tag, 0, len(resp.TagList))
40+
for _, tag := range resp.TagList {
41+
tags = append(tags, &svcapitypes.Tag{
42+
Key: tag.Key,
43+
Value: tag.Value,
44+
})
45+
}
46+
return tags, nil
47+
}
48+
49+
// updateTags updates tags of given ParameterGroup to desired tags.
50+
func (rm *resourceManager) updateTags(
51+
ctx context.Context,
52+
desired *resource,
53+
latest *resource,
54+
) (err error) {
55+
rlog := ackrtlog.FromContext(ctx)
56+
exit := rlog.Trace("rm.syncTags")
57+
defer func() { exit(err) }()
58+
59+
arn := (*string)(latest.ko.Status.ACKResourceMetadata.ARN)
60+
61+
toAdd, toDelete := computeTagsDelta(
62+
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
63+
)
64+
65+
if len(toDelete) > 0 {
66+
rlog.Debug("removing tags from parameter group", "tags", toDelete)
67+
_, err = rm.sdkapi.UntagResourceWithContext(
68+
ctx,
69+
&svcsdk.UntagResourceInput{
70+
ResourceArn: arn,
71+
TagKeys: toDelete,
72+
},
73+
)
74+
rm.metrics.RecordAPICall("UPDATE", "UntagResource", err)
75+
if err != nil {
76+
return err
77+
}
78+
}
79+
80+
if len(toAdd) > 0 {
81+
rlog.Debug("adding tags to parameter group", "tags", toAdd)
82+
_, err = rm.sdkapi.TagResourceWithContext(
83+
ctx,
84+
&svcsdk.TagResourceInput{
85+
ResourceArn: arn,
86+
Tags: sdkTagsFromResourceTags(toAdd),
87+
},
88+
)
89+
rm.metrics.RecordAPICall("UPDATE", "TagResource", err)
90+
if err != nil {
91+
return err
92+
}
93+
}
94+
95+
return nil
96+
}
97+
98+
func computeTagsDelta(
99+
desired []*svcapitypes.Tag,
100+
latest []*svcapitypes.Tag,
101+
) (added []*svcapitypes.Tag, removed []*string) {
102+
toDelete := []*string{}
103+
toAdd := []*svcapitypes.Tag{}
104+
105+
desiredTags := map[string]string{}
106+
key := ""
107+
value := ""
108+
for _, tag := range desired {
109+
if tag.Key != nil {
110+
key = *tag.Key
111+
value = ""
112+
if tag.Value != nil {
113+
value = *tag.Value
114+
}
115+
desiredTags[key] = value
116+
}
117+
}
118+
119+
for _, tag := range desired {
120+
toAdd = append(toAdd, tag)
121+
}
122+
for _, tag := range latest {
123+
_, ok := desiredTags[*tag.Key]
124+
if !ok {
125+
toDelete = append(toDelete, tag.Key)
126+
}
127+
}
128+
return toAdd, toDelete
129+
}
130+
131+
func sdkTagsFromResourceTags(
132+
rTags []*svcapitypes.Tag,
133+
) []*svcsdk.Tag {
134+
tags := make([]*svcsdk.Tag, len(rTags))
135+
for i := range rTags {
136+
tags[i] = &svcsdk.Tag{
137+
Key: rTags[i].Key,
138+
Value: rTags[i].Value,
139+
}
140+
}
141+
return tags
142+
}

pkg/resource/subnet_group/sdk.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
var resourceSyncedCondition *ackv1alpha1.Condition = nil
2-
for _, condition := range ko.Status.Conditions {
3-
if condition.Type == ackv1alpha1.ConditionTypeResourceSynced {
4-
resourceSyncedCondition = condition
5-
break
6-
}
7-
}
8-
if resourceSyncedCondition == nil {
9-
resourceSyncedCondition = &ackv1alpha1.Condition{
10-
Type: ackv1alpha1.ConditionTypeResourceSynced,
11-
Status: corev1.ConditionTrue,
12-
}
13-
ko.Status.Conditions = append(ko.Status.Conditions, resourceSyncedCondition)
14-
} else {
15-
resourceSyncedCondition.Status = corev1.ConditionTrue
16-
}
1+
var resourceSyncedCondition *ackv1alpha1.Condition = nil
2+
for _, condition := range ko.Status.Conditions {
3+
if condition.Type == ackv1alpha1.ConditionTypeResourceSynced {
4+
resourceSyncedCondition = condition
5+
break
6+
}
7+
}
8+
if resourceSyncedCondition == nil {
9+
resourceSyncedCondition = &ackv1alpha1.Condition{
10+
Type: ackv1alpha1.ConditionTypeResourceSynced,
11+
Status: corev1.ConditionTrue,
12+
}
13+
ko.Status.Conditions = append(ko.Status.Conditions, resourceSyncedCondition)
14+
} else {
15+
resourceSyncedCondition.Status = corev1.ConditionTrue
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resourceARN := (*string)(ko.Status.ACKResourceMetadata.ARN)
2+
tags, err := rm.getTags(ctx, *resourceARN)
3+
if err != nil {
4+
return nil, err
5+
}
6+
ko.Spec.Tags = tags
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var subnets []*string
2-
for _, subnet := range resp.SubnetGroups[0].Subnets {
3-
if subnet.Identifier != nil{
4-
subnets = append(subnets, subnet.Identifier)
5-
}
6-
}
1+
var subnets []*string
2+
for _, subnet := range resp.SubnetGroups[0].Subnets {
3+
if subnet.Identifier != nil{
4+
subnets = append(subnets, subnet.Identifier)
5+
}
6+
}
77

8-
ko.Spec.SubnetIDs = subnets
8+
ko.Spec.SubnetIDs = subnets
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
if delta.DifferentAt("Spec.Tags") {
2+
err = rm.updateTags(ctx, desired, latest)
3+
if err != nil {
4+
return nil, err
5+
}
6+
}
7+
8+
if !delta.DifferentExcept("Spec.Tags") {
9+
return desired, nil
10+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
id: "SUBNET_UPDATE_WITH_TAGS"
2+
description: "In this test we create SubnetGroup and update tags"
3+
#marks:
4+
# - slow
5+
# - blocked
6+
resource:
7+
apiVersion: $CRD_GROUP/$CRD_VERSION
8+
kind: SubnetGroup
9+
metadata:
10+
name: subnet$RANDOM_SUFFIX
11+
steps:
12+
- id: "SG_INITIAL_CREATE"
13+
description: "Create SG with no tags"
14+
create:
15+
spec:
16+
description: Subnet group create
17+
name: subnet$RANDOM_SUFFIX
18+
subnetIDs:
19+
- $SUBNET1
20+
wait:
21+
status:
22+
conditions:
23+
ACK.ResourceSynced:
24+
status: "True"
25+
timeout: 100
26+
- id: "SG_ADD_TAGS"
27+
description: "Add tags in SubnetGroup"
28+
patch:
29+
spec:
30+
tags:
31+
- key: "test_key_1"
32+
value: "test_value_1"
33+
- key: "test_key_2"
34+
- key:
35+
wait:
36+
status:
37+
conditions:
38+
ACK.ResourceSynced:
39+
status: "True"
40+
timeout: 100
41+
- id: "SG_DELETE_TAGS"
42+
description: "Delete tags in SG"
43+
patch:
44+
spec:
45+
tags:
46+
- key: "test_key_1"
47+
value: "test_value_1"
48+
wait:
49+
status:
50+
conditions:
51+
ACK.ResourceSynced:
52+
status: "True"
53+
timeout: 100
54+
- id: "SG_ADD_AND_DELETE_TAGS"
55+
description: "Add some tags and delete some tags in PG"
56+
patch:
57+
spec:
58+
tags:
59+
- key: "test_key_2"
60+
value: "test_value_2"
61+
wait:
62+
status:
63+
conditions:
64+
ACK.ResourceSynced:
65+
status: "True"
66+
timeout: 100
67+
- id: "DELETE_SUBNET"
68+
description: "Delete subnet group"
69+
delete: subnet$RANDOM_SUFFIX

0 commit comments

Comments
 (0)