diff --git a/build/terraform b/build/terraform index 5e693fabb30c..645b298d3e49 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 5e693fabb30cc197f4d12b962a9ba49411f2a51a +Subproject commit 645b298d3e49e1da8c4a128884ca72456e9c4550 diff --git a/build/terraform-beta b/build/terraform-beta index 0df09eeed6bc..d365481e9607 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 0df09eeed6bcf5cccba7e1a0074e7883eb0035d7 +Subproject commit d365481e9607af631fa6b7fc89e3f2127d6b472d diff --git a/third_party/terraform/resources/resource_compute_network_peering.go.erb b/third_party/terraform/resources/resource_compute_network_peering.go.erb index 6785c064f095..28cf5f143b1d 100644 --- a/third_party/terraform/resources/resource_compute_network_peering.go.erb +++ b/third_party/terraform/resources/resource_compute_network_peering.go.erb @@ -19,6 +19,9 @@ func resourceComputeNetworkPeering() *schema.Resource { Create: resourceComputeNetworkPeeringCreate, Read: resourceComputeNetworkPeeringRead, Delete: resourceComputeNetworkPeeringDelete, + Importer: &schema.ResourceImporter{ + State: resourceComputeNetworkPeeringImport, + }, Schema: map[string]*schema.Schema{ "name": { @@ -206,3 +209,25 @@ func getNetworkPeeringLockName(networkName, peerNetworkName string) string { return fmt.Sprintf("network_peering/%s/%s", networks[0], networks[1]) } + +func resourceComputeNetworkPeeringImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + splits := strings.Split(d.Id(), "/") + if len(splits) != 3 { + return nil, fmt.Errorf("Error parsing network peering import format, expected: {project}/{network}/{name}") + } + + // Build the template for the network self_link + urlTemplate, err := replaceVars(d, config, "{{ComputeBasePath}}projects/%s/global/networks/%s") + if err != nil { + return nil, err + } + d.Set("network", ConvertSelfLinkToV1(fmt.Sprintf(urlTemplate, splits[0], splits[1]))) + d.Set("name", splits[2]) + + // Replace import id for the resource id + id := fmt.Sprintf("%s/%s", splits[1], splits[2]) + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} diff --git a/third_party/terraform/tests/resource_compute_network_peering_test.go.erb b/third_party/terraform/tests/resource_compute_network_peering_test.go.erb index 65043bbd59cd..63e8c76b8cb9 100644 --- a/third_party/terraform/tests/resource_compute_network_peering_test.go.erb +++ b/third_party/terraform/tests/resource_compute_network_peering_test.go.erb @@ -16,6 +16,9 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) { t.Parallel() var peering_beta computeBeta.NetworkPeering + primaryNetworkName := acctest.RandomWithPrefix("network-test-1") + peeringName := acctest.RandomWithPrefix("peering-test-1") + importId := fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), primaryNetworkName, peeringName) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -23,7 +26,7 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) { CheckDestroy: testAccComputeNetworkPeeringDestroy, Steps: []resource.TestStep{ { - Config: testAccComputeNetworkPeering_basic(), + Config: testAccComputeNetworkPeering_basic(primaryNetworkName, peeringName), Check: resource.ComposeTestCheckFunc( testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.foo", &peering_beta), testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta), @@ -39,6 +42,12 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) { <% end -%> ), }, + { + ResourceName: "google_compute_network_peering.foo", + ImportState: true, + ImportStateVerify: true, + ImportStateId: importId, + }, }, }) @@ -129,24 +138,24 @@ func testAccCheckComputeNetworkPeeringExportCustomRoutes(v bool, peering *comput } <% end -%> -func testAccComputeNetworkPeering_basic() string { +func testAccComputeNetworkPeering_basic(primaryNetworkName, peeringName string) string { s := ` resource "google_compute_network" "network1" { - name = "network-test-1-%s" - auto_create_subnetworks = false -} - -resource "google_compute_network" "network2" { - name = "network-test-2-%s" + name = "%s" auto_create_subnetworks = false } resource "google_compute_network_peering" "foo" { - name = "peering-test-1-%s" + name = "%s" network = google_compute_network.network1.self_link peer_network = google_compute_network.network2.self_link } +resource "google_compute_network" "network2" { + name = "network-test-2-%s" + auto_create_subnetworks = false +} + resource "google_compute_network_peering" "bar" { network = google_compute_network.network2.self_link peer_network = google_compute_network.network1.self_link @@ -160,5 +169,5 @@ resource "google_compute_network_peering" "bar" { ` <% end -%> s = s + `}` - return fmt.Sprintf(s, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10)) + return fmt.Sprintf(s, primaryNetworkName, peeringName, acctest.RandString(10), acctest.RandString(10)) } diff --git a/third_party/terraform/website/docs/r/compute_network_peering.html.markdown b/third_party/terraform/website/docs/r/compute_network_peering.html.markdown index 21d2317629fa..15f09ccf0e69 100644 --- a/third_party/terraform/website/docs/r/compute_network_peering.html.markdown +++ b/third_party/terraform/website/docs/r/compute_network_peering.html.markdown @@ -71,3 +71,11 @@ exported: `ACTIVE` when there's a matching configuration in the peer network. * `state_details` - Details about the current state of the peering. + +## Import + +VPC network peerings can be imported using the name and project of the primary network the peering exists in and the name of the network peering + +``` +$ terraform import google_compute_network_peering.peering_network project-name/network-name/peering-name +```