Skip to content

Commit

Permalink
Add import/export custom routes beta feature to network peering
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Helmerich committed May 16, 2019
1 parent 7eea62a commit 2a28eac
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 143 deletions.
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 @@ -40,19 +42,40 @@ func resourceComputeNetworkPeering() *schema.Resource {
DiffSuppressFunc: compareSelfLinkRelativePaths,
},
"auto_create_routes": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: true,
Type: schema.TypeBool,
Optional: true,
Deprecated: "auto_create_routes is deprecated use exchange_subnet_routes instead",
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,
},
"exchange_subnet_routes": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: 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 +87,21 @@ 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{}
<% unless version == 'ga' -%>
request.NetworkPeering = expandNetworkPeering(d)
<% else -%>
request.Name = d.Get("name").(string)
request.PeerNetwork = d.Get("peer_network").(string)
request.AutoCreateRoutes = d.Get("auto_create_routes").(bool)
<% end -%>

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 +120,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 @@ -108,6 +134,12 @@ 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("exchange_subnet_routes", peering.ExchangeSubnetRoutes)
<% 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 +177,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,7 +186,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
Expand All @@ -163,6 +195,22 @@ func findPeeringFromNetwork(network *compute.Network, peeringName string) *compu
return nil
}

func expandNetworkPeering(d *schema.ResourceData) *computeBeta.NetworkPeering {
// In case auto_create_routes is set, already switch to the new parameter
// deprecation message will be shown, but the parameter will still work
if d.Get("auto_create_routes").(bool) == true {
d.Set("exchange_subnet_routes", true)
}

return &computeBeta.NetworkPeering{
ExportCustomRoutes: d.Get("export_custom_routes").(bool),
ImportCustomRoutes: d.Get("import_custom_routes").(bool),
ExchangeSubnetRoutes: d.Get("exchange_subnet_routes").(bool),
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
// cannot happen at the same time.
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 2a28eac

Please sign in to comment.