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 import/export custom routes beta feature to network peering #754

Merged
merged 1 commit into from
May 24, 2019
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
55 changes: 40 additions & 15 deletions google-beta/resource_compute_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"

"github.com/hashicorp/terraform/helper/schema"
computeBeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)
Expand Down Expand Up @@ -39,11 +40,14 @@ func resourceComputeNetworkPeering() *schema.Resource {
ValidateFunc: validateRegexp(peerNetworkLinkRegex),
DiffSuppressFunc: compareSelfLinkRelativePaths,
},
// The API only accepts true as a value for exchange_subnet_routes or auto_create_routes (of which only one can be set in a valid request).
// Also, you can't set auto_create_routes if you use the networkPeering object. auto_create_routes is also deprecated
"auto_create_routes": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: true,
Type: schema.TypeBool,
Optional: true,
Deprecated: "auto_create_routes has been deprecated because it's redundant and not user-configurable. It can safely be removed from your config",
ForceNew: true,
Default: true,
},
"state": {
Type: schema.TypeString,
Expand All @@ -53,6 +57,18 @@ func resourceComputeNetworkPeering() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"export_custom_routes": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: false,
},
"import_custom_routes": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: false,
},
},
}
}
Expand All @@ -64,18 +80,15 @@ func resourceComputeNetworkPeeringCreate(d *schema.ResourceData, meta interface{
return err
}

request := &compute.NetworksAddPeeringRequest{
Name: d.Get("name").(string),
PeerNetwork: d.Get("peer_network").(string),
AutoCreateRoutes: d.Get("auto_create_routes").(bool),
}
request := &computeBeta.NetworksAddPeeringRequest{}
request.NetworkPeering = expandNetworkPeering(d)

addOp, err := config.clientCompute.Networks.AddPeering(networkFieldValue.Project, networkFieldValue.Name, request).Do()
addOp, err := config.clientComputeBeta.Networks.AddPeering(networkFieldValue.Project, networkFieldValue.Name, request).Do()
if err != nil {
return fmt.Errorf("Error adding network peering: %s", err)
}

err = computeOperationWait(config.clientCompute, addOp, networkFieldValue.Project, "Adding Network Peering")
err = computeSharedOperationWait(config.clientCompute, addOp, networkFieldValue.Project, "Adding Network Peering")
if err != nil {
return err
}
Expand All @@ -94,7 +107,7 @@ func resourceComputeNetworkPeeringRead(d *schema.ResourceData, meta interface{})
return err
}

network, err := config.clientCompute.Networks.Get(networkFieldValue.Project, networkFieldValue.Name).Do()
network, err := config.clientComputeBeta.Networks.Get(networkFieldValue.Project, networkFieldValue.Name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Network %q", networkFieldValue.Name))
}
Expand All @@ -107,7 +120,9 @@ func resourceComputeNetworkPeeringRead(d *schema.ResourceData, meta interface{})
}

d.Set("peer_network", peering.Network)
d.Set("auto_create_routes", peering.AutoCreateRoutes)
d.Set("name", peering.Name)
d.Set("import_custom_routes", peering.ImportCustomRoutes)
d.Set("export_custom_routes", peering.ExportCustomRoutes)
d.Set("state", peering.State)
d.Set("state_details", peering.StateDetails)

