Skip to content

Commit

Permalink
DRY it out, and reverse fix for hashicorp#5552.
Browse files Browse the repository at this point in the history
  • Loading branch information
sl1pm4t committed Sep 4, 2016
1 parent c197b27 commit 448d413
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 37 deletions.
50 changes: 50 additions & 0 deletions builtin/providers/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,53 @@ func getZonalResourceFromRegion(getResource func(string) (interface{}, error), r
// Resource does not exist in this region
return nil, nil
}

// getNetworkLink reads the "network" field from the given resource data and if the value:
// - is a resource URL, returns the string unchanged
// - is the network name only, then looks up the resource URL using the google client
func getNetworkLink(d *schema.ResourceData, config *Config, field string) (string, error) {
if v, ok := d.GetOk(field); ok {
network := v.(string)

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

if !strings.HasPrefix(network, "https://www.googleapis.com/compute/") {
// Network value provided is just the name, lookup the network SelfLink
networkData, err := config.clientCompute.Networks.Get(
project, network).Do()
if err != nil {
return "", fmt.Errorf("Error reading network: %s", err)
}
network = networkData.SelfLink
}

return network, nil

} else {
return "", nil
}
}

// getNetworkName reads the "network" field from the given resource data and if the value:
// - is a resource URL, extracts the network name from the URL and returns it
// - is the network name only (i.e not prefixed with http://www.googleapis.com/compute/...), is returned unchanged
func getNetworkName(d *schema.ResourceData, field string) (string, error) {
if v, ok := d.GetOk(field); ok {
network := v.(string)

if strings.HasPrefix(network, "https://www.googleapis.com/compute/") {
// extract the network name from SelfLink URL
networkName := network[strings.LastIndex(network, "/")+1:]
if networkName == "" {
return "", fmt.Errorf("network url not valid")
}
return networkName, nil
}

return network, nil
}
return "", nil
}
5 changes: 2 additions & 3 deletions builtin/providers/google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,13 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
if networkName != "" && subnetworkName != "" {
return fmt.Errorf("Cannot specify both network and subnetwork values.")
} else if networkName != "" {
network, err := config.clientCompute.Networks.Get(
project, networkName).Do()
networkLink, err = getNetworkLink(d, config, prefix+".network")
if err != nil {
return fmt.Errorf(
"Error referencing network '%s': %s",
networkName, err)
}
networkLink = network.SelfLink

} else {
region := getRegionFromZone(d.Get("zone").(string))
subnetwork, err := config.clientCompute.Subnetworks.Get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,12 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network

var networkLink, subnetworkLink string
if networkName != "" {
network, err := config.clientCompute.Networks.Get(
project, networkName).Do()
networkLink, err = getNetworkLink(d, config, prefix+".network")
if err != nil {
return nil, fmt.Errorf("Error referencing network '%s': %s",
networkName, err)
}
networkLink = network.SelfLink

} else {
// lookup subnetwork link using region and subnetwork name
region, err := getRegion(d, config)
Expand Down
5 changes: 2 additions & 3 deletions builtin/providers/google/resource_compute_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error
}

// Look up the network to attach the route to
network, err := config.clientCompute.Networks.Get(
project, d.Get("network").(string)).Do()
network, err := getNetworkLink(d, config, "network")
if err != nil {
return fmt.Errorf("Error reading network: %s", err)
}
Expand Down Expand Up @@ -149,7 +148,7 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error
route := &compute.Route{
Name: d.Get("name").(string),
DestRange: d.Get("dest_range").(string),
Network: network.SelfLink,
Network: network,
NextHopInstance: nextHopInstance,
NextHopVpnTunnel: nextHopVpnTunnel,
NextHopIp: nextHopIp,
Expand Down
13 changes: 3 additions & 10 deletions builtin/providers/google/resource_compute_subnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,9 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
return err
}

// Network can be provided as just the name, or the full resource URL
network := d.Get("network").(string)
if !strings.HasPrefix(network, "https://www.googleapis.com/compute/") {
// Network value provided is just the name, lookup the network URL
networkData, err := config.clientCompute.Networks.Get(
project, d.Get("network").(string)).Do()
if err != nil {
return fmt.Errorf("Error reading network: %s", err)
}
network = networkData.SelfLink
network, err := getNetworkLink(d, config, "network")
if err != nil {
return err
}

// Build the subnetwork parameters
Expand Down
14 changes: 3 additions & 11 deletions builtin/providers/google/resource_compute_vpn_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"strings"
)

func resourceComputeVpnGateway() *schema.Resource {
Expand Down Expand Up @@ -72,16 +71,9 @@ func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) e
}

