2015-08-20 21:18:41 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-04-27 19:28:31 +02:00
|
|
|
"os"
|
2015-08-20 21:18:41 +02:00
|
|
|
"testing"
|
|
|
|
|
2017-04-27 19:28:31 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-08-20 21:18:41 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add two key value pairs
|
|
|
|
func TestAccComputeProjectMetadata_basic(t *testing.T) {
|
2017-04-27 19:28:31 +02:00
|
|
|
skipIfEnvNotSet(t,
|
|
|
|
[]string{
|
|
|
|
"GOOGLE_ORG",
|
|
|
|
"GOOGLE_BILLING_ACCOUNT",
|
|
|
|
}...,
|
|
|
|
)
|
|
|
|
|
|
|
|
billingId := os.Getenv("GOOGLE_BILLING_ACCOUNT")
|
2015-08-20 21:18:41 +02:00
|
|
|
var project compute.Project
|
2017-04-27 20:39:13 +02:00
|
|
|
projectID := "terrafom-test-" + acctest.RandString(10)
|
2015-08-20 21:18:41 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeProjectMetadataDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-04-27 20:39:13 +02:00
|
|
|
Config: testAccComputeProject_basic0_metadata(projectID, pname, org, billingId),
|
2015-08-20 21:18:41 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeProjectExists(
|
2017-04-27 20:39:13 +02:00
|
|
|
"google_compute_project_metadata.fizzbuzz", projectID, &project),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "banana", "orange"),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "sofa", "darwinism"),
|
|
|
|
testAccCheckComputeProjectMetadataSize(projectID, 2),
|
2015-08-20 21:18:41 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add three key value pairs, then replace one and modify a second
|
|
|
|
func TestAccComputeProjectMetadata_modify_1(t *testing.T) {
|
2017-04-27 19:28:31 +02:00
|
|
|
skipIfEnvNotSet(t,
|
|
|
|
[]string{
|
|
|
|
"GOOGLE_ORG",
|
|
|
|
"GOOGLE_BILLING_ACCOUNT",
|
|
|
|
}...,
|
|
|
|
)
|
|
|
|
|
|
|
|
billingId := os.Getenv("GOOGLE_BILLING_ACCOUNT")
|
2015-08-20 21:18:41 +02:00
|
|
|
var project compute.Project
|
2017-04-27 20:39:13 +02:00
|
|
|
projectID := "terrafom-test-" + acctest.RandString(10)
|
2015-08-20 21:18:41 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeProjectMetadataDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-04-27 20:39:13 +02:00
|
|
|
Config: testAccComputeProject_modify0_metadata(projectID, pname, org, billingId),
|
2015-08-20 21:18:41 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeProjectExists(
|
2017-04-27 20:39:13 +02:00
|
|
|
"google_compute_project_metadata.fizzbuzz", projectID, &project),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "paper", "pen"),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "genghis_khan", "french bread"),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "happy", "smiling"),
|
|
|
|
testAccCheckComputeProjectMetadataSize(projectID, 3),
|
2015-08-20 21:18:41 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2017-04-27 20:39:13 +02:00
|
|
|
Config: testAccComputeProject_modify1_metadata(projectID, pname, org, billingId),
|
2015-08-20 21:18:41 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeProjectExists(
|
2017-04-27 20:39:13 +02:00
|
|
|
"google_compute_project_metadata.fizzbuzz", projectID, &project),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "paper", "pen"),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "paris", "french bread"),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "happy", "laughing"),
|
|
|
|
testAccCheckComputeProjectMetadataSize(projectID, 3),
|
2015-08-20 21:18:41 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add two key value pairs, and replace both
|
|
|
|
func TestAccComputeProjectMetadata_modify_2(t *testing.T) {
|
2017-04-27 19:28:31 +02:00
|
|
|
skipIfEnvNotSet(t,
|
|
|
|
[]string{
|
|
|
|
"GOOGLE_ORG",
|
|
|
|
"GOOGLE_BILLING_ACCOUNT",
|
|
|
|
}...,
|
|
|
|
)
|
|
|
|
|
|
|
|
billingId := os.Getenv("GOOGLE_BILLING_ACCOUNT")
|
2015-08-20 21:18:41 +02:00
|
|
|
var project compute.Project
|
2017-04-27 20:39:13 +02:00
|
|
|
projectID := "terraform-test-" + acctest.RandString(10)
|
2015-08-20 21:18:41 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeProjectMetadataDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-04-27 20:39:13 +02:00
|
|
|
Config: testAccComputeProject_basic0_metadata(projectID, pname, org, billingId),
|
2015-08-20 21:18:41 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeProjectExists(
|
2017-04-27 20:39:13 +02:00
|
|
|
"google_compute_project_metadata.fizzbuzz", projectID, &project),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "banana", "orange"),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "sofa", "darwinism"),
|
|
|
|
testAccCheckComputeProjectMetadataSize(projectID, 2),
|
2015-08-20 21:18:41 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2017-04-27 20:39:13 +02:00
|
|
|
Config: testAccComputeProject_basic1_metadata(projectID, pname, org, billingId),
|
2015-08-20 21:18:41 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeProjectExists(
|
2017-04-27 20:39:13 +02:00
|
|
|
"google_compute_project_metadata.fizzbuzz", projectID, &project),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "kiwi", "papaya"),
|
|
|
|
testAccCheckComputeProjectMetadataContains(projectID, "finches", "darwinism"),
|
|
|
|
testAccCheckComputeProjectMetadataSize(projectID, 2),
|
2015-08-20 21:18:41 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeProjectMetadataDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
2017-04-27 19:28:31 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_compute_project_metadata" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := config.clientCompute.Projects.Get(rs.Primary.ID).Do()
|
|
|
|
if err == nil && len(project.CommonInstanceMetadata.Items) > 0 {
|
|
|
|
return fmt.Errorf("Error, metadata items still exist in %s", rs.Primary.ID)
|
|
|
|
}
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:39:13 +02:00
|
|
|
func testAccCheckComputeProjectExists(n, projectID string, project *compute.Project) resource.TestCheckFunc {
|
2015-08-20 21:18:41 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
2017-04-27 20:39:13 +02:00
|
|
|
found, err := config.clientCompute.Projects.Get(projectID).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if "common_metadata" != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Common metadata not found, found %s", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
*project = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:39:13 +02:00
|
|
|
func testAccCheckComputeProjectMetadataContains(projectID, key, value string) resource.TestCheckFunc {
|
2015-08-20 21:18:41 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
2017-04-27 20:39:13 +02:00
|
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failed to load project service for %s: %s", config.Project, err)
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:06:25 +02:00
|
|
|
for _, kv := range project.CommonInstanceMetadata.Items {
|
2015-08-20 21:18:41 +02:00
|
|
|
if kv.Key == key {
|
2015-08-31 16:06:25 +02:00
|
|
|
if kv.Value != nil && *kv.Value == value {
|
2015-08-20 21:18:41 +02:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Error, key value mismatch, wanted (%s, %s), got (%s, %s)",
|
2015-08-31 16:06:25 +02:00
|
|
|
key, value, kv.Key, *kv.Value)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 19:28:31 +02:00
|
|
|
return fmt.Errorf("Error, key %s not present in %s", key, project.SelfLink)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:39:13 +02:00
|
|
|
func testAccCheckComputeProjectMetadataSize(projectID string, size int) resource.TestCheckFunc {
|
2015-08-20 21:18:41 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
2017-04-27 20:39:13 +02:00
|
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failed to load project service for %s: %s", config.Project, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if size > len(project.CommonInstanceMetadata.Items) {
|
2015-08-31 16:06:25 +02:00
|
|
|
return fmt.Errorf("Error, expected at least %d metadata items, got %d", size,
|
2015-08-20 21:18:41 +02:00
|
|
|
len(project.CommonInstanceMetadata.Items))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:39:13 +02:00
|
|
|
func testAccComputeProject_basic0_metadata(projectID, name, org, billing string) string {
|
2017-04-27 19:28:31 +02:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_project" "project" {
|
|
|
|
project_id = "%s"
|
|
|
|
name = "%s"
|
|
|
|
org_id = "%s"
|
|
|
|
billing_account = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_project_services" "services" {
|
|
|
|
project = "${google_project.project.project_id}"
|
|
|
|
services = ["compute-component.googleapis.com"]
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:18:41 +02:00
|
|
|
resource "google_compute_project_metadata" "fizzbuzz" {
|
2017-04-27 19:28:31 +02:00
|
|
|
project = "${google_project.project.project_id}"
|
|
|
|
metadata {
|
|
|
|
banana = "orange"
|
|
|
|
sofa = "darwinism"
|
|
|
|
}
|
|
|
|
depends_on = ["google_project_services.services"]
|
2017-04-27 20:39:13 +02:00
|
|
|
}`, projectID, name, org, billing)
|
2017-04-27 19:28:31 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:39:13 +02:00
|
|
|
func testAccComputeProject_basic1_metadata(projectID, name, org, billing string) string {
|
2017-04-27 19:28:31 +02:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_project" "project" {
|
|
|
|
project_id = "%s"
|
|
|
|
name = "%s"
|
|
|
|
org_id = "%s"
|
|
|
|
billing_account = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_project_services" "services" {
|
|
|
|
project = "${google_project.project.project_id}"
|
|
|
|
services = ["compute-component.googleapis.com"]
|
|
|
|
}
|
2015-08-20 21:18:41 +02:00
|
|
|
|
|
|
|
resource "google_compute_project_metadata" "fizzbuzz" {
|
2017-04-27 19:28:31 +02:00
|
|
|
project = "${google_project.project.project_id}"
|
|
|
|
metadata {
|
|
|
|
kiwi = "papaya"
|
|
|
|
finches = "darwinism"
|
|
|
|
}
|
|
|
|
depends_on = ["google_project_services.services"]
|
2017-04-27 20:39:13 +02:00
|
|
|
}`, projectID, name, org, billing)
|
2017-04-27 19:28:31 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:39:13 +02:00
|
|
|
func testAccComputeProject_modify0_metadata(projectID, name, org, billing string) string {
|
2017-04-27 19:28:31 +02:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_project" "project" {
|
|
|
|
project_id = "%s"
|
|
|
|
name = "%s"
|
|
|
|
org_id = "%s"
|
|
|
|
billing_account = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_project_services" "services" {
|
|
|
|
project = "${google_project.project.project_id}"
|
|
|
|
services = ["compute-component.googleapis.com"]
|
|
|
|
}
|
2015-08-20 21:18:41 +02:00
|
|
|
|
|
|
|
resource "google_compute_project_metadata" "fizzbuzz" {
|
2017-04-27 19:28:31 +02:00
|
|
|
project = "${google_project.project.project_id}"
|
|
|
|
metadata {
|
|
|
|
paper = "pen"
|
|
|
|
genghis_khan = "french bread"
|
|
|
|
happy = "smiling"
|
|
|
|
}
|
|
|
|
depends_on = ["google_project_services.services"]
|
2017-04-27 20:39:13 +02:00
|
|
|
}`, projectID, name, org, billing)
|
2017-04-27 19:28:31 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:39:13 +02:00
|
|
|
func testAccComputeProject_modify1_metadata(projectID, name, org, billing string) string {
|
2017-04-27 19:28:31 +02:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_project" "project" {
|
|
|
|
project_id = "%s"
|
|
|
|
name = "%s"
|
|
|
|
org_id = "%s"
|
|
|
|
billing_account = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_project_services" "services" {
|
|
|
|
project = "${google_project.project.project_id}"
|
|
|
|
services = ["compute-component.googleapis.com"]
|
|
|
|
}
|
2015-08-20 21:18:41 +02:00
|
|
|
|
|
|
|
resource "google_compute_project_metadata" "fizzbuzz" {
|
2017-04-27 19:28:31 +02:00
|
|
|
project = "${google_project.project.project_id}"
|
|
|
|
metadata {
|
|
|
|
paper = "pen"
|
|
|
|
paris = "french bread"
|
|
|
|
happy = "laughing"
|
|
|
|
}
|
|
|
|
depends_on = ["google_project_services.services"]
|
2017-04-27 20:39:13 +02:00
|
|
|
}`, projectID, name, org, billing)
|
2017-04-27 19:28:31 +02:00
|
|
|
}
|