Skip to content

Commit

Permalink
Copy network_interface attributes to Terraform state. (hashicorp#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMilley authored and rosbo committed Oct 11, 2017
1 parent 97f1b69 commit 1cf09ff
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
"alias_ip_range": flattenAliasIpRange(iface.AliasIpRanges),
})
}
d.Set("network_interface", networkInterfaces)

// Fall back on internal ip if there is no external ip. This makes sense in the situation where
// terraform is being used on a cloud instance and can therefore access the instances it creates
Expand Down
63 changes: 63 additions & 0 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ func TestAccComputeInstance_IP(t *testing.T) {
})
}

func TestAccComputeInstance_GenerateIP(t *testing.T) {
var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_generateIp(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceAccessConfigHasIP(&instance),
testAccCheckComputeInstanceHasAssignedIP,
),
},
},
})
}

func TestAccComputeInstance_diskEncryption(t *testing.T) {
var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
Expand Down Expand Up @@ -1109,6 +1131,19 @@ func testAccCheckComputeInstanceHasAliasIpRange(instance *compute.Instance, subn
}
}

func testAccCheckComputeInstanceHasAssignedIP(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_instance" {
continue
}
ip := rs.Primary.Attributes["network_interface.0.access_config.0.assigned_nat_ip"]
if ip == "" {
return fmt.Errorf("No assigned NatIP for instance %s", rs.Primary.Attributes["name"])
}
}
return nil
}

func testAccComputeInstance_basic(instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {
Expand Down Expand Up @@ -1346,6 +1381,34 @@ resource "google_compute_instance" "foobar" {
`, ip, instance)
}

func testAccComputeInstance_generateIp(instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params{
image = "debian-8-jessie-v20160803"
}
}
network_interface {
network = "default"
access_config {
// generate ephemeral IP
}
}
metadata {
foo = "bar"
}
}
`, instance)
}

func testAccComputeInstance_disks_encryption(bootEncryptionKey string, diskNameToEncryptionKey map[string]*compute.CustomerEncryptionKey, instance string) string {
diskNames := []string{}
for k, _ := range diskNameToEncryptionKey {
Expand Down

0 comments on commit 1cf09ff

Please sign in to comment.