Skip to content

allow SetFieldConfig.From to refer to nested fields #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions pkg/generate/code/set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"

"github.com/aws-controllers-k8s/code-generator/pkg/fieldpath"
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
"github.com/aws-controllers-k8s/code-generator/pkg/model"
"github.com/aws-controllers-k8s/code-generator/pkg/names"
Expand Down Expand Up @@ -236,11 +237,11 @@ func SetResource(
// And this would indicate to the code generator that the Spec.Name
// field should be set from the value of the Output shape's Bucket
// field.
var found bool
if setCfg != nil && setCfg.From != nil {
sourceMemberShapeRef, found = outputShape.MemberRefs[*setCfg.From]
fp := fieldpath.FromString(*setCfg.From)
sourceMemberShapeRef = fp.ShapeRef(sourceMemberShapeRef)
}
if !found || sourceMemberShapeRef.Shape == nil {
if sourceMemberShapeRef == nil || sourceMemberShapeRef.Shape == nil {
// Technically this should not happen, so let's bail here if it
// does...
msg := fmt.Sprintf(
Expand All @@ -251,7 +252,7 @@ func SetResource(
}
}

sourceMemberShape := sourceMemberShapeRef.Shape
targetMemberShape := targetMemberShapeRef.Shape

// fieldVarName is the name of the variable that is used for temporary
// storage of complex member field values
Expand Down Expand Up @@ -289,7 +290,7 @@ func SetResource(
"%s.%s", targetAdaptedVarName, f.Names.Camel,
)

switch sourceMemberShape.Type {
switch targetMemberShape.Type {
case "list", "structure", "map":
{
memberVarName := fmt.Sprintf("f%d", memberIndex)
Expand Down Expand Up @@ -321,17 +322,6 @@ func SetResource(
// We have some special instructions to pull the value from a
// different field or member...
sourceAdaptedVarName = sourceVarName + "." + *setCfg.From
var found bool
sourceMemberShapeRef, found = outputShape.MemberRefs[*setCfg.From]
if !found {
// This is likely a typo in the generator.yaml file...
msg := fmt.Sprintf(
"expected to find a member shape of %s called %s",
outputShape.ShapeName,
*setCfg.From,
)
panic(msg)
}
}
out += setResourceForScalar(
qualifiedTargetVar,
Expand Down
99 changes: 98 additions & 1 deletion pkg/generate/code/set_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3071,4 +3071,101 @@ func TestSetResource_EC2_SecurityGroups_SetResourceIdentifiers(t *testing.T) {
expected,
code.SetResource(crd.Config(), crd, model.OpTypeCreate, "resp", "ko", 1),
)
}
}

func TestSetResource_IAM_Role_NestedSetConfig(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

g := testutil.NewModelForService(t, "iam")

crd := testutil.GetCRDByName(t, g, "Role")
require.NotNil(crd)

// The input and output shapes for the PermissionsBoundary are different
// and we have a custom SetConfig for this field in our generator.yaml file
// to configure the SetResource function to set the value of the resource's
// Spec.PermissionsBoundary to the value of the (nested)
// GetRoleResponse.Role.PermissionsBoundary.PermissionsBoundaryArn field
expected := `
if ko.Status.ACKResourceMetadata == nil {
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
}
if resp.Role.Arn != nil {
arn := ackv1alpha1.AWSResourceName(*resp.Role.Arn)
ko.Status.ACKResourceMetadata.ARN = &arn
}
if resp.Role.AssumeRolePolicyDocument != nil {
ko.Spec.AssumeRolePolicyDocument = resp.Role.AssumeRolePolicyDocument
} else {
ko.Spec.AssumeRolePolicyDocument = nil
}
if resp.Role.CreateDate != nil {
ko.Status.CreateDate = &metav1.Time{*resp.Role.CreateDate}
} else {
ko.Status.CreateDate = nil
}
if resp.Role.Description != nil {
ko.Spec.Description = resp.Role.Description
} else {
ko.Spec.Description = nil
}
if resp.Role.MaxSessionDuration != nil {
ko.Spec.MaxSessionDuration = resp.Role.MaxSessionDuration
} else {
ko.Spec.MaxSessionDuration = nil
}
if resp.Role.Path != nil {
ko.Spec.Path = resp.Role.Path
} else {
ko.Spec.Path = nil
}
if resp.Role.PermissionsBoundary != nil {
ko.Spec.PermissionsBoundary = resp.Role.PermissionsBoundary.PermissionsBoundaryArn
} else {
ko.Spec.PermissionsBoundary = nil
}
if resp.Role.RoleId != nil {
ko.Status.RoleID = resp.Role.RoleId
} else {
ko.Status.RoleID = nil
}
if resp.Role.RoleLastUsed != nil {
f8 := &svcapitypes.RoleLastUsed{}
if resp.Role.RoleLastUsed.LastUsedDate != nil {
f8.LastUsedDate = &metav1.Time{*resp.Role.RoleLastUsed.LastUsedDate}
}
if resp.Role.RoleLastUsed.Region != nil {
f8.Region = resp.Role.RoleLastUsed.Region
}
ko.Status.RoleLastUsed = f8
} else {
ko.Status.RoleLastUsed = nil
}
if resp.Role.RoleName != nil {
ko.Spec.Name = resp.Role.RoleName
} else {
ko.Spec.Name = nil
}
if resp.Role.Tags != nil {
f10 := []*svcapitypes.Tag{}
for _, f10iter := range resp.Role.Tags {
f10elem := &svcapitypes.Tag{}
if f10iter.Key != nil {
f10elem.Key = f10iter.Key
}
if f10iter.Value != nil {
f10elem.Value = f10iter.Value
}
f10 = append(f10, f10elem)
}
ko.Spec.Tags = f10
} else {
ko.Spec.Tags = nil
}
`
assert.Equal(
expected,
code.SetResource(crd.Config(), crd, model.OpTypeGet, "resp", "ko", 1),
)
}
Loading