mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-07-03 08:42:39 +00:00
Remove all instances of GOOGLE_XPN_HOST_PROJECT environment variable. (#815)
Remove all instances of GOOGLE_XPN_HOST_PROJECT environment variable. Instead of GOOGLE_XPN_HOST_PROJECT being required to run some tests, I added the ability to create and tear down the necessary project structure. This allows us to remove one environment variable, and use two others which are already widely-required: org and billing ID.
This commit is contained in:
parent
72e97befe5
commit
5766f2b107
|
@ -2,7 +2,6 @@ package google
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
@ -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
|
||||
|
||||
|
@ -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),
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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_snapshot" "snapdisk" {
|
||||
name = "%s"
|
||||
source_disk = "${google_compute_disk.foobar.name}"
|
||||
zone = "us-central1-a"
|
||||
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_disk" "seconddisk" {
|
||||
|
||||
resource "google_compute_snapshot" "snapdisk" {
|
||||
name = "%s"
|
||||
source_disk = "${google_compute_disk.foobar.name}"
|
||||
zone = "us-central1-a"
|
||||
project = "%s"
|
||||
}
|
||||
|
||||
resource "google_compute_disk" "seconddisk" {
|
||||
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 {
|
||||
|
|
|
@ -2,9 +2,9 @@ package google
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
|
@ -204,10 +204,10 @@ 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) },
|
||||
|
@ -215,10 +215,11 @@ func TestAccComputeInstanceTemplate_subnet_xpn(t *testing.T) {
|
|||
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),
|
||||
),
|
||||
},
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -852,12 +857,45 @@ 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" {
|
||||
|
@ -865,7 +903,7 @@ func testAccComputeInstanceTemplate_subnet_xpn(xpn_host string) string {
|
|||
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" {
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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"
|
||||
|
@ -586,11 +586,11 @@ 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) },
|
||||
|
@ -598,10 +598,11 @@ func TestAccComputeInstance_subnet_xpn(t *testing.T) {
|
|||
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),
|
||||
),
|
||||
},
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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{
|
||||
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user