Skip to content

Commit a548cb1

Browse files
author
Nicholas Thomson
committed
Refactor and simplify describe attrs
1 parent a1d1c9d commit a548cb1

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

pkg/resource/vpc/hook.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,52 @@ package vpc
1616
import (
1717
"context"
1818

19-
svcapitypes "github.com/aws-controllers-k8s/ec2-controller/apis/v1alpha1"
2019
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2120
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2221
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
2322
)
2423

24+
type DNSAttrs struct {
25+
EnableSupport *bool
26+
EnableHostnames *bool
27+
}
28+
2529
func newDescribeVpcAttributePayload(
26-
r *resource,
30+
vpcID string,
2731
attribute string,
2832
) *svcsdk.DescribeVpcAttributeInput {
2933
res := &svcsdk.DescribeVpcAttributeInput{}
30-
res.SetVpcId(*r.ko.Status.VPCID)
34+
res.SetVpcId(vpcID)
3135
res.SetAttribute(attribute)
3236
return res
3337
}
3438

39+
func (rm *resourceManager) getDNSAttributes(
40+
ctx context.Context,
41+
vpcID string,
42+
) (res *DNSAttrs, err error) {
43+
res = &DNSAttrs{}
44+
dnsSupport, err := rm.sdkapi.DescribeVpcAttributeWithContext(
45+
ctx,
46+
newDescribeVpcAttributePayload(vpcID, svcsdk.VpcAttributeNameEnableDnsSupport),
47+
)
48+
if err != nil {
49+
return nil, err
50+
}
51+
res.EnableSupport = dnsSupport.EnableDnsSupport.Value
52+
53+
dnsHostnames, err := rm.sdkapi.DescribeVpcAttributeWithContext(
54+
ctx,
55+
newDescribeVpcAttributePayload(vpcID, svcsdk.VpcAttributeNameEnableDnsHostnames),
56+
)
57+
if err != nil {
58+
return nil, err
59+
}
60+
res.EnableHostnames = dnsHostnames.EnableDnsHostnames.Value
61+
62+
return res, nil
63+
}
64+
3565
func newModifyDNSSupportAttributeInputPayload(
3666
r *resource,
3767
) *svcsdk.ModifyVpcAttributeInput {
@@ -117,26 +147,6 @@ func (rm *resourceManager) createAttributes(
117147
return nil
118148
}
119149

120-
func (rm *resourceManager) addAttributesToSpec(
121-
ctx context.Context,
122-
r *resource,
123-
ko *svcapitypes.VPC,
124-
) (err error) {
125-
dnsSupport, err := rm.sdkapi.DescribeVpcAttributeWithContext(ctx, newDescribeVpcAttributePayload(r, svcsdk.VpcAttributeNameEnableDnsSupport))
126-
if err != nil {
127-
return err
128-
}
129-
ko.Spec.EnableDNSSupport = dnsSupport.EnableDnsSupport.Value
130-
131-
dnsHostnames, err := rm.sdkapi.DescribeVpcAttributeWithContext(ctx, newDescribeVpcAttributePayload(r, svcsdk.VpcAttributeNameEnableDnsHostnames))
132-
if err != nil {
133-
return err
134-
}
135-
ko.Spec.EnableDNSHostnames = dnsHostnames.EnableDnsHostnames.Value
136-
137-
return nil
138-
}
139-
140150
func (rm *resourceManager) customUpdate(
141151
ctx context.Context,
142152
desired *resource,

pkg/resource/vpc/sdk.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
err = rm.createAttributes(ctx, &resource{ko})
22
if err != nil {
33
return nil, err
4-
}
4+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
if err := rm.addAttributesToSpec(ctx, r, ko); err != nil {
1+
if dnsAttrs, err := rm.getDNSAttributes(ctx, *ko.Status.VPCID); err != nil {
22
return nil, err
3+
} else {
4+
ko.Spec.EnableDNSSupport = dnsAttrs.EnableSupport
5+
ko.Spec.EnableDNSHostnames = dnsAttrs.EnableHostnames
36
}

0 commit comments

Comments
 (0)