Skip to content

Add new $field.set[].To configuration #441

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 1 commit into from
May 11, 2023
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
13 changes: 13 additions & 0 deletions pkg/config/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ type SetFieldConfig struct {
// f17elem = *f17iter.DBSecurityGroupName
// ```
From *string `json:"from,omitempty"`
// To instructs the code generator to output Go code that sets the value of
// an Input sdkField with the content of a CR field.
//
// ```yaml
// resources:
// User:
// fields:
// URL:
// set:
// - method: Update
// to: NewURL
// ```
To *string `json:"to,omitempty"`
// Ignore instructs the code generator to ignore this field in the Output
// shape when setting the value of the resource's field in the Spec. This
// is useful when we know that, for example, the returned value of field in
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/code/set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/aws-controllers-k8s/code-generator/pkg/util"
)

// SetResource returns the Go code that sets a CRD's field value to the value
// SetResource returns the Go code that sets a CRD's field value from the value
// of an output shape's member fields. Status fields are always updated.
//
// Assume a CRD called Repository that looks like this pseudo-schema:
Expand Down
9 changes: 9 additions & 0 deletions pkg/generate/code/set_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ func SetSDK(
op.ExportedName,
memberName,
)

// Check if we have any configurations instructing the code
// generator to set an SDK input field from this specific
// field path.
fallbackFieldName := r.GetMatchingInputShapeFieldName(opType, fieldName)
if fallbackFieldName != "" {
fieldName = fallbackFieldName
}

inSpec, inStatus := r.HasMember(fieldName, op.ExportedName)
if inSpec {
sourceAdaptedVarName += cfg.PrefixConfig.SpecField
Expand Down
24 changes: 24 additions & 0 deletions pkg/generate/code/set_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2164,3 +2164,27 @@ func Test_SetSDK_ECR_Repository_newListRequestPayload(t *testing.T) {
code.SetSDK(crd.Config(), crd, model.OpTypeList, "r.ko", "res", 1),
)
}

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

g := testutil.NewModelForServiceWithOptions(t, "iam", &testutil.TestingModelOptions{
GeneratorConfigFile: "generator-user-newpath.yaml",
})

crd := testutil.GetCRDByName(t, g, "User")
require.NotNil(crd)
expected := `
if r.ko.Spec.Path != nil {
res.SetNewPath(*r.ko.Spec.Path)
}
if r.ko.Spec.UserName != nil {
res.SetUserName(*r.ko.Spec.UserName)
}
`
assert.Equal(
expected,
code.SetSDK(crd.Config(), crd, model.OpTypeUpdate, "r.ko", "res", 1),
)
}
30 changes: 30 additions & 0 deletions pkg/model/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,36 @@ func (r *CRD) GetPrimaryKeyField() (*Field, error) {
return primaryField, nil
}

// GetMatchingInputShapeFieldName returns the name of the field in the Input shape.
// For simplicity, we assume that there will be only one setConfig for the
// any unique sdkField, per operation. Which means that we will never set
// two different sdk field from the same
func (r *CRD) GetMatchingInputShapeFieldName(opType OpType, sdkField string) string {
// At this stage nil-checks for r.cfg is not necessary
for _, f := range r.Fields {
if f.FieldConfig == nil {
continue
}
rmMethod := ResourceManagerMethodFromOpType(opType)
for _, setCfg := range f.FieldConfig.Set {
if setCfg == nil {
continue
}
if setCfg.Ignore == true || setCfg.To == nil {
continue
}
// If the Method attribute is nil, that means the setter config applies to
// all resource manager methods for this field.
if setCfg.Method == nil || strings.EqualFold(rmMethod, *setCfg.Method) {
if setCfg.To != nil && *setCfg.To == sdkField {
return f.Names.Camel
}
}
}
}
return ""
}

// SetOutputCustomMethodName returns custom set output operation as *string for
// given operation on custom resource
func (r *CRD) SetOutputCustomMethodName(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ignore:
resource_names:
- AccessKey
- AccountAlias
- Group
- InstanceProfile
- LoginProfile
- OpenIDConnectProvider
- Policy
- PolicyVersion
- Role
- SAMLProvider
- ServiceLinkedRole
- ServiceSpecificCredential
# User
- VirtualMFADevice
resources:
User:
fields:
Path:
set:
- method: Update
to: NewPath