Skip to content
Open
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
24 changes: 20 additions & 4 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,21 +710,37 @@ func (r Resource) IdentityProperties() []*Type {
identities = nil
}
importFormat := r.ExtractIdentifiers(ImportIdFormats(r.ImportFormat, identities, r.BaseUrl)[0])
optionalValues := map[string]bool{"project": false, "zone": false, "region": false}
includedValues := make(map[string]bool)
for _, p := range r.AllProperties() {
if slices.Contains(importFormat, google.Underscore(p.Name)) {
name := google.Underscore(p.Name)
if slices.Contains(importFormat, name) {
props = append(props, p)
optionalValues[p.Name] = true
includedValues[name] = true
}
}

hasField := map[string]bool{"project": r.HasProject(), "zone": r.HasZone(), "region": r.HasRegion()}
for _, field := range []string{"project", "zone", "region"} { // prevents duplicates
if slices.Contains(importFormat, field) && !optionalValues[field] && hasField[field] {
if slices.Contains(importFormat, field) && !includedValues[field] && hasField[field] {
props = append(props, &Type{Name: field, Type: "string"})
includedValues[field] = true
}
}

for _, field := range importFormat {
if includedValues[field] || field == "project" || field == "zone" || field == "region" {
continue
}
// Avoid creating an identity-only synthetic `name` field when the resource
// schema does not define one. This commonly happens with singleton resources
// when import format defaults to base_url/{{name}}.
if field == "name" {
continue
}
props = append(props, &Type{Name: field, Type: "string", Required: true})
includedValues[field] = true
}

if len(r.CustomCode.CustomIdentity) > 0 {
for _, fieldName := range r.CustomCode.CustomIdentity {
props = append(props, &Type{Name: google.Underscore(fieldName), Type: "string", Required: true})
Expand Down
46 changes: 46 additions & 0 deletions mmv1/api/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,49 @@ func TestSample_TestServiceDependencies(t *testing.T) {
})
}
}

func TestIdentityPropertiesIncludesMissingImportIdentifiers(t *testing.T) {
t.Parallel()

r := api.Resource{
BaseUrl: "projects/{{project}}/datasets",
ImportFormat: []string{"projects/{{project}}/datasets/{{dataset_id}}"},
}

got := r.IdentityProperties()
gotNames := make([]string, 0, len(got))
for _, p := range got {
gotNames = append(gotNames, p.Name)
}

wantNames := []string{"project", "dataset_id"}
if diff := cmp.Diff(wantNames, gotNames); diff != "" {
t.Fatalf("IdentityProperties() names mismatch (-want +got):\n%s", diff)
}

if !got[1].Required {
t.Fatalf("dataset_id should be required when synthesized from import format")
}
}

func TestIdentityPropertiesSkipsSyntheticNameWhenNotInSchema(t *testing.T) {
t.Parallel()

r := api.Resource{
BaseUrl: "projects/{{project}}/locations/{{location}}/encryptionSpec",
Parameters: []*api.Type{
{Name: "location", Type: "string"},
},
}

got := r.IdentityProperties()
gotNames := make([]string, 0, len(got))
for _, p := range got {
gotNames = append(gotNames, p.Name)
}

wantNames := []string{"location", "project"}
if diff := cmp.Diff(wantNames, gotNames); diff != "" {
t.Fatalf("IdentityProperties() names mismatch (-want +got):\n%s", diff)
}
}
2 changes: 0 additions & 2 deletions mmv1/products/bigquery/Dataset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ custom_code:
pre_read: 'templates/terraform/pre_read/bigquery_dataset.go.tmpl'
custom_diff:
- 'customCollationDiff'
# exluding resource_identity due to dataset_id field
exclude_identity_generation: true
exclude_sweeper: true
examples:
- name: 'bigquery_dataset_basic'
Expand Down
Loading