name := d.Get("name").(string)
network := d.Get("network").(string)

if !strings.HasPrefix(network, "https://www.googleapis.com/compute/") {
// Network value provided is just the name, lookup the network URL
networkData, err := config.clientCompute.Networks.Get(
project, d.Get("network").(string)).Do()
if err != nil {
return fmt.Errorf("Error reading network: %s", err)
}
network = networkData.SelfLink
network, err := getNetworkLink(d, config, "network")
if err != nil {
return err
}

vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
Expand Down
10 changes: 7 additions & 3 deletions builtin/providers/google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,12 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
cluster.MonitoringService = v.(string)
}

if v, ok := d.GetOk("network"); ok {
cluster.Network = v.(string)
if _, ok := d.GetOk("network"); ok {
network, err := getNetworkName(d, "network")
if err != nil {
return err
}
cluster.Network = network
}

if v, ok := d.GetOk("subnetwork"); ok {
Expand Down Expand Up @@ -425,7 +429,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
d.Set("description", cluster.Description)
d.Set("logging_service", cluster.LoggingService)
d.Set("monitoring_service", cluster.MonitoringService)
d.Set("network", cluster.Network)
d.Set("network", d.Get("network").(string))
d.Set("subnetwork", cluster.Subnetwork)
d.Set("node_config", flattenClusterNodeConfig(cluster.NodeConfig))
d.Set("instance_group_urls", cluster.InstanceGroupUrls)
Expand Down
51 changes: 51 additions & 0 deletions builtin/providers/google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ func TestAccContainerCluster_withNodeConfig(t *testing.T) {
})
}

func TestAccContainerCluster_network(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccContainerCluster_networkRef,
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerClusterExists(
"google_container_cluster.with_net_ref_by_url"),
testAccCheckContainerClusterExists(
"google_container_cluster.with_net_ref_by_name"),
),
},
},
})
}

func testAccCheckContainerClusterDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -124,3 +143,35 @@ resource "google_container_cluster" "with_node_config" {
]
}
}`, acctest.RandString(10))

var testAccContainerCluster_networkRef = fmt.Sprintf(`
resource "google_compute_network" "container_network" {
name = "container-net-%s"
auto_create_subnetworks = true
}
resource "google_container_cluster" "with_net_ref_by_url" {
name = "cluster-test-%s"
zone = "us-central1-a"
initial_node_count = 1
master_auth {
username = "mr.yoda"
password = "adoy.rm"
}
network = "${google_compute_network.container_network.self_link}"
}
resource "google_container_cluster" "with_net_ref_by_name" {
name = "cluster-test-%s"
zone = "us-central1-a"
initial_node_count = 1
master_auth {
username = "mr.yoda"
password = "adoy.rm"
}
network = "${google_compute_network.container_network.name}"
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ the type is "local-ssd", in which case scratch must be true).

The `network_interface` block supports:

* `network` - (Optional) The name of the network to attach this interface to.
* `network` - (Optional) The name or self_link of the network to attach this interface to.
Either `network` or `subnetwork` must be provided.

* `subnetwork` - (Optional) the name of the subnetwork to attach this interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ The `disk` block supports:

The `network_interface` block supports:

* `network` - (Optional) The name of the network to attach this interface to.
* `network` - (Optional) The name or self_link of the network to attach this interface to.
Use `network` attribute for Legacy or Auto subnetted networks and
`subnetwork` for custom subnetted networks.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The following arguments are supported:
* `name` - (Required) A unique name for the resource, required by GCE.
Changing this forces a new resource to be created.

* `network` - (Required) The name of the network to attach this route to.
* `network` - (Required) The name or self_link of the network to attach this route to.

* `priority` - (Required) The priority of this route, used to break ties.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ resource "google_container_cluster" "primary" {
`monitoring.googleapis.com` and `none`. Defaults to
`monitoring.googleapis.com`

* `network` - (Optional) The name of the Google Compute Engine network to which
* `network` - (Optional) The name or self_link of the Google Compute Engine network to which
the cluster is connected

* `node_config` - (Optional) The machine type and image to use for all nodes in
Expand Down

0 comments on commit 448d413

Please sign in to comment.