Skip to content

Commit ad46714

Browse files
committed
handle renamed fields in SetResource generation
If fields were renamed in some operations, the code generated for `SetResource()` was not properly looking up the renamed field. This patch updates `SetResource()` to first check if the field has been renamed for the referred Operation and if so, look up the CRDField object in the CRD.SpecFields collection instead of the StatusFields collection.
1 parent f2f50ac commit ad46714

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

pkg/generate/code/set_resource.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ func SetResource(
197197
var found bool
198198
var targetMemberShapeRef *awssdkmodel.ShapeRef
199199
targetAdaptedVarName := targetVarName
200-
f, found = r.SpecFields[memberName]
200+
201+
// Check that the field has potentially been renamed
202+
renamedName, _ := r.InputFieldRename(
203+
op.Name, memberName,
204+
)
205+
f, found = r.SpecFields[renamedName]
201206
if found {
202207
targetAdaptedVarName += cfg.PrefixConfig.SpecField
203208
if !performSpecUpdate {
@@ -452,7 +457,11 @@ func setResourceReadMany(
452457
var found bool
453458
var targetMemberShapeRef *awssdkmodel.ShapeRef
454459
targetAdaptedVarName := targetVarName
455-
f, found = r.SpecFields[memberName]
460+
// Check that the field has potentially been renamed
461+
renamedName, _ := r.InputFieldRename(
462+
op.Name, memberName,
463+
)
464+
f, found = r.SpecFields[renamedName]
456465
if found {
457466
targetAdaptedVarName += cfg.PrefixConfig.SpecField
458467
} else {

pkg/generate/code/set_resource_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,3 +1945,79 @@ func TestSetResource_SQS_Queue_GetAttributes(t *testing.T) {
19451945
code.SetResourceGetAttributes(crd.Config(), crd, "resp", "ko", 1),
19461946
)
19471947
}
1948+
1949+
func TestSetResource_RDS_DBSubnetGroup_ReadMany(t *testing.T) {
1950+
assert := assert.New(t)
1951+
require := require.New(t)
1952+
1953+
g := testutil.NewGeneratorForService(t, "rds")
1954+
1955+
crd := testutil.GetCRDByName(t, g, "DBSubnetGroup")
1956+
require.NotNil(crd)
1957+
1958+
// There are renamed fields for Name and Description in order to
1959+
// "de-stutter" the field names. We want to verify that the SetResource for
1960+
// the DescribeDBSubnetGroups API operation sets these fields in the Spec
1961+
// properly
1962+
expected := `
1963+
found := false
1964+
for _, elem := range resp.DBSubnetGroups {
1965+
if elem.DBSubnetGroupArn != nil {
1966+
if ko.Status.ACKResourceMetadata == nil {
1967+
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
1968+
}
1969+
tmpARN := ackv1alpha1.AWSResourceName(*elem.DBSubnetGroupArn)
1970+
ko.Status.ACKResourceMetadata.ARN = &tmpARN
1971+
}
1972+
if elem.DBSubnetGroupDescription != nil {
1973+
ko.Spec.Description = elem.DBSubnetGroupDescription
1974+
}
1975+
if elem.DBSubnetGroupName != nil {
1976+
ko.Spec.Name = elem.DBSubnetGroupName
1977+
}
1978+
if elem.SubnetGroupStatus != nil {
1979+
ko.Status.SubnetGroupStatus = elem.SubnetGroupStatus
1980+
}
1981+
if elem.Subnets != nil {
1982+
f4 := []*svcapitypes.Subnet{}
1983+
for _, f4iter := range elem.Subnets {
1984+
f4elem := &svcapitypes.Subnet{}
1985+
if f4iter.SubnetAvailabilityZone != nil {
1986+
f4elemf0 := &svcapitypes.AvailabilityZone{}
1987+
if f4iter.SubnetAvailabilityZone.Name != nil {
1988+
f4elemf0.Name = f4iter.SubnetAvailabilityZone.Name
1989+
}
1990+
f4elem.SubnetAvailabilityZone = f4elemf0
1991+
}
1992+
if f4iter.SubnetIdentifier != nil {
1993+
f4elem.SubnetIdentifier = f4iter.SubnetIdentifier
1994+
}
1995+
if f4iter.SubnetOutpost != nil {
1996+
f4elemf2 := &svcapitypes.Outpost{}
1997+
if f4iter.SubnetOutpost.Arn != nil {
1998+
f4elemf2.ARN = f4iter.SubnetOutpost.Arn
1999+
}
2000+
f4elem.SubnetOutpost = f4elemf2
2001+
}
2002+
if f4iter.SubnetStatus != nil {
2003+
f4elem.SubnetStatus = f4iter.SubnetStatus
2004+
}
2005+
f4 = append(f4, f4elem)
2006+
}
2007+
ko.Status.Subnets = f4
2008+
}
2009+
if elem.VpcId != nil {
2010+
ko.Status.VPCID = elem.VpcId
2011+
}
2012+
found = true
2013+
break
2014+
}
2015+
if !found {
2016+
return nil, ackerr.NotFound
2017+
}
2018+
`
2019+
assert.Equal(
2020+
expected,
2021+
code.SetResource(crd.Config(), crd, model.OpTypeList, "resp", "ko", 1, false),
2022+
)
2023+
}

pkg/generate/generator.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,14 @@ func (g *Generator) GetCRDs() ([]*ackmodel.CRD, error) {
174174
if memberShapeRef.Shape == nil {
175175
return nil, ErrNilShapePointer
176176
}
177-
memberNames := names.New(memberName)
178-
if _, found := crd.SpecFields[memberName]; found {
177+
// Check that the field in the output shape isn't the same as
178+
// fields in the input shape (where the input shape has potentially
179+
// been renamed)
180+
renamedName, _ := crd.InputFieldRename(
181+
createOp.Name, memberName,
182+
)
183+
memberNames := names.New(renamedName)
184+
if _, found := crd.SpecFields[renamedName]; found {
179185
// We don't put fields that are already in the Spec struct into
180186
// the Status struct
181187
continue
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
ignore:
22
shape_names:
3-
- DBSecurityGroupMembershipList
3+
- DBSecurityGroupMembershipList
4+
resources:
5+
DBSubnetGroup:
6+
renames:
7+
operations:
8+
DescribeDBSubnetGroups:
9+
input_fields:
10+
DBSubnetGroupName: Name
11+
DBSubnetGroupDescription: Description
12+
CreateDBSubnetGroup:
13+
input_fields:
14+
DBSubnetGroupName: Name
15+
DBSubnetGroupDescription: Description
16+
DeleteDBSubnetGroup:
17+
input_fields:
18+
DBSubnetGroupName: Name

0 commit comments

Comments
 (0)