2016-08-10 04:44:53 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2016-08-21 07:25:00 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-09-14 17:39:21 +00:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2017-02-20 17:32:24 +00:00
|
|
|
"strings"
|
2016-08-10 04:44:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-09-14 17:39:21 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2016-11-23 06:55:40 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-08-21 07:25:00 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2016-08-10 04:44:53 +00:00
|
|
|
"google.golang.org/api/cloudresourcemanager/v1"
|
|
|
|
)
|
|
|
|
|
2016-08-21 07:25:00 +00:00
|
|
|
var (
|
2016-11-23 06:55:40 +00:00
|
|
|
pname = "Terraform Acceptance Tests"
|
|
|
|
originalPolicy *cloudresourcemanager.Policy
|
2016-08-21 07:25:00 +00:00
|
|
|
)
|
2016-08-10 04:44:53 +00:00
|
|
|
|
2017-09-22 18:03:08 +00:00
|
|
|
// Test that a Project resource can be created without an organization
|
2018-02-23 23:14:24 +00:00
|
|
|
func TestAccProject_createWithoutOrg(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-09-22 18:03:08 +00:00
|
|
|
creds := multiEnvSearch(credsEnvVars)
|
|
|
|
if strings.Contains(creds, "iam.gserviceaccount.com") {
|
|
|
|
t.Skip("Service accounts cannot create projects without a parent. Requires user credentials.")
|
|
|
|
}
|
|
|
|
|
|
|
|
pid := "terraform-" + acctest.RandString(10)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
// This step creates a new project
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccProject_createWithoutOrg(pid, pname),
|
2017-09-22 18:03:08 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleProjectExists("google_project.acceptance", pid),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:40 +00:00
|
|
|
// Test that a Project resource can be created and an IAM policy
|
|
|
|
// associated
|
2018-02-23 23:14:24 +00:00
|
|
|
func TestAccProject_create(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-11-20 23:45:51 +00:00
|
|
|
org := getTestOrgFromEnv(t)
|
2016-11-23 06:55:40 +00:00
|
|
|
pid := "terraform-" + acctest.RandString(10)
|
2016-08-21 07:25:00 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
2017-09-22 18:03:08 +00:00
|
|
|
// This step creates a new project
|
2016-08-21 07:25:00 +00:00
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccProject_create(pid, pname, org),
|
2016-08-21 07:25:00 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-11-23 06:55:40 +00:00
|
|
|
testAccCheckGoogleProjectExists("google_project.acceptance", pid),
|
2016-08-21 07:25:00 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2016-08-10 04:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 17:32:24 +00:00
|
|
|
// Test that a Project resource can be created with an associated
|
|
|
|
// billing account
|
2018-02-23 23:14:24 +00:00
|
|
|
func TestAccProject_billing(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
2017-11-20 23:45:51 +00:00
|
|
|
org := getTestOrgFromEnv(t)
|
2017-12-19 22:40:24 +00:00
|
|
|
skipIfEnvNotSet(t, "GOOGLE_BILLING_ACCOUNT_2")
|
|
|
|
billingId2 := os.Getenv("GOOGLE_BILLING_ACCOUNT_2")
|
2017-11-20 23:45:51 +00:00
|
|
|
billingId := getTestBillingAccountFromEnv(t)
|
2017-02-20 17:32:24 +00:00
|
|
|
pid := "terraform-" + acctest.RandString(10)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
// This step creates a new project with a billing account
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccProject_createBilling(pid, pname, org, billingId),
|
2017-02-20 17:32:24 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleProjectHasBillingAccount("google_project.acceptance", pid, billingId),
|
|
|
|
),
|
|
|
|
},
|
2017-12-19 22:40:24 +00:00
|
|
|
// Make sure import supports billing account
|
2017-12-14 00:45:52 +00:00
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_project.acceptance",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2017-02-20 17:32:24 +00:00
|
|
|
// Update to a different billing account
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccProject_createBilling(pid, pname, org, billingId2),
|
2017-02-20 17:32:24 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleProjectHasBillingAccount("google_project.acceptance", pid, billingId2),
|
|
|
|
),
|
|
|
|
},
|
2017-07-12 23:05:14 +00:00
|
|
|
// Unlink the billing account
|
|
|
|
resource.TestStep{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccProject_create(pid, pname, org),
|
2017-07-12 23:05:14 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleProjectHasBillingAccount("google_project.acceptance", pid, ""),
|
|
|
|
),
|
|
|
|
},
|
2017-02-20 17:32:24 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:40:24 +00:00
|
|
|
// Test that a Project resource can be created with labels
|
2018-02-23 23:14:24 +00:00
|
|
|
func TestAccProject_labels(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-11-20 23:45:51 +00:00
|
|
|
org := getTestOrgFromEnv(t)
|
2017-02-07 06:09:53 +00:00
|
|
|
pid := "terraform-" + acctest.RandString(10)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccProject_labels(pid, pname, org, map[string]string{"test": "that"}),
|
2017-02-07 06:09:53 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-12-19 22:40:24 +00:00
|
|
|
testAccCheckGoogleProjectHasLabels("google_project.acceptance", pid, map[string]string{"test": "that"}),
|
2017-02-07 06:09:53 +00:00
|
|
|
),
|
|
|
|
},
|
2017-12-19 22:40:24 +00:00
|
|
|
// Make sure import supports labels
|
2017-09-14 17:39:21 +00:00
|
|
|
{
|
2017-12-19 22:40:24 +00:00
|
|
|
ResourceName: "google_project.acceptance",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
2017-09-14 17:39:21 +00:00
|
|
|
},
|
|
|
|
// update project with labels
|
|
|
|
{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccProject_labels(pid, pname, org, map[string]string{"label": "label-value"}),
|
2017-09-14 17:39:21 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleProjectExists("google_project.acceptance", pid),
|
|
|
|
testAccCheckGoogleProjectHasLabels("google_project.acceptance", pid, map[string]string{"label": "label-value"}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
// update project delete labels
|
|
|
|
{
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccProject_create(pid, pname, org),
|
2017-09-14 17:39:21 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleProjectExists("google_project.acceptance", pid),
|
|
|
|
testAccCheckGoogleProjectHasNoLabels("google_project.acceptance", pid),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:25:49 +00:00
|
|
|
func TestAccProject_deleteDefaultNetwork(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
org := getTestOrgFromEnv(t)
|
|
|
|
pid := "terraform-" + acctest.RandString(10)
|
|
|
|
billingId := getTestBillingAccountFromEnv(t)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccProject_deleteDefaultNetwork(pid, pname, org, billingId),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:40 +00:00
|
|
|
func testAccCheckGoogleProjectExists(r, pid string) resource.TestCheckFunc {
|
2016-08-21 07:25:00 +00:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[r]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:55:40 +00:00
|
|
|
if rs.Primary.ID != pid {
|
|
|
|
return fmt.Errorf("Expected project %q to match ID %q in state", pid, rs.Primary.ID)
|
2016-08-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-10 04:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 17:32:24 +00:00
|
|
|
func testAccCheckGoogleProjectHasBillingAccount(r, pid, billingId string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[r]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// State should match expected
|
|
|
|
if rs.Primary.Attributes["billing_account"] != billingId {
|
|
|
|
return fmt.Errorf("Billing ID in state (%s) does not match expected value (%s)", rs.Primary.Attributes["billing_account"], billingId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual value in API should match state and expected
|
|
|
|
// Read the billing account
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
ba, err := config.clientBilling.Projects.GetBillingInfo(prefixedProject(pid)).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading billing account for project %q: %v", prefixedProject(pid), err)
|
|
|
|
}
|
|
|
|
if billingId != strings.TrimPrefix(ba.BillingAccountName, "billingAccounts/") {
|
|
|
|
return fmt.Errorf("Billing ID returned by API (%s) did not match expected value (%s)", ba.BillingAccountName, billingId)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:39:21 +00:00
|
|
|
func testAccCheckGoogleProjectHasLabels(r, pid string, expected map[string]string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[r]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// State should have the same number of labels
|
|
|
|
if rs.Primary.Attributes["labels.%"] != strconv.Itoa(len(expected)) {
|
|
|
|
return fmt.Errorf("Expected %d labels, got %s", len(expected), rs.Primary.Attributes["labels.%"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual value in API should match state and expected
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientResourceManager.Projects.Get(pid).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := found.Labels
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
// Determine only the different attributes
|
|
|
|
for k, v := range expected {
|
|
|
|
if av, ok := actual[k]; ok && v == av {
|
|
|
|
delete(expected, k)
|
|
|
|
delete(actual, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spewConf := spew.NewDefaultConfig()
|
|
|
|
spewConf.SortKeys = true
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Labels not equivalent. Difference is shown below. Top is actual, bottom is expected."+
|
|
|
|
"\n\n%s\n\n%s",
|
|
|
|
spewConf.Sdump(actual), spewConf.Sdump(expected),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGoogleProjectHasNoLabels(r, pid string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[r]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// State should have zero labels
|
|
|
|
if rs.Primary.Attributes["labels.%"] != "0" {
|
|
|
|
return fmt.Errorf("Expected 0 labels, got %s", rs.Primary.Attributes["labels.%"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual value in API should match state and expected
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientResourceManager.Projects.Get(pid).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
spewConf := spew.NewDefaultConfig()
|
|
|
|
spewConf.SortKeys = true
|
|
|
|
if found.Labels != nil {
|
|
|
|
return fmt.Errorf("Labels should be empty. Actual \n%s", spewConf.Sdump(found.Labels))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func testAccProject_labels(pid, name, org string, labels map[string]string) string {
|
2017-09-14 17:39:21 +00:00
|
|
|
r := fmt.Sprintf(`
|
|
|
|
resource "google_project" "acceptance" {
|
|
|
|
project_id = "%s"
|
|
|
|
name = "%s"
|
|
|
|
org_id = "%s"
|
|
|
|
labels {`, pid, name, org)
|
|
|
|
|
|
|
|
l := ""
|
|
|
|
for key, value := range labels {
|
|
|
|
l += fmt.Sprintf("%q = %q\n", key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
l += fmt.Sprintf("}\n}")
|
|
|
|
return r + l
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:25:49 +00:00
|
|
|
func testAccProject_deleteDefaultNetwork(pid, name, org, billing string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_project" "acceptance" {
|
|
|
|
project_id = "%s"
|
|
|
|
name = "%s"
|
|
|
|
org_id = "%s"
|
|
|
|
billing_account = "%s" # requires billing to enable compute API
|
|
|
|
auto_create_network = false
|
|
|
|
}`, pid, name, org, billing)
|
|
|
|
}
|
|
|
|
|
2017-02-20 17:32:24 +00:00
|
|
|
func skipIfEnvNotSet(t *testing.T, envs ...string) {
|
|
|
|
for _, k := range envs {
|
|
|
|
if os.Getenv(k) == "" {
|
|
|
|
t.Skipf("Environment variable %s is not set", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|