Skip to content

Commit 31d373d

Browse files
committed
Add new $field.set[].To configuration
Add new generator config to allow setting an `sdkField` from a specific CR field
1 parent 6657565 commit 31d373d

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed

pkg/config/field.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ type SetFieldConfig struct {
191191
// f17elem = *f17iter.DBSecurityGroupName
192192
// ```
193193
From *string `json:"from,omitempty"`
194+
// resources:
195+
// User:
196+
// fields:
197+
// URL:
198+
// set:
199+
// - method: Update
200+
// to: NewURL
201+
To *string `json:"to,omitempty"`
194202
// Ignore instructs the code generator to ignore this field in the Output
195203
// shape when setting the value of the resource's field in the Spec. This
196204
// is useful when we know that, for example, the returned value of field in

pkg/generate/code/set_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/aws-controllers-k8s/code-generator/pkg/util"
2828
)
2929

30-
// SetResource returns the Go code that sets a CRD's field value to the value
30+
// SetResource returns the Go code that sets a CRD's field value from the value
3131
// of an output shape's member fields. Status fields are always updated.
3232
//
3333
// Assume a CRD called Repository that looks like this pseudo-schema:

pkg/generate/code/set_sdk.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ func SetSDK(
222222
op.ExportedName,
223223
memberName,
224224
)
225+
226+
// Check if we have any configurations instructing the code
227+
// generator to set an SDK input field from this specific
228+
// field path.
229+
fallbackFieldName := r.GetSDKFieldSetter(opType, fieldName)
230+
if fallbackFieldName != "" {
231+
fieldName = fallbackFieldName
232+
}
233+
225234
inSpec, inStatus := r.HasMember(fieldName, op.ExportedName)
226235
if inSpec {
227236
sourceAdaptedVarName += cfg.PrefixConfig.SpecField

pkg/generate/code/set_sdk_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,3 +2164,27 @@ func Test_SetSDK_ECR_Repository_newListRequestPayload(t *testing.T) {
21642164
code.SetSDK(crd.Config(), crd, model.OpTypeList, "r.ko", "res", 1),
21652165
)
21662166
}
2167+
2168+
func TestSetSDK_IAM_User_NewPath(t *testing.T) {
2169+
assert := assert.New(t)
2170+
require := require.New(t)
2171+
2172+
g := testutil.NewModelForServiceWithOptions(t, "iam", &testutil.TestingModelOptions{
2173+
GeneratorConfigFile: "generator-user-newpath.yaml",
2174+
})
2175+
2176+
crd := testutil.GetCRDByName(t, g, "User")
2177+
require.NotNil(crd)
2178+
expected := `
2179+
if r.ko.Spec.Path != nil {
2180+
res.SetNewPath(*r.ko.Spec.Path)
2181+
}
2182+
if r.ko.Spec.UserName != nil {
2183+
res.SetUserName(*r.ko.Spec.UserName)
2184+
}
2185+
`
2186+
assert.Equal(
2187+
expected,
2188+
code.SetSDK(crd.Config(), crd, model.OpTypeUpdate, "r.ko", "res", 1),
2189+
)
2190+
}

pkg/model/crd.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,35 @@ func (r *CRD) GetPrimaryKeyField() (*Field, error) {
415415
return primaryField, nil
416416
}
417417

418+
// For simplicity, we assume that there will be only one setConfig for the
419+
// any unique sdkField, per operation. Which means that we will never set
420+
// two different sdk field from the same
421+
func (r *CRD) GetSDKFieldSetter(opType OpType, sdkField string) string {
422+
// At this stage nil-checks for r.cfg is not necessary
423+
for _, f := range r.Fields {
424+
if f.FieldConfig == nil {
425+
continue
426+
}
427+
rmMethod := ResourceManagerMethodFromOpType(opType)
428+
for _, setCfg := range f.FieldConfig.Set {
429+
if setCfg == nil {
430+
continue
431+
}
432+
if setCfg.Ignore == true || setCfg.To == nil {
433+
continue
434+
}
435+
// If the Method attribute is nil, that means the setter config applies to
436+
// all resource manager methods for this field.
437+
if setCfg.Method == nil || strings.EqualFold(rmMethod, *setCfg.Method) {
438+
if setCfg.To != nil && *setCfg.To == sdkField {
439+
return f.Names.Camel
440+
}
441+
}
442+
}
443+
}
444+
return ""
445+
}
446+
418447
// SetOutputCustomMethodName returns custom set output operation as *string for
419448
// given operation on custom resource
420449
func (r *CRD) SetOutputCustomMethodName(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ignore:
2+
resource_names:
3+
- AccessKey
4+
- AccountAlias
5+
- Group
6+
- InstanceProfile
7+
- LoginProfile
8+
- OpenIDConnectProvider
9+
- Policy
10+
- PolicyVersion
11+
- Role
12+
- SAMLProvider
13+
- ServiceLinkedRole
14+
- ServiceSpecificCredential
15+
# User
16+
- VirtualMFADevice
17+
resources:
18+
User:
19+
fields:
20+
Path:
21+
set:
22+
- method: Update
23+
to: NewPath

0 commit comments

Comments
 (0)