diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index d7a79d636..2f478f4be 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -65,6 +65,11 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Required: true, }, + + "address": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, }, }, }, @@ -141,8 +146,9 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err networksCount := d.Get("network.#").(int) networks := make([]*compute.NetworkInterface, 0, networksCount) for i := 0; i < networksCount; i++ { + prefix := fmt.Sprintf("network.%d", i) // Load up the name of this network - networkName := d.Get(fmt.Sprintf("network.%d.source", i)).(string) + networkName := d.Get(prefix + ".source").(string) network, err := config.clientCompute.Networks.Get( config.Project, networkName).Do() if err != nil { @@ -155,7 +161,8 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err var iface compute.NetworkInterface iface.AccessConfigs = []*compute.AccessConfig{ &compute.AccessConfig{ - Type: "ONE_TO_ONE_NAT", + Type: "ONE_TO_ONE_NAT", + NatIP: d.Get(prefix + ".address").(string), }, } iface.Network = network.SelfLink diff --git a/builtin/providers/google/resource_compute_instance_test.go b/builtin/providers/google/resource_compute_instance_test.go index 2f4c0afc0..6b5a7ff52 100644 --- a/builtin/providers/google/resource_compute_instance_test.go +++ b/builtin/providers/google/resource_compute_instance_test.go @@ -30,6 +30,26 @@ func TestAccComputeInstance_basic(t *testing.T) { }) } +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), + ), + }, + }, + }) +} + func testAccCheckComputeInstanceDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -101,6 +121,20 @@ func testAccCheckComputeInstanceMetadata( } } +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 + } +} + func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc { return func(s *terraform.State) error { if instance.Tags == nil { @@ -136,3 +170,28 @@ resource "google_compute_instance" "foobar" { foo = "bar" } }` + +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 { + source = "debian-7-wheezy-v20140814" + } + + network { + source = "default" + address = "${google_compute_address.foo.address}" + } + + metadata { + foo = "bar" + } +}`