Skip to content

Commit 4ce2c17

Browse files
author
Nicholas Thomson
committed
Fix field type casting for ARNs
1 parent 1246ada commit 4ce2c17

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

pkg/generate/code/resource_reference.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func ResolveReferencesForField(field *model.Field, sourceVarName string, indentL
243243
// see https://github.com/aws-controllers-k8s/community/issues/1291
244244
panic(fmt.Errorf("references within lists inside lists aren't supported. crd: %q, path: %q", r.Kind, field.Path))
245245
}
246-
fieldAccessPrefix = fmt.Sprintf("%s.%s", fieldAccessPrefix, fp.At(idx))
246+
fieldAccessPrefix = fmt.Sprintf("%s.%s", fieldAccessPrefix, cur.GetReferenceFieldName().Camel)
247247

248248
iterVarName := fmt.Sprintf("iter%d", idx)
249249

@@ -282,7 +282,7 @@ func ResolveReferencesForField(field *model.Field, sourceVarName string, indentL
282282
outPrefix += fmt.Sprintf("%s\tif err := getReferencedResourceState_%s(ctx, apiReader, obj, *arr.Name, namespace); err != nil {\n", indent, field.FieldConfig.References.Resource)
283283
outPrefix += fmt.Sprintf("%s\t\treturn err\n", indent)
284284
outPrefix += fmt.Sprintf("%s\t}\n", indent)
285-
outPrefix += fmt.Sprintf("%s\t%s = obj.%s\n", indent, targetVarName, field.FieldConfig.References.Path)
285+
outPrefix += fmt.Sprintf("%s\t%s = (%s)(obj.%s)\n", indent, targetVarName, field.GoType, field.FieldConfig.References.Path)
286286
outPrefix += fmt.Sprintf("%s}\n", indent)
287287
}
288288
}

pkg/generate/code/resource_reference_test.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,49 @@ func Test_ResolveReferencesForField_SingleReference(t *testing.T) {
214214
` if ko.Spec.APIRef != nil && ko.Spec.APIRef.From != nil {
215215
arr := ko.Spec.APIRef.From
216216
if arr == nil || arr.Name == nil || *arr.Name == "" {
217-
return fmt.Errorf("provided resource reference is nil or empty: \"APIRef"\")
217+
return fmt.Errorf("provided resource reference is nil or empty: APIRef")
218218
}
219219
obj := &svcapitypes.API{}
220220
if err := getReferencedResourceState_API(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
221221
return err
222222
}
223-
ko.Spec.APIID = obj.Status.APIID
223+
ko.Spec.APIID = (*string)(obj.Status.APIID)
224224
}
225225
`
226226

227227
field := crd.Fields["APIID"]
228228
assert.Equal(expected, code.ResolveReferencesForField(field, "ko", 1))
229229
}
230230

