Skip to content
Open
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
14 changes: 8 additions & 6 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ type ResourceConfig struct {
// TagConfig contains instructions for the code generator to generate
// custom code for ensuring tags
TagConfig *TagConfig `json:"tags,omitempty"`
// IgnoreIdempotencyToken instructs the code generator to automatically
// exclude fields that have the smithy.api#idempotencyToken trait (or the
// equivalent idempotencyToken trait in the v1 SDK model). These fields are
// SDK implementation details that are auto-filled by the SDK middleware
// when nil and should not be exposed in the CRD.
IgnoreIdempotencyToken bool `json:"ignore_idempotency_token,omitempty"`
// IncludeIdempotencyToken instructs the code generator to keep fields
// that have the smithy.api#idempotencyToken trait (or the equivalent
// idempotencyToken trait in the v1 SDK model). By default, these fields
// are automatically excluded because they are SDK implementation details
// that are auto-filled by the SDK middleware when nil and should not be
// exposed in the CRD. Set this to true only if you need to expose
// idempotency token fields in the CRD.
Comment on lines +124 to +125
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p: It would be good to include an example of when this would be needed or clarify that this is intended to avoid breaking CRD changes in resources that already have it.

IncludeIdempotencyToken bool `json:"include_idempotency_token,omitempty"`
}

// TagConfig instructs the code generator on how to generate functions that
Expand Down
39 changes: 26 additions & 13 deletions pkg/generate/code/set_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2207,9 +2207,6 @@ func TestSetSDK_MQ_Broker_Create(t *testing.T) {
}
res.Configuration = f3
}
if r.ko.Spec.CreatorRequestID != nil {
res.CreatorRequestId = r.ko.Spec.CreatorRequestID
}
if r.ko.Spec.DeploymentMode != nil {
res.DeploymentMode = svcsdktypes.DeploymentMode(*r.ko.Spec.DeploymentMode)
}
Expand Down Expand Up @@ -5179,9 +5176,6 @@ func TestEMRContainers_VirtualCluster_WithUnion(t *testing.T) {
assert.NotNil(crd.Ops.Create)

expected := `
if r.ko.Spec.ClientToken != nil {
res.ClientToken = r.ko.Spec.ClientToken
}
if r.ko.Spec.ContainerProvider != nil {
f1 := &svcsdktypes.ContainerProvider{}
if r.ko.Spec.ContainerProvider.ID != nil {
Expand Down Expand Up @@ -6496,11 +6490,10 @@ func TestSetSDK_EMRServerless_Application_Create(t *testing.T) {
"Should use original SDK shape name WorkerTypeSpecificationInput, not renamed version")

// Verify idempotency token fields are excluded from the generated code
// when ignore_idempotency_token is enabled on the resource in generator.yaml.
// The ClientToken field in CreateApplicationInput has the
// smithy.api#idempotencyToken trait, and the emr-serverless test config
// has ignore_idempotency_token: true on Application, so the code generator
// should exclude it from the CRD and generated SDK code. The SDK middleware
// by default. The ClientToken field in CreateApplicationInput has the
// smithy.api#idempotencyToken trait, so the code generator should
// exclude it from the CRD and generated SDK code unless
// include_idempotency_token is set to true. The SDK middleware
// auto-fills it with a UUID when nil.
assert.NotContains(got, "ClientToken",
"ClientToken (idempotency token) should not appear in Create SDK code")
Expand Down Expand Up @@ -6535,8 +6528,7 @@ func TestSetSDK_EMRServerless_Application_Update(t *testing.T) {
"Should use original SDK shape name in Update operation")
}

// Verify idempotency token fields are excluded from Update as well
// when ignore_idempotency_token is enabled on the resource.
// Verify idempotency token fields are excluded from Update by default.
// UpdateApplicationInput.ClientToken also has the
// smithy.api#idempotencyToken trait.
assert.NotContains(got, "ClientToken",
Expand Down Expand Up @@ -6566,6 +6558,27 @@ func TestSetSDK_EMRServerless_Application_InitialCapacityConfig(t *testing.T) {
assert.Contains(got, "WorkerCount")
}

// TestSetSDK_EMRServerless_Application_IncludeIdempotencyToken tests that
// setting include_idempotency_token: true in generator.yaml causes the
// ClientToken field to appear in the generated SDK code.
func TestSetSDK_EMRServerless_Application_IncludeIdempotencyToken(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

g := testutil.NewModelForServiceWithOptions(t, "emr-serverless", &testutil.TestingModelOptions{
GeneratorConfigFile: "generator-include-idempotency-token.yaml",
})

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

got, err := code.SetSDK(crd.Config(), crd, model.OpTypeCreate, "r.ko", "res", 1)
require.NoError(err)

assert.Contains(got, "ClientToken",
"ClientToken should appear in Create SDK code when include_idempotency_token is true")
}

// TestSetSDK_QuickSight_DataSet_Create tests that the SetSDK code generation
// for the QuickSight DataSet resource correctly handles:
//
Expand Down
13 changes: 7 additions & 6 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ func (m *Model) GetCRDs() ([]*CRD, error) {
// Idempotency tokens are SDK implementation details that are
// auto-filled by the SDK middleware when nil. They should not
// be exposed in the CRD as they are not resource properties.
// This filtering is opt-in via resources.<name>.ignore_idempotency_token
// in generator.yaml.
resConfig := m.cfg.GetResourceConfig(crdName)
if resConfig != nil && resConfig.IgnoreIdempotencyToken &&
(memberShapeRef.IdempotencyToken || memberShapeRef.Shape.IdempotencyToken) {
continue
// This filtering is on by default. Set include_idempotency_token
// to true in generator.yaml to keep these fields.
if memberShapeRef.IdempotencyToken || memberShapeRef.Shape.IdempotencyToken {
resConfig := m.cfg.GetResourceConfig(crdName)
if resConfig == nil || !resConfig.IncludeIdempotencyToken {
continue
}
}

// If this is the wrapper field and we have input_wrapper_field_path
Expand Down
1 change: 0 additions & 1 deletion pkg/model/model_ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func TestEC2_Volume(t *testing.T) {

expSpecFieldCamel := []string{
"AvailabilityZone",
"ClientToken",
"DryRun",
"Encrypted",
"IOPS",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ignore:
resource_names: []
field_paths:
- Configuration.Configurations
sdk_names:
model_name: emr-serverless
resources:
Application:
include_idempotency_token: true
fields:
WorkerTypeSpecifications:
from:
operation: GetApplication
path: Application.WorkerTypeSpecifications
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ sdk_names:
model_name: emr-serverless
resources:
Application:
ignore_idempotency_token: true
fields:
WorkerTypeSpecifications:
from:
Expand Down