Add subnetwork_project field to allow for XPN in GCE instances

This commit is contained in:
Dana Hoffman 2016-10-27 08:25:58 -07:00
parent 8b09830895
commit 8ae5f3a38c
2 changed files with 77 additions and 6 deletions

View File

@ -146,6 +146,12 @@ func resourceComputeInstance() *schema.Resource {
ForceNew: true,
},
"subnetwork_project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
@ -472,6 +478,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
// Load up the name of this network_interface
networkName := d.Get(prefix + ".network").(string)
subnetworkName := d.Get(prefix + ".subnetwork").(string)
subnetworkProject := d.Get(prefix + ".subnetwork_project").(string)
address := d.Get(prefix + ".address").(string)
var networkLink, subnetworkLink string
@ -487,8 +494,11 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
} else {
region := getRegionFromZone(d.Get("zone").(string))
if subnetworkProject == "" {
subnetworkProject = project
}
subnetwork, err := config.clientCompute.Subnetworks.Get(
project, region, subnetworkName).Do()
subnetworkProject, region, subnetworkName).Do()
if err != nil {
return fmt.Errorf(
"Error referencing subnetwork '%s' in region '%s': %s",
@ -707,11 +717,12 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
}
networkInterfaces = append(networkInterfaces, map[string]interface{}{
"name": iface.Name,
"address": iface.NetworkIP,
"network": d.Get(fmt.Sprintf("network_interface.%d.network", i)),
"subnetwork": d.Get(fmt.Sprintf("network_interface.%d.subnetwork", i)),
"access_config": accessConfigs,
"name": iface.Name,
"address": iface.NetworkIP,
"network": d.Get(fmt.Sprintf("network_interface.%d.network", i)),
"subnetwork": d.Get(fmt.Sprintf("network_interface.%d.subnetwork", i)),
"subnetwork_project": d.Get(fmt.Sprintf("network_interface.%d.subnetwork_project", i)),
"access_config": accessConfigs,
})
}
}

View File

@ -2,6 +2,7 @@ package google
import (
"fmt"
"os"
"strings"
"testing"
@ -417,6 +418,31 @@ func TestAccComputeInstance_subnet_custom(t *testing.T) {
})
}
func TestAccComputeInstance_subnet_xpn(t *testing.T) {
var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
if xpn_host == "" {
t.Fatal("GOOGLE_XPN_HOST_PROJECT must be set for TestAccComputeInstance_subnet_xpn test")
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_subnet_xpn(instanceName, xpn_host),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasSubnet(&instance),
),
},
},
})
}
func TestAccComputeInstance_address_auto(t *testing.T) {
var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
@ -1039,6 +1065,40 @@ func testAccComputeInstance_subnet_custom(instance string) string {
}`, acctest.RandString(10), acctest.RandString(10), instance)
}
func testAccComputeInstance_subnet_xpn(instance string, xpn_host string) string {
return fmt.Sprintf(`
resource "google_compute_network" "inst-test-network" {
name = "inst-test-network-%s"
auto_create_subnetworks = false
project = "%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}"
project = "%s"
}
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
disk {
image = "debian-8-jessie-v20160803"
}
network_interface {
subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
subnetwork_project = "${google_compute_subnetwork.inst-test-subnetwork.project}"
access_config { }
}
}`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, instance)
}
func testAccComputeInstance_address_auto(instance string) string {
return fmt.Sprintf(`
resource "google_compute_network" "inst-test-network" {