Skip to content

Commit 7af1f34

Browse files
Add a test check function to get cai assets metadata (#13421) (#22107)
[upstream:a01be1afe8991b48de5989c9f121902784c4b0ec] Signed-off-by: Modular Magician <[email protected]>
1 parent c293efa commit 7af1f34

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

.changelog/13421.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```

google/acctest/tgc_utils.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package acctest
4+
5+
import (
6+
"fmt"
7+
"log"
8+
"regexp"
9+
"strings"
10+
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-testing/terraform"
13+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
14+
)
15+
16+
// Hardcode the Terraform resource name -> API service name mapping temporarily.
17+
// TODO: [tgc] read the mapping from the resource metadata files.
18+
var ApiServiceNames = map[string]string{
19+
"google_compute_instance": "compute.googleapis.com",
20+
"google_project": "cloudresourcemanager.googleapis.com",
21+
}
22+
23+
// Gets the test metadata for tgc:
24+
// - test config
25+
// - cai asset name
26+
// For example: //compute.googleapis.com/projects/ci-test-188019/zones/us-central1-a/instances/tf-test-mi3fqaucf8
27+
func GetTestMetadataForTgc(service, address, config string) resource.TestCheckFunc {
28+
return func(s *terraform.State) error {
29+
splits := strings.Split(address, ".")
30+
if splits == nil || len(splits) < 2 {
31+
return fmt.Errorf("The resource address %s is invalid.", address)
32+
}
33+
resourceType := splits[0]
34+
resourceName := splits[1]
35+
36+
rState := s.RootModule().Resources[address]
37+
if rState == nil || rState.Primary == nil {
38+
return fmt.Errorf("The resource state is unavailable. Please check if the address %s.%s is correct.", resourceType, resourceName)
39+
}
40+
41+
// Convert the resource ID into CAI asset name
42+
// and then print out the CAI asset name in the logs
43+
if apiServiceName, ok := ApiServiceNames[resourceType]; !ok {
44+
return fmt.Errorf("The Cai product backend name for resource %s doesn't exist.", resourceType)
45+
} else {
46+
var rName string
47+
switch resourceType {
48+
case "google_project":
49+
rName = fmt.Sprintf("projects/%s", rState.Primary.Attributes["number"])
50+
default:
51+
rName = rState.Primary.ID
52+
}
53+
54+
caiAssetName := fmt.Sprintf("//%s/%s", apiServiceName, rName)
55+
log.Printf("[DEBUG]TGC CAI asset name: %s", caiAssetName)
56+
}
57+
58+
// The acceptance tests names will be also used for the tgc tests.
59+
// "service" is logged and will be used to put the tgc tests into specific service packages.
60+
log.Printf("[DEBUG]TGC Terraform service: %s", service)
61+
log.Printf("[DEBUG]TGC Terraform resource: %s", resourceType)
62+
63+
re := regexp.MustCompile(`\"(tf[-_]?test[-_]?.*?)([a-z0-9]+)\"`)
64+
config = re.ReplaceAllString(config, `"${1}tgc"`)
65+
66+
// Replace resource name with the resource's real name,
67+
// which is used to get the main resource object by checking the address after parsing raw config.
68+
// For example, replace `"google_compute_instance" "foobar"` with `"google_compute_instance" "tf-test-mi3fqaucf8"`
69+
n := tpgresource.GetResourceNameFromSelfLink(rState.Primary.ID)
70+
old := fmt.Sprintf(`"%s" "%s"`, resourceType, resourceName)
71+
new := fmt.Sprintf(`"%s" "%s"`, resourceType, n)
72+
config = strings.Replace(config, old, new, 1)
73+
74+
log.Printf("[DEBUG]TGC raw_config starts %sEnd of TGC raw_config", config)
75+
return nil
76+
}
77+
}

google/services/resourcemanager/resource_google_project_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func TestAccProject_create(t *testing.T) {
6565
Config: testAccProject(pid, org),
6666
Check: resource.ComposeTestCheckFunc(
6767
testAccCheckGoogleProjectExists("google_project.acceptance", pid),
68+
acctest.GetTestMetadataForTgc("resourcemanager", "google_project.acceptance",
69+
testAccProject(pid, org)),
6870
),
6971
},
7072
},
@@ -157,6 +159,14 @@ func TestAccProject_labels(t *testing.T) {
157159
testAccCheckGoogleProjectHasNoLabels(t, "google_project.acceptance", pid),
158160
),
159161
},
162+
{
163+
Config: testAccProject_labels(pid, org, map[string]string{"label": "label-value"}),
164+
Check: resource.ComposeTestCheckFunc(
165+
testAccCheckGoogleProjectHasLabels(t, "google_project.acceptance", pid, map[string]string{"label": "label-value"}),
166+
acctest.GetTestMetadataForTgc("resourcemanager", "google_project.acceptance",
167+
testAccProject_labels(pid, org, map[string]string{"test": "that"})),
168+
),
169+
},
160170
},
161171
})
162172
}
@@ -190,6 +200,10 @@ func TestAccProject_parentFolder(t *testing.T) {
190200
Steps: []resource.TestStep{
191201
{
192202
Config: testAccProject_parentFolder(pid, folderDisplayName, org),
203+
Check: resource.ComposeTestCheckFunc(
204+
acctest.GetTestMetadataForTgc("resourcemanager", "google_project.acceptance",
205+
testAccProject_parentFolder(pid, folderDisplayName, org)),
206+
),
193207
},
194208
},
195209
})
@@ -613,7 +627,8 @@ resource "google_project" "acceptance" {
613627
}
614628

615629
func testAccProject_tagsAllowDestroy(pid, org string, tags map[string]string) string {
616-
r := fmt.Sprintf(`resource "google_project" "acceptance" {
630+
r := fmt.Sprintf(
631+
`resource "google_project" "acceptance" {
617632
project_id = "%s"
618633
name = "%s"
619634
org_id = "%s"

0 commit comments

Comments
 (0)