From cfbe11850d2978e555215798f20638e083e7645c Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Mon, 1 May 2017 17:16:05 -0700 Subject: [PATCH] provider/google: add support for networkIP in compute instance templates (#13515) --- .../resource_compute_instance_template.go | 13 ++++- ...resource_compute_instance_template_test.go | 56 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/builtin/providers/google/resource_compute_instance_template.go b/builtin/providers/google/resource_compute_instance_template.go index e34b2c2c0..f4c2dd327 100644 --- a/builtin/providers/google/resource_compute_instance_template.go +++ b/builtin/providers/google/resource_compute_instance_template.go @@ -197,6 +197,12 @@ func resourceComputeInstanceTemplate() *schema.Resource { Computed: true, }, + "network_ip": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "subnetwork": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -462,7 +468,9 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network var iface compute.NetworkInterface iface.Network = networkLink iface.Subnetwork = subnetworkLink - + if v, ok := d.GetOk(prefix + ".network_ip"); ok { + iface.NetworkIP = v.(string) + } accessConfigsCount := d.Get(prefix + ".access_config.#").(int) iface.AccessConfigs = make([]*compute.AccessConfig, accessConfigsCount) for j := 0; j < accessConfigsCount; j++ { @@ -648,6 +656,9 @@ func flattenNetworkInterfaces(networkInterfaces []*compute.NetworkInterface) ([] networkUrl := strings.Split(networkInterface.Network, "/") networkInterfaceMap["network"] = networkUrl[len(networkUrl)-1] } + if networkInterface.NetworkIP != "" { + networkInterfaceMap["network_ip"] = networkInterface.NetworkIP + } if networkInterface.Subnetwork != "" { subnetworkUrl := strings.Split(networkInterface.Subnetwork, "/") networkInterfaceMap["subnetwork"] = subnetworkUrl[len(subnetworkUrl)-1] diff --git a/builtin/providers/google/resource_compute_instance_template_test.go b/builtin/providers/google/resource_compute_instance_template_test.go index 6388a1dfe..62a8beef5 100644 --- a/builtin/providers/google/resource_compute_instance_template_test.go +++ b/builtin/providers/google/resource_compute_instance_template_test.go @@ -54,6 +54,29 @@ func TestAccComputeInstanceTemplate_IP(t *testing.T) { }) } +func TestAccComputeInstanceTemplate_networkIP(t *testing.T) { + var instanceTemplate compute.InstanceTemplate + networkIP := "10.128.0.2" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeInstanceTemplate_networkIP(networkIP), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceTemplateExists( + "google_compute_instance_template.foobar", &instanceTemplate), + testAccCheckComputeInstanceTemplateNetwork(&instanceTemplate), + testAccCheckComputeInstanceTemplateNetworkIP( + "google_compute_instance_template.foobar", networkIP, &instanceTemplate), + ), + }, + }, + }) +} + func TestAccComputeInstanceTemplate_disks(t *testing.T) { var instanceTemplate compute.InstanceTemplate @@ -335,6 +358,17 @@ func testAccCheckComputeInstanceTemplateStartupScript(instanceTemplate *compute. } } +func testAccCheckComputeInstanceTemplateNetworkIP(n, networkIP string, instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc { + return func(s *terraform.State) error { + ip := instanceTemplate.Properties.NetworkInterfaces[0].NetworkIP + err := resource.TestCheckResourceAttr(n, "network_interface.0.network_ip", ip)(s) + if err != nil { + return err + } + return resource.TestCheckResourceAttr(n, "network_interface.0.network_ip", networkIP)(s) + } +} + var testAccComputeInstanceTemplate_basic = fmt.Sprintf(` resource "google_compute_instance_template" "foobar" { name = "instancet-test-%s" @@ -392,6 +426,28 @@ resource "google_compute_instance_template" "foobar" { } }`, acctest.RandString(10), acctest.RandString(10)) +func testAccComputeInstanceTemplate_networkIP(networkIP string) string { + return fmt.Sprintf(` +resource "google_compute_instance_template" "foobar" { + name = "instancet-test-%s" + machine_type = "n1-standard-1" + tags = ["foo", "bar"] + + disk { + source_image = "debian-8-jessie-v20160803" + } + + network_interface { + network = "default" + network_ip = "%s" + } + + metadata { + foo = "bar" + } +}`, acctest.RandString(10), networkIP) +} + var testAccComputeInstanceTemplate_disks = fmt.Sprintf(` resource "google_compute_disk" "foobar" { name = "instancet-test-%s"