Skip to content

Commit

Permalink
add new network peering fields (#3630) (#6586)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 13, 2020
1 parent 6d4431d commit 047d0e4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 95 deletions.
3 changes: 3 additions & 0 deletions .changelog/3630.txt
Original file line number Diff line number Diff line change
@@ -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`
```
28 changes: 23 additions & 5 deletions google/resource_compute_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"},
}
}

Expand Down
142 changes: 52 additions & 90 deletions google/resource_compute_network_peering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
6 changes: 6 additions & 0 deletions website/docs/r/compute_network_peering.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 047d0e4

Please sign in to comment.