2015-02-10 11:29:49 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-19 02:28:46 +02:00
|
|
|
"strings"
|
2015-02-10 11:29:49 +01:00
|
|
|
"testing"
|
|
|
|
|
2016-01-05 22:47:10 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-02-10 11:29:49 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
2015-02-10 11:29:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeInstanceTemplate_basic(t *testing.T) {
|
|
|
|
var instanceTemplate compute.InstanceTemplate
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstanceTemplate_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceTemplateExists(
|
|
|
|
"google_compute_instance_template.foobar", &instanceTemplate),
|
|
|
|
testAccCheckComputeInstanceTemplateTag(&instanceTemplate, "foo"),
|
|
|
|
testAccCheckComputeInstanceTemplateMetadata(&instanceTemplate, "foo", "bar"),
|
2016-03-26 17:52:30 +01:00
|
|
|
testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20160301", true, true),
|
2015-02-10 11:29:49 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccComputeInstanceTemplate_IP(t *testing.T) {
|
|
|
|
var instanceTemplate compute.InstanceTemplate
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstanceTemplate_ip,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceTemplateExists(
|
|
|
|
"google_compute_instance_template.foobar", &instanceTemplate),
|
|
|
|
testAccCheckComputeInstanceTemplateNetwork(&instanceTemplate),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccComputeInstanceTemplate_disks(t *testing.T) {
|
|
|
|
var instanceTemplate compute.InstanceTemplate
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstanceTemplate_disks,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceTemplateExists(
|
|
|
|
"google_compute_instance_template.foobar", &instanceTemplate),
|
2016-03-26 17:52:30 +01:00
|
|
|
testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20160301", true, true),
|
2015-03-18 18:42:03 +01:00
|
|
|
testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "terraform-test-foobar", false, false),
|
2015-02-10 11:29:49 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-17 12:09:46 +01:00
|
|
|
func TestAccComputeInstanceTemplate_subnet_auto(t *testing.T) {
|
|
|
|
var instanceTemplate compute.InstanceTemplate
|
|
|
|
network := "network-" + acctest.RandString(10)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstanceTemplate_subnet_auto(network),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceTemplateExists(
|
|
|
|
"google_compute_instance_template.foobar", &instanceTemplate),
|
|
|
|
testAccCheckComputeInstanceTemplateNetworkName(&instanceTemplate, network),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccComputeInstanceTemplate_subnet_custom(t *testing.T) {
|
|
|
|
var instanceTemplate compute.InstanceTemplate
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeInstanceTemplate_subnet_custom,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeInstanceTemplateExists(
|
|
|
|
"google_compute_instance_template.foobar", &instanceTemplate),
|
|
|
|
testAccCheckComputeInstanceTemplateSubnetwork(&instanceTemplate),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-10 11:29:49 +01:00
|
|
|
func testAccCheckComputeInstanceTemplateDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_compute_instance_template" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.InstanceTemplates.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Instance template still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeInstanceTemplateExists(n string, instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
|
|
|
|
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)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.InstanceTemplates.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Instance template not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*instanceTemplate = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeInstanceTemplateMetadata(
|
|
|
|
instanceTemplate *compute.InstanceTemplate,
|
|
|
|
k string, v string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if instanceTemplate.Properties.Metadata == nil {
|
|
|
|
return fmt.Errorf("no metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range instanceTemplate.Properties.Metadata.Items {
|
|
|
|
if k != item.Key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:06:25 +02:00
|
|
|
if item.Value != nil && v == *item.Value {
|
2015-02-10 11:29:49 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:06:25 +02:00
|
|
|
return fmt.Errorf("bad value for %s: %s", k, *item.Value)
|
2015-02-10 11:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("metadata not found: %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeInstanceTemplateNetwork(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
for _, i := range instanceTemplate.Properties.NetworkInterfaces {
|
|
|
|
for _, c := range i.AccessConfigs {
|
|
|
|
if c.NatIP == "" {
|
|
|
|
return fmt.Errorf("no NAT IP")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 12:09:46 +01:00
|
|
|
func testAccCheckComputeInstanceTemplateNetworkName(instanceTemplate *compute.InstanceTemplate, network string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
for _, i := range instanceTemplate.Properties.NetworkInterfaces {
|
|
|
|
if !strings.Contains(i.Network, network) {
|
|
|
|
return fmt.Errorf("Network doesn't match expected value, Expected: %s Actual: %s", network, i.Network[strings.LastIndex("/", i.Network)+1:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 11:29:49 +01:00
|
|
|
func testAccCheckComputeInstanceTemplateDisk(instanceTemplate *compute.InstanceTemplate, source string, delete bool, boot bool) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if instanceTemplate.Properties.Disks == nil {
|
|
|
|
return fmt.Errorf("no disks")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, disk := range instanceTemplate.Properties.Disks {
|
|
|
|
if disk.InitializeParams == nil {
|
|
|
|
// Check disk source
|
|
|
|
if disk.Source == source {
|
|
|
|
if disk.AutoDelete == delete && disk.Boot == boot {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check source image
|
|
|
|
if disk.InitializeParams.SourceImage == source {
|
|
|
|
if disk.AutoDelete == delete && disk.Boot == boot {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Disk not found: %s", source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 12:09:46 +01:00
|
|
|
func testAccCheckComputeInstanceTemplateSubnetwork(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
for _, i := range instanceTemplate.Properties.NetworkInterfaces {
|
|
|
|
if i.Subnetwork == "" {
|
|
|
|
return fmt.Errorf("no subnet")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 11:29:49 +01:00
|
|
|
func testAccCheckComputeInstanceTemplateTag(instanceTemplate *compute.InstanceTemplate, n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if instanceTemplate.Properties.Tags == nil {
|
|
|
|
return fmt.Errorf("no tags")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range instanceTemplate.Properties.Tags.Items {
|
|
|
|
if k == n {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("tag not found: %s", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:47:10 +01:00
|
|
|
var testAccComputeInstanceTemplate_basic = fmt.Sprintf(`
|
2015-02-10 11:29:49 +01:00
|
|
|
resource "google_compute_instance_template" "foobar" {
|
2016-01-05 22:47:10 +01:00
|
|
|
name = "instancet-test-%s"
|
2015-02-10 11:29:49 +01:00
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
can_ip_forward = false
|
|
|
|
tags = ["foo", "bar"]
|
|
|
|
|
|
|
|
disk {
|
2016-03-26 17:52:30 +01:00
|
|
|
source_image = "debian-7-wheezy-v20160301"
|
2015-02-10 11:29:49 +01:00
|
|
|
auto_delete = true
|
|
|
|
boot = true
|
|
|
|
}
|
|
|
|
|
2015-02-16 18:04:56 +01:00
|
|
|
network_interface {
|
|
|
|
network = "default"
|
2015-02-10 11:29:49 +01:00
|
|
|
}
|
|
|
|
|
2015-10-28 16:33:49 +01:00
|
|
|
scheduling {
|
|
|
|
preemptible = false
|
|
|
|
automatic_restart = true
|
|
|
|
}
|
|
|
|
|
2015-02-10 11:29:49 +01:00
|
|
|
metadata {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
|
|
|
|
service_account {
|
|
|
|
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
|
|
|
|
}
|
2016-01-05 22:47:10 +01:00
|
|
|
}`, acctest.RandString(10))
|
2015-02-10 11:29:49 +01:00
|
|
|
|
2016-01-05 22:47:10 +01:00
|
|
|
var testAccComputeInstanceTemplate_ip = fmt.Sprintf(`
|
2015-02-10 11:29:49 +01:00
|
|
|
resource "google_compute_address" "foo" {
|
2016-01-05 22:47:10 +01:00
|
|
|
name = "instancet-test-%s"
|
2015-02-10 11:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_instance_template" "foobar" {
|
2016-01-05 22:47:10 +01:00
|
|
|
name = "instancet-test-%s"
|
2015-02-10 11:29:49 +01:00
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
tags = ["foo", "bar"]
|
|
|
|
|
|
|
|
disk {
|
2016-03-26 17:52:30 +01:00
|
|
|
source_image = "debian-7-wheezy-v20160301"
|
2015-02-10 11:29:49 +01:00
|
|
|
}
|
|
|
|
|
2015-02-16 18:04:56 +01:00
|
|
|
network_interface {
|
|
|
|
network = "default"
|
|
|
|
access_config {
|
|
|
|
nat_ip = "${google_compute_address.foo.address}"
|
|
|
|
}
|
2015-02-10 11:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
2016-01-05 22:47:10 +01:00
|
|
|
}`, acctest.RandString(10), acctest.RandString(10))
|
2015-02-10 11:29:49 +01:00
|
|
|
|
2016-01-05 22:47:10 +01:00
|
|
|
var testAccComputeInstanceTemplate_disks = fmt.Sprintf(`
|
2015-03-18 18:42:03 +01:00
|
|
|
resource "google_compute_disk" "foobar" {
|
2016-01-05 22:47:10 +01:00
|
|
|
name = "instancet-test-%s"
|
2016-03-26 17:52:30 +01:00
|
|
|
image = "debian-7-wheezy-v20160301"
|
2015-03-18 18:42:03 +01:00
|
|
|
size = 10
|
|
|
|
type = "pd-ssd"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
}
|
|
|
|
|
2015-02-10 11:29:49 +01:00
|
|
|
resource "google_compute_instance_template" "foobar" {
|
2016-01-05 22:47:10 +01:00
|
|
|
name = "instancet-test-%s"
|
2015-02-10 11:29:49 +01:00
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
|
|
|
|
disk {
|
2016-03-26 17:52:30 +01:00
|
|
|
source_image = "debian-7-wheezy-v20160301"
|
2015-02-10 11:29:49 +01:00
|
|
|
auto_delete = true
|
2015-08-19 08:57:04 +02:00
|
|
|
disk_size_gb = 100
|
2015-02-10 11:29:49 +01:00
|
|
|
boot = true
|
|
|
|
}
|
|
|
|
|
|
|
|
disk {
|
2015-03-18 18:42:03 +01:00
|
|
|
source = "terraform-test-foobar"
|
2015-02-10 11:29:49 +01:00
|
|
|
auto_delete = false
|
|
|
|
boot = false
|
|
|
|
}
|
|
|
|
|
2015-02-16 18:04:56 +01:00
|
|
|
network_interface {
|
|
|
|
network = "default"
|
2015-02-10 11:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
2016-01-05 22:47:10 +01:00
|
|
|
}`, acctest.RandString(10), acctest.RandString(10))
|
2016-02-17 12:09:46 +01:00
|
|
|
|
|
|
|
func testAccComputeInstanceTemplate_subnet_auto(network string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_compute_network" "auto-network" {
|
|
|
|
name = "%s"
|
|
|
|
auto_create_subnetworks = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_instance_template" "foobar" {
|
|
|
|
name = "instance-tpl-%s"
|
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
|
|
|
|
disk {
|
|
|
|
source_image = "debian-7-wheezy-v20160211"
|
|
|
|
auto_delete = true
|
|
|
|
disk_size_gb = 10
|
|
|
|
boot = true
|
|
|
|
}
|
|
|
|
|
|
|
|
network_interface {
|
|
|
|
network = "${google_compute_network.auto-network.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}`, network, acctest.RandString(10))
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccComputeInstanceTemplate_subnet_custom = fmt.Sprintf(`
|
|
|
|
resource "google_compute_network" "network" {
|
|
|
|
name = "network-%s"
|
|
|
|
auto_create_subnetworks = false
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_subnetwork" "subnetwork" {
|
|
|
|
name = "subnetwork-%s"
|
|
|
|
ip_cidr_range = "10.0.0.0/24"
|
|
|
|
region = "us-central1"
|
|
|
|
network = "${google_compute_network.network.self_link}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_instance_template" "foobar" {
|
|
|
|
name = "instance-test-%s"
|
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
region = "us-central1"
|
|
|
|
|
|
|
|
disk {
|
|
|
|
source_image = "debian-7-wheezy-v20160211"
|
|
|
|
auto_delete = true
|
|
|
|
disk_size_gb = 10
|
|
|
|
boot = true
|
|
|
|
}
|
|
|
|
|
|
|
|
network_interface {
|
|
|
|
subnetwork = "${google_compute_subnetwork.subnetwork.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
|