Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all instances of GOOGLE_XPN_HOST_PROJECT environment variable. #815

Merged
merged 6 commits into from
Dec 11, 2017
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
42 changes: 21 additions & 21 deletions google/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package google

import (
"fmt"
"os"
"regexp"
"strconv"
"testing"
Expand Down Expand Up @@ -90,12 +89,10 @@ func TestAccComputeDisk_update(t *testing.T) {
func TestAccComputeDisk_fromSnapshot(t *testing.T) {
t.Parallel()

skipIfEnvNotSet(t, "GOOGLE_XPN_HOST_PROJECT")

diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
firstDiskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
snapshotName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
projectName := getTestProjectFromEnv()

var disk compute.Disk

Expand All @@ -105,14 +102,14 @@ func TestAccComputeDisk_fromSnapshot(t *testing.T) {
CheckDestroy: testAccCheckComputeDiskDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeDisk_fromSnapshot(firstDiskName, snapshotName, diskName, xpn_host, "self_link"),
Config: testAccComputeDisk_fromSnapshot(projectName, firstDiskName, snapshotName, diskName, "self_link"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeDiskExists(
"google_compute_disk.seconddisk", &disk),
),
},
resource.TestStep{
Config: testAccComputeDisk_fromSnapshot(firstDiskName, snapshotName, diskName, xpn_host, "name"),
Config: testAccComputeDisk_fromSnapshot(projectName, firstDiskName, snapshotName, diskName, "name"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeDiskExists(
"google_compute_disk.seconddisk", &disk),
Expand Down Expand Up @@ -202,6 +199,7 @@ func testAccCheckComputeDiskDestroy(s *terraform.State) error {

func testAccCheckComputeDiskExists(n string, disk *compute.Disk) resource.TestCheckFunc {
return func(s *terraform.State) error {
p := getTestProjectFromEnv()
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
Expand All @@ -214,7 +212,7 @@ func testAccCheckComputeDiskExists(n string, disk *compute.Disk) resource.TestCh
config := testAccProvider.Meta().(*Config)

found, err := config.clientCompute.Disks.Get(
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
p, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
if err != nil {
return err
}
Expand Down Expand Up @@ -345,29 +343,31 @@ resource "google_compute_disk" "foobar" {
}`, diskName)
}

func testAccComputeDisk_fromSnapshot(firstDiskName, snapshotName, diskName, xpn_host string, ref_selector string) string {
func testAccComputeDisk_fromSnapshot(projectName, firstDiskName, snapshotName, diskName, ref_selector string) string {
return fmt.Sprintf(`
resource "google_compute_disk" "foobar" {
name = "%s"
image = "debian-8-jessie-v20160803"
size = 50
type = "pd-ssd"
zone = "us-central1-a"
project = "%s"
}
resource "google_compute_disk" "foobar" {
name = "d1-%s"
image = "debian-8-jessie-v20160803"
size = 50
type = "pd-ssd"
zone = "us-central1-a"
project = "%s"
}

resource "google_compute_snapshot" "snapdisk" {
name = "%s"
source_disk = "${google_compute_disk.foobar.name}"
zone = "us-central1-a"
name = "%s"
source_disk = "${google_compute_disk.foobar.name}"
zone = "us-central1-a"
project = "%s"
}

resource "google_compute_disk" "seconddisk" {
name = "%s"
name = "d2-%s"
snapshot = "${google_compute_snapshot.snapdisk.%s}"
type = "pd-ssd"
zone = "us-central1-a"
}`, firstDiskName, xpn_host, snapshotName, xpn_host, diskName, ref_selector)
project = "%s"
}`, firstDiskName, projectName, snapshotName, projectName, diskName, ref_selector, projectName)
}

func testAccComputeDisk_encryption(diskName string) string {
Expand Down
63 changes: 51 additions & 12 deletions google/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package google

import (
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -204,21 +204,22 @@ func TestAccComputeInstanceTemplate_subnet_custom(t *testing.T) {
func TestAccComputeInstanceTemplate_subnet_xpn(t *testing.T) {
t.Parallel()

skipIfEnvNotSet(t, "GOOGLE_XPN_HOST_PROJECT")

var instanceTemplate compute.InstanceTemplate
var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
org := getTestOrgFromEnv(t)
billingId := getTestBillingAccountFromEnv(t)
projectName := fmt.Sprintf("tf-xpntest-%d", time.Now().Unix())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstanceTemplate_subnet_xpn(xpn_host),
Config: testAccComputeInstanceTemplate_subnet_xpn(org, billingId, projectName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
"google_compute_instance_template.foobar", &instanceTemplate),
testAccCheckComputeInstanceTemplateExistsInProject(
"google_compute_instance_template.foobar", fmt.Sprintf("%s-service", projectName),
&instanceTemplate),
testAccCheckComputeInstanceTemplateSubnetwork(&instanceTemplate),
),
},
Expand Down Expand Up @@ -351,6 +352,10 @@ func testAccCheckComputeInstanceTemplateDestroy(s *terraform.State) error {
}

func testAccCheckComputeInstanceTemplateExists(n string, instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
return testAccCheckComputeInstanceTemplateExistsInProject(n, getTestProjectFromEnv(), instanceTemplate)
}

func testAccCheckComputeInstanceTemplateExistsInProject(n, p string, instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -364,7 +369,7 @@ func testAccCheckComputeInstanceTemplateExists(n string, instanceTemplate *compu
config := testAccProvider.Meta().(*Config)

found, err := config.clientCompute.InstanceTemplates.Get(
config.Project, rs.Primary.ID).Do()
p, rs.Primary.ID).Do()
if err != nil {
return err
}
Expand Down Expand Up @@ -852,20 +857,53 @@ resource "google_compute_instance_template" "foobar" {
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
}

func testAccComputeInstanceTemplate_subnet_xpn(xpn_host string) string {
func testAccComputeInstanceTemplate_subnet_xpn(org, billingId, projectName string) string {
return fmt.Sprintf(`
resource "google_project" "host_project" {
name = "Test Project XPN Host"
project_id = "%s-host"
org_id = "%s"
billing_account = "%s"
}

resource "google_project_service" "host_project" {
project = "${google_project.host_project.project_id}"
service = "compute.googleapis.com"
}

resource "google_compute_shared_vpc_host_project" "host_project" {
project = "${google_project_service.host_project.project}"
}

resource "google_project" "service_project" {
name = "Test Project XPN Service"
project_id = "%s-service"
org_id = "%s"
billing_account = "%s"
}

resource "google_project_service" "service_project" {
project = "${google_project.service_project.project_id}"
service = "compute.googleapis.com"
}

resource "google_compute_shared_vpc_service_project" "service_project" {
host_project = "${google_compute_shared_vpc_host_project.host_project.project}"
service_project = "${google_project_service.service_project.project}"
}

resource "google_compute_network" "network" {
name = "network-%s"
auto_create_subnetworks = false
project = "%s"
project = "${google_compute_shared_vpc_host_project.host_project.project}"
}

resource "google_compute_subnetwork" "subnetwork" {
name = "subnetwork-%s"
ip_cidr_range = "10.0.0.0/24"
region = "us-central1"
network = "${google_compute_network.network.self_link}"
project = "%s"
project = "${google_compute_shared_vpc_host_project.host_project.project}"
}

resource "google_compute_instance_template" "foobar" {
Expand All @@ -888,7 +926,8 @@ func testAccComputeInstanceTemplate_subnet_xpn(xpn_host string) string {
metadata {
foo = "bar"
}
}`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, acctest.RandString(10))
project = "${google_compute_shared_vpc_service_project.service_project.service_project}"
}`, projectName, org, billingId, projectName, org, billingId, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
}

func testAccComputeInstanceTemplate_startup_script() string {
Expand Down
65 changes: 53 additions & 12 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package google

import (
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -586,22 +586,23 @@ func TestAccComputeInstance_subnet_custom(t *testing.T) {
func TestAccComputeInstance_subnet_xpn(t *testing.T) {
t.Parallel()

skipIfEnvNotSet(t, "GOOGLE_XPN_HOST_PROJECT")

var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
org := getTestOrgFromEnv(t)
billingId := getTestBillingAccountFromEnv(t)
projectName := fmt.Sprintf("tf-xpntest-%d", time.Now().Unix())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_subnet_xpn(instanceName, xpn_host),
Config: testAccComputeInstance_subnet_xpn(org, billingId, projectName, instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceExistsInProject(
"google_compute_instance.foobar", fmt.Sprintf("%s-service", projectName),
&instance),
testAccCheckComputeInstanceHasSubnet(&instance),
),
},
Expand Down Expand Up @@ -874,6 +875,10 @@ func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
}

func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc {
return testAccCheckComputeInstanceExistsInProject(n, getTestProjectFromEnv(), instance)
}

func testAccCheckComputeInstanceExistsInProject(n, p string, instance *compute.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -887,7 +892,7 @@ func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) res
config := testAccProvider.Meta().(*Config)

found, err := config.clientCompute.Instances.Get(
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
p, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
if err != nil {
return err
}
Expand Down Expand Up @@ -1990,11 +1995,46 @@ resource "google_compute_instance" "foobar" {
`, acctest.RandString(10), acctest.RandString(10), instance)
}

func testAccComputeInstance_subnet_xpn(instance, xpn_host string) string {
func testAccComputeInstance_subnet_xpn(org, billingId, projectName, instance string) string {
return fmt.Sprintf(`

resource "google_project" "host_project" {
name = "Test Project XPN Host"
project_id = "%s-host"
org_id = "%s"
billing_account = "%s"
}

resource "google_project_service" "host_project" {
project = "${google_project.host_project.project_id}"
service = "compute.googleapis.com"
}

resource "google_compute_shared_vpc_host_project" "host_project" {
project = "${google_project_service.host_project.project}"
}

resource "google_project" "service_project" {
name = "Test Project XPN Service"
project_id = "%s-service"
org_id = "%s"
billing_account = "%s"
}

resource "google_project_service" "service_project" {
project = "${google_project.service_project.project_id}"
service = "compute.googleapis.com"
}

resource "google_compute_shared_vpc_service_project" "service_project" {
host_project = "${google_compute_shared_vpc_host_project.host_project.project}"
service_project = "${google_project_service.service_project.project}"
}


resource "google_compute_network" "inst-test-network" {
name = "inst-test-network-%s"
project = "%s"
project = "${google_compute_shared_vpc_host_project.host_project.project}"

auto_create_subnetworks = false
}
Expand All @@ -2004,13 +2044,14 @@ resource "google_compute_subnetwork" "inst-test-subnetwork" {
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
network = "${google_compute_network.inst-test-network.self_link}"
project = "%s"
project = "${google_compute_shared_vpc_host_project.host_project.project}"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
project = "${google_compute_shared_vpc_service_project.service_project.service_project}"

boot_disk {
initialize_params{
Expand All @@ -2025,7 +2066,7 @@ resource "google_compute_instance" "foobar" {
}

}
`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, instance)
`, projectName, org, billingId, projectName, org, billingId, acctest.RandString(10), acctest.RandString(10), instance)
}

func testAccComputeInstance_address_auto(instance string) string {
Expand Down