Skip to content

Commit eef7ca5

Browse files
committed
implement SetSDK for Secret nested fields
Implements the `pkg/generate/code.SetSDK()` function for all nested fields that are SecretKeyReference types. This involved passing a field path down into the recursive `setSDKForXXX()` functions to keep track of where we were in the processing of nested fields. We look up whether a field is a SecretKeyReference by calling the `pkg/model.CRD:IsSecretField()` method, which accepts a field path, which is why we needed to pass this field path down into the recursive code generators. Issue aws-controllers-k8s/community#743
1 parent 06e0102 commit eef7ca5

File tree

2 files changed

+233
-2
lines changed

2 files changed

+233
-2
lines changed

pkg/generate/code/set_sdk.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,15 @@ func SetSDK(
226226
if found {
227227
sourceAdaptedVarName += cfg.PrefixConfig.SpecField
228228
} else {
229-
f, found = r.StatusFields[memberName]
229+
f, found = r.StatusFields[renamedName]
230230
if !found {
231231
// TODO(jaypipes): check generator config for exceptions?
232232
continue
233233
}
234234
sourceAdaptedVarName += cfg.PrefixConfig.StatusField
235235
}
236236
sourceAdaptedVarName += "." + f.Names.Camel
237+
sourceFieldPath := f.Names.Camel
237238

238239
if r.IsSecretField(memberName) {
239240
out += setSDKForSecret(
@@ -321,6 +322,7 @@ func SetSDK(
321322
cfg, r,
322323
memberName,
323324
memberVarName,
325+
sourceFieldPath,
324326
sourceAdaptedVarName,
325327
memberShapeRef,
326328
indentLevel+1,
@@ -330,6 +332,7 @@ func SetSDK(
330332
memberName,
331333
targetVarName,
332334
inputShape.Type,
335+
sourceFieldPath,
333336
memberVarName,
334337
memberShapeRef,
335338
indentLevel+1,
@@ -341,6 +344,7 @@ func SetSDK(
341344
memberName,
342345
targetVarName,
343346
inputShape.Type,
347+
sourceFieldPath,
344348
sourceAdaptedVarName,
345349
memberShapeRef,
346350
indentLevel+1,
@@ -508,6 +512,7 @@ func SetSDKGetAttributes(
508512
memberName,
509513
targetVarName,
510514
inputShape.Type,
515+
cleanMemberName,
511516
sourceVarPath,
512517
field.ShapeRef,
513518
indentLevel+1,
@@ -702,6 +707,7 @@ func SetSDKSetAttributes(
702707
memberName,
703708
targetVarName,
704709
inputShape.Type,
710+
cleanMemberName,
705711
sourceVarPath,
706712
field.ShapeRef,
707713
indentLevel+1,
@@ -724,6 +730,8 @@ func setSDKForContainer(
724730
targetFieldName string,
725731
// The variable name that we want to set a value to
726732
targetVarName string,
733+
// The path to the field that we access our source value from
734+
sourceFieldPath string,
727735
// The struct or struct field that we access our source value from
728736
sourceVarName string,
729737
// ShapeRef of the target struct field
@@ -737,6 +745,7 @@ func setSDKForContainer(
737745
targetFieldName,
738746
targetVarName,
739747
targetShapeRef,
748+
sourceFieldPath,
740749
sourceVarName,
741750
indentLevel,
742751
)
@@ -746,6 +755,7 @@ func setSDKForContainer(
746755
targetFieldName,
747756
targetVarName,
748757
targetShapeRef,
758+
sourceFieldPath,
749759
sourceVarName,
750760
indentLevel,
751761
)
@@ -755,6 +765,7 @@ func setSDKForContainer(
755765
targetFieldName,
756766
targetVarName,
757767
targetShapeRef,
768+
sourceFieldPath,
758769
sourceVarName,
759770
indentLevel,
760771
)
@@ -764,6 +775,7 @@ func setSDKForContainer(
764775
targetFieldName,
765776
targetVarName,
766777
targetShapeRef.Shape.Type,
778+
sourceFieldPath,
767779
sourceVarName,
768780
targetShapeRef,
769781
indentLevel,
@@ -842,6 +854,8 @@ func setSDKForStruct(
842854
targetVarName string,
843855
// Shape Ref of the target struct field
844856
targetShapeRef *awssdkmodel.ShapeRef,
857+
// The path to the field that we access our source value from
858+
sourceFieldPath string,
845859
// The struct or struct field that we access our source value from
846860
sourceVarName string,
847861
indentLevel int,
@@ -855,14 +869,28 @@ func setSDKForStruct(
855869
memberShape := memberShapeRef.Shape
856870
cleanMemberNames := names.New(memberName)
857871
cleanMemberName := cleanMemberNames.Camel
858-
memberVarName := fmt.Sprintf("%sf%d", targetVarName, memberIndex)
859872
sourceAdaptedVarName := sourceVarName + "." + cleanMemberName
873+
memberFieldPath := sourceFieldPath + "." + cleanMemberName
874+
if r.IsSecretField(memberFieldPath) {
875+
out += setSDKForSecret(
876+
cfg, r,
877+
memberName,
878+
targetVarName,
879+
sourceAdaptedVarName,
880+
indentLevel,
881+
)
882+
continue
883+
}
860884
out += fmt.Sprintf(
861885
"%sif %s != nil {\n", indent, sourceAdaptedVarName,
862886
)
863887
switch memberShape.Type {
864888
case "list", "structure", "map":
865889
{
890+
memberVarName := fmt.Sprintf(
891+
"%sf%d",
892+
targetVarName, memberIndex,
893+
)
866894
out += varEmptyConstructorSDKType(
867895
cfg, r,
868896
memberVarName,
@@ -873,6 +901,7 @@ func setSDKForStruct(
873901
cfg, r,
874902
memberName,
875903
memberVarName,
904+
memberFieldPath,
876905
sourceAdaptedVarName,
877906
memberShapeRef,
878907
indentLevel+1,
@@ -882,6 +911,7 @@ func setSDKForStruct(
882911
memberName,
883912
targetVarName,
884913
targetShape.Type,
914+
memberFieldPath,
885915
memberVarName,
886916
memberShapeRef,
887917
indentLevel+1,
@@ -893,6 +923,7 @@ func setSDKForStruct(
893923
memberName,
894924
targetVarName,
895925
targetShape.Type,
926+
memberFieldPath,
896927
sourceAdaptedVarName,
897928
memberShapeRef,
898929
indentLevel+1,
@@ -916,6 +947,8 @@ func setSDKForSlice(
916947
targetVarName string,
917948
// Shape Ref of the target struct field
918949
targetShapeRef *awssdkmodel.ShapeRef,
950+
// The path to the field that we access our source value from
951+
sourceFieldPath string,
919952
// The struct or struct field that we access our source value from
920953
sourceVarName string,
921954
indentLevel int,
@@ -948,6 +981,7 @@ func setSDKForSlice(
948981
cfg, r,
949982
containerFieldName,
950983
elemVarName,
984+
sourceFieldPath+".",
951985
iterVarName,
952986
&targetShape.MemberRef,
953987
indentLevel+1,
@@ -976,6 +1010,8 @@ func setSDKForMap(
9761010
targetVarName string,
9771011
// Shape Ref of the target struct field
9781012
targetShapeRef *awssdkmodel.ShapeRef,
1013+
// The path to the field that we access our source value from
1014+
sourceFieldPath string,
9791015
// The struct or struct field that we access our source value from
9801016
sourceVarName string,
9811017
indentLevel int,
@@ -1009,6 +1045,7 @@ func setSDKForMap(
10091045
cfg, r,
10101046
containerFieldName,
10111047
valVarName,
1048+
sourceFieldPath+".",
10121049
valIterVarName,
10131050
&targetShape.ValueRef,
10141051
indentLevel+1,
@@ -1117,6 +1154,8 @@ func setSDKForScalar(
11171154
targetVarName string,
11181155
// The type of shape of the target variable
11191156
targetVarType string,
1157+
// The path to the field that we access our source value from
1158+
sourceFieldPath string,
11201159
// The struct or struct field that we access our source value from
11211160
sourceVarName string,
11221161
shapeRef *awssdkmodel.ShapeRef,

pkg/generate/code/set_sdk_test.go

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,3 +1405,195 @@ func TestSetSDK_SQS_Queue_GetAttributes(t *testing.T) {
14051405
code.SetSDKGetAttributes(crd.Config(), crd, "r.ko", "res", 1),
14061406
)
14071407
}
1408+
1409+
func TestSetSDK_MQ_Broker_Create(t *testing.T) {
1410+
assert := assert.New(t)
1411+
require := require.New(t)
1412+
1413+
g := testutil.NewGeneratorForService(t, "mq")
1414+
1415+
crd := testutil.GetCRDByName(t, g, "Broker")
1416+
require.NotNil(crd)
1417+
1418+
expected := `
1419+
if r.ko.Spec.AuthenticationStrategy != nil {
1420+
res.SetAuthenticationStrategy(*r.ko.Spec.AuthenticationStrategy)
1421+
}
1422+
if r.ko.Spec.AutoMinorVersionUpgrade != nil {
1423+
res.SetAutoMinorVersionUpgrade(*r.ko.Spec.AutoMinorVersionUpgrade)
1424+
}
1425+
if r.ko.Spec.BrokerName != nil {
1426+
res.SetBrokerName(*r.ko.Spec.BrokerName)
1427+
}
1428+
if r.ko.Spec.Configuration != nil {
1429+
f3 := &svcsdk.ConfigurationId{}
1430+
if r.ko.Spec.Configuration.ID != nil {
1431+
f3.SetId(*r.ko.Spec.Configuration.ID)
1432+
}
1433+
if r.ko.Spec.Configuration.Revision != nil {
1434+
f3.SetRevision(*r.ko.Spec.Configuration.Revision)
1435+
}
1436+
res.SetConfiguration(f3)
1437+
}
1438+
if r.ko.Spec.CreatorRequestID != nil {
1439+
res.SetCreatorRequestId(*r.ko.Spec.CreatorRequestID)
1440+
}
1441+
if r.ko.Spec.DeploymentMode != nil {
1442+
res.SetDeploymentMode(*r.ko.Spec.DeploymentMode)
1443+
}
1444+
if r.ko.Spec.EncryptionOptions != nil {
1445+
f6 := &svcsdk.EncryptionOptions{}
1446+
if r.ko.Spec.EncryptionOptions.KMSKeyID != nil {
1447+
f6.SetKmsKeyId(*r.ko.Spec.EncryptionOptions.KMSKeyID)
1448+
}
1449+
if r.ko.Spec.EncryptionOptions.UseAWSOwnedKey != nil {
1450+
f6.SetUseAwsOwnedKey(*r.ko.Spec.EncryptionOptions.UseAWSOwnedKey)
1451+
}
1452+
res.SetEncryptionOptions(f6)
1453+
}
1454+
if r.ko.Spec.EngineType != nil {
1455+
res.SetEngineType(*r.ko.Spec.EngineType)
1456+
}
1457+
if r.ko.Spec.EngineVersion != nil {
1458+
res.SetEngineVersion(*r.ko.Spec.EngineVersion)
1459+
}
1460+
if r.ko.Spec.HostInstanceType != nil {
1461+
res.SetHostInstanceType(*r.ko.Spec.HostInstanceType)
1462+
}
1463+
if r.ko.Spec.LdapServerMetadata != nil {
1464+
f10 := &svcsdk.LdapServerMetadataInput{}
1465+
if r.ko.Spec.LdapServerMetadata.Hosts != nil {
1466+
f10f0 := []*string{}
1467+
for _, f10f0iter := range r.ko.Spec.LdapServerMetadata.Hosts {
1468+
var f10f0elem string
1469+
f10f0elem = *f10f0iter
1470+
f10f0 = append(f10f0, &f10f0elem)
1471+
}
1472+
f10.SetHosts(f10f0)
1473+
}
1474+
if r.ko.Spec.LdapServerMetadata.RoleBase != nil {
1475+
f10.SetRoleBase(*r.ko.Spec.LdapServerMetadata.RoleBase)
1476+
}
1477+
if r.ko.Spec.LdapServerMetadata.RoleName != nil {
1478+
f10.SetRoleName(*r.ko.Spec.LdapServerMetadata.RoleName)
1479+
}
1480+
if r.ko.Spec.LdapServerMetadata.RoleSearchMatching != nil {
1481+
f10.SetRoleSearchMatching(*r.ko.Spec.LdapServerMetadata.RoleSearchMatching)
1482+
}
1483+
if r.ko.Spec.LdapServerMetadata.RoleSearchSubtree != nil {
1484+
f10.SetRoleSearchSubtree(*r.ko.Spec.LdapServerMetadata.RoleSearchSubtree)
1485+
}
1486+
if r.ko.Spec.LdapServerMetadata.ServiceAccountPassword != nil {
1487+
f10.SetServiceAccountPassword(*r.ko.Spec.LdapServerMetadata.ServiceAccountPassword)
1488+
}
1489+
if r.ko.Spec.LdapServerMetadata.ServiceAccountUsername != nil {
1490+
f10.SetServiceAccountUsername(*r.ko.Spec.LdapServerMetadata.ServiceAccountUsername)
1491+
}
1492+
if r.ko.Spec.LdapServerMetadata.UserBase != nil {
1493+
f10.SetUserBase(*r.ko.Spec.LdapServerMetadata.UserBase)
1494+
}
1495+
if r.ko.Spec.LdapServerMetadata.UserRoleName != nil {
1496+
f10.SetUserRoleName(*r.ko.Spec.LdapServerMetadata.UserRoleName)
1497+
}
1498+
if r.ko.Spec.LdapServerMetadata.UserSearchMatching != nil {
1499+
f10.SetUserSearchMatching(*r.ko.Spec.LdapServerMetadata.UserSearchMatching)
1500+
}
1501+
if r.ko.Spec.LdapServerMetadata.UserSearchSubtree != nil {
1502+
f10.SetUserSearchSubtree(*r.ko.Spec.LdapServerMetadata.UserSearchSubtree)
1503+
}
1504+
res.SetLdapServerMetadata(f10)
1505+
}
1506+
if r.ko.Spec.Logs != nil {
1507+
f11 := &svcsdk.Logs{}
1508+
if r.ko.Spec.Logs.Audit != nil {
1509+
f11.SetAudit(*r.ko.Spec.Logs.Audit)
1510+
}
1511+
if r.ko.Spec.Logs.General != nil {
1512+
f11.SetGeneral(*r.ko.Spec.Logs.General)
1513+
}
1514+
res.SetLogs(f11)
1515+
}
1516+
if r.ko.Spec.MaintenanceWindowStartTime != nil {
1517+
f12 := &svcsdk.WeeklyStartTime{}
1518+
if r.ko.Spec.MaintenanceWindowStartTime.DayOfWeek != nil {
1519+
f12.SetDayOfWeek(*r.ko.Spec.MaintenanceWindowStartTime.DayOfWeek)
1520+
}
1521+
if r.ko.Spec.MaintenanceWindowStartTime.TimeOfDay != nil {
1522+
f12.SetTimeOfDay(*r.ko.Spec.MaintenanceWindowStartTime.TimeOfDay)
1523+
}
1524+
if r.ko.Spec.MaintenanceWindowStartTime.TimeZone != nil {
1525+
f12.SetTimeZone(*r.ko.Spec.MaintenanceWindowStartTime.TimeZone)
1526+
}
1527+
res.SetMaintenanceWindowStartTime(f12)
1528+
}
1529+
if r.ko.Spec.PubliclyAccessible != nil {
1530+
res.SetPubliclyAccessible(*r.ko.Spec.PubliclyAccessible)
1531+
}
1532+
if r.ko.Spec.SecurityGroups != nil {
1533+
f14 := []*string{}
1534+
for _, f14iter := range r.ko.Spec.SecurityGroups {
1535+
var f14elem string
1536+
f14elem = *f14iter
1537+
f14 = append(f14, &f14elem)
1538+
}
1539+
res.SetSecurityGroups(f14)
1540+
}
1541+
if r.ko.Spec.StorageType != nil {
1542+
res.SetStorageType(*r.ko.Spec.StorageType)
1543+
}
1544+
if r.ko.Spec.SubnetIDs != nil {
1545+
f16 := []*string{}
1546+
for _, f16iter := range r.ko.Spec.SubnetIDs {
1547+
var f16elem string
1548+
f16elem = *f16iter
1549+
f16 = append(f16, &f16elem)
1550+
}
1551+
res.SetSubnetIds(f16)
1552+
}
1553+
if r.ko.Spec.Tags != nil {
1554+
f17 := map[string]*string{}
1555+
for f17key, f17valiter := range r.ko.Spec.Tags {
1556+
var f17val string
1557+
f17val = *f17valiter
1558+
f17[f17key] = &f17val
1559+
}
1560+
res.SetTags(f17)
1561+
}
1562+
if r.ko.Spec.Users != nil {
1563+
f18 := []*svcsdk.User{}
1564+
for _, f18iter := range r.ko.Spec.Users {
1565+
f18elem := &svcsdk.User{}
1566+
if f18iter.ConsoleAccess != nil {
1567+
f18elem.SetConsoleAccess(*f18iter.ConsoleAccess)
1568+
}
1569+
if f18iter.Groups != nil {
1570+
f18elemf1 := []*string{}
1571+
for _, f18elemf1iter := range f18iter.Groups {
1572+
var f18elemf1elem string
1573+
f18elemf1elem = *f18elemf1iter
1574+
f18elemf1 = append(f18elemf1, &f18elemf1elem)
1575+
}
1576+
f18elem.SetGroups(f18elemf1)
1577+
}
1578+
if f18iter.Password != nil {
1579+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f18iter.Password)
1580+
if err != nil {
1581+
return nil, err
1582+
}
1583+
if tmpSecret != "" {
1584+
f18elem.SetPassword(tmpSecret)
1585+
}
1586+
}
1587+
if f18iter.Username != nil {
1588+
f18elem.SetUsername(*f18iter.Username)
1589+
}
1590+
f18 = append(f18, f18elem)
1591+
}
1592+
res.SetUsers(f18)
1593+
}
1594+
`
1595+
assert.Equal(
1596+
expected,
1597+
code.SetSDK(crd.Config(), crd, model.OpTypeCreate, "r.ko", "res", 1),
1598+
)
1599+
}

0 commit comments

Comments
 (0)