Skip to content

Commit

Permalink
provider/google: Support static private IP addresses
Browse files Browse the repository at this point in the history
The private address of an instance's network interface may now be specified.
If no value is provided, an address will be chosen by Google Compute Engine
and that value will be read into Terraform state.
  • Loading branch information
evandbrown committed Apr 22, 2016
1 parent c0215dc commit 24a60be
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 4 deletions.
6 changes: 3 additions & 3 deletions builtin/providers/google/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ func testAccPreCheck(t *testing.T) {
"CLOUDSDK_CORE_PROJECT",
}
if v := multiEnvSearch(projs); v == "" {
t.Fatalf("One of %s must be set for acceptance tests", strings.Join(creds, ", "))
t.Fatalf("One of %s must be set for acceptance tests", strings.Join(projs, ", "))
}

regs := []string{
"GOOGLE_REGION",
"GCLOUD_REGION",
"CLOUDSDK_COMPUTE_REGION",
}
if v := multiEnvSearch(regs); v != "us-central-1" {
t.Fatalf("One of %s must be set to us-central-1 for acceptance tests", strings.Join(creds, ", "))
if v := multiEnvSearch(regs); v != "us-central1" {
t.Fatalf("One of %s must be set to us-central-1 for acceptance tests", strings.Join(regs, ", "))
}
}

Expand Down
6 changes: 5 additions & 1 deletion builtin/providers/google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func resourceComputeInstance() *schema.Resource {

"address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},

Expand Down Expand Up @@ -465,9 +467,10 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
networkInterfaces = make([]*compute.NetworkInterface, 0, networkInterfacesCount)
for i := 0; i < networkInterfacesCount; i++ {
prefix := fmt.Sprintf("network_interface.%d", i)
// Load up the name of this network_interfac
// Load up the name of this network_interface
networkName := d.Get(prefix + ".network").(string)
subnetworkName := d.Get(prefix + ".subnetwork").(string)
address := d.Get(prefix + ".address").(string)
var networkLink, subnetworkLink string

if networkName != "" && subnetworkName != "" {
Expand Down Expand Up @@ -497,6 +500,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
var iface compute.NetworkInterface
iface.Network = networkLink
iface.Subnetwork = subnetworkLink
iface.NetworkIP = address

// Handle access_config structs
accessConfigsCount := d.Get(prefix + ".access_config.#").(int)
Expand Down
122 changes: 122 additions & 0 deletions builtin/providers/google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,47 @@ func TestAccComputeInstance_subnet_custom(t *testing.T) {
})
}

func TestAccComputeInstance_address_auto(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_address_auto(instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasAnyAddress(&instance),
),
},
},
})
}

func TestAccComputeInstance_address_custom(t *testing.T) {
var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
var address = "10.0.200.200"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_address_custom(instanceName, address),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasAddress(&instance, address),
),
},
},
})
}
func testAccCheckComputeInstanceDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -505,6 +546,30 @@ func testAccCheckComputeInstanceHasSubnet(instance *compute.Instance) resource.T
}
}

func testAccCheckComputeInstanceHasAnyAddress(instance *compute.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, i := range instance.NetworkInterfaces {
if i.NetworkIP == "" {
return fmt.Errorf("no address")
}
}

return nil
}
}

func testAccCheckComputeInstanceHasAddress(instance *compute.Instance, address string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, i := range instance.NetworkInterfaces {
if i.NetworkIP != address {
return fmt.Errorf("Wrong address found: expected %v, got %v", address, i.NetworkIP)
}
}

return nil
}
}

func testAccComputeInstance_basic_deprecated_network(instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {
Expand Down Expand Up @@ -857,3 +922,60 @@ func testAccComputeInstance_subnet_custom(instance string) string {
}`, acctest.RandString(10), acctest.RandString(10), instance)
}

func testAccComputeInstance_address_auto(instance string) string {
return fmt.Sprintf(`
resource "google_compute_network" "inst-test-network" {
name = "inst-test-network-%s"
}
resource "google_compute_subnetwork" "inst-test-subnetwork" {
name = "inst-test-subnetwork-%s"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
network = "${google_compute_network.inst-test-network.self_link}"
}
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
disk {
image = "debian-7-wheezy-v20160301"
}
network_interface {
subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
access_config { }
}
}`, acctest.RandString(10), acctest.RandString(10), instance)
}

func testAccComputeInstance_address_custom(instance, address string) string {
return fmt.Sprintf(`
resource "google_compute_network" "inst-test-network" {
name = "inst-test-network-%s"
}
resource "google_compute_subnetwork" "inst-test-subnetwork" {
name = "inst-test-subnetwork-%s"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
network = "${google_compute_network.inst-test-network.self_link}"
}
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
disk {
image = "debian-7-wheezy-v20160301"
}
network_interface {
subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
address = "%s"
access_config { }
}
}`, acctest.RandString(10), acctest.RandString(10), instance, address)
}

0 comments on commit 24a60be

Please sign in to comment.