diff --git a/.changelog/3630.txt b/.changelog/3630.txt new file mode 100644 index 00000000000..df5ecc676f3 --- /dev/null +++ b/.changelog/3630.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +* added `export_subnet_routes_with_public_ip` and `import_subnet_routes_with_public_ip` to `google_compute_network_peering` +``` diff --git a/google/resource_compute_network_peering.go b/google/resource_compute_network_peering.go index 072407a8756..a59a10b2afc 100644 --- a/google/resource_compute_network_peering.go +++ b/google/resource_compute_network_peering.go @@ -66,6 +66,19 @@ func resourceComputeNetworkPeering() *schema.Resource { Default: false, }, + "export_subnet_routes_with_public_ip": { + Type: schema.TypeBool, + ForceNew: true, + Optional: true, + Default: true, + }, + + "import_subnet_routes_with_public_ip": { + Type: schema.TypeBool, + ForceNew: true, + Optional: true, + }, + "state": { Type: schema.TypeString, Computed: true, @@ -148,6 +161,8 @@ func resourceComputeNetworkPeeringRead(d *schema.ResourceData, meta interface{}) d.Set("name", peering.Name) d.Set("import_custom_routes", peering.ImportCustomRoutes) d.Set("export_custom_routes", peering.ExportCustomRoutes) + d.Set("import_subnet_routes_with_public_ip", peering.ImportSubnetRoutesWithPublicIp) + d.Set("export_subnet_routes_with_public_ip", peering.ExportSubnetRoutesWithPublicIp) d.Set("state", peering.State) d.Set("state_details", peering.StateDetails) @@ -207,11 +222,14 @@ func findPeeringFromNetwork(network *compute.Network, peeringName string) *compu } func expandNetworkPeering(d *schema.ResourceData) *compute.NetworkPeering { return &compute.NetworkPeering{ - ExchangeSubnetRoutes: true, - Name: d.Get("name").(string), - Network: d.Get("peer_network").(string), - ExportCustomRoutes: d.Get("export_custom_routes").(bool), - ImportCustomRoutes: d.Get("import_custom_routes").(bool), + ExchangeSubnetRoutes: true, + Name: d.Get("name").(string), + Network: d.Get("peer_network").(string), + ExportCustomRoutes: d.Get("export_custom_routes").(bool), + ImportCustomRoutes: d.Get("import_custom_routes").(bool), + ExportSubnetRoutesWithPublicIp: d.Get("export_subnet_routes_with_public_ip").(bool), + ImportSubnetRoutesWithPublicIp: d.Get("import_subnet_routes_with_public_ip").(bool), + ForceSendFields: []string{"ExportSubnetRoutesWithPublicIp"}, } } diff --git a/google/resource_compute_network_peering_test.go b/google/resource_compute_network_peering_test.go index cd67b77983a..cec5ceed185 100644 --- a/google/resource_compute_network_peering_test.go +++ b/google/resource_compute_network_peering_test.go @@ -2,17 +2,14 @@ package google import ( "fmt" - "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" - "google.golang.org/api/compute/v1" ) func TestAccComputeNetworkPeering_basic(t *testing.T) { t.Parallel() - var peering_beta compute.NetworkPeering primaryNetworkName := fmt.Sprintf("network-test-1-%d", randInt(t)) peeringName := fmt.Sprintf("peering-test-1-%d", randInt(t)) @@ -25,19 +22,6 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccComputeNetworkPeering_basic(primaryNetworkName, peeringName, randString(t, 10)), - Check: resource.ComposeTestCheckFunc( - // network foo - testAccCheckComputeNetworkPeeringExist(t, "google_compute_network_peering.foo", &peering_beta), - testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta), - testAccCheckComputeNetworkPeeringImportCustomRoutes(false, &peering_beta), - testAccCheckComputeNetworkPeeringExportCustomRoutes(false, &peering_beta), - - // network bar - testAccCheckComputeNetworkPeeringExist(t, "google_compute_network_peering.bar", &peering_beta), - testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta), - testAccCheckComputeNetworkPeeringImportCustomRoutes(true, &peering_beta), - testAccCheckComputeNetworkPeeringExportCustomRoutes(true, &peering_beta), - ), }, { ResourceName: "google_compute_network_peering.foo", @@ -50,6 +34,31 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) { } +func TestAccComputeNetworkPeering_subnetRoutes(t *testing.T) { + t.Parallel() + + primaryNetworkName := fmt.Sprintf("network-test-1-%d", randInt(t)) + peeringName := fmt.Sprintf("peering-test-%d", randInt(t)) + importId := fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), primaryNetworkName, peeringName) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccComputeNetworkPeeringDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeNetworkPeering_subnetRoutes(primaryNetworkName, peeringName, randString(t, 10)), + }, + { + ResourceName: "google_compute_network_peering.bar", + ImportState: true, + ImportStateVerify: true, + ImportStateId: importId, + }, + }, + }) +} + func testAccComputeNetworkPeeringDestroyProducer(t *testing.T) func(s *terraform.State) error { return func(s *terraform.State) error { config := googleProviderConfig(t) @@ -70,73 +79,8 @@ func testAccComputeNetworkPeeringDestroyProducer(t *testing.T) func(s *terraform } } -func testAccCheckComputeNetworkPeeringExist(t *testing.T, n string, peering *compute.NetworkPeering) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - config := googleProviderConfig(t) - - parts := strings.Split(rs.Primary.ID, "/") - if len(parts) != 2 { - return fmt.Errorf("Invalid network peering identifier: %s", rs.Primary.ID) - } - - networkName, peeringName := parts[0], parts[1] - - network, err := config.clientCompute.Networks.Get(config.Project, networkName).Do() - if err != nil { - return err - } - - found := findPeeringFromNetwork(network, peeringName) - if found == nil { - return fmt.Errorf("Network peering '%s' not found in network '%s'", peeringName, network.Name) - } - *peering = *found - - return nil - } -} - -func testAccCheckComputeNetworkPeeringAutoCreateRoutes(v bool, peering *compute.NetworkPeering) resource.TestCheckFunc { - return func(s *terraform.State) error { - - if peering.ExchangeSubnetRoutes != v { - return fmt.Errorf("should ExchangeSubnetRouts set to %t if AutoCreateRoutes is set to %t", v, v) - } - return nil - } -} - -func testAccCheckComputeNetworkPeeringImportCustomRoutes(v bool, peering *compute.NetworkPeering) resource.TestCheckFunc { - return func(s *terraform.State) error { - if peering.ImportCustomRoutes != v { - return fmt.Errorf("should ImportCustomRoutes set to %t", v) - } - - return nil - } -} - -func testAccCheckComputeNetworkPeeringExportCustomRoutes(v bool, peering *compute.NetworkPeering) resource.TestCheckFunc { - return func(s *terraform.State) error { - if peering.ExportCustomRoutes != v { - return fmt.Errorf("should ExportCustomRoutes set to %t", v) - } - - return nil - } -} - func testAccComputeNetworkPeering_basic(primaryNetworkName, peeringName, suffix string) string { - s := ` + return fmt.Sprintf(` resource "google_compute_network" "network1" { name = "%s" auto_create_subnetworks = false @@ -157,12 +101,30 @@ resource "google_compute_network_peering" "bar" { network = google_compute_network.network2.self_link peer_network = google_compute_network.network1.self_link name = "peering-test-2-%s" -` - - s = s + - `import_custom_routes = true - export_custom_routes = true - ` - s = s + `}` - return fmt.Sprintf(s, primaryNetworkName, peeringName, suffix, suffix) + import_custom_routes = true + export_custom_routes = true +} +`, primaryNetworkName, peeringName, suffix, suffix) +} + +func testAccComputeNetworkPeering_subnetRoutes(primaryNetworkName, peeringName, suffix string) string { + return fmt.Sprintf(` +resource "google_compute_network" "network1" { + name = "%s" + auto_create_subnetworks = false +} + +resource "google_compute_network" "network2" { + name = "network-test-2-%s" + auto_create_subnetworks = false +} + +resource "google_compute_network_peering" "bar" { + network = google_compute_network.network1.self_link + peer_network = google_compute_network.network2.self_link + name = "%s" + import_subnet_routes_with_public_ip = true + export_subnet_routes_with_public_ip = false +} +`, primaryNetworkName, suffix, peeringName) } diff --git a/website/docs/r/compute_network_peering.html.markdown b/website/docs/r/compute_network_peering.html.markdown index 7739dd506bd..9c90599d4af 100644 --- a/website/docs/r/compute_network_peering.html.markdown +++ b/website/docs/r/compute_network_peering.html.markdown @@ -62,6 +62,12 @@ Whether to export the custom routes to the peer network. Defaults to `false`. * `import_custom_routes` - (Optional) Whether to export the custom routes from the peer network. Defaults to `false`. +* `export_subnet_routes_with_public_ip` - (Optional) +Whether subnet routes with public IP range are exported. The default value is true, all subnet routes are exported. The IPv4 special-use ranges (https://en.wikipedia.org/wiki/IPv4#Special_addresses) are always exported to peers and are not controlled by this field. + +* `import_subnet_routes_with_public_ip` - (Optional) +Whether subnet routes with public IP range are imported. The default value is false. The IPv4 special-use ranges (https://en.wikipedia.org/wiki/IPv4#Special_addresses) are always imported from peers and are not controlled by this field. + ## Attributes Reference In addition to the arguments listed above, the following computed attributes are