Skip to content

Commit 287334e

Browse files
authored
handle renamed fields for resource identifiers (#117)
The code that outputs Go code that sets `Spec` or `Status` fields to a resource identifier field was not accounting for cases where we renamed the field. Such is the case for many of the RDS controller's resources, since all the identifier fields for all resources are "de-stuttured" by renaming, for example, `DBSubnetGroupName` to just `Name` or `DBClusterParameterGroupName` to just `Name`. This patch simply ensures we account for renamed fields in the Spec and Status during calls to `pkg/generate/code.SetResourceIdentifiers` and adds a unit test to ensure we don't regress in the future. Issue aws-controllers-k8s/community#852 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent a73e0a1 commit 287334e

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

pkg/generate/code/set_resource.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -922,21 +922,25 @@ func SetResourceIdentifiers(
922922
continue
923923

924924
}
925+
// Check that the field has potentially been renamed
926+
renamedName, _ := r.InputFieldRename(
927+
op.Name, memberName,
928+
)
925929

926930
isPrimaryIdentifier := memberName == primaryIdentifier
927-
cleanMemberNames := names.New(memberName)
931+
cleanMemberNames := names.New(renamedName)
928932
cleanMemberName := cleanMemberNames.Camel
929933

930934
memberPath := ""
931-
_, inSpec := r.SpecFields[memberName]
932-
_, inStatus := r.StatusFields[memberName]
935+
_, inSpec := r.SpecFields[renamedName]
936+
_, inStatus := r.StatusFields[renamedName]
933937
switch {
934938
case inSpec:
935939
memberPath = cfg.PrefixConfig.SpecField
936940
case inStatus:
937941
memberPath = cfg.PrefixConfig.StatusField
938942
case isPrimaryIdentifier:
939-
panic("Primary identifier field '" + memberName + "' cannot be found in either spec or status.")
943+
panic("Primary identifier field '" + memberName + "' in operation '" + op.Name + "' cannot be found in either spec or status.")
940944
default:
941945
continue
942946
}

pkg/generate/code/set_resource_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,30 @@ func TestSetResource_RDS_DBInstances_SetResourceIdentifiers(t *testing.T) {
26902690
)
26912691
}
26922692

2693+
func TestSetResource_RDS_DBSubnetGroup_SetResourceIdentifiers(t *testing.T) {
2694+
assert := assert.New(t)
2695+
require := require.New(t)
2696+
2697+
g := testutil.NewGeneratorForService(t, "rds")
2698+
2699+
crd := testutil.GetCRDByName(t, g, "DBSubnetGroup")
2700+
require.NotNil(crd)
2701+
2702+
// In our testdata generator.yaml file, we've renamed the original
2703+
// DBSubnetGroupName to just Name...
2704+
expected := `
2705+
if identifier.NameOrID == "" {
2706+
return ackerrors.MissingNameIdentifier
2707+
}
2708+
r.ko.Spec.Name = &identifier.NameOrID
2709+
2710+
`
2711+
assert.Equal(
2712+
expected,
2713+
code.SetResourceIdentifiers(crd.Config(), crd, "identifier", "r.ko", 1),
2714+
)
2715+
}
2716+
26932717
func TestSetResource_APIGWV2_ApiMapping_SetResourceIdentifiers(t *testing.T) {
26942718
assert := assert.New(t)
26952719
require := require.New(t)

0 commit comments

Comments
 (0)