Expand Down Expand Up @@ -145,7 +160,7 @@ func resourceComputeNetworkPeeringDelete(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error removing peering `%s` from network `%s`: %s", name, networkFieldValue.Name, err)
}
} else {
err = computeOperationWait(config.clientCompute, removeOp, networkFieldValue.Project, "Removing Network Peering")
err = computeSharedOperationWait(config.clientCompute, removeOp, networkFieldValue.Project, "Removing Network Peering")
if err != nil {
return err
}
Expand All @@ -154,14 +169,24 @@ func resourceComputeNetworkPeeringDelete(d *schema.ResourceData, meta interface{
return nil
}

func findPeeringFromNetwork(network *compute.Network, peeringName string) *compute.NetworkPeering {
func findPeeringFromNetwork(network *computeBeta.Network, peeringName string) *computeBeta.NetworkPeering {
for _, p := range network.Peerings {
if p.Name == peeringName {
return p
}
}
return nil
}
func expandNetworkPeering(d *schema.ResourceData) *computeBeta.NetworkPeering {
return &computeBeta.NetworkPeering{
ExportCustomRoutes: d.Get("export_custom_routes").(bool),
ImportCustomRoutes: d.Get("import_custom_routes").(bool),
// auto_create_routes was replaced by exchange_subnet_routes in the network peering object
ExchangeSubnetRoutes: true,
Name: d.Get("name").(string),
Network: d.Get("peer_network").(string),
}
}

func getNetworkPeeringLockName(networkName, peerNetworkName string) string {
// Whether you delete the peering from network A to B or the one from B to A, they
Expand Down
102 changes: 66 additions & 36 deletions google-beta/resource_compute_network_peering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package google

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/compute/v1"
"strings"
"testing"
computeBeta "google.golang.org/api/compute/v0.beta"
)

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

var peering compute.NetworkPeering
var peering_beta computeBeta.NetworkPeering

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -23,10 +23,14 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) {
{
Config: testAccComputeNetworkPeering_basic(),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.foo", &peering),
testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering),
testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.bar", &peering),
testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering),
testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.foo", &peering_beta),
testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta),
testAccCheckComputeNetworkPeeringImportCustomRoutes(false, &peering_beta),
testAccCheckComputeNetworkPeeringExportCustomRoutes(false, &peering_beta),
testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.bar", &peering_beta),
testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta),
testAccCheckComputeNetworkPeeringImportCustomRoutes(true, &peering_beta),
testAccCheckComputeNetworkPeeringExportCustomRoutes(true, &peering_beta),
),
},
},
Expand All @@ -42,7 +46,7 @@ func testAccComputeNetworkPeeringDestroy(s *terraform.State) error {
continue
}

