github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_instance_template_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"google.golang.org/api/compute/v1"
    12  )
    13  
    14  func TestAccComputeInstanceTemplate_basic(t *testing.T) {
    15  	var instanceTemplate compute.InstanceTemplate
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccComputeInstanceTemplate_basic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckComputeInstanceTemplateExists(
    26  						"google_compute_instance_template.foobar", &instanceTemplate),
    27  					testAccCheckComputeInstanceTemplateTag(&instanceTemplate, "foo"),
    28  					testAccCheckComputeInstanceTemplateMetadata(&instanceTemplate, "foo", "bar"),
    29  					testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160803", true, true),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccComputeInstanceTemplate_IP(t *testing.T) {
    37  	var instanceTemplate compute.InstanceTemplate
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccComputeInstanceTemplate_ip,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckComputeInstanceTemplateExists(
    48  						"google_compute_instance_template.foobar", &instanceTemplate),
    49  					testAccCheckComputeInstanceTemplateNetwork(&instanceTemplate),
    50  				),
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func TestAccComputeInstanceTemplate_disks(t *testing.T) {
    57  	var instanceTemplate compute.InstanceTemplate
    58  
    59  	resource.Test(t, resource.TestCase{
    60  		PreCheck:     func() { testAccPreCheck(t) },
    61  		Providers:    testAccProviders,
    62  		CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
    63  		Steps: []resource.TestStep{
    64  			resource.TestStep{
    65  				Config: testAccComputeInstanceTemplate_disks,
    66  				Check: resource.ComposeTestCheckFunc(
    67  					testAccCheckComputeInstanceTemplateExists(
    68  						"google_compute_instance_template.foobar", &instanceTemplate),
    69  					testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160803", true, true),
    70  					testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "terraform-test-foobar", false, false),
    71  				),
    72  			},
    73  		},
    74  	})
    75  }
    76  
    77  func TestAccComputeInstanceTemplate_subnet_auto(t *testing.T) {
    78  	var instanceTemplate compute.InstanceTemplate
    79  	network := "network-" + acctest.RandString(10)
    80  
    81  	resource.Test(t, resource.TestCase{
    82  		PreCheck:     func() { testAccPreCheck(t) },
    83  		Providers:    testAccProviders,
    84  		CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
    85  		Steps: []resource.TestStep{
    86  			resource.TestStep{
    87  				Config: testAccComputeInstanceTemplate_subnet_auto(network),
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckComputeInstanceTemplateExists(
    90  						"google_compute_instance_template.foobar", &instanceTemplate),
    91  					testAccCheckComputeInstanceTemplateNetworkName(&instanceTemplate, network),
    92  				),
    93  			},
    94  		},
    95  	})
    96  }
    97  
    98  func TestAccComputeInstanceTemplate_subnet_custom(t *testing.T) {
    99  	var instanceTemplate compute.InstanceTemplate
   100  
   101  	resource.Test(t, resource.TestCase{
   102  		PreCheck:     func() { testAccPreCheck(t) },
   103  		Providers:    testAccProviders,
   104  		CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
   105  		Steps: []resource.TestStep{
   106  			resource.TestStep{
   107  				Config: testAccComputeInstanceTemplate_subnet_custom,
   108  				Check: resource.ComposeTestCheckFunc(
   109  					testAccCheckComputeInstanceTemplateExists(
   110  						"google_compute_instance_template.foobar", &instanceTemplate),
   111  					testAccCheckComputeInstanceTemplateSubnetwork(&instanceTemplate),
   112  				),
   113  			},
   114  		},
   115  	})
   116  }
   117  
   118  func testAccCheckComputeInstanceTemplateDestroy(s *terraform.State) error {
   119  	config := testAccProvider.Meta().(*Config)
   120  
   121  	for _, rs := range s.RootModule().Resources {
   122  		if rs.Type != "google_compute_instance_template" {
   123  			continue
   124  		}
   125  
   126  		_, err := config.clientCompute.InstanceTemplates.Get(
   127  			config.Project, rs.Primary.ID).Do()
   128  		if err == nil {
   129  			return fmt.Errorf("Instance template still exists")
   130  		}
   131  	}
   132  
   133  	return nil
   134  }
   135  
   136  func testAccCheckComputeInstanceTemplateExists(n string, instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
   137  	return func(s *terraform.State) error {
   138  		rs, ok := s.RootModule().Resources[n]
   139  		if !ok {
   140  			return fmt.Errorf("Not found: %s", n)
   141  		}
   142  
   143  		if rs.Primary.ID == "" {
   144  			return fmt.Errorf("No ID is set")
   145  		}
   146  
   147  		config := testAccProvider.Meta().(*Config)
   148  
   149  		found, err := config.clientCompute.InstanceTemplates.Get(
   150  			config.Project, rs.Primary.ID).Do()
   151  		if err != nil {
   152  			return err
   153  		}
   154  
   155  		if found.Name != rs.Primary.ID {
   156  			return fmt.Errorf("Instance template not found")
   157  		}
   158  
   159  		*instanceTemplate = *found
   160  
   161  		return nil
   162  	}
   163  }
   164  
   165  func testAccCheckComputeInstanceTemplateMetadata(
   166  	instanceTemplate *compute.InstanceTemplate,
   167  	k string, v string) resource.TestCheckFunc {
   168  	return func(s *terraform.State) error {
   169  		if instanceTemplate.Properties.Metadata == nil {
   170  			return fmt.Errorf("no metadata")
   171  		}
   172  
   173  		for _, item := range instanceTemplate.Properties.Metadata.Items {
   174  			if k != item.Key {
   175  				continue
   176  			}
   177  
   178  			if item.Value != nil && v == *item.Value {
   179  				return nil
   180  			}
   181  
   182  			return fmt.Errorf("bad value for %s: %s", k, *item.Value)
   183  		}
   184  
   185  		return fmt.Errorf("metadata not found: %s", k)
   186  	}
   187  }
   188  
   189  func testAccCheckComputeInstanceTemplateNetwork(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
   190  	return func(s *terraform.State) error {
   191  		for _, i := range instanceTemplate.Properties.NetworkInterfaces {
   192  			for _, c := range i.AccessConfigs {
   193  				if c.NatIP == "" {
   194  					return fmt.Errorf("no NAT IP")
   195  				}
   196  			}
   197  		}
   198  
   199  		return nil
   200  	}
   201  }
   202  
   203  func testAccCheckComputeInstanceTemplateNetworkName(instanceTemplate *compute.InstanceTemplate, network string) resource.TestCheckFunc {
   204  	return func(s *terraform.State) error {
   205  		for _, i := range instanceTemplate.Properties.NetworkInterfaces {
   206  			if !strings.Contains(i.Network, network) {
   207  				return fmt.Errorf("Network doesn't match expected value, Expected: %s Actual: %s", network, i.Network[strings.LastIndex("/", i.Network)+1:])
   208  			}
   209  		}
   210  
   211  		return nil
   212  	}
   213  }
   214  
   215  func testAccCheckComputeInstanceTemplateDisk(instanceTemplate *compute.InstanceTemplate, source string, delete bool, boot bool) resource.TestCheckFunc {
   216  	return func(s *terraform.State) error {
   217  		if instanceTemplate.Properties.Disks == nil {
   218  			return fmt.Errorf("no disks")
   219  		}
   220  
   221  		for _, disk := range instanceTemplate.Properties.Disks {
   222  			if disk.InitializeParams == nil {
   223  				// Check disk source
   224  				if disk.Source == source {
   225  					if disk.AutoDelete == delete && disk.Boot == boot {
   226  						return nil
   227  					}
   228  				}
   229  			} else {
   230  				// Check source image
   231  				if disk.InitializeParams.SourceImage == source {
   232  					if disk.AutoDelete == delete && disk.Boot == boot {
   233  						return nil
   234  					}
   235  				}
   236  			}
   237  		}
   238  
   239  		return fmt.Errorf("Disk not found: %s", source)
   240  	}
   241  }
   242  
   243  func testAccCheckComputeInstanceTemplateSubnetwork(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
   244  	return func(s *terraform.State) error {
   245  		for _, i := range instanceTemplate.Properties.NetworkInterfaces {
   246  			if i.Subnetwork == "" {
   247  				return fmt.Errorf("no subnet")
   248  			}
   249  		}
   250  
   251  		return nil
   252  	}
   253  }
   254  
   255  func testAccCheckComputeInstanceTemplateTag(instanceTemplate *compute.InstanceTemplate, n string) resource.TestCheckFunc {
   256  	return func(s *terraform.State) error {
   257  		if instanceTemplate.Properties.Tags == nil {
   258  			return fmt.Errorf("no tags")
   259  		}
   260  
   261  		for _, k := range instanceTemplate.Properties.Tags.Items {
   262  			if k == n {
   263  				return nil
   264  			}
   265  		}
   266  
   267  		return fmt.Errorf("tag not found: %s", n)
   268  	}
   269  }
   270  
   271  var testAccComputeInstanceTemplate_basic = fmt.Sprintf(`
   272  resource "google_compute_instance_template" "foobar" {
   273  	name = "instancet-test-%s"
   274  	machine_type = "n1-standard-1"
   275  	can_ip_forward = false
   276  	tags = ["foo", "bar"]
   277  
   278  	disk {
   279  		source_image = "debian-8-jessie-v20160803"
   280  		auto_delete = true
   281  		boot = true
   282  	}
   283  
   284  	network_interface {
   285  		network = "default"
   286  	}
   287  
   288  	scheduling {
   289  		preemptible = false
   290  		automatic_restart = true
   291  	}
   292  
   293  	metadata {
   294  		foo = "bar"
   295  	}
   296  
   297  	service_account {
   298  		scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   299  	}
   300  }`, acctest.RandString(10))
   301  
   302  var testAccComputeInstanceTemplate_ip = fmt.Sprintf(`
   303  resource "google_compute_address" "foo" {
   304  	name = "instancet-test-%s"
   305  }
   306  
   307  resource "google_compute_instance_template" "foobar" {
   308  	name = "instancet-test-%s"
   309  	machine_type = "n1-standard-1"
   310  	tags = ["foo", "bar"]
   311  
   312  	disk {
   313  		source_image = "debian-8-jessie-v20160803"
   314  	}
   315  
   316  	network_interface {
   317  		network = "default"
   318  		access_config {
   319  			nat_ip = "${google_compute_address.foo.address}"
   320  		}
   321  	}
   322  
   323  	metadata {
   324  		foo = "bar"
   325  	}
   326  }`, acctest.RandString(10), acctest.RandString(10))
   327  
   328  var testAccComputeInstanceTemplate_disks = fmt.Sprintf(`
   329  resource "google_compute_disk" "foobar" {
   330  	name = "instancet-test-%s"
   331  	image = "debian-8-jessie-v20160803"
   332  	size = 10
   333  	type = "pd-ssd"
   334  	zone = "us-central1-a"
   335  }
   336  
   337  resource "google_compute_instance_template" "foobar" {
   338  	name = "instancet-test-%s"
   339  	machine_type = "n1-standard-1"
   340  
   341  	disk {
   342  		source_image = "debian-8-jessie-v20160803"
   343  		auto_delete = true
   344  		disk_size_gb = 100
   345  		boot = true
   346  	}
   347  
   348  	disk {
   349  		source = "terraform-test-foobar"
   350  		auto_delete = false
   351  		boot = false
   352  	}
   353  
   354  	network_interface {
   355  		network = "default"
   356  	}
   357  
   358  	metadata {
   359  		foo = "bar"
   360  	}
   361  }`, acctest.RandString(10), acctest.RandString(10))
   362  
   363  func testAccComputeInstanceTemplate_subnet_auto(network string) string {
   364  	return fmt.Sprintf(`
   365  	resource "google_compute_network" "auto-network" {
   366  		name = "%s"
   367  		auto_create_subnetworks = true
   368  	}
   369  
   370  	resource "google_compute_instance_template" "foobar" {
   371  		name = "instance-tpl-%s"
   372  		machine_type = "n1-standard-1"
   373  
   374  		disk {
   375  			source_image = "debian-8-jessie-v20160803"
   376  			auto_delete = true
   377  			disk_size_gb = 10
   378  			boot = true
   379  		}
   380  
   381  		network_interface {
   382  			network = "${google_compute_network.auto-network.name}"
   383  		}
   384  
   385  		metadata {
   386  			foo = "bar"
   387  		}
   388  	}`, network, acctest.RandString(10))
   389  }
   390  
   391  var testAccComputeInstanceTemplate_subnet_custom = fmt.Sprintf(`
   392  resource "google_compute_network" "network" {
   393  	name = "network-%s"
   394  	auto_create_subnetworks = false
   395  }
   396  
   397  resource "google_compute_subnetwork" "subnetwork" {
   398  	name = "subnetwork-%s"
   399  	ip_cidr_range = "10.0.0.0/24"
   400  	region = "us-central1"
   401  	network = "${google_compute_network.network.self_link}"
   402  }
   403  
   404  resource "google_compute_instance_template" "foobar" {
   405  	name = "instance-test-%s"
   406  	machine_type = "n1-standard-1"
   407  	region = "us-central1"
   408  
   409  	disk {
   410  		source_image = "debian-8-jessie-v20160803"
   411  		auto_delete = true
   412  		disk_size_gb = 10
   413  		boot = true
   414  	}
   415  
   416  	network_interface {
   417  		subnetwork = "${google_compute_subnetwork.subnetwork.name}"
   418  	}
   419  
   420  	metadata {
   421  		foo = "bar"
   422  	}
   423  }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))