Skip to content

Commit 42b9e5c

Browse files
authored
Switched to API field name as primary metadata (#14711)
1 parent 2086ef8 commit 42b9e5c

File tree

4 files changed

+112
-12
lines changed

4 files changed

+112
-12
lines changed

docs/content/reference/metadata.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The API URL patterns used by this resource that represent variants e.g., "folder
3939

4040
The list of fields used by this resource. Each field can contain the following attributes:
4141

42-
- `field`: The name of the field in Terraform, including the path e.g., "build_config.source.storage_source.bucket"
43-
- `api_field`: The name of the field in the API, including the path e.g., "build_config.source.storage_source.bucket". Defaults to the value of `field`.
44-
- `provider_only`: If true, the field is only present in the provider. This primarily applies for virtual fields and url-only parameters. When set to true, `api_field` should be left empty, as it will be ignored. Default: `false`.
42+
- `api_field`: The name of the field in the REST API, including the path e.g., "buildConfig.source.storageSource.bucket".
43+
- `field`: The name of the field in Terraform, including the path e.g., "build_config.source.storage_source.bucket". Defaults to the value of `api_field` converted to snake_case.
44+
- `provider_only`: If true, the field is only present in the provider. This primarily applies for virtual fields and url-only parameters. When set to true, `field` should be set and `api_field` should be left empty. Default: `false`.
4545
- `json`: If true, this is a JSON field which "covers" all child API fields. As a special case, JSON fields which cover an entire resource can have `api_field` set to `*`.

mmv1/api/type.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,20 +467,37 @@ func (t Type) MetadataLineage() string {
467467
return fmt.Sprintf("%s.%s", t.ParentMetadata.MetadataLineage(), google.Underscore(t.Name))
468468
}
469469

470+
// Returns a dot notation path to where the field is nested within the parent
471+
// object. eg: parent.meta.label.foo
472+
// This format converts the API field names directly to snake_case, which can be compared against the MetadataLineage
473+
// to determine whether to include an explicit Terraform field name.
474+
func (t Type) MetadataDefaultLineage() string {
475+
apiName := t.ApiName
476+
if t.ParentMetadata == nil {
477+
return google.Underscore(apiName)
478+
}
479+
480+
if t.ParentMetadata.IsA("Array") {
481+
return t.ParentMetadata.MetadataDefaultLineage()
482+
}
483+
484+
return fmt.Sprintf("%s.%s", t.ParentMetadata.MetadataDefaultLineage(), google.Underscore(apiName))
485+
}
486+
470487
// Returns a dot notation path to where the field is nested within the parent
471488
// object. eg: parent.meta.label.foo
472489
// This format is intended for to represent an API type.
473490
func (t Type) MetadataApiLineage() string {
474491
apiName := t.ApiName
475492
if t.ParentMetadata == nil {
476-
return google.Underscore(apiName)
493+
return apiName
477494
}
478495

479496
if t.ParentMetadata.IsA("Array") {
480497
return t.ParentMetadata.MetadataApiLineage()
481498
}
482499

483-
return fmt.Sprintf("%s.%s", t.ParentMetadata.MetadataApiLineage(), google.Underscore(apiName))
500+
return fmt.Sprintf("%s.%s", t.ParentMetadata.MetadataApiLineage(), apiName)
484501
}
485502

486503
// Returns the lineage in snake case

mmv1/api/type_test.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func TestMetadataLineage(t *testing.T) {
256256
}
257257
}
258258

259-
func TestMetadataApiLineage(t *testing.T) {
259+
func TestMetadataDefaultLineage(t *testing.T) {
260260
t.Parallel()
261261

262262
root := Type{
@@ -323,6 +323,87 @@ func TestMetadataApiLineage(t *testing.T) {
323323
},
324324
}
325325

326+
for _, tc := range cases {
327+
tc := tc
328+
329+
t.Run(tc.description, func(t *testing.T) {
330+
t.Parallel()
331+
332+
got := tc.obj.MetadataDefaultLineage()
333+
if got != tc.expected {
334+
t.Errorf("expected %q to be %q", got, tc.expected)
335+
}
336+
})
337+
}
338+
}
339+
340+
func TestMetadataApiLineage(t *testing.T) {
341+
t.Parallel()
342+
343+
root := Type{
344+
Name: "root",
345+
Type: "NestedObject",
346+
Properties: []*Type{
347+
{
348+
Name: "foo",
349+
Type: "NestedObject",
350+
Properties: []*Type{
351+
{
352+
Name: "bars",
353+
Type: "Array",
354+
ItemType: &Type{
355+
Type: "NestedObject",
356+
Properties: []*Type{
357+
{
358+
Name: "fooBar",
359+
Type: "String",
360+
},
361+
},
362+
},
363+
},
364+
},
365+
},
366+
{
367+
Name: "baz",
368+
ApiName: "bazbaz",
369+
Type: "String",
370+
},
371+
},
372+
}
373+
root.SetDefault(&Resource{})
374+
375+
cases := []struct {
376+
description string
377+
obj Type
378+
expected string
379+
}{
380+
{
381+
description: "root type",
382+
obj: root,
383+
expected: "root",
384+
},
385+
{
386+
description: "sub type",
387+
obj: *root.Properties[0],
388+
expected: "root.foo",
389+
},
390+
{
391+
description: "array",
392+
obj: *root.Properties[0].Properties[0],
393+
expected: "root.foo.bars",
394+
},
395+
{
396+
description: "array of objects",
397+
obj: *root.Properties[0].Properties[0].ItemType.Properties[0],
398+
expected: "root.foo.bars.fooBar",
399+
},
400+
{
401+
description: "with api name",
402+
obj: *root.Properties[1],
403+
expected: "root.bazbaz",
404+
},
405+
}
406+
326407
for _, tc := range cases {
327408
tc := tc
328409

mmv1/templates/terraform/metadata.yaml.tmpl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ autogen_status: true
1515
{{- end }}
1616
fields:
1717
{{- range $p := $.LeafProperties }}
18+
{{- if $p.ProviderOnly }}
1819
- field: '{{ $p.MetadataLineage }}'
19-
{{- if and (ne $p.MetadataLineage $p.MetadataApiLineage) (not $p.ProviderOnly) }}
20-
api_field: '{{ $p.MetadataApiLineage }}'
21-
{{- end }}
20+
provider_only: true
21+
{{- else}}
22+
- api_field: '{{ $p.MetadataApiLineage }}'
23+
{{- if ne $p.MetadataLineage $p.MetadataDefaultLineage }}
24+
field: '{{ $p.MetadataLineage }}'
25+
{{- end}}
2226
{{- if $p.IsJsonField }}
2327
json: true
2428
{{- end }}
25-
{{- if $p.ProviderOnly }}
26-
provider_only: true
27-
{{- end }}
29+
{{- end}}
2830
{{- end }}

0 commit comments

Comments
 (0)