From f1405c0b7937d5849dfbd0bf334d36ec1fc6c3ee Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Tue, 26 Nov 2019 22:20:56 +0000 Subject: [PATCH] Network peering import Signed-off-by: Modular Magician --- google/resource_compute_network_peering.go | 26 ++++++++++++++++ .../resource_compute_network_peering_test.go | 30 ++++++++++++------- google/resource_sql_database_instance_test.go | 1 - .../r/compute_network_peering.html.markdown | 8 +++++ 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/google/resource_compute_network_peering.go b/google/resource_compute_network_peering.go index 0dd4e0a6f3e..a4e988ddfcf 100644 --- a/google/resource_compute_network_peering.go +++ b/google/resource_compute_network_peering.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "sort" + "strings" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" computeBeta "google.golang.org/api/compute/v0.beta" @@ -18,6 +19,9 @@ func resourceComputeNetworkPeering() *schema.Resource { Create: resourceComputeNetworkPeeringCreate, Read: resourceComputeNetworkPeeringRead, Delete: resourceComputeNetworkPeeringDelete, + Importer: &schema.ResourceImporter{ + State: resourceComputeNetworkPeeringImport, + }, Schema: map[string]*schema.Schema{ "name": { @@ -181,3 +185,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/google/resource_compute_network_peering_test.go b/google/resource_compute_network_peering_test.go index c03d62da09e..3f645b66681 100644 --- a/google/resource_compute_network_peering_test.go +++ b/google/resource_compute_network_peering_test.go @@ -15,13 +15,17 @@ 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) }, Providers: testAccProviders, 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), @@ -29,6 +33,12 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) { testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta), ), }, + { + ResourceName: "google_compute_network_peering.foo", + ImportState: true, + ImportStateVerify: true, + ImportStateId: importId, + }, }, }) @@ -97,24 +107,24 @@ func testAccCheckComputeNetworkPeeringAutoCreateRoutes(v bool, peering *computeB } } -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 @@ -122,5 +132,5 @@ resource "google_compute_network_peering" "bar" { ` 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/google/resource_sql_database_instance_test.go b/google/resource_sql_database_instance_test.go index fe27c5476cb..435660d4986 100644 --- a/google/resource_sql_database_instance_test.go +++ b/google/resource_sql_database_instance_test.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" - sqladmin "google.golang.org/api/sqladmin/v1beta4" ) diff --git a/website/docs/r/compute_network_peering.html.markdown b/website/docs/r/compute_network_peering.html.markdown index 21d2317629f..15f09ccf0e6 100644 --- a/website/docs/r/compute_network_peering.html.markdown +++ b/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 +```