From 5c4ad61aa9b2ece550f6dccc987f4826f43835fe Mon Sep 17 00:00:00 2001 From: marcohelmerich Date: Fri, 24 May 2019 21:19:38 +0200 Subject: [PATCH] Add import/export custom routes beta feature to network peering (#1695) Merged PR #1695. --- build/terraform | 2 +- build/terraform-beta | 2 +- ...> resource_compute_network_peering.go.erb} | 70 ++++++-- .../resource_compute_network_peering_test.go | 125 ------------- ...source_compute_network_peering_test.go.erb | 165 ++++++++++++++++++ 5 files changed, 218 insertions(+), 146 deletions(-) rename third_party/terraform/resources/{resource_compute_network_peering.go => resource_compute_network_peering.go.erb} (64%) delete mode 100644 third_party/terraform/tests/resource_compute_network_peering_test.go create mode 100644 third_party/terraform/tests/resource_compute_network_peering_test.go.erb diff --git a/build/terraform b/build/terraform index 40c5f72e19c6..216cd160c3e3 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 40c5f72e19c66193befc74328b0259e15606155c +Subproject commit 216cd160c3e3446fb4b7b0001dfacdef26fb0fff diff --git a/build/terraform-beta b/build/terraform-beta index 1427ea7e2a08..5ffa0be5294c 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 1427ea7e2a08250454909d3141f9d58a6f9af702 +Subproject commit 5ffa0be5294c244bbc4a41990d1f8c721da419c5 diff --git a/third_party/terraform/resources/resource_compute_network_peering.go b/third_party/terraform/resources/resource_compute_network_peering.go.erb similarity index 64% rename from third_party/terraform/resources/resource_compute_network_peering.go rename to third_party/terraform/resources/resource_compute_network_peering.go.erb index 288d3a5fa357..7efcd7e640aa 100644 --- a/third_party/terraform/resources/resource_compute_network_peering.go +++ b/third_party/terraform/resources/resource_compute_network_peering.go.erb @@ -1,3 +1,4 @@ +<% autogen_exception -%> package google import ( @@ -8,6 +9,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" "google.golang.org/api/googleapi" + computeBeta "google.golang.org/api/compute/v0.beta" ) const peerNetworkLinkRegex = "projects/(" + ProjectRegex + ")/global/networks/((?:[a-z](?:[-a-z0-9]*[a-z0-9])?))$" @@ -39,20 +41,37 @@ 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, - Computed: true, + Type: schema.TypeString, + Computed: true, }, "state_details": { - Type: schema.TypeString, - Computed: true, + Type: schema.TypeString, + Computed: true, }, + <% unless version == 'ga' -%> + "export_custom_routes": { + Type: schema.TypeBool, + ForceNew: true, + Optional: true, + Default: false, + }, + "import_custom_routes": { + Type: schema.TypeBool, + ForceNew: true, + Optional: true, + Default: false, + }, + <% end -%> }, } } @@ -64,18 +83,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 } @@ -94,7 +110,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)) } @@ -107,7 +123,11 @@ 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) + <% unless version == 'ga' -%> + d.Set("import_custom_routes", peering.ImportCustomRoutes) + d.Set("export_custom_routes", peering.ExportCustomRoutes) + <% end -%> d.Set("state", peering.State) d.Set("state_details", peering.StateDetails) @@ -145,7 +165,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 } @@ -154,7 +174,7 @@ 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 @@ -162,6 +182,18 @@ func findPeeringFromNetwork(network *compute.Network, peeringName string) *compu } return nil } +func expandNetworkPeering(d *schema.ResourceData) *computeBeta.NetworkPeering { + return &computeBeta.NetworkPeering{ + <% unless version == 'ga' -%> + ExportCustomRoutes: d.Get("export_custom_routes").(bool), + ImportCustomRoutes: d.Get("import_custom_routes").(bool), + <% end -%> + // 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 diff --git a/third_party/terraform/tests/resource_compute_network_peering_test.go b/third_party/terraform/tests/resource_compute_network_peering_test.go deleted file mode 100644 index 2ac126f1938c..000000000000 --- a/third_party/terraform/tests/resource_compute_network_peering_test.go +++ /dev/null @@ -1,125 +0,0 @@ -package google - -import ( - "fmt" - "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" -) - -func TestAccComputeNetworkPeering_basic(t *testing.T) { - t.Parallel() - - var peering compute.NetworkPeering - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccComputeNetworkPeeringDestroy, - Steps: []resource.TestStep{ - { - 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), - ), - }, - }, - }) - -} - -func testAccComputeNetworkPeeringDestroy(s *terraform.State) error { - config := testAccProvider.Meta().(*Config) - - 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 testAccCheckComputeNetworkPeeringExist(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 := testAccProvider.Meta().(*Config) - - 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.AutoCreateRoutes != v { - return fmt.Errorf("should AutoCreateRoutes set to %t", v) - } - - return nil - } -} - -func testAccComputeNetworkPeering_basic() string { - return fmt.Sprintf(` -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" { - name = "peering-test-2-%s" - auto_create_routes = true - network = "${google_compute_network.network2.self_link}" - peer_network = "${google_compute_network.network1.self_link}" -} -`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10)) -} diff --git a/third_party/terraform/tests/resource_compute_network_peering_test.go.erb b/third_party/terraform/tests/resource_compute_network_peering_test.go.erb new file mode 100644 index 000000000000..bc7fba9e6d6d --- /dev/null +++ b/third_party/terraform/tests/resource_compute_network_peering_test.go.erb @@ -0,0 +1,165 @@ +<% autogen_exception -%> +package google + +import ( + "fmt" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "strings" + "testing" + "google.golang.org/api/compute/v1" + computeBeta "google.golang.org/api/compute/v0.beta" +) + +func TestAccComputeNetworkPeering_basic(t *testing.T) { + t.Parallel() + var peering_beta computeBeta.NetworkPeering + + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccComputeNetworkPeeringDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeNetworkPeering_basic(), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.foo", &peering_beta), + testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta), + <% unless version == 'ga' -%> + testAccCheckComputeNetworkPeeringImportCustomRoutes(false, &peering_beta), + testAccCheckComputeNetworkPeeringExportCustomRoutes(false, &peering_beta), + <% end -%> + testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.bar", &peering_beta), + testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta), + <% unless version == 'ga' -%> + testAccCheckComputeNetworkPeeringImportCustomRoutes(true, &peering_beta), + testAccCheckComputeNetworkPeeringExportCustomRoutes(true, &peering_beta), + <% end -%> + ), + }, + }, + }) + +} + +func testAccComputeNetworkPeeringDestroy(s *terraform.State) error { + config := testAccProvider.Meta().(*Config) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "google_compute_network_peering" { + continue + } + + _, err := config.clientComputeBeta.Networks.Get( + config.Project, rs.Primary.ID).Do() + if err == nil { + return fmt.Errorf("Network peering still exists") + } + } + + return nil +} + +func testAccCheckComputeNetworkPeeringExist(n string, peering *computeBeta.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 := testAccProvider.Meta().(*Config) + + 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.clientComputeBeta.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 *computeBeta.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 + } +} + +<% unless version == 'ga' -%> +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) + } + + return nil + } +} + +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) + } + + return nil + } +} +<% end -%> + +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 + ` + + <% unless version == 'ga' -%> + s = s + + `import_custom_routes = true + export_custom_routes = true + ` + <% end -%> + s = s + `}` + return fmt.Sprintf(s, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10)) +}