Skip to content

Commit 77037be

Browse files
zbikmarcstack72
authored andcommitted
providers/google: Add subnetwork_project field to enable cross-project networking in instance templates (hashicorp#11110)
* Add subnetwork_project field to allow for XPN in GCE instance templates * Missing os import * Removing unneeded check * fix formatting * Add subnetwork_project to read
1 parent d796eb5 commit 77037be

3 files changed

Lines changed: 81 additions & 5 deletions

File tree

builtin/providers/google/resource_compute_instance_template.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ func resourceComputeInstanceTemplate() *schema.Resource {
203203
ForceNew: true,
204204
},
205205

206+
"subnetwork_project": &schema.Schema{
207+
Type: schema.TypeString,
208+
Optional: true,
209+
ForceNew: true,
210+
},
211+
206212
"access_config": &schema.Schema{
207213
Type: schema.TypeList,
208214
Optional: true,
@@ -406,14 +412,16 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network
406412
for i := 0; i < networksCount; i++ {
407413
prefix := fmt.Sprintf("network_interface.%d", i)
408414

409-
var networkName, subnetworkName string
415+
var networkName, subnetworkName, subnetworkProject string
410416
if v, ok := d.GetOk(prefix + ".network"); ok {
411417
networkName = v.(string)
412418
}
413419
if v, ok := d.GetOk(prefix + ".subnetwork"); ok {
414420
subnetworkName = v.(string)
415421
}
416-
422+
if v, ok := d.GetOk(prefix + ".subnetwork_project"); ok {
423+
subnetworkProject = v.(string)
424+
}
417425
if networkName == "" && subnetworkName == "" {
418426
return nil, fmt.Errorf("network or subnetwork must be provided")
419427
}
@@ -435,8 +443,11 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network
435443
if err != nil {
436444
return nil, err
437445
}
446+
if subnetworkProject == "" {
447+
subnetworkProject = project
448+
}
438449
subnetwork, err := config.clientCompute.Subnetworks.Get(
439-
project, region, subnetworkName).Do()
450+
subnetworkProject, region, subnetworkName).Do()
440451
if err != nil {
441452
return nil, fmt.Errorf(
442453
"Error referencing subnetwork '%s' in region '%s': %s",
@@ -639,6 +650,7 @@ func flattenNetworkInterfaces(networkInterfaces []*compute.NetworkInterface) ([]
639650
subnetworkUrl := strings.Split(networkInterface.Subnetwork, "/")
640651
networkInterfaceMap["subnetwork"] = subnetworkUrl[len(subnetworkUrl)-1]
641652
region = subnetworkUrl[len(subnetworkUrl)-3]
653+
networkInterfaceMap["subnetwork_project"] = subnetworkUrl[len(subnetworkUrl)-5]
642654
}
643655

644656
if networkInterface.AccessConfigs != nil {

builtin/providers/google/resource_compute_instance_template_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67
"testing"
78

@@ -115,6 +116,27 @@ func TestAccComputeInstanceTemplate_subnet_custom(t *testing.T) {
115116
})
116117
}
117118

119+
func TestAccComputeInstanceTemplate_subnet_xpn(t *testing.T) {
120+
var instanceTemplate compute.InstanceTemplate
121+
var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
122+
123+
resource.Test(t, resource.TestCase{
124+
PreCheck: func() { testAccPreCheck(t) },
125+
Providers: testAccProviders,
126+
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
127+
Steps: []resource.TestStep{
128+
resource.TestStep{
129+
Config: testAccComputeInstanceTemplate_subnet_xpn(xpn_host),
130+
Check: resource.ComposeTestCheckFunc(
131+
testAccCheckComputeInstanceTemplateExists(
132+
"google_compute_instance_template.foobar", &instanceTemplate),
133+
testAccCheckComputeInstanceTemplateSubnetwork(&instanceTemplate),
134+
),
135+
},
136+
},
137+
})
138+
}
139+
118140
func TestAccComputeInstanceTemplate_metadata_startup_script(t *testing.T) {
119141
var instanceTemplate compute.InstanceTemplate
120142

@@ -467,6 +489,45 @@ resource "google_compute_instance_template" "foobar" {
467489
}
468490
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
469491

492+
func testAccComputeInstanceTemplate_subnet_xpn(xpn_host string) string {
493+
return fmt.Sprintf(`
494+
resource "google_compute_network" "network" {
495+
name = "network-%s"
496+
auto_create_subnetworks = false
497+
project = "%s"
498+
}
499+
500+
resource "google_compute_subnetwork" "subnetwork" {
501+
name = "subnetwork-%s"
502+
ip_cidr_range = "10.0.0.0/24"
503+
region = "us-central1"
504+
network = "${google_compute_network.network.self_link}"
505+
project = "%s"
506+
}
507+
508+
resource "google_compute_instance_template" "foobar" {
509+
name = "instance-test-%s"
510+
machine_type = "n1-standard-1"
511+
region = "us-central1"
512+
513+
disk {
514+
source_image = "debian-8-jessie-v20160803"
515+
auto_delete = true
516+
disk_size_gb = 10
517+
boot = true
518+
}
519+
520+
network_interface {
521+
subnetwork = "${google_compute_subnetwork.subnetwork.name}"
522+
subnetwork_project = "${google_compute_subnetwork.subnetwork.project}"
523+
}
524+
525+
metadata {
526+
foo = "bar"
527+
}
528+
}`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, acctest.RandString(10))
529+
}
530+
470531
var testAccComputeInstanceTemplate_startup_script = fmt.Sprintf(`
471532
resource "google_compute_instance_template" "foobar" {
472533
name = "instance-test-%s"
@@ -486,6 +547,6 @@ resource "google_compute_instance_template" "foobar" {
486547
network_interface{
487548
network = "default"
488549
}
489-
550+
490551
metadata_startup_script = "echo 'Hello'"
491552
}`, acctest.RandString(10))

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ The following arguments are supported:
138138

139139
* `metadata_startup_script` - (Optional) An alternative to using the
140140
startup-script metadata key, mostly to match the compute_instance resource.
141-
This replaces the startup-script metadata key on the created instance and
141+
This replaces the startup-script metadata key on the created instance and
142142
thus the two mechanisms are not allowed to be used simultaneously.
143143

144144
* `network_interface` - (Required) Networks to attach to instances created from
@@ -208,6 +208,9 @@ The `network_interface` block supports:
208208
to. The subnetwork must exist in the same `region` this instance will be
209209
created in. Either `network` or `subnetwork` must be provided.
210210

211+
* `subnetwork_project` - (Optional) The project in which the subnetwork belongs.
212+
If it is not provided, the provider project is used.
213+
211214
* `access_config` - (Optional) Access configurations, i.e. IPs via which this
212215
instance can be accessed via the Internet. Omit to ensure that the instance
213216
is not accessible from the Internet (this means that ssh provisioners will

0 commit comments

Comments
 (0)