Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new network peering fields #3630

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<% autogen_exception -%>
package google

import (
"fmt"
"log"
"sort"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"google.golang.org/api/compute/v1"
Expand Down Expand Up @@ -65,20 +66,33 @@ 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,
Type: schema.TypeString,
Computed: true,
},

"state_details": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
},

"auto_create_routes": {
Type: schema.TypeBool,
Optional: true,
Removed: "auto_create_routes has been removed because it's redundant and not user-configurable. It can safely be removed from your config",
Type: schema.TypeBool,
Optional: true,
Removed: "auto_create_routes has been removed because it's redundant and not user-configurable. It can safely be removed from your config",
Computed: true,
},
},
Expand Down Expand Up @@ -147,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 @@ -206,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
130 changes: 130 additions & 0 deletions third_party/terraform/tests/resource_compute_network_peering_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccComputeNetworkPeering_basic(t *testing.T) {
t.Parallel()

primaryNetworkName := fmt.Sprintf("network-test-1-%d", randInt(t))
peeringName := fmt.Sprintf("peering-test-1-%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_basic(primaryNetworkName, peeringName, randString(t, 10)),
},
{
ResourceName: "google_compute_network_peering.foo",
ImportState: true,
ImportStateVerify: true,
ImportStateId: importId,
},
},
})

}

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)

for _, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_network_peering" {
continue
}

_, err := config.clientCompute.Networks.Get(
config.Project, rs.Primary.ID).Do()
if err == nil {
return fmt.Errorf("Network peering still exists")
}
}

return nil
}
}

func testAccComputeNetworkPeering_basic(primaryNetworkName, peeringName, suffix string) string {
return fmt.Sprintf(`
resource "google_compute_network" "network1" {
name = "%s"
auto_create_subnetworks = false
}

resource "google_compute_network_peering" "foo" {
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
name = "peering-test-2-%s"
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)
}

This file was deleted.

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