|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform/helper/resource" |
| 9 | + "github.com/hashicorp/terraform/terraform" |
| 10 | + "os" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccComputeSharedVpc_basic(t *testing.T) { |
| 14 | + skipIfEnvNotSet(t, "GOOGLE_ORG", "GOOGLE_BILLING_ACCOUNT") |
| 15 | + billingId := os.Getenv("GOOGLE_BILLING_ACCOUNT") |
| 16 | + |
| 17 | + hostProject := "xpn-host-" + acctest.RandString(10) |
| 18 | + serviceProject := "xpn-service-" + acctest.RandString(10) |
| 19 | + |
| 20 | + resource.Test(t, resource.TestCase{ |
| 21 | + PreCheck: func() { testAccPreCheck(t) }, |
| 22 | + Providers: testAccProviders, |
| 23 | + Steps: []resource.TestStep{ |
| 24 | + resource.TestStep{ |
| 25 | + Config: testAccComputeSharedVpc_basic(hostProject, serviceProject, org, billingId), |
| 26 | + Check: resource.ComposeTestCheckFunc( |
| 27 | + testAccCheckComputeSharedVpcHostProject(hostProject, true), |
| 28 | + testAccCheckComputeSharedVpcServiceProject(hostProject, serviceProject, true), |
| 29 | + ), |
| 30 | + }, |
| 31 | + // Use a separate TestStep rather than a CheckDestroy because we need the project to still exist. |
| 32 | + resource.TestStep{ |
| 33 | + Config: testAccComputeSharedVpc_disabled(hostProject, serviceProject, org, billingId), |
| 34 | + Check: resource.ComposeTestCheckFunc( |
| 35 | + testAccCheckComputeSharedVpcHostProject(hostProject, false), |
| 36 | + testAccCheckComputeSharedVpcServiceProject(hostProject, serviceProject, false), |
| 37 | + ), |
| 38 | + }, |
| 39 | + }, |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func testAccCheckComputeSharedVpcHostProject(hostProject string, enabled bool) resource.TestCheckFunc { |
| 44 | + return func(s *terraform.State) error { |
| 45 | + config := testAccProvider.Meta().(*Config) |
| 46 | + |
| 47 | + found, err := config.clientCompute.Projects.Get(hostProject).Do() |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("Error reading project %s: %s", hostProject, err) |
| 50 | + } |
| 51 | + |
| 52 | + if found.Name != hostProject { |
| 53 | + return fmt.Errorf("Project %s not found", hostProject) |
| 54 | + } |
| 55 | + |
| 56 | + if enabled != (found.XpnProjectStatus == "HOST") { |
| 57 | + return fmt.Errorf("Project %q shared VPC status was not expected, got %q", hostProject, found.XpnProjectStatus) |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func testAccCheckComputeSharedVpcServiceProject(hostProject, serviceProject string, enabled bool) resource.TestCheckFunc { |
| 65 | + return func(s *terraform.State) error { |
| 66 | + config := testAccProvider.Meta().(*Config) |
| 67 | + serviceHostProject, err := config.clientCompute.Projects.GetXpnHost(serviceProject).Do() |
| 68 | + if err != nil { |
| 69 | + if enabled { |
| 70 | + return fmt.Errorf("Expected service project to be enabled.") |
| 71 | + } |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + if enabled != (serviceHostProject.Name == hostProject) { |
| 76 | + return fmt.Errorf("Wrong host project for the given service project. Expected '%s', got '%s'", hostProject, serviceHostProject.Name) |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func testAccComputeSharedVpc_basic(hostProject, serviceProject, org, billing string) string { |
| 84 | + return fmt.Sprintf(` |
| 85 | +resource "google_project" "host" { |
| 86 | + project_id = "%s" |
| 87 | + name = "%s" |
| 88 | + org_id = "%s" |
| 89 | + billing_account = "%s" |
| 90 | +} |
| 91 | +
|
| 92 | +resource "google_project" "service" { |
| 93 | + project_id = "%s" |
| 94 | + name = "%s" |
| 95 | + org_id = "%s" |
| 96 | + billing_account = "%s" |
| 97 | +} |
| 98 | +
|
| 99 | +resource "google_project_services" "host" { |
| 100 | + project = "${google_project.host.project_id}" |
| 101 | + services = ["compute.googleapis.com"] |
| 102 | +} |
| 103 | +
|
| 104 | +resource "google_project_services" "service" { |
| 105 | + project = "${google_project.service.project_id}" |
| 106 | + services = ["compute.googleapis.com"] |
| 107 | +} |
| 108 | +
|
| 109 | +resource "google_compute_shared_vpc_host_project" "host" { |
| 110 | + project = "${google_project.host.project_id}" |
| 111 | + depends_on = ["google_project_services.host"] |
| 112 | +} |
| 113 | +
|
| 114 | +resource "google_compute_shared_vpc_service_project" "service" { |
| 115 | + host_project = "${google_project.host.project_id}" |
| 116 | + service_project = "${google_project.service.project_id}" |
| 117 | + depends_on = ["google_compute_shared_vpc_host_project.host", "google_project_services.service"] |
| 118 | +}`, hostProject, hostProject, org, billing, serviceProject, serviceProject, org, billing) |
| 119 | +} |
| 120 | + |
| 121 | +func testAccComputeSharedVpc_disabled(hostProject, serviceProject, org, billing string) string { |
| 122 | + return fmt.Sprintf(` |
| 123 | +resource "google_project" "host" { |
| 124 | + project_id = "%s" |
| 125 | + name = "%s" |
| 126 | + org_id = "%s" |
| 127 | + billing_account = "%s" |
| 128 | +} |
| 129 | +
|
| 130 | +resource "google_project" "service" { |
| 131 | + project_id = "%s" |
| 132 | + name = "%s" |
| 133 | + org_id = "%s" |
| 134 | + billing_account = "%s" |
| 135 | +} |
| 136 | +
|
| 137 | +resource "google_project_services" "host" { |
| 138 | + project = "${google_project.host.project_id}" |
| 139 | + services = ["compute.googleapis.com"] |
| 140 | +} |
| 141 | +
|
| 142 | +resource "google_project_services" "service" { |
| 143 | + project = "${google_project.service.project_id}" |
| 144 | + services = ["compute.googleapis.com"] |
| 145 | +} |
| 146 | +`, hostProject, hostProject, org, billing, serviceProject, serviceProject, org, billing) |
| 147 | +} |
0 commit comments