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

Network peering gets network project from network self_link #498

Merged
merged 1 commit into from
Oct 3, 2017
Merged
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
58 changes: 15 additions & 43 deletions google/resource_compute_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package google
import (
"fmt"
"log"
"regexp"
"sort"

"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -60,56 +59,43 @@ func resourceComputeNetworkPeering() *schema.Resource {

func resourceComputeNetworkPeeringCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

networkLink := d.Get("network").(string)
networkName := getNameFromNetworkLink(networkLink)
networkFieldValue := ParseNetworkFieldValue(d.Get("network").(string), config)

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

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

err = computeOperationWait(config, addOp, project, "Adding Network Peering")
err = computeOperationWait(config, addOp, networkFieldValue.Project, "Adding Network Peering")
if err != nil {
return err
}

d.SetId(fmt.Sprintf("%s/%s", networkName, d.Get("name").(string)))
d.SetId(fmt.Sprintf("%s/%s", networkFieldValue.Name, d.Get("name").(string)))

return resourceComputeNetworkPeeringRead(d, meta)
}

func resourceComputeNetworkPeeringRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

peeringName := d.Get("name").(string)
networkLink := d.Get("network").(string)
networkName := getNameFromNetworkLink(networkLink)
networkFieldValue := ParseNetworkFieldValue(d.Get("network").(string), config)

network, err := config.clientCompute.Networks.Get(project, networkName).Do()
network, err := config.clientCompute.Networks.Get(networkFieldValue.Project, networkFieldValue.Name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Network %q", networkName))
return handleNotFoundError(err, d, fmt.Sprintf("Network %q", network.Name))
}

peering := findPeeringFromNetwork(network, peeringName)
if peering == nil {
log.Printf("[WARN] Removing network peering %s from network %s because it's gone", peeringName, networkName)
log.Printf("[WARN] Removing network peering %s from network %s because it's gone", peeringName, network.Name)
d.SetId("")
return nil
}
Expand All @@ -126,35 +112,28 @@ func resourceComputeNetworkPeeringDelete(d *schema.ResourceData, meta interface{
config := meta.(*Config)

// Remove the `network` to `peer_network` peering
project, err := getProject(d, config)
if err != nil {
return err
}

name := d.Get("name").(string)
networkLink := d.Get("network").(string)
peerNetworkLink := d.Get("peer_network").(string)
networkName := getNameFromNetworkLink(networkLink)
peerNetworkName := getNameFromNetworkLink(peerNetworkLink)
networkFieldValue := ParseNetworkFieldValue(d.Get("network").(string), config)
peerNetworkFieldValue := ParseNetworkFieldValue(d.Get("peer_network").(string), config)

request := &compute.NetworksRemovePeeringRequest{
Name: name,
}

// Only one delete peering operation at a time can be performed inside any peered VPCs.
peeringLockName := getNetworkPeeringLockName(networkName, peerNetworkName)
peeringLockName := getNetworkPeeringLockName(networkFieldValue.Name, peerNetworkFieldValue.Name)
mutexKV.Lock(peeringLockName)
defer mutexKV.Unlock(peeringLockName)

removeOp, err := config.clientCompute.Networks.RemovePeering(project, networkName, request).Do()
removeOp, err := config.clientCompute.Networks.RemovePeering(networkFieldValue.Project, networkFieldValue.Name, request).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[WARN] Peering `%s` already removed from network `%s`", name, networkName)
log.Printf("[WARN] Peering `%s` already removed from network `%s`", name, networkFieldValue.Name)
} else {
return fmt.Errorf("Error removing peering `%s` from network `%s`: %s", name, networkName, err)
return fmt.Errorf("Error removing peering `%s` from network `%s`: %s", name, networkFieldValue.Name, err)
}
} else {
err = computeOperationWait(config, removeOp, project, "Removing Network Peering")
err = computeOperationWait(config, removeOp, networkFieldValue.Project, "Removing Network Peering")
if err != nil {
return err
}
Expand All @@ -172,13 +151,6 @@ func findPeeringFromNetwork(network *compute.Network, peeringName string) *compu
return nil
}

func getNameFromNetworkLink(network string) string {
r := regexp.MustCompile(peerNetworkLinkRegex)

m := r.FindStringSubmatch(network)
return m[2]
}

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