providers/google: can assign IP to instance

This commit is contained in:
Mitchell Hashimoto 2014-08-25 15:47:21 -07:00
parent 7f3bb1a45a
commit f5d4b605c2
2 changed files with 68 additions and 2 deletions

View File

@ -65,6 +65,11 @@ func resourceComputeInstance() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, 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) networksCount := d.Get("network.#").(int)
networks := make([]*compute.NetworkInterface, 0, networksCount) networks := make([]*compute.NetworkInterface, 0, networksCount)
for i := 0; i < networksCount; i++ { for i := 0; i < networksCount; i++ {
prefix := fmt.Sprintf("network.%d", i)
// Load up the name of this network // 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( network, err := config.clientCompute.Networks.Get(
config.Project, networkName).Do() config.Project, networkName).Do()
if err != nil { if err != nil {
@ -156,6 +162,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
iface.AccessConfigs = []*compute.AccessConfig{ iface.AccessConfigs = []*compute.AccessConfig{
&compute.AccessConfig{ &compute.AccessConfig{
Type: "ONE_TO_ONE_NAT", Type: "ONE_TO_ONE_NAT",
NatIP: d.Get(prefix + ".address").(string),
}, },
} }
iface.Network = network.SelfLink iface.Network = network.SelfLink

View File

@ -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 { func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config) 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 { func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if instance.Tags == nil { if instance.Tags == nil {
@ -136,3 +170,28 @@ resource "google_compute_instance" "foobar" {
foo = "bar" 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"
}
}`