mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-03 01:01:06 +00:00
providers/google: Add subnetwork_project field to enable cross-project networking in instance templates (#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
This commit is contained in:
parent
44bf0bec96
commit
17af2f69af
@ -203,6 +203,12 @@ func resourceComputeInstanceTemplate() *schema.Resource {
|
|||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"subnetwork_project": &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
|
||||||
"access_config": &schema.Schema{
|
"access_config": &schema.Schema{
|
||||||
Type: schema.TypeList,
|
Type: schema.TypeList,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
@ -406,14 +412,16 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network
|
|||||||
for i := 0; i < networksCount; i++ {
|
for i := 0; i < networksCount; i++ {
|
||||||
prefix := fmt.Sprintf("network_interface.%d", i)
|
prefix := fmt.Sprintf("network_interface.%d", i)
|
||||||
|
|
||||||
var networkName, subnetworkName string
|
var networkName, subnetworkName, subnetworkProject string
|
||||||
if v, ok := d.GetOk(prefix + ".network"); ok {
|
if v, ok := d.GetOk(prefix + ".network"); ok {
|
||||||
networkName = v.(string)
|
networkName = v.(string)
|
||||||
}
|
}
|
||||||
if v, ok := d.GetOk(prefix + ".subnetwork"); ok {
|
if v, ok := d.GetOk(prefix + ".subnetwork"); ok {
|
||||||
subnetworkName = v.(string)
|
subnetworkName = v.(string)
|
||||||
}
|
}
|
||||||
|
if v, ok := d.GetOk(prefix + ".subnetwork_project"); ok {
|
||||||
|
subnetworkProject = v.(string)
|
||||||
|
}
|
||||||
if networkName == "" && subnetworkName == "" {
|
if networkName == "" && subnetworkName == "" {
|
||||||
return nil, fmt.Errorf("network or subnetwork must be provided")
|
return nil, fmt.Errorf("network or subnetwork must be provided")
|
||||||
}
|
}
|
||||||
@ -435,8 +443,11 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if subnetworkProject == "" {
|
||||||
|
subnetworkProject = project
|
||||||
|
}
|
||||||
subnetwork, err := config.clientCompute.Subnetworks.Get(
|
subnetwork, err := config.clientCompute.Subnetworks.Get(
|
||||||
project, region, subnetworkName).Do()
|
subnetworkProject, region, subnetworkName).Do()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"Error referencing subnetwork '%s' in region '%s': %s",
|
"Error referencing subnetwork '%s' in region '%s': %s",
|
||||||
@ -639,6 +650,7 @@ func flattenNetworkInterfaces(networkInterfaces []*compute.NetworkInterface) ([]
|
|||||||
subnetworkUrl := strings.Split(networkInterface.Subnetwork, "/")
|
subnetworkUrl := strings.Split(networkInterface.Subnetwork, "/")
|
||||||
networkInterfaceMap["subnetwork"] = subnetworkUrl[len(subnetworkUrl)-1]
|
networkInterfaceMap["subnetwork"] = subnetworkUrl[len(subnetworkUrl)-1]
|
||||||
region = subnetworkUrl[len(subnetworkUrl)-3]
|
region = subnetworkUrl[len(subnetworkUrl)-3]
|
||||||
|
networkInterfaceMap["subnetwork_project"] = subnetworkUrl[len(subnetworkUrl)-5]
|
||||||
}
|
}
|
||||||
|
|
||||||
if networkInterface.AccessConfigs != nil {
|
if networkInterface.AccessConfigs != nil {
|
||||||
|
@ -2,6 +2,7 @@ package google
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -115,6 +116,27 @@ func TestAccComputeInstanceTemplate_subnet_custom(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAccComputeInstanceTemplate_subnet_xpn(t *testing.T) {
|
||||||
|
var instanceTemplate compute.InstanceTemplate
|
||||||
|
var xpn_host = os.Getenv("GOOGLE_XPN_HOST_PROJECT")
|
||||||
|
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
Providers: testAccProviders,
|
||||||
|
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
resource.TestStep{
|
||||||
|
Config: testAccComputeInstanceTemplate_subnet_xpn(xpn_host),
|
||||||
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
testAccCheckComputeInstanceTemplateExists(
|
||||||
|
"google_compute_instance_template.foobar", &instanceTemplate),
|
||||||
|
testAccCheckComputeInstanceTemplateSubnetwork(&instanceTemplate),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestAccComputeInstanceTemplate_metadata_startup_script(t *testing.T) {
|
func TestAccComputeInstanceTemplate_metadata_startup_script(t *testing.T) {
|
||||||
var instanceTemplate compute.InstanceTemplate
|
var instanceTemplate compute.InstanceTemplate
|
||||||
|
|
||||||
@ -467,6 +489,45 @@ resource "google_compute_instance_template" "foobar" {
|
|||||||
}
|
}
|
||||||
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
|
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
|
||||||
|
|
||||||
|
func testAccComputeInstanceTemplate_subnet_xpn(xpn_host string) string {
|
||||||
|
return fmt.Sprintf(`
|
||||||
|
resource "google_compute_network" "network" {
|
||||||
|
name = "network-%s"
|
||||||
|
auto_create_subnetworks = false
|
||||||
|
project = "%s"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_compute_subnetwork" "subnetwork" {
|
||||||
|
name = "subnetwork-%s"
|
||||||
|
ip_cidr_range = "10.0.0.0/24"
|
||||||
|
region = "us-central1"
|
||||||
|
network = "${google_compute_network.network.self_link}"
|
||||||
|
project = "%s"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_compute_instance_template" "foobar" {
|
||||||
|
name = "instance-test-%s"
|
||||||
|
machine_type = "n1-standard-1"
|
||||||
|
region = "us-central1"
|
||||||
|
|
||||||
|
disk {
|
||||||
|
source_image = "debian-8-jessie-v20160803"
|
||||||
|
auto_delete = true
|
||||||
|
disk_size_gb = 10
|
||||||
|
boot = true
|
||||||
|
}
|
||||||
|
|
||||||
|
network_interface {
|
||||||
|
subnetwork = "${google_compute_subnetwork.subnetwork.name}"
|
||||||
|
subnetwork_project = "${google_compute_subnetwork.subnetwork.project}"
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata {
|
||||||
|
foo = "bar"
|
||||||
|
}
|
||||||
|
}`, acctest.RandString(10), xpn_host, acctest.RandString(10), xpn_host, acctest.RandString(10))
|
||||||
|
}
|
||||||
|
|
||||||
var testAccComputeInstanceTemplate_startup_script = fmt.Sprintf(`
|
var testAccComputeInstanceTemplate_startup_script = fmt.Sprintf(`
|
||||||
resource "google_compute_instance_template" "foobar" {
|
resource "google_compute_instance_template" "foobar" {
|
||||||
name = "instance-test-%s"
|
name = "instance-test-%s"
|
||||||
@ -486,6 +547,6 @@ resource "google_compute_instance_template" "foobar" {
|
|||||||
network_interface{
|
network_interface{
|
||||||
network = "default"
|
network = "default"
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata_startup_script = "echo 'Hello'"
|
metadata_startup_script = "echo 'Hello'"
|
||||||
}`, acctest.RandString(10))
|
}`, acctest.RandString(10))
|
||||||
|
Loading…
Reference in New Issue
Block a user