Skip to content

Commit

Permalink
Add import/export custom routes beta feature to network peering (#1695)
Browse files Browse the repository at this point in the history
Merged PR #1695.
  • Loading branch information
marcohelmerich authored and modular-magician committed May 24, 2019
1 parent 9025e1c commit 5c4ad61
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 146 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<% autogen_exception -%>
package google

import (
Expand All @@ -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])?))$"
Expand Down Expand Up @@ -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 -%>
},
}
}
Expand All @@ -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
}
Expand All @@ -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))
}
Expand All @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -154,14 +174,26 @@ 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{
<% 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
Expand Down
125 changes: 0 additions & 125 deletions third_party/terraform/tests/resource_compute_network_peering_test.go

This file was deleted.

Loading

0 comments on commit 5c4ad61

Please sign in to comment.