Skip to content

Commit 39d7c30

Browse files
authored
include Spec fields in GetAttributes SetResource (#380)
Attempting to generate the SNS controller with code-generator <= v0.21.0 results in various `make test` failures that look like this: ``` [jaypipes@thelio sns-controller]$ make test go test -v ./... ? github.com/aws-controllers-k8s/sns-controller/apis/v1alpha1 [no test files] pkg/resource/topic/sdk.go:225:8: undefined: err pkg/resource/platform_application/sdk.go:75:6: resp declared but not used pkg/resource/platform_application/sdk.go:217:8: undefined: err pkg/resource/platform_endpoint/sdk.go:75:6: resp declared but not used pkg/resource/platform_endpoint/sdk.go:113:48: r.ko.Spec.EndpointARN undefined (type "github.com/aws-controllers-k8s/sns-controller/apis/v1alpha1".PlatformEndpointSpec has no field or method EndpointARN) pkg/resource/platform_endpoint/sdk.go:202:8: undefined: err pkg/resource/platform_endpoint/sdk.go:270:48: r.ko.Spec.EndpointARN undefined (type "github.com/aws-controllers-k8s/sns-controller/apis/v1alpha1".PlatformEndpointSpec has no field or method EndpointARN) make: *** [Makefile:19: test] Error 2 ``` We were not including Spec fields in the processing of SetResourceGetAttributes, which resulted in a bunch of `resp variable declared and not used` compile-time failures. This patch fixes that by pulling attributes from the resulting Attributes map into Spec fields when the field is_read_only=false. In addition, fixes the template for sdk_update_set_attributes to declare an `err` variable before referencing it. Signed-off-by: Jay Pipes <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 12246c7 commit 39d7c30

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

pkg/generate/code/set_resource.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,6 @@ func SetResourceGetAttributes(
793793

794794
out := "\n"
795795
indent := strings.Repeat("\t", indentLevel)
796-
adaptiveTargetVarName := targetVarName + cfg.PrefixConfig.StatusField
797796

798797
// did we output an ACKResourceMetadata guard and constructor snippet?
799798
mdGuardOut := false
@@ -806,6 +805,7 @@ func SetResourceGetAttributes(
806805
}
807806
sort.Strings(sortedAttrFieldNames)
808807
for _, fieldName := range sortedAttrFieldNames {
808+
adaptiveTargetVarName := targetVarName + cfg.PrefixConfig.StatusField
809809
if r.IsPrimaryARNField(fieldName) {
810810
if !mdGuardOut {
811811
out += ackResourceMetadataGuardConstructor(
@@ -850,16 +850,17 @@ func SetResourceGetAttributes(
850850
}
851851

852852
fieldNames := names.New(fieldName)
853-
if fieldConfig.IsReadOnly {
854-
out += fmt.Sprintf(
855-
"%s%s.%s = %s.Attributes[\"%s\"]\n",
856-
indent,
857-
adaptiveTargetVarName,
858-
fieldNames.Camel,
859-
sourceVarName,
860-
fieldName,
861-
)
853+
if !fieldConfig.IsReadOnly {
854+
adaptiveTargetVarName = targetVarName + cfg.PrefixConfig.SpecField
862855
}
856+
out += fmt.Sprintf(
857+
"%s%s.%s = %s.Attributes[\"%s\"]\n",
858+
indent,
859+
adaptiveTargetVarName,
860+
fieldNames.Camel,
861+
sourceVarName,
862+
fieldName,
863+
)
863864
}
864865
return out
865866
}

pkg/generate/code/set_resource_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,12 +2712,16 @@ func TestSetResource_SNS_Topic_GetAttributes(t *testing.T) {
27122712
// (and thus in the Spec fields). Two of them are the tesource's ARN and
27132713
// AWS Owner account ID, both of which are handled specially.
27142714
expected := `
2715+
ko.Spec.DeliveryPolicy = resp.Attributes["DeliveryPolicy"]
2716+
ko.Spec.DisplayName = resp.Attributes["DisplayName"]
27152717
ko.Status.EffectiveDeliveryPolicy = resp.Attributes["EffectiveDeliveryPolicy"]
2718+
ko.Spec.KMSMasterKeyID = resp.Attributes["KmsMasterKeyId"]
27162719
if ko.Status.ACKResourceMetadata == nil {
27172720
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
27182721
}
27192722
tmpOwnerID := ackv1alpha1.AWSAccountID(*resp.Attributes["Owner"])
27202723
ko.Status.ACKResourceMetadata.OwnerAccountID = &tmpOwnerID
2724+
ko.Spec.Policy = resp.Attributes["Policy"]
27212725
tmpARN := ackv1alpha1.AWSResourceName(*resp.Attributes["TopicArn"])
27222726
ko.Status.ACKResourceMetadata.ARN = &tmpARN
27232727
`
@@ -2766,13 +2770,24 @@ func TestSetResource_SQS_Queue_GetAttributes(t *testing.T) {
27662770
// (and thus in the Spec fields). One of them is the resource's ARN which
27672771
// is handled specially.
27682772
expected := `
2773+
ko.Spec.ContentBasedDeduplication = resp.Attributes["ContentBasedDeduplication"]
27692774
ko.Status.CreatedTimestamp = resp.Attributes["CreatedTimestamp"]
2775+
ko.Spec.DelaySeconds = resp.Attributes["DelaySeconds"]
2776+
ko.Spec.FifoQueue = resp.Attributes["FifoQueue"]
2777+
ko.Spec.KMSDataKeyReusePeriodSeconds = resp.Attributes["KmsDataKeyReusePeriodSeconds"]
2778+
ko.Spec.KMSMasterKeyID = resp.Attributes["KmsMasterKeyId"]
27702779
ko.Status.LastModifiedTimestamp = resp.Attributes["LastModifiedTimestamp"]
2780+
ko.Spec.MaximumMessageSize = resp.Attributes["MaximumMessageSize"]
2781+
ko.Spec.MessageRetentionPeriod = resp.Attributes["MessageRetentionPeriod"]
2782+
ko.Spec.Policy = resp.Attributes["Policy"]
27712783
if ko.Status.ACKResourceMetadata == nil {
27722784
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
27732785
}
27742786
tmpARN := ackv1alpha1.AWSResourceName(*resp.Attributes["QueueArn"])
27752787
ko.Status.ACKResourceMetadata.ARN = &tmpARN
2788+
ko.Spec.ReceiveMessageWaitTimeSeconds = resp.Attributes["ReceiveMessageWaitTimeSeconds"]
2789+
ko.Spec.RedrivePolicy = resp.Attributes["RedrivePolicy"]
2790+
ko.Spec.VisibilityTimeout = resp.Attributes["VisibilityTimeout"]
27762791
`
27772792
assert.Equal(
27782793
expected,

templates/pkg/resource/sdk_update_set_attributes.go.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ func (rm *resourceManager) sdkUpdate(
55
latest *resource,
66
delta *ackcompare.Delta,
77
) (*resource, error) {
8+
var err error
89
rlog := ackrtlog.FromContext(ctx)
910
exit := rlog.Trace("rm.sdkUpdate")
1011
defer func() {

0 commit comments

Comments
 (0)