2014-08-25 23:57:17 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-02 15:52:49 +02:00
|
|
|
"strings"
|
2014-09-09 22:43:03 +02:00
|
|
|
"testing"
|
2014-08-25 23:57:17 +02:00
|
|
|
|
|
|
|
"code.google.com/p/google-api-go-client/compute/v1"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeInstance_basic(t *testing.T) {
|
|
|
|
var instance compute.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstance_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceExists(
|
|
|
|
"google_compute_instance.foobar", &instance),
|
2014-08-26 00:25:45 +02:00
|
|
|
testAccCheckComputeInstanceTag(&instance, "foo"),
|
|
|
|
testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
|
2014-09-02 15:52:49 +02:00
|
|
|
testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
|
2014-08-25 23:57:17 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-26 00:47:21 +02:00
|
|
|
func TestAccComputeInstance_IP(t *testing.T) {
|
|
|
|
var instance compute.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstance_ip,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceExists(
|
|
|
|
"google_compute_instance.foobar", &instance),
|
|
|
|
testAccCheckComputeInstanceNetwork(&instance),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:52:49 +02:00
|
|
|
//!NB requires that disk with name terraform-test-disk is present in gce,
|
|
|
|
//if created as dependency then it tries to remove it while it is still attached
|
|
|
|
//to instance and that fails with an error
|
|
|
|
func TestAccComputeInstance_disks(t *testing.T) {
|
|
|
|
var instance compute.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstance_disks,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
|
|
|
|
testAccCheckComputeInstanceDisk(&instance, "terraform-test-disk", false, false),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:48:49 +02:00
|
|
|
func TestAccComputeInstance_update(t *testing.T) {
|
|
|
|
var instance compute.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstance_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceExists(
|
|
|
|
"google_compute_instance.foobar", &instance),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstance_update,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceExists(
|
|
|
|
"google_compute_instance.foobar", &instance),
|
|
|
|
testAccCheckComputeInstanceMetadata(
|
|
|
|
&instance, "bar", "baz"),
|
2014-08-26 22:52:18 +02:00
|
|
|
testAccCheckComputeInstanceTag(&instance, "baz"),
|
2014-08-26 22:48:49 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.Resources {
|
|
|
|
if rs.Type != "google_compute_instance" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.Instances.Get(
|
|
|
|
config.Project, rs.Attributes["zone"], rs.ID).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Instance still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.Instances.Get(
|
|
|
|
config.Project, rs.Attributes["zone"], rs.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != rs.ID {
|
|
|
|
return fmt.Errorf("Instance not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*instance = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 00:25:45 +02:00
|
|
|
func testAccCheckComputeInstanceMetadata(
|
|
|
|
instance *compute.Instance,
|
|
|
|
k string, v string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if instance.Metadata == nil {
|
|
|
|
return fmt.Errorf("no metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range instance.Metadata.Items {
|
|
|
|
if k != item.Key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == item.Value {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("bad value for %s: %s", k, item.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("metadata not found: %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 00:47:21 +02:00
|
|
|
func testAccCheckComputeInstanceNetwork(instance *compute.Instance) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
for _, i := range instance.NetworkInterfaces {
|
|
|
|
for _, c := range i.AccessConfigs {
|
|
|
|
if c.NatIP == "" {
|
|
|
|
return fmt.Errorf("no NAT IP")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:52:49 +02:00
|
|
|
func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if instance.Disks == nil {
|
|
|
|
return fmt.Errorf("no disks")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, disk := range instance.Disks {
|
2014-09-09 22:43:03 +02:00
|
|
|
if strings.LastIndex(disk.Source, "/"+source) == (len(disk.Source)-len(source)-1) && disk.AutoDelete == delete && disk.Boot == boot {
|
2014-09-02 15:52:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Disk not found: %s", source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 00:25:45 +02:00
|
|
|
func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
|
2014-08-26 00:10:30 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if instance.Tags == nil {
|
|
|
|
return fmt.Errorf("no tags")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range instance.Tags.Items {
|
|
|
|
if k == n {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("tag not found: %s", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:57:17 +02:00
|
|
|
const testAccComputeInstance_basic = `
|
|
|
|
resource "google_compute_instance" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
zone = "us-central1-a"
|
2014-08-26 00:10:30 +02:00
|
|
|
tags = ["foo", "bar"]
|
2014-08-25 23:57:17 +02:00
|
|
|
|
|
|
|
disk {
|
2014-08-26 06:35:23 +02:00
|
|
|
image = "debian-7-wheezy-v20140814"
|
2014-08-25 23:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
network {
|
|
|
|
source = "default"
|
|
|
|
}
|
2014-08-26 00:25:45 +02:00
|
|
|
|
|
|
|
metadata {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
2014-08-25 23:57:17 +02:00
|
|
|
}`
|
2014-08-26 00:47:21 +02:00
|
|
|
|
2014-08-26 22:48:49 +02:00
|
|
|
const testAccComputeInstance_update = `
|
|
|
|
resource "google_compute_instance" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
zone = "us-central1-a"
|
2014-08-26 22:52:18 +02:00
|
|
|
tags = ["baz"]
|
2014-08-26 22:48:49 +02:00
|
|
|
|
|
|
|
disk {
|
|
|
|
image = "debian-7-wheezy-v20140814"
|
|
|
|
}
|
|
|
|
|
|
|
|
network {
|
|
|
|
source = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
bar = "baz"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2014-08-26 00:47:21 +02:00
|
|
|
const testAccComputeInstance_ip = `
|
|
|
|
resource "google_compute_address" "foo" {
|
|
|
|
name = "foo"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_instance" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
tags = ["foo", "bar"]
|
|
|
|
|
|
|
|
disk {
|
2014-08-26 06:35:23 +02:00
|
|
|
image = "debian-7-wheezy-v20140814"
|
2014-08-26 00:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
network {
|
|
|
|
source = "default"
|
|
|
|
address = "${google_compute_address.foo.address}"
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}`
|
2014-09-02 15:52:49 +02:00
|
|
|
|
|
|
|
const testAccComputeInstance_disks = `
|
|
|
|
resource "google_compute_instance" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
|
|
|
|
disk {
|
|
|
|
image = "debian-7-wheezy-v20140814"
|
|
|
|
}
|
|
|
|
|
|
|
|
disk {
|
|
|
|
disk = "terraform-test-disk"
|
|
|
|
auto_delete = false
|
|
|
|
}
|
|
|
|
|
|
|
|
network {
|
|
|
|
source = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}`
|