Skip to content

Commit 4f256a5

Browse files
committed
Merge branch 'google-xpn' of https://github.com/danawillow/terraform
2 parents 60658fd + ab01c23 commit 4f256a5

3 files changed

Lines changed: 81 additions & 7 deletions

File tree

builtin/providers/google/resource_compute_instance.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ func resourceComputeInstance() *schema.Resource {
145145
ForceNew: true,
146146
},
147147

148+
"subnetwork_project": &schema.Schema{
149+
Type: schema.TypeString,
150+
Optional: true,
151+
ForceNew: true,
152+
},
153+
148154
"name": &schema.Schema{
149155
Type: schema.TypeString,
150156
Computed: true,
@@ -485,6 +491,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
485491
// Load up the name of this network_interface
486492
networkName := d.Get(prefix + ".network").(string)
487493
subnetworkName := d.Get(prefix + ".subnetwork").(string)
494+
subnetworkProject := d.Get(prefix + ".subnetwork_project").(string)
488495
address := d.Get(prefix + ".address").(string)
489496
var networkLink, subnetworkLink string
490497

@@ -500,8 +507,11 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
500507

501508
} else {
502509
region := getRegionFromZone(d.Get("zone").(string))
510+
if subnetworkProject == "" {
511+
subnetworkProject = project
512+
}
503513
subnetwork, err := config.clientCompute.Subnetworks.Get(
504-
project, region, subnetworkName).Do()
514+
subnetworkProject, region, subnetworkName).Do()
505515
if err != nil {
506516
return fmt.Errorf(
507517
"Error referencing subnetwork '%s' in region '%s': %s",
@@ -726,11 +736,12 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
726736
}
727737

728738
networkInterfaces = append(networkInterfaces, map[string]interface{}{
729-
"name": iface.Name,
730-
"address": iface.NetworkIP,
731-
"network": d.Get(fmt.Sprintf("network_interface.%d.network", i)),
732-
"subnetwork": d.Get(fmt.Sprintf("network_interface.%d.subnetwork", i)),
733-
"access_config": accessConfigs,
739+
"name": iface.Name,
740+
"address": iface.NetworkIP,
741+
"network": d.Get(fmt.Sprintf("network_interface.%d.network", i)),
742+
"subnetwork": d.Get(fmt.Sprintf("network_interface.%d.subnetwork", i)),
743+
"subnetwork_project": d.Get(fmt.Sprintf("network_interface.%d.subnetwork_project", i)),
744+
"access_config": accessConfigs,
734745
})
735746
}
736747
}

builtin/providers/google/resource_compute_instance_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"os"
56
"regexp"
67
"strings"
78
"testing"
@@ -418,6 +419,31 @@ func TestAccComputeInstance_subnet_custom(t *testing.T) {
418419
})
419420
}
420421

422+
func TestAccComputeInstance_subnet_xpn(t *testing.T) {
423+
var instance compute.Instance
424+
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
425+
var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
426+
if xpn_host == "" {
427+
t.Fatal("GOOGLE_XPN_HOST_PROJECT must be set for TestAccComputeInstance_subnet_xpn test")
428+
}
429+
430+
resource.Test(t, resource.TestCase{
431+
PreCheck: func() { testAccPreCheck(t) },
432+
Providers: testAccProviders,
433+
CheckDestroy: testAccCheckComputeInstanceDestroy,
434+
Steps: []resource.TestStep{
435+
resource.TestStep{
436+
Config: testAccComputeInstance_subnet_xpn(instanceName, xpn_host),
437+
Check: resource.ComposeTestCheckFunc(
438+
testAccCheckComputeInstanceExists(
439+
"google_compute_instance.foobar", &instance),
440+
testAccCheckComputeInstanceHasSubnet(&instance),
441+
),
442+
},
443+
},
444+
})
445+
}
446+
421447
func TestAccComputeInstance_address_auto(t *testing.T) {
422448
var instance compute.Instance
423449
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
@@ -1083,6 +1109,40 @@ func testAccComputeInstance_subnet_custom(instance string) string {
10831109
}`, acctest.RandString(10), acctest.RandString(10), instance)
10841110
}
10851111

1112+
func testAccComputeInstance_subnet_xpn(instance, xpn_host string) string {
1113+
return fmt.Sprintf(`
1114+
resource "google_compute_network" "inst-test-network" {
1115+
name = "inst-test-network-%s"
1116+
auto_create_subnetworks = false
1117+
project = "%s"
1118+
}
1119+
1120+
resource "google_compute_subnetwork" "inst-test-subnetwork" {
1121+
name = "inst-test-subnetwork-%s"
1122+
ip_cidr_range = "10.0.0.0/16"
1123+
region = "us-central1"
1124+
network = "${google_compute_network.inst-test-network.self_link}"
1125+
project = "%s"
1126+
}
1127+
1128+
resource "google_compute_instance" "foobar" {
1129+
name = "%s"
1130+
machine_type = "n1-standard-1"
1131+
zone = "us-central1-a"
1132+
1133+
disk {
1134+
image = "debian-8-jessie-v20160803"
1135+
}
1136+
1137+
network_interface {
1138+
subnetwork = "${google_compute_subnetwork.inst-test-subnetwork.name}"
1139+
subnetwork_project = "${google_compute_subnetwork.inst-test-subnetwork.project}"
1140+
access_config { }
1141+
}
1142+
1143+
}`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, instance)
1144+
}
1145+
10861146
func testAccComputeInstance_address_auto(instance string) string {
10871147
return fmt.Sprintf(`
10881148
resource "google_compute_network" "inst-test-network" {

website/source/docs/providers/google/r/compute_instance.html.markdown

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,13 @@ The `network_interface` block supports:
141141
* `network` - (Optional) The name or self_link of the network to attach this interface to.
142142
Either `network` or `subnetwork` must be provided.
143143

144-
* `subnetwork` - (Optional) the name of the subnetwork to attach this interface
144+
* `subnetwork` - (Optional) The name of the subnetwork to attach this interface
145145
to. The subnetwork must exist in the same region this instance will be
146146
created in. Either `network` or `subnetwork` must be provided.
147147

148+
* `subnetwork_project` - (Optional) The project in which the subnetwork belongs.
149+
If it is not provided, the provider project is used.
150+
148151
* `address` - (Optional) The private IP address to assign to the instance. If
149152
empty, the address will be automatically assigned.
150153

0 commit comments

Comments
 (0)