231+
func Test_ResolveReferencesForField_ReferencingARN(t *testing.T) {
232+
assert := assert.New(t)
233+
require := require.New(t)
234+
235+
g := testutil.NewModelForServiceWithOptions(t, "iam",
236+
&testutil.TestingModelOptions{
237+
GeneratorConfigFile: "generator.yaml",
238+
})
239+
240+
crd := testutil.GetCRDByName(t, g, "User")
241+
require.NotNil(crd)
242+
expected :=
243+
` if ko.Spec.PermissionsBoundaryRef != nil && ko.Spec.PermissionsBoundaryRef.From != nil {
244+
arr := ko.Spec.PermissionsBoundaryRef.From
245+
if arr == nil || arr.Name == nil || *arr.Name == "" {
246+
return fmt.Errorf("provided resource reference is nil or empty: PermissionsBoundaryRef")
247+
}
248+
obj := &svcapitypes.Policy{}
249+
if err := getReferencedResourceState_Policy(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
250+
return err
251+
}
252+
ko.Spec.PermissionsBoundary = (*string)(obj.Status.ACKResourceMetadata.ARN)
253+
}
254+
`
255+
256+
field := crd.Fields["PermissionsBoundary"]
257+
assert.Equal(expected, code.ResolveReferencesForField(field, "ko", 1))
258+
}
259+
231260
func Test_ResolveReferencesForField_SliceOfReferences(t *testing.T) {
232261
assert := assert.New(t)
233262
require := require.New(t)
@@ -241,10 +270,10 @@ func Test_ResolveReferencesForField_SliceOfReferences(t *testing.T) {
241270
require.NotNil(crd)
242271
expected :=
243272
` ko.Spec.SecurityGroupIDs = []*string{}
244-
for _, iter0 := range ko.Spec.SecurityGroupIDs {
273+
for _, iter0 := range ko.Spec.SecurityGroupRefs {
245274
arr := iter0.From
246275
if arr == nil || arr.Name == nil || *arr.Name == "" {
247-
return fmt.Errorf("provided resource reference is nil or empty: \"SecurityGroupRefs"\")
276+
return fmt.Errorf("provided resource reference is nil or empty: SecurityGroupRefs")
248277
}
249278
if err := getReferencedResourceState_SecurityGroup(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
250279
return err
@@ -273,13 +302,13 @@ func Test_ResolveReferencesForField_NestedSingleReference(t *testing.T) {
273302
if ko.Spec.JWTConfiguration.IssuerRef != nil && ko.Spec.JWTConfiguration.IssuerRef.From != nil {
274303
arr := ko.Spec.JWTConfiguration.IssuerRef.From
275304
if arr == nil || arr.Name == nil || *arr.Name == "" {
276-
return fmt.Errorf("provided resource reference is nil or empty: \"JWTConfiguration.IssuerRef"\")
305+
return fmt.Errorf("provided resource reference is nil or empty: JWTConfiguration.IssuerRef")
277306
}
278307
obj := &svcapitypes.API{}
279308
if err := getReferencedResourceState_API(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
280309
return err
281310
}
282-
ko.Spec.JWTConfiguration.Issuer = obj.Status.APIID
311+
ko.Spec.JWTConfiguration.Issuer = (*string)(obj.Status.APIID)
283312
}
284313
}
285314
`
@@ -307,13 +336,13 @@ func Test_ResolveReferencesForField_SingleReference_DeeplyNested(t *testing.T) {
307336
if ko.Spec.Logging.LoggingEnabled.TargetBucketRef != nil && ko.Spec.Logging.LoggingEnabled.TargetBucketRef.From != nil {
308337
arr := ko.Spec.Logging.LoggingEnabled.TargetBucketRef.From
309338
if arr == nil || arr.Name == nil || *arr.Name == "" {
310-
return fmt.Errorf("provided resource reference is nil or empty: \"Logging.LoggingEnabled.TargetBucketRef"\")
339+
return fmt.Errorf("provided resource reference is nil or empty: Logging.LoggingEnabled.TargetBucketRef")
311340
}
312341
obj := &svcapitypes.Bucket{}
313342
if err := getReferencedResourceState_Bucket(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
314343
return err
315344
}
316-
ko.Spec.Logging.LoggingEnabled.TargetBucket = obj.Spec.Name
345+
ko.Spec.Logging.LoggingEnabled.TargetBucket = (*string)(obj.Spec.Name)
317346
}
318347
}
319348
}

pkg/testdata/models/apis/iam/0000-00-00/generator.yaml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ignore:
1212
- SAMLProvider
1313
- ServiceLinkedRole
1414
- ServiceSpecificCredential
15-
- User
15+
#- User
1616
- VirtualMFADevice
1717
resources:
1818
Role:
@@ -43,3 +43,28 @@ resources:
4343
type: "map[string]*bool"
4444
MyCustomInteger:
4545
type: "*int64"
46+
User:
47+
renames:
48+
operations:
49+
CreateUser:
50+
input_fields:
51+
UserName: Name
52+
fields:
53+
PermissionsBoundary:
54+
references:
55+
resource: Policy
56+
path: Status.ACKResourceMetadata.ARN
57+
set:
58+
# The input and output shapes are different...
59+
- from: PermissionsBoundary.PermissionsBoundaryArn
60+
# In order to support attaching zero or more policies to a user, we use
61+
# custom update code path code that uses the Attach/DetachUserPolicy API
62+
# calls to manage the set of PolicyARNs attached to this User.
63+
Policies:
64+
type: "[]*string"
65+
references:
66+
resource: Policy
67+
path: Status.ACKResourceMetadata.ARN
68+
Tags:
69+
compare:
70+
is_ignored: true

0 commit comments

Comments
 (0)