Skip to content
Merged
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
39 changes: 19 additions & 20 deletions google-beta/acctest/tgc_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,16 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
caiAssetNameFormat := ""
if len(yamlMetadata.CaiAssetNameFormats) == 1 {
caiAssetNameFormat = yamlMetadata.CaiAssetNameFormats[0]
caiAssetName := formatCaiAssetName(caiAssetNameFormat, rState.Primary.Attributes)
if _, ok := serviceWithProjectNumber[metadata.Service]; ok {
caiAssetName = strings.Replace(caiAssetName, projectId, projectNumber, 1)
}
metadata.CaiAssetNames = []string{caiAssetName}
} else {
if strings.HasPrefix(yamlMetadata.Resource, "google_container_") {
caiAssetNameFormat = resolveContainerCaiAssetNameFormat(yamlMetadata.CaiAssetNameFormats, rState.Primary.Attributes["location"])
metadata.CaiAssetNames = resolveContainerCaiAssetName(yamlMetadata.CaiAssetNameFormats, rState.Primary.ID, rState.Primary.Attributes["location"])
}
}

caiAssetName := formatCaiAssetName(caiAssetNameFormat, rState.Primary.Attributes)
if _, ok := serviceWithProjectNumber[metadata.Service]; ok {
caiAssetName = strings.Replace(caiAssetName, projectId, projectNumber, 1)
}
metadata.CaiAssetNames = []string{caiAssetName}
}
}

Expand Down Expand Up @@ -196,23 +195,23 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
}
}

// resolveContainerCaiAssetNameFormat determines the correct CAI asset name format
// resolveContainerCaiAssetName determines the correct CAI asset name
// for GKE resources based on whether the resource location is a zone or a region.
func resolveContainerCaiAssetNameFormat(caiAssetNameFormats []string, location string) string {
func resolveContainerCaiAssetName(caiAssetNameFormats []string, rName, location string) []string {
serviceDomain := getServiceDomain(caiAssetNameFormats[0])
if tpgresource.IsZone(location) {
for _, nameFormat := range caiAssetNameFormats {
if strings.Contains(nameFormat, "/zones/") {
return nameFormat
}
}
} else {
for _, nameFormat := range caiAssetNameFormats {
if strings.Contains(nameFormat, "/locations/") {
return nameFormat
}
}
rName = strings.Replace(rName, "/locations/", "/zones/", 1)
}

return []string{fmt.Sprintf("//%s/%s", serviceDomain, rName)}
}

func getServiceDomain(caiAssetName string) string {
// caiAssetName format: //container.googleapis.com/projects/...
parts := strings.Split(caiAssetName, "/")
if len(parts) >= 3 {
return parts[2]
}
return ""
}

Expand Down
106 changes: 106 additions & 0 deletions google-beta/acctest/tgc_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: Handwritten ***
//
// ----------------------------------------------------------------------------
//
// This code is generated by Magic Modules using the following:
//
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/acctest/tgc_utils_test.go
//
// DO NOT EDIT this file directly. Any changes made to this file will be
// overwritten during the next generation cycle.
//
// ----------------------------------------------------------------------------
package acctest

import (
"reflect"
"testing"
)

func TestGetServiceDomain(t *testing.T) {
tests := []struct {
name string
cai string
want string
}{
{
name: "valid cai",
cai: "//container.googleapis.com/projects/foo",
want: "container.googleapis.com",
},
{
name: "valid cai with deeper path",
cai: "//storage.googleapis.com/projects/foo/buckets/bar",
want: "storage.googleapis.com",
},
{
name: "invalid cai - too short",
cai: "foo",
want: "",
},
{
name: "empty",
cai: "",
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getServiceDomain(tt.cai); got != tt.want {
t.Errorf("getServiceDomain() = %v, want %v", got, tt.want)
}
})
}
}

func TestResolveContainerCaiAssetName(t *testing.T) {
formats := []string{
"//container.googleapis.com/projects/{{project}}/zones/{{location}}/clusters/{{cluster}}",
}

tests := []struct {
name string
rName string
location string
want []string
}{
{
name: "zonal location replacement",
rName: "projects/p/locations/us-central1-a/clusters/c",
location: "us-central1-a",
want: []string{
"//container.googleapis.com/projects/p/zones/us-central1-a/clusters/c",
},
},
{
name: "regional location no change",
rName: "projects/p/locations/us-central1/clusters/c",
location: "us-central1",
want: []string{
"//container.googleapis.com/projects/p/locations/us-central1/clusters/c",
},
},
{
name: "unknown location type defaults to no change",
rName: "projects/p/locations/global/clusters/c",
location: "global",
want: []string{
"//container.googleapis.com/projects/p/locations/global/clusters/c",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := resolveContainerCaiAssetName(formats, tt.rName, tt.location)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("resolveContainerCaiAssetName() = %v, want %v", got, tt.want)
}
})
}
}
Loading