Skip to content

Commit f291561

Browse files
committed
tgc-revival: modify cai asset name format for container resources
1 parent 377a737 commit f291561

File tree

9 files changed

+68
-18
lines changed

9 files changed

+68
-18
lines changed

mmv1/api/metadata/metadata.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ func FromResource(r api.Resource) Metadata {
1414
ApiServiceName: r.ProductMetadata.ServiceName(),
1515
ApiVersion: r.ProductMetadata.ServiceVersion(),
1616
ApiResourceTypeKind: r.ApiResourceTypeKind,
17-
CaiAssetNameFormat: r.CAIFormatOverride(),
1817
ApiVariantPatterns: r.ApiVariantPatterns,
1918
AutogenStatus: r.AutogenStatus != "",
2019
Fields: FromProperties(r.AllNestedProperties(google.Concat(r.RootProperties(), r.UserVirtualFields()))),
2120
}
2221

22+
if r.CAIFormatOverride() != "" {
23+
m.CaiAssetNameFormats = []string{r.CAIFormatOverride()}
24+
}
25+
2326
if m.ApiVersion == "" {
2427
m.ApiVersion = r.ServiceVersion()
2528
}
@@ -55,9 +58,9 @@ type Metadata struct {
5558
// The API "resource type kind" used for this resource. For example, "Function".
5659
ApiResourceTypeKind string `yaml:"api_resource_type_kind"`
5760

58-
// The custom CAI asset name format for this resource is typically specified (for example, //cloudsql.googleapis.com/projects/{{project}}/instances/{{name}}).
59-
// This will only have a value if it's different than the Terraform resource ID format.
60-
CaiAssetNameFormat string `yaml:"cai_asset_name_format,omitempty"`
61+
// The custom CAI asset name formats for this resource is typically specified (for example, //cloudsql.googleapis.com/projects/{{project}}/instances/{{name}}).
62+
// This will only have values if they are different than the Terraform resource ID format.
63+
CaiAssetNameFormats []string `yaml:"cai_asset_name_format,omitempty"`
6164

6265
// The API URL patterns used by this resource that represent variants. For example, "folders/{folder}/feeds/{feed}". Each pattern must match the value defined
6366
// in the API exactly. The use of `api_variant_patterns` is only meaningful when the resource type has multiple parent types available.

mmv1/third_party/terraform/acctest/resource_inventory_reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Metadata struct {
2727
ApiServiceName string `yaml:"api_service_name"`
2828
ApiVersion string `yaml:"api_version"`
2929
ApiResourceTypeKind string `yaml:"api_resource_type_kind"`
30-
CaiAssetNameFormat string `yaml:"cai_asset_name_format"`
30+
CaiAssetNameFormats []string `yaml:"cai_asset_name_formats"`
3131
ApiVariantPatterns []string `yaml:"api_variant_patterns"`
3232
AutogenStatus bool `yaml:"autogen_status,omitempty"`
3333
Fields []MetadataField `yaml:"fields"`

mmv1/third_party/terraform/acctest/tgc_utils.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1515
"github.com/hashicorp/terraform-plugin-testing/terraform"
1616
"github.com/hashicorp/terraform-provider-google/google/envvar"
17+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
1718
)
1819

1920
type ResourceMetadata struct {
@@ -116,7 +117,7 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
116117
if !ok {
117118
log.Printf("[DEBUG]TGC Terraform error: unknown resource type %s", metadata.ResourceType)
118119
metadata.CaiAssetNames = []string{"unknown"}
119-
} else if yamlMetadata.CaiAssetNameFormat == "" {
120+
} else if len(yamlMetadata.CaiAssetNameFormats) == 0 {
120121
log.Printf("[DEBUG]TGC Terraform error: unknown CAI asset name format for resource type %s", yamlMetadata.CaiAssetNameFormat)
121122

122123
var rName string
@@ -137,19 +138,19 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
137138

138139
metadata.CaiAssetNames = []string{fmt.Sprintf("//%s/%s", yamlMetadata.ApiServiceName, rName)}
139140
} else {
140-
paramsMap := make(map[string]any, 0)
141-
params := extractIdentifiers(yamlMetadata.CaiAssetNameFormat)
142-
for _, param := range params {
143-
v := rState.Primary.Attributes[param]
144-
paramsMap[param] = v
141+
caiAssetNameFormat := ""
142+
if len(yamlMetadata.CaiAssetNameFormats) == 1 {
143+
caiAssetNameFormat = yamlMetadata.CaiAssetNameFormats[0]
144+
} else {
145+
if strings.HasPrefix(yamlMetadata.Resource, "google_container_") {
146+
caiAssetNameFormat = resolveContainerCaiAssetNameFormat(yamlMetadata.CaiAssetNameFormats, rState.Primary.Attributes["location"])
147+
}
145148
}
146149

147-
caiAssetName := replacePlaceholders(yamlMetadata.CaiAssetNameFormat, paramsMap)
148-
150+
caiAssetName := formatCaiAssetName(caiAssetNameFormat, rState.Primary.Attributes)
149151
if _, ok := serviceWithProjectNumber[metadata.Service]; ok {
150152
caiAssetName = strings.Replace(caiAssetName, projectId, projectNumber, 1)
151153
}
152-
153154
metadata.CaiAssetNames = []string{caiAssetName}
154155
}
155156
}
@@ -179,6 +180,42 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
179180
}
180181
}
181182

