From 430ed48a44200412cabff95bc981e736f18038c0 Mon Sep 17 00:00:00 2001 From: Matt Morrison Date: Mon, 15 Feb 2016 16:17:55 +1300 Subject: [PATCH] Update google resources where necessary to make use of subnetworks, update som docs --- builtin/providers/google/provider.go | 8 ++++ .../google/resource_compute_instance.go | 41 ++++++++++++---- .../resource_compute_instance_template.go | 9 ++++ .../google/resource_compute_network.go | 7 +-- .../google/resource_compute_subnetwork.go | 4 +- .../google/resource_compute_vpn_tunnel.go | 27 +++++++++-- .../google/r/compute_instance.html.markdown | 7 ++- .../google/r/compute_network.html.markdown | 13 ++++- .../google/r/compute_subnetwork.html.markdown | 47 +++++++++++++++++++ 9 files changed, 142 insertions(+), 21 deletions(-) create mode 100644 website/source/docs/providers/google/r/compute_subnetwork.html.markdown diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index c7d345b87..ebb4a5dfd 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -145,3 +145,11 @@ func validateCredentials(v interface{}, k string) (warnings []string, errors []e return } + +func getRegionFromZone(zone string) string { + if zone != "" && len(zone) > 2 { + region := zone[:len(zone)-2] + return region + } + return "" +} diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 8e6a3f937..28578736d 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -111,7 +111,13 @@ func resourceComputeInstance() *schema.Resource { Schema: map[string]*schema.Schema{ "network": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, + ForceNew: true, + }, + + "subnetwork": &schema.Schema{ + Type: schema.TypeString, + Optional: true, ForceNew: true, }, @@ -445,17 +451,36 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err prefix := fmt.Sprintf("network_interface.%d", i) // Load up the name of this network_interfac networkName := d.Get(prefix + ".network").(string) - network, err := config.clientCompute.Networks.Get( - config.Project, networkName).Do() - if err != nil { - return fmt.Errorf( - "Error referencing network '%s': %s", - networkName, err) + subnetworkName := d.Get(prefix + ".subnetwork").(string) + var networkLink, subnetworkLink string + + if networkName != "" && subnetworkName != "" { + return fmt.Errorf("Cannot specify both network and subnetwork values.") + } else if networkName != "" { + network, err := config.clientCompute.Networks.Get( + config.Project, networkName).Do() + 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( + config.Project, region, subnetworkName).Do() + if err != nil { + return fmt.Errorf( + "Error referencing subnetwork '%s' in region '%s': %s", + subnetworkName, region, err) + } + subnetworkLink = subnetwork.SelfLink } // Build the networkInterface var iface compute.NetworkInterface - iface.Network = network.SelfLink + iface.Network = networkLink + iface.Subnetwork = subnetworkLink // Handle access_config structs accessConfigsCount := d.Get(prefix + ".access_config.#").(int) diff --git a/builtin/providers/google/resource_compute_instance_template.go b/builtin/providers/google/resource_compute_instance_template.go index c9eabdece..f7a0ce8bf 100644 --- a/builtin/providers/google/resource_compute_instance_template.go +++ b/builtin/providers/google/resource_compute_instance_template.go @@ -141,6 +141,12 @@ func resourceComputeInstanceTemplate() *schema.Resource { ForceNew: true, }, + "subnetwork": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "access_config": &schema.Schema{ Type: schema.TypeList, Optional: true, @@ -337,9 +343,12 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) (error, []*compute. source += v.(string) } + subnetworkLink := d.Get("subnetwork").(string) + // Build the networkInterface var iface compute.NetworkInterface iface.Network = source + iface.Subnetwork = subnetworkLink accessConfigsCount := d.Get(prefix + ".access_config.#").(int) iface.AccessConfigs = make([]*compute.AccessConfig, accessConfigsCount) diff --git a/builtin/providers/google/resource_compute_network.go b/builtin/providers/google/resource_compute_network.go index 3f8536361..36c242942 100644 --- a/builtin/providers/google/resource_compute_network.go +++ b/builtin/providers/google/resource_compute_network.go @@ -24,9 +24,10 @@ func resourceComputeNetwork() *schema.Resource { }, "ipv4_range": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Deprecated: "Please use custom subnetworks instead", }, "gateway_ipv4": &schema.Schema{ diff --git a/builtin/providers/google/resource_compute_subnetwork.go b/builtin/providers/google/resource_compute_subnetwork.go index 23905cd5d..daf97b910 100644 --- a/builtin/providers/google/resource_compute_subnetwork.go +++ b/builtin/providers/google/resource_compute_subnetwork.go @@ -34,7 +34,7 @@ func resourceComputeSubnetwork() *schema.Resource { ForceNew: true, }, - "ipCidrRange": &schema.Schema{ + "ip_cidr_range": &schema.Schema{ Type: schema.TypeString, Required: true, ForceNew: true, @@ -70,7 +70,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e subnetwork := &compute.Subnetwork{ Name: d.Get("name").(string), Description: d.Get("description").(string), - IpCidrRange: d.Get("ipCidrRange").(string), + IpCidrRange: d.Get("ip_cidr_range").(string), Network: d.Get("network").(string), } region := d.Get("region").(string) diff --git a/builtin/providers/google/resource_compute_vpn_tunnel.go b/builtin/providers/google/resource_compute_vpn_tunnel.go index b9ce28529..d797c6d01 100644 --- a/builtin/providers/google/resource_compute_vpn_tunnel.go +++ b/builtin/providers/google/resource_compute_vpn_tunnel.go @@ -55,6 +55,13 @@ func resourceComputeVpnTunnel() *schema.Resource { Default: 2, ForceNew: true, }, + "local_traffic_selector": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, "detailed_status": &schema.Schema{ Type: schema.TypeString, Computed: true, @@ -82,14 +89,24 @@ func resourceComputeVpnTunnelCreate(d *schema.ResourceData, meta interface{}) er return fmt.Errorf("Only IKE version 1 or 2 supported, not %d", ikeVersion) } + // Build up the list of sources + var localTrafficSelectors []string + if v := d.Get("local_traffic_selector").(*schema.Set); v.Len() > 0 { + localTrafficSelectors = make([]string, v.Len()) + for i, v := range v.List() { + localTrafficSelectors[i] = v.(string) + } + } + vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute) vpnTunnel := &compute.VpnTunnel{ - Name: name, - PeerIp: peerIp, - SharedSecret: sharedSecret, - TargetVpnGateway: targetVpnGateway, - IkeVersion: int64(ikeVersion), + Name: name, + PeerIp: peerIp, + SharedSecret: sharedSecret, + TargetVpnGateway: targetVpnGateway, + IkeVersion: int64(ikeVersion), + LocalTrafficSelector: localTrafficSelectors, } if v, ok := d.GetOk("description"); ok { diff --git a/website/source/docs/providers/google/r/compute_instance.html.markdown b/website/source/docs/providers/google/r/compute_instance.html.markdown index 1074d0117..109f2af9b 100644 --- a/website/source/docs/providers/google/r/compute_instance.html.markdown +++ b/website/source/docs/providers/google/r/compute_instance.html.markdown @@ -120,7 +120,12 @@ the type is "local-ssd", in which case scratch must be true). The `network_interface` block supports: -* `network` - (Required) The name of the network to attach this interface to. +* `network` - (Optional) The name 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 to. The subnetwork + must exist in the same region this instance is to be created in. Either `network` + or `subnetwork` must be provided. * `access_config` - (Optional) Access configurations, i.e. IPs via which this instance can be accessed via the Internet. Omit to ensure that the instance is not accessible from the Internet diff --git a/website/source/docs/providers/google/r/compute_network.html.markdown b/website/source/docs/providers/google/r/compute_network.html.markdown index 1306a0928..bd9820551 100644 --- a/website/source/docs/providers/google/r/compute_network.html.markdown +++ b/website/source/docs/providers/google/r/compute_network.html.markdown @@ -26,8 +26,17 @@ 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. -* `ipv4_range` - (Required) The IPv4 address range that machines in this - network are assigned to, represented as a CIDR block. +* `ipv4_range` - (Optional) The IPv4 address range that machines in this + network are assigned to, represented as a CIDR block. If not + set, an auto or custom subnetted network will be created, depending + on the value of `auto_create_subnetworks` attribute. + +* `auto_create_subnetworks` - (Optional) If set to true, this network + will be created in auto subnet mode, and Google will create a + subnet for each region automatically. + If set to false, and `ipv4_range` is not set, a custom subnetted + network will be created that can support `google_compute_subnetwork` + resources. ## Attributes Reference diff --git a/website/source/docs/providers/google/r/compute_subnetwork.html.markdown b/website/source/docs/providers/google/r/compute_subnetwork.html.markdown new file mode 100644 index 000000000..4bc373ec1 --- /dev/null +++ b/website/source/docs/providers/google/r/compute_subnetwork.html.markdown @@ -0,0 +1,47 @@ +--- +layout: "google" +page_title: "Google: google_compute_subnetwork" +sidebar_current: "docs-google-compute-subnetwork" +description: |- + Manages a subnetwork within GCE. +--- + +# google\_compute\_subnetwork + +Manages a subnetwork within GCE. + +## Example Usage + +``` +resource "google_compute_subnetwork" "default-us-east1" { + name = "default-us-east1" + ip_cidr_range = "10.0.0.0/16" + network = "${google_compute_network.default.self_link}" + region = "us-east1" +} +``` + +## Argument Reference + +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) A link to the parent network of this subnetwork. + The parent network must have been created in custom subnet mode. + +* `ip_cidr_range` - (Required) The IP address range that machines in this + network are assigned to, represented as a CIDR block. + +* `region` - (Required) The region this subnetwork will be created in. + +* `description` - (Optional) Description of this subnetwork. + +## Attributes Reference + +The following attributes are exported: + +* `name` - The name of the resource. +* `ip_cidr_range` - The CIDR block of this network. +* `gateway_address` - The IP address of the gateway.