_, err := config.clientCompute.Networks.Get(
_, err := config.clientComputeBeta.Networks.Get(
config.Project, rs.Primary.ID).Do()
if err == nil {
return fmt.Errorf("Network peering still exists")
Expand All @@ -52,7 +56,7 @@ func testAccComputeNetworkPeeringDestroy(s *terraform.State) error {
return nil
}

func testAccCheckComputeNetworkPeeringExist(n string, peering *compute.NetworkPeering) resource.TestCheckFunc {
func testAccCheckComputeNetworkPeeringExist(n string, peering *computeBeta.NetworkPeering) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -72,7 +76,7 @@ func testAccCheckComputeNetworkPeeringExist(n string, peering *compute.NetworkPe

networkName, peeringName := parts[0], parts[1]

network, err := config.clientCompute.Networks.Get(config.Project, networkName).Do()
network, err := config.clientComputeBeta.Networks.Get(config.Project, networkName).Do()
if err != nil {
return err
}
Expand All @@ -87,39 +91,65 @@ func testAccCheckComputeNetworkPeeringExist(n string, peering *compute.NetworkPe
}
}

func testAccCheckComputeNetworkPeeringAutoCreateRoutes(v bool, peering *compute.NetworkPeering) resource.TestCheckFunc {
func testAccCheckComputeNetworkPeeringAutoCreateRoutes(v bool, peering *computeBeta.NetworkPeering) resource.TestCheckFunc {
return func(s *terraform.State) error {
if peering.AutoCreateRoutes != v {
return fmt.Errorf("should AutoCreateRoutes set to %t", v)
}

if peering.ExchangeSubnetRoutes != v {
return fmt.Errorf("should ExchangeSubnetRouts set to %t if AutoCreateRoutes is set to %t", v, v)
}
return nil
}
}

func testAccComputeNetworkPeering_basic() string {
return fmt.Sprintf(`
resource "google_compute_network" "network1" {
name = "network-test-1-%s"
auto_create_subnetworks = false
}
func testAccCheckComputeNetworkPeeringImportCustomRoutes(v bool, peering *computeBeta.NetworkPeering) resource.TestCheckFunc {
return func(s *terraform.State) error {
if peering.ImportCustomRoutes != v {
return fmt.Errorf("should ImportCustomRoutes set to %t", v)
}

resource "google_compute_network" "network2" {
name = "network-test-2-%s"
auto_create_subnetworks = false
return nil
}
}

resource "google_compute_network_peering" "foo" {
name = "peering-test-1-%s"
network = "${google_compute_network.network1.self_link}"
peer_network = "${google_compute_network.network2.self_link}"
}
func testAccCheckComputeNetworkPeeringExportCustomRoutes(v bool, peering *computeBeta.NetworkPeering) resource.TestCheckFunc {
return func(s *terraform.State) error {
if peering.ExportCustomRoutes != v {
return fmt.Errorf("should ExportCustomRoutes set to %t", v)
}

resource "google_compute_network_peering" "bar" {
name = "peering-test-2-%s"
auto_create_routes = true
network = "${google_compute_network.network2.self_link}"
peer_network = "${google_compute_network.network1.self_link}"
return nil
}
}
`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))

func testAccComputeNetworkPeering_basic() 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"
auto_create_subnetworks = false
}

resource "google_compute_network_peering" "foo" {
name = "peering-test-1-%s"
network = "${google_compute_network.network1.self_link}"
peer_network = "${google_compute_network.network2.self_link}"
}

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"
auto_create_routes = true
`

s = s +
`import_custom_routes = true
export_custom_routes = true
`
s = s + `}`
return fmt.Sprintf(s, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
}
33 changes: 0 additions & 33 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,20 +644,6 @@ func resourceContainerCluster() *schema.Resource {
},
},

"vertical_pod_autoscaling": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},

"resource_labels": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -853,10 +839,6 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
cluster.PrivateClusterConfig = expandPrivateClusterConfig(v)
}

if v, ok := d.GetOk("vertical_pod_autoscaling"); ok {
cluster.VerticalPodAutoscaling = expandVerticalPodAutoscaling(v)
}

if v, ok := d.GetOk("database_encryption"); ok {
cluster.DatabaseEncryption = expandDatabaseEncryption(v)
}
Expand Down Expand Up @@ -1021,10 +1003,6 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
return err
}

if err := d.Set("vertical_pod_autoscaling", flattenVerticalPodAutoscaling(cluster.VerticalPodAutoscaling)); err != nil {
return err
}

igUrls, err := getInstanceGroupUrlsFromManagerUrls(config, cluster.InstanceGroupUrls)
if err != nil {
return err
Expand Down Expand Up @@ -1976,17 +1954,6 @@ func expandPrivateClusterConfig(configured interface{}) *containerBeta.PrivateCl
}
}

func expandVerticalPodAutoscaling(configured interface{}) *containerBeta.VerticalPodAutoscaling {
l := configured.([]interface{})
if len(l) == 0 {
return nil
}
config := l[0].(map[string]interface{})
return &containerBeta.VerticalPodAutoscaling{
Enabled: config["enabled"].(bool),
}
}

func expandDatabaseEncryption(configured interface{}) *containerBeta.DatabaseEncryption {
l := configured.([]interface{})
if len(l) == 0 {
Expand Down
28 changes: 0 additions & 28 deletions google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3091,34 +3091,6 @@ resource "google_container_cluster" "shared_vpc_cluster" {
}`, projectName, org, billingId, projectName, org, billingId, acctest.RandString(10), acctest.RandString(10), name)
}

func testAccContainerCluster_withVerticalPodAutoscalingEnabled(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_vertical_pod_autoscaling" {
name = "%s"
zone = "us-central1-a"
initial_node_count = 1

vertical_pod_autoscaling {
enabled = true
}
}
`, clusterName)
}

func testAccContainerCluster_withVerticalPodAutoscalingDisabled(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_vertical_pod_autoscaling" {
name = "%s"
zone = "us-central1-a"
initial_node_count = 1

vertical_pod_autoscaling {
enabled = false
}
}
`, clusterName)
}

func testAccContainerCluster_withoutResourceLabels(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_resource_labels" {
Expand Down