Skip to content

Commit a1d1c9d

Browse files
author
Nicholas Thomson
committed
Support attribute update
1 parent ba96195 commit a1d1c9d

File tree

6 files changed

+79
-12
lines changed

6 files changed

+79
-12
lines changed

apis/v1alpha1/generator.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ resources:
172172
- InvalidVpcID.Malformed
173173
- InvalidVpcID.NotFound
174174
Vpc:
175+
update_operation:
176+
custom_method_name: customUpdate
175177
exceptions:
176178
terminal_codes:
177179
- InvalidParameterCombination
@@ -187,8 +189,8 @@ resources:
187189
hooks:
188190
sdk_create_post_set_output:
189191
template_path: hooks/vpc/sdk_create_post_set_output.go.tpl
190-
sdk_read_one_post_set_output:
191-
template_path: hooks/vpc/sdk_read_one_post_set_output.go.tpl
192+
sdk_read_many_post_set_output:
193+
template_path: hooks/vpc/sdk_read_many_post_set_output.go.tpl
192194
VpcEndpoint:
193195
fields:
194196
VpcId:

generator.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ resources:
172172
- InvalidVpcID.Malformed
173173
- InvalidVpcID.NotFound
174174
Vpc:
175+
update_operation:
176+
custom_method_name: customUpdate
175177
exceptions:
176178
terminal_codes:
177179
- InvalidParameterCombination
@@ -187,8 +189,8 @@ resources:
187189
hooks:
188190
sdk_create_post_set_output:
189191
template_path: hooks/vpc/sdk_create_post_set_output.go.tpl
190-
sdk_read_one_post_set_output:
191-
template_path: hooks/vpc/sdk_read_one_post_set_output.go.tpl
192+
sdk_read_many_post_set_output:
193+
template_path: hooks/vpc/sdk_read_many_post_set_output.go.tpl
192194
VpcEndpoint:
193195
fields:
194196
VpcId:

pkg/resource/vpc/hook.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"context"
1818

1919
svcapitypes "github.com/aws-controllers-k8s/ec2-controller/apis/v1alpha1"
20+
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2021
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2122
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
2223
)
@@ -135,3 +136,33 @@ func (rm *resourceManager) addAttributesToSpec(
135136

136137
return nil
137138
}
139+
140+
func (rm *resourceManager) customUpdate(
141+
ctx context.Context,
142+
desired *resource,
143+
latest *resource,
144+
delta *ackcompare.Delta,
145+
) (updated *resource, err error) {
146+
rlog := ackrtlog.FromContext(ctx)
147+
exit := rlog.Trace("rm.customUpdateVPC")
148+
defer exit(err)
149+
150+
// Merge in the information we read from the API call above to the copy of
151+
// the original Kubernetes object we passed to the function
152+
ko := desired.ko.DeepCopy()
153+
154+
if delta.DifferentAt("Spec.EnableDNSSupport") {
155+
if err := rm.syncDNSSupportAttribute(ctx, desired); err != nil {
156+
return nil, err
157+
}
158+
}
159+
160+
if delta.DifferentAt("Spec.EnableDNSHostnames") {
161+
if err := rm.syncDNSHostnamesAttribute(ctx, desired); err != nil {
162+
return nil, err
163+
}
164+
}
165+
166+
rm.setStatusDefaults(ko)
167+
return &resource{ko}, nil
168+
}

pkg/resource/vpc/sdk.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/tests/test_vpc.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
CREATE_WAIT_AFTER_SECONDS = 10
2929
DELETE_WAIT_AFTER_SECONDS = 10
30+
MODIFY_WAIT_AFTER_SECONDS = 5
3031

3132

3233
def get_vpc(ec2_client, vpc_id: str) -> dict:
@@ -46,6 +47,17 @@ def get_vpc(ec2_client, vpc_id: str) -> dict:
4647
def vpc_exists(ec2_client, vpc_id: str) -> bool:
4748
return get_vpc(ec2_client, vpc_id) is not None
4849

50+
def get_vpc_attribute(ec2_client, vpc_id: str, attribute_name: str) -> dict:
51+
return ec2_client.describe_vpc_attribute(Attribute=attribute_name, VpcId=vpc_id)
52+
53+
def get_dns_support(ec2_client, vpc_id: str) -> bool:
54+
attribute = get_vpc_attribute(ec2_client, vpc_id, 'enableDnsSupport')
55+
return attribute['EnableDnsSupport']['Value']
56+
57+
def get_dns_hostnames(ec2_client, vpc_id: str) -> bool:
58+
attribute = get_vpc_attribute(ec2_client, vpc_id, 'enableDnsHostnames')
59+
return attribute['EnableDnsHostnames']['Value']
60+
4961
@service_marker
5062
@pytest.mark.canary
5163
class TestVpc:
@@ -128,12 +140,30 @@ def test_enable_attributes(self, ec2_client):
128140
assert exists
129141

130142
# Assert the attributes are set correctly
131-
dns_support = ec2_client.describe_vpc_attribute(Attribute='enableDnsSupport', VpcId=resource_id)
132-
assert dns_support['EnableDnsSupport']['Value']
133-
134-
dns_hostnames = ec2_client.describe_vpc_attribute(Attribute='enableDnsHostnames', VpcId=resource_id)
135-
assert dns_hostnames['EnableDnsHostnames']['Value']
136-
143+
assert get_dns_support(ec2_client, resource_id)
144+
assert get_dns_hostnames(ec2_client, resource_id)
145+
146+
# Disable the DNS support
147+
updates = {
148+
"spec": {"enableDNSSupport": False}
149+
}
150+
k8s.patch_custom_resource(ref, updates)
151+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
152+
153+
# Assert DNS support has been updated
154+
assert not get_dns_support(ec2_client, resource_id)
155+
assert get_dns_hostnames(ec2_client, resource_id)
156+
157+
# Disable the DNS hostname
158+
updates = {
159+
"spec": {"enableDNSHostnames": False}
160+
}
161+
k8s.patch_custom_resource(ref, updates)
162+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
163+
164+
# Assert DNS hostname has been updated
165+
assert not get_dns_support(ec2_client, resource_id)
166+
assert not get_dns_hostnames(ec2_client, resource_id)
137167

138168
# Delete k8s resource
139169
_, deleted = k8s.delete_custom_resource(ref, 2, 5)

0 commit comments

Comments
 (0)