183+
// resolveContainerCaiAssetNameFormat determines the correct CAI asset name format
184+
// for GKE resources based on whether the resource location is a zone or a region.
185+
func resolveContainerCaiAssetNameFormat(caiAssetNameFormats []string, location string) string {
186+
if tpgresource.IsZone(location) {
187+
for _, nameFormat := range caiAssetNameFormats {
188+
if strings.Contains(nameFormat, "/zones/") {
189+
return nameFormat
190+
}
191+
}
192+
} else {
193+
for _, nameFormat := range caiAssetNameFormats {
194+
if strings.Contains(nameFormat, "/locations/") {
195+
return nameFormat
196+
}
197+
}
198+
}
199+
200+
return ""
201+
}
202+
203+
// FormatCaiAssetName constructs a fully qualified Cloud Asset Inventory (CAI) asset name
204+
// by interpolating values from the provided attributes map into a format template.
205+
// It extracts required placeholders (e.g., "{{project}}", "{{name}}") from the
206+
// caiAssetNameFormat string and replaces them with their corresponding values from the map.
207+
func formatCaiAssetName(caiAssetNameFormat string, attributes map[string]string) string {
208+
paramsMap := make(map[string]any, 0)
209+
params := extractIdentifiers(caiAssetNameFormat)
210+
for _, param := range params {
211+
v := attributes[param]
212+
paramsMap[param] = v
213+
}
214+
215+
caiAssetName := replacePlaceholders(caiAssetNameFormat, paramsMap)
216+
return caiAssetName
217+
}
218+
182219
// For example, for the url "projects/{{project}}/schemas/{{schema}}",
183220
// the identifiers are "project", "schema".
184221
func extractIdentifiers(url string) []string {

mmv1/third_party/terraform/services/bigtable/resource_bigtable_instance_meta.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
resource: 'google_bigtable_instance'
22
generation_type: 'handwritten'
33
api_service_name: 'bigtableadmin.googleapis.com'
4-
cai_asset_name_format: '//bigtable.googleapis.com/projects/{{project}}/instances/{{name}}'
4+
cai_asset_name_formats:
5+
- '//bigtable.googleapis.com/projects/{{project}}/instances/{{name}}'
56
api_version: 'v2'
67
api_resource_type_kind: 'Instance'
78
fields:

mmv1/third_party/terraform/services/bigtable/resource_bigtable_table_meta.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ generation_type: 'handwritten'
33
api_service_name: 'bigtableadmin.googleapis.com'
44
api_version: 'v2'
55
api_resource_type_kind: 'Table'
6-
cai_asset_name_format: '//bigtable.googleapis.com/projects/{{project}}/instances/{{instance_name}}/tables/{{name}}'
6+
cai_asset_name_formats:
7+
- '//bigtable.googleapis.com/projects/{{project}}/instances/{{instance_name}}/tables/{{name}}'
78
fields:
89
- api_field: 'automatedBackupPolicy.frequency'
910
- api_field: 'automatedBackupPolicy.retentionPeriod'

mmv1/third_party/terraform/services/container/resource_container_cluster_meta.yaml.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ api_version: 'v1'
99
api_resource_type_kind: 'Cluster'
1010
api_variant_patterns:
1111
- 'projects/{project}/locations/{location}/clusters/{cluster}'
12+
cai_asset_name_formats:
13+
- {{"//container.googleapis.com/projects/{{project}}/locations/{{location}}/clusters/{{name}}"}}
14+
- {{"//container.googleapis.com/projects/{{project}}/zones/{{location}}/clusters/{{name}}"}}
1215
fields:
1316
- field: 'addons_config.cloudrun_config.disabled'
1417
api_field: 'addonsConfig.cloudRunConfig.disabled'

mmv1/third_party/terraform/services/container/resource_container_node_pool_meta.yaml.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ api_version: 'v1beta1'
77
api_version: 'v1'
88
{{- end }}
99
api_resource_type_kind: 'NodePool'
10+
cai_asset_name_formats:
11+
- {{"//container.googleapis.com/projects/{{project}}/locations/{{location}}/clusters/{{cluster}}/nodePools/{{name}}"}}
12+
- {{"//container.googleapis.com/projects/{{project}}/zones/{{location}}/clusters/{{cluster}}/nodePools/{{name}}"}}
1013
fields:
1114
- api_field: 'autoscaling.locationPolicy'
1215
- api_field: 'autoscaling.maxNodeCount'

mmv1/third_party/terraform/services/resourcemanager/resource_google_service_account_meta.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ generation_type: 'handwritten'
33
api_service_name: 'iam.googleapis.com'
44
api_version: 'v1'
55
api_resource_type_kind: 'ServiceAccount'
6-
cai_asset_name_format: '//iam.googleapis.com/projects/{{project}}/serviceAccounts/{{unique_id}}'
6+
cai_asset_name_formats:
7+
- '//iam.googleapis.com/projects/{{project}}/serviceAccounts/{{unique_id}}'
78
fields:
89
- field: 'account_id'
910
- field: 'create_ignore_already_exists'

mmv1/third_party/terraform/services/tags/resource_tags_location_tag_binding_meta.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ generation_type: 'handwritten'
33
api_service_name: 'cloudresourcemanager.googleapis.com'
44
api_version: 'v3'
55
api_resource_type_kind: 'TagBinding'
6-
cai_asset_name_format: '//cloudresourcemanager.googleapis.com/{{name}}'
6+
cai_asset_name_formats:
7+
- '//cloudresourcemanager.googleapis.com/{{name}}'
78
fields:
89
- field: 'location'
910
- api_field: 'name'

0 commit comments

Comments
 (0)