providers/google: can assign IP to instance
This commit is contained in:
parent
7f3bb1a45a
commit
f5d4b605c2
|
@ -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 {
|
||||
|
@ -156,6 +162,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
|||
iface.AccessConfigs = []*compute.AccessConfig{
|
||||
&compute.AccessConfig{
|
||||
Type: "ONE_TO_ONE_NAT",
|
||||
NatIP: d.Get(prefix + ".address").(string),
|
||||
},
|
||||
}
|
||||
iface.Network = network.SelfLink
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}`
|
||||
|
|
Loading…
Reference in New Issue