Skip to content
8 changes: 8 additions & 0 deletions mmv1/third_party/terraform/envvar/envvar_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ var UniverseDomainEnvVars = []string{
"GOOGLE_UNIVERSE_DOMAIN",
}

var ProjectPrefixEnvVars = []string{
"GOOGLE_PROJECT_PREFIX",
}

// This is the billing account that will be charged for the infrastructure used during testing. For
// that reason, it is also the billing account used for creating new projects.
var BillingAccountEnvVars = []string{
Expand Down Expand Up @@ -146,6 +150,10 @@ func GetTestUniverseDomainFromEnv(t *testing.T) string {
return transport_tpg.MultiEnvSearch(UniverseDomainEnvVars)
}

func GetProjectPrefixFromEnv() string {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a followup PR could you change the env var to include "Universe" in the namespace? i.e. GOOGLE_UNIVERSE_PROJECT_PREFIX / GetUniverseProjectPrefixFromEnv

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

return transport_tpg.MultiEnvSearch(ProjectPrefixEnvVars)
}

// AccTestPreCheck ensures at least one of the region env variables is set.
func GetTestRegionFromEnv() string {
return transport_tpg.MultiEnvSearch(RegionEnvVars)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ func TestAccUniverseDomainDisk(t *testing.T) {
})
}

func TestAccUniverseDomainDiskImage(t *testing.T) {
// Skip this test in all env since this can only run in specific test project.
t.Skip()

universeDomain := envvar.GetTestUniverseDomainFromEnv(t)
zone := envvar.GetTestZoneFromEnv()
prefix := envvar.GetProjectPrefixFromEnv()

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeDiskDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccUniverseDomain_basic_disk_image(universeDomain, zone, prefix),
},
},
})
}

func TestAccDefaultUniverseDomainDisk(t *testing.T) {
universeDomain := "googleapis.com"

Expand Down Expand Up @@ -85,6 +105,23 @@ resource "google_compute_instance_template" "instance_template" {
`, universeDomain)
}

func testAccUniverseDomain_basic_disk_image(universeDomain, zone, prefix string) string {
return fmt.Sprintf(`
provider "google" {
universe_domain = "%s"
}

resource "google_compute_disk" "primary" {
name = "async-test-disk"
type = "pd-ssd"
zone = "%s"

physical_block_size_bytes = 4096
image = "projects/%s:debian-cloud/global/images/debian-12"
}
`, universeDomain, zone, prefix)
}

func testAccCheckComputeDiskDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
3 changes: 3 additions & 0 deletions mmv1/third_party/terraform/services/compute/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func ResolveImage(c *transport_tpg.Config, project, name, userAgent string) (str
break
}
}
if c.UniverseDomain != "" && c.UniverseDomain != "googleapis.com" {
resolveImageLink = regexp.MustCompile(fmt.Sprintf("^https://compute.%s/compute/[a-z0-9]+/projects/(%s)/global/images/(%s)", c.UniverseDomain, verify.ProjectRegex, resolveImageImageRegex))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix works but we should probably make it more clear. Out of band, could we move resolveImageLink into a function that returns the appropriate link for the current universe? That'll guard against direct access of the raw value, move the logic together, and give a good spot to document the differences with a function comment.

}
switch {
case resolveImageLink.MatchString(name): // https://www.googleapis.com/compute/v1/projects/xyz/global/images/xyz
return name, nil
Expand Down
Loading