Skip to content

Commit 5204077

Browse files
authored
Allow calling GoCodeSetResourceForStruct for non-CRD SDK shapes (#387)
Issue: aws-controllers-k8s/community#1609 Previously, the code-generator was unable to let us use `GoCodeSetResourceForStruct` for fields that are not part of a CRD top level fields. This caused issues when trying to generate custom code that transforms an eventbridge `v1alpha1.Target` to an `svcsdk.Target`. This patch fixes the issue by allowing users to call `GoCodeSetResourceForStruct` for any field, not just CRD top level fields. In addition, unit tests have been added to ensure the correct behavior. This patch is needed to allow us to properly execute the template written here https://github.com/embano1/eventbridge-controller/blob/aminemicha/templates/hooks/rule/sdk_file_end.go.tpl By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 7b72e0a commit 5204077

File tree

6 files changed

+5894
-10
lines changed

6 files changed

+5894
-10
lines changed

pkg/generate/ack/controller.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,20 @@ var (
111111
return code.SetSDKForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, sourceFieldPath, sourceVarName, indentLevel)
112112
},
113113
"GoCodeSetResourceForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceVarName string, sourceShapeRef *awssdkmodel.ShapeRef, indentLevel int) string {
114-
f, ok := r.Fields[targetFieldName]
115-
if !ok {
116-
return ""
117-
}
114+
var setCfg *ackgenconfig.SetFieldConfig = nil
118115
// We may have some special instructions for how to handle setting the
119116
// field value...
120-
setCfg := f.GetSetterConfig(model.OpTypeList)
121-
117+
//
118+
// We do not want to return an empty string if the setConfig is not provided,
119+
// so that we can allow non top-level fields to be used with this function.
120+
f, ok := r.Fields[targetFieldName]
121+
if ok {
122+
setCfg = f.GetSetterConfig(ackmodel.OpTypeList)
123+
}
122124
if setCfg != nil && setCfg.Ignore {
123125
return ""
124126
}
125-
return code.SetResourceForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, setCfg, sourceVarName, sourceShapeRef, "", model.OpTypeList, indentLevel)
127+
return code.SetResourceForStruct(r.Config(), r, targetVarName, targetShapeRef, setCfg, sourceVarName, sourceShapeRef, "", model.OpTypeList, indentLevel)
126128
},
127129
"GoCodeCompare": func(r *ackmodel.CRD, deltaVarName string, sourceVarName string, targetVarName string, indentLevel int) string {
128130
return code.CompareResource(r.Config(), r, deltaVarName, sourceVarName, targetVarName, indentLevel)

pkg/generate/code/set_resource.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,6 @@ func setResourceForContainer(
12041204
case "structure":
12051205
return SetResourceForStruct(
12061206
cfg, r,
1207-
targetFieldName,
12081207
targetVarName,
12091208
targetShapeRef,
12101209
targetSetCfg,
@@ -1255,8 +1254,6 @@ func setResourceForContainer(
12551254
func SetResourceForStruct(
12561255
cfg *ackgenconfig.Config,
12571256
r *model.CRD,
1258-
// The name of the CR field we're outputting for
1259-
targetFieldName string,
12601257
// The variable name that we want to set a value to
12611258
targetVarName string,
12621259
// Shape Ref of the target struct field

pkg/generate/code/set_resource_test.go

Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,6 +3273,336 @@ func TestSetResource_EC2_DHCPOptions_NestedSetConfig(t *testing.T) {
32733273
)
32743274
}
32753275

3276+
func TestSetResource_EventBridge_Rule_SetResourceForStruct(t *testing.T) {
3277+
assert := assert.New(t)
3278+
require := require.New(t)
3279+
3280+
g := testutil.NewModelForService(t, "eventbridge")
3281+
3282+
crd := testutil.GetCRDByName(t, g, "Rule")
3283+
require.NotNil(crd)
3284+
3285+
// Getting CRD Target shape
3286+
field, ok := crd.SpecFields["Targets"]
3287+
require.True(ok)
3288+
koShape := &field.ShapeRef.Shape.MemberRef
3289+
3290+
// Getting SDK Target shape
3291+
putTargetsOP, ok := g.SDKAPI.API.Operations["PutTargets"]
3292+
require.True(ok)
3293+
targetsShape, ok := putTargetsOP.InputRef.Shape.MemberRefs["Targets"]
3294+
require.True(ok)
3295+
targetShape := &targetsShape.Shape.MemberRef
3296+
3297+
expected := ` if resp.Arn != nil {
3298+
fx.ARN = resp.Arn
3299+
}
3300+
if resp.BatchParameters != nil {
3301+
fxf1 := &svcapitypes.BatchParameters{}
3302+
if resp.BatchParameters.ArrayProperties != nil {
3303+
fxf1f0 := &svcapitypes.BatchArrayProperties{}
3304+
if resp.BatchParameters.ArrayProperties.Size != nil {
3305+
fxf1f0.Size = resp.BatchParameters.ArrayProperties.Size
3306+
}
3307+
fxf1.ArrayProperties = fxf1f0
3308+
}
3309+
if resp.BatchParameters.JobDefinition != nil {
3310+
fxf1.JobDefinition = resp.BatchParameters.JobDefinition
3311+
}
3312+
if resp.BatchParameters.JobName != nil {
3313+
fxf1.JobName = resp.BatchParameters.JobName
3314+
}
3315+
if resp.BatchParameters.RetryStrategy != nil {
3316+
fxf1f3 := &svcapitypes.BatchRetryStrategy{}
3317+
if resp.BatchParameters.RetryStrategy.Attempts != nil {
3318+
fxf1f3.Attempts = resp.BatchParameters.RetryStrategy.Attempts
3319+
}
3320+
fxf1.RetryStrategy = fxf1f3
3321+
}
3322+
fx.BatchParameters = fxf1
3323+
}
3324+
if resp.DeadLetterConfig != nil {
3325+
fxf2 := &svcapitypes.DeadLetterConfig{}
3326+
if resp.DeadLetterConfig.Arn != nil {
3327+
fxf2.ARN = resp.DeadLetterConfig.Arn
3328+
}
3329+
fx.DeadLetterConfig = fxf2
3330+
}
3331+
if resp.EcsParameters != nil {
3332+
fxf3 := &svcapitypes.EcsParameters{}
3333+
if resp.EcsParameters.CapacityProviderStrategy != nil {
3334+
fxf3f0 := []*svcapitypes.CapacityProviderStrategyItem{}
3335+
for _, fxf3f0iter := range resp.EcsParameters.CapacityProviderStrategy {
3336+
fxf3f0elem := &svcapitypes.CapacityProviderStrategyItem{}
3337+
if fxf3f0iter.Base != nil {
3338+
fxf3f0elem.Base = fxf3f0iter.Base
3339+
}
3340+
if fxf3f0iter.CapacityProvider != nil {
3341+
fxf3f0elem.CapacityProvider = fxf3f0iter.CapacityProvider
3342+
}
3343+
if fxf3f0iter.Weight != nil {
3344+
fxf3f0elem.Weight = fxf3f0iter.Weight
3345+
}
3346+
fxf3f0 = append(fxf3f0, fxf3f0elem)
3347+
}
3348+
fxf3.CapacityProviderStrategy = fxf3f0
3349+
}
3350+
if resp.EcsParameters.EnableECSManagedTags != nil {
3351+
fxf3.EnableECSManagedTags = resp.EcsParameters.EnableECSManagedTags
3352+
}
3353+
if resp.EcsParameters.EnableExecuteCommand != nil {
3354+
fxf3.EnableExecuteCommand = resp.EcsParameters.EnableExecuteCommand
3355+
}
3356+
if resp.EcsParameters.Group != nil {
3357+
fxf3.Group = resp.EcsParameters.Group
3358+
}
3359+
if resp.EcsParameters.LaunchType != nil {
3360+
fxf3.LaunchType = resp.EcsParameters.LaunchType
3361+
}
3362+
if resp.EcsParameters.NetworkConfiguration != nil {
3363+
fxf3f5 := &svcapitypes.NetworkConfiguration{}
3364+
if resp.EcsParameters.NetworkConfiguration.AwsvpcConfiguration != nil {
3365+
fxf3f5f0 := &svcapitypes.AWSVPCConfiguration{}
3366+
if resp.EcsParameters.NetworkConfiguration.AwsvpcConfiguration.AssignPublicIp != nil {
3367+
fxf3f5f0.AssignPublicIP = resp.EcsParameters.NetworkConfiguration.AwsvpcConfiguration.AssignPublicIp
3368+
}
3369+
if resp.EcsParameters.NetworkConfiguration.AwsvpcConfiguration.SecurityGroups != nil {
3370+
fxf3f5f0f1 := []*string{}
3371+
for _, fxf3f5f0f1iter := range resp.EcsParameters.NetworkConfiguration.AwsvpcConfiguration.SecurityGroups {
3372+
var fxf3f5f0f1elem string
3373+
fxf3f5f0f1elem = *fxf3f5f0f1iter
3374+
fxf3f5f0f1 = append(fxf3f5f0f1, &fxf3f5f0f1elem)
3375+
}
3376+
fxf3f5f0.SecurityGroups = fxf3f5f0f1
3377+
}
3378+
if resp.EcsParameters.NetworkConfiguration.AwsvpcConfiguration.Subnets != nil {
3379+
fxf3f5f0f2 := []*string{}
3380+
for _, fxf3f5f0f2iter := range resp.EcsParameters.NetworkConfiguration.AwsvpcConfiguration.Subnets {
3381+
var fxf3f5f0f2elem string
3382+
fxf3f5f0f2elem = *fxf3f5f0f2iter
3383+
fxf3f5f0f2 = append(fxf3f5f0f2, &fxf3f5f0f2elem)
3384+
}
3385+
fxf3f5f0.Subnets = fxf3f5f0f2
3386+
}
3387+
fxf3f5.AWSvpcConfiguration = fxf3f5f0
3388+
}
3389+
fxf3.NetworkConfiguration = fxf3f5
3390+
}
3391+
if resp.EcsParameters.PlacementConstraints != nil {
3392+
fxf3f6 := []*svcapitypes.PlacementConstraint{}
3393+
for _, fxf3f6iter := range resp.EcsParameters.PlacementConstraints {
3394+
fxf3f6elem := &svcapitypes.PlacementConstraint{}
3395+
if fxf3f6iter.Expression != nil {
3396+
fxf3f6elem.Expression = fxf3f6iter.Expression
3397+
}
3398+
if fxf3f6iter.Type != nil {
3399+
fxf3f6elem.Type = fxf3f6iter.Type
3400+
}
3401+
fxf3f6 = append(fxf3f6, fxf3f6elem)
3402+
}
3403+
fxf3.PlacementConstraints = fxf3f6
3404+
}
3405+
if resp.EcsParameters.PlacementStrategy != nil {
3406+
fxf3f7 := []*svcapitypes.PlacementStrategy{}
3407+
for _, fxf3f7iter := range resp.EcsParameters.PlacementStrategy {
3408+
fxf3f7elem := &svcapitypes.PlacementStrategy{}
3409+
if fxf3f7iter.Field != nil {
3410+
fxf3f7elem.Field = fxf3f7iter.Field
3411+
}
3412+
if fxf3f7iter.Type != nil {
3413+
fxf3f7elem.Type = fxf3f7iter.Type
3414+
}
3415+
fxf3f7 = append(fxf3f7, fxf3f7elem)
3416+
}
3417+
fxf3.PlacementStrategy = fxf3f7
3418+
}
3419+
if resp.EcsParameters.PlatformVersion != nil {
3420+
fxf3.PlatformVersion = resp.EcsParameters.PlatformVersion
3421+
}
3422+
if resp.EcsParameters.PropagateTags != nil {
3423+
fxf3.PropagateTags = resp.EcsParameters.PropagateTags
3424+
}
3425+
if resp.EcsParameters.ReferenceId != nil {
3426+
fxf3.ReferenceID = resp.EcsParameters.ReferenceId
3427+
}
3428+
if resp.EcsParameters.Tags != nil {
3429+
fxf3f11 := []*svcapitypes.Tag{}
3430+
for _, fxf3f11iter := range resp.EcsParameters.Tags {
3431+
fxf3f11elem := &svcapitypes.Tag{}
3432+
if fxf3f11iter.Key != nil {
3433+
fxf3f11elem.Key = fxf3f11iter.Key
3434+
}
3435+
if fxf3f11iter.Value != nil {
3436+
fxf3f11elem.Value = fxf3f11iter.Value
3437+
}
3438+
fxf3f11 = append(fxf3f11, fxf3f11elem)
3439+
}
3440+
fxf3.Tags = fxf3f11
3441+
}
3442+
if resp.EcsParameters.TaskCount != nil {
3443+
fxf3.TaskCount = resp.EcsParameters.TaskCount
3444+
}
3445+
if resp.EcsParameters.TaskDefinitionArn != nil {
3446+
fxf3.TaskDefinitionARN = resp.EcsParameters.TaskDefinitionArn
3447+
}
3448+
fx.EcsParameters = fxf3
3449+
}
3450+
if resp.HttpParameters != nil {
3451+
fxf4 := &svcapitypes.HTTPParameters{}
3452+
if resp.HttpParameters.HeaderParameters != nil {
3453+
fxf4f0 := map[string]*string{}
3454+
for fxf4f0key, fxf4f0valiter := range resp.HttpParameters.HeaderParameters {
3455+
var fxf4f0val string
3456+
fxf4f0val = *fxf4f0valiter
3457+
fxf4f0[fxf4f0key] = &fxf4f0val
3458+
}
3459+
fxf4.HeaderParameters = fxf4f0
3460+
}
3461+
if resp.HttpParameters.PathParameterValues != nil {
3462+
fxf4f1 := []*string{}
3463+
for _, fxf4f1iter := range resp.HttpParameters.PathParameterValues {
3464+
var fxf4f1elem string
3465+
fxf4f1elem = *fxf4f1iter
3466+
fxf4f1 = append(fxf4f1, &fxf4f1elem)
3467+
}
3468+
fxf4.PathParameterValues = fxf4f1
3469+
}
3470+
if resp.HttpParameters.QueryStringParameters != nil {
3471+
fxf4f2 := map[string]*string{}
3472+
for fxf4f2key, fxf4f2valiter := range resp.HttpParameters.QueryStringParameters {
3473+
var fxf4f2val string
3474+
fxf4f2val = *fxf4f2valiter
3475+
fxf4f2[fxf4f2key] = &fxf4f2val
3476+
}
3477+
fxf4.QueryStringParameters = fxf4f2
3478+
}
3479+
fx.HTTPParameters = fxf4
3480+
}
3481+
if resp.Id != nil {
3482+
fx.ID = resp.Id
3483+
}
3484+
if resp.Input != nil {
3485+
fx.Input = resp.Input
3486+
}
3487+
if resp.InputPath != nil {
3488+
fx.InputPath = resp.InputPath
3489+
}
3490+
if resp.InputTransformer != nil {
3491+
fxf8 := &svcapitypes.InputTransformer{}
3492+
if resp.InputTransformer.InputPathsMap != nil {
3493+
fxf8f0 := map[string]*string{}
3494+
for fxf8f0key, fxf8f0valiter := range resp.InputTransformer.InputPathsMap {
3495+
var fxf8f0val string
3496+
fxf8f0val = *fxf8f0valiter
3497+
fxf8f0[fxf8f0key] = &fxf8f0val
3498+
}
3499+
fxf8.InputPathsMap = fxf8f0
3500+
}
3501+
if resp.InputTransformer.InputTemplate != nil {
3502+
fxf8.InputTemplate = resp.InputTransformer.InputTemplate
3503+
}
3504+
fx.InputTransformer = fxf8
3505+
}
3506+
if resp.KinesisParameters != nil {
3507+
fxf9 := &svcapitypes.KinesisParameters{}
3508+
if resp.KinesisParameters.PartitionKeyPath != nil {
3509+
fxf9.PartitionKeyPath = resp.KinesisParameters.PartitionKeyPath
3510+
}
3511+
fx.KinesisParameters = fxf9
3512+
}
3513+
if resp.RedshiftDataParameters != nil {
3514+
fxf10 := &svcapitypes.RedshiftDataParameters{}
3515+
if resp.RedshiftDataParameters.Database != nil {
3516+
fxf10.Database = resp.RedshiftDataParameters.Database
3517+
}
3518+
if resp.RedshiftDataParameters.DbUser != nil {
3519+
fxf10.DBUser = resp.RedshiftDataParameters.DbUser
3520+
}
3521+
if resp.RedshiftDataParameters.SecretManagerArn != nil {
3522+
fxf10.SecretManagerARN = resp.RedshiftDataParameters.SecretManagerArn
3523+
}
3524+
if resp.RedshiftDataParameters.Sql != nil {
3525+
fxf10.Sql = resp.RedshiftDataParameters.Sql
3526+
}
3527+
if resp.RedshiftDataParameters.StatementName != nil {
3528+
fxf10.StatementName = resp.RedshiftDataParameters.StatementName
3529+
}
3530+
if resp.RedshiftDataParameters.WithEvent != nil {
3531+
fxf10.WithEvent = resp.RedshiftDataParameters.WithEvent
3532+
}
3533+
fx.RedshiftDataParameters = fxf10
3534+
}
3535+
if resp.RetryPolicy != nil {
3536+
fxf11 := &svcapitypes.RetryPolicy{}
3537+
if resp.RetryPolicy.MaximumEventAgeInSeconds != nil {
3538+
fxf11.MaximumEventAgeInSeconds = resp.RetryPolicy.MaximumEventAgeInSeconds
3539+
}
3540+
if resp.RetryPolicy.MaximumRetryAttempts != nil {
3541+
fxf11.MaximumRetryAttempts = resp.RetryPolicy.MaximumRetryAttempts
3542+
}
3543+
fx.RetryPolicy = fxf11
3544+
}
3545+
if resp.RoleArn != nil {
3546+
fx.RoleARN = resp.RoleArn
3547+
}
3548+
if resp.RunCommandParameters != nil {
3549+
fxf13 := &svcapitypes.RunCommandParameters{}
3550+
if resp.RunCommandParameters.RunCommandTargets != nil {
3551+
fxf13f0 := []*svcapitypes.RunCommandTarget{}
3552+
for _, fxf13f0iter := range resp.RunCommandParameters.RunCommandTargets {
3553+
fxf13f0elem := &svcapitypes.RunCommandTarget{}
3554+
if fxf13f0iter.Key != nil {
3555+
fxf13f0elem.Key = fxf13f0iter.Key
3556+
}
3557+
if fxf13f0iter.Values != nil {
3558+
fxf13f0elemf1 := []*string{}
3559+
for _, fxf13f0elemf1iter := range fxf13f0iter.Values {
3560+
var fxf13f0elemf1elem string
3561+
fxf13f0elemf1elem = *fxf13f0elemf1iter
3562+
fxf13f0elemf1 = append(fxf13f0elemf1, &fxf13f0elemf1elem)
3563+
}
3564+
fxf13f0elem.Values = fxf13f0elemf1
3565+
}
3566+
fxf13f0 = append(fxf13f0, fxf13f0elem)
3567+
}
3568+
fxf13.RunCommandTargets = fxf13f0
3569+
}
3570+
fx.RunCommandParameters = fxf13
3571+
}
3572+
if resp.SageMakerPipelineParameters != nil {
3573+
fxf14 := &svcapitypes.SageMakerPipelineParameters{}
3574+
if resp.SageMakerPipelineParameters.PipelineParameterList != nil {
3575+
fxf14f0 := []*svcapitypes.SageMakerPipelineParameter{}
3576+
for _, fxf14f0iter := range resp.SageMakerPipelineParameters.PipelineParameterList {
3577+
fxf14f0elem := &svcapitypes.SageMakerPipelineParameter{}
3578+
if fxf14f0iter.Name != nil {
3579+
fxf14f0elem.Name = fxf14f0iter.Name
3580+
}
3581+
if fxf14f0iter.Value != nil {
3582+
fxf14f0elem.Value = fxf14f0iter.Value
3583+
}
3584+
fxf14f0 = append(fxf14f0, fxf14f0elem)
3585+
}
3586+
fxf14.PipelineParameterList = fxf14f0
3587+
}
3588+
fx.SageMakerPipelineParameters = fxf14
3589+
}
3590+
if resp.SqsParameters != nil {
3591+
fxf15 := &svcapitypes.SQSParameters{}
3592+
if resp.SqsParameters.MessageGroupId != nil {
3593+
fxf15.MessageGroupID = resp.SqsParameters.MessageGroupId
3594+
}
3595+
fx.SQSParameters = fxf15
3596+
}
3597+
`
3598+
assert.Equal(
3599+
expected,
3600+
code.SetResourceForStruct(
3601+
crd.Config(), crd, "fx", koShape, nil, "resp", targetShape, "", model.OpTypeList, 1,
3602+
),
3603+
)
3604+
}
3605+
32763606
func TestSetResource_EC2_Instance_Create(t *testing.T) {
32773607
// Check that the RunInstances output (Reservation)
32783608
// uses the first element of the returned list of Instances

0 commit comments

Comments
 (0)