Update google resources where necessary to make use of subnetworks, update som docs

This commit is contained in:
Matt Morrison 2016-02-15 16:17:55 +13:00 committed by James Nugent
parent aedc5ba4af
commit 430ed48a44
9 changed files with 142 additions and 21 deletions

View File

@ -145,3 +145,11 @@ func validateCredentials(v interface{}, k string) (warnings []string, errors []e
return return
} }
func getRegionFromZone(zone string) string {
if zone != "" && len(zone) > 2 {
region := zone[:len(zone)-2]
return region
}
return ""
}

View File

@ -111,7 +111,13 @@ func resourceComputeInstance() *schema.Resource {
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"network": &schema.Schema{ "network": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
ForceNew: true,
},
"subnetwork": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true, ForceNew: true,
}, },
@ -445,6 +451,12 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
prefix := fmt.Sprintf("network_interface.%d", i) prefix := fmt.Sprintf("network_interface.%d", i)
// Load up the name of this network_interfac // Load up the name of this network_interfac
networkName := d.Get(prefix + ".network").(string) networkName := d.Get(prefix + ".network").(string)
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( network, err := config.clientCompute.Networks.Get(
config.Project, networkName).Do() config.Project, networkName).Do()
if err != nil { if err != nil {
@ -452,10 +464,23 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
"Error referencing network '%s': %s", "Error referencing network '%s': %s",
networkName, err) 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 // Build the networkInterface
var iface compute.NetworkInterface var iface compute.NetworkInterface
iface.Network = network.SelfLink iface.Network = networkLink
iface.Subnetwork = subnetworkLink
// Handle access_config structs // Handle access_config structs
accessConfigsCount := d.Get(prefix + ".access_config.#").(int) accessConfigsCount := d.Get(prefix + ".access_config.#").(int)

View File

@ -141,6 +141,12 @@ func resourceComputeInstanceTemplate() *schema.Resource {
ForceNew: true, ForceNew: true,
}, },
"subnetwork": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"access_config": &schema.Schema{ "access_config": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Optional: true, Optional: true,
@ -337,9 +343,12 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) (error, []*compute.
source += v.(string) source += v.(string)
} }
subnetworkLink := d.Get("subnetwork").(string)
// Build the networkInterface // Build the networkInterface
var iface compute.NetworkInterface var iface compute.NetworkInterface
iface.Network = source iface.Network = source
iface.Subnetwork = subnetworkLink
accessConfigsCount := d.Get(prefix + ".access_config.#").(int) accessConfigsCount := d.Get(prefix + ".access_config.#").(int)
iface.AccessConfigs = make([]*compute.AccessConfig, accessConfigsCount) iface.AccessConfigs = make([]*compute.AccessConfig, accessConfigsCount)

View File

@ -27,6 +27,7 @@ func resourceComputeNetwork() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
Deprecated: "Please use custom subnetworks instead",
}, },
"gateway_ipv4": &schema.Schema{ "gateway_ipv4": &schema.Schema{

View File

@ -34,7 +34,7 @@ func resourceComputeSubnetwork() *schema.Resource {
ForceNew: true, ForceNew: true,
}, },
"ipCidrRange": &schema.Schema{ "ip_cidr_range": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
@ -70,7 +70,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
subnetwork := &compute.Subnetwork{ subnetwork := &compute.Subnetwork{
Name: d.Get("name").(string), Name: d.Get("name").(string),
Description: d.Get("description").(string), Description: d.Get("description").(string),
IpCidrRange: d.Get("ipCidrRange").(string), IpCidrRange: d.Get("ip_cidr_range").(string),
Network: d.Get("network").(string), Network: d.Get("network").(string),
} }
region := d.Get("region").(string) region := d.Get("region").(string)

View File

@ -55,6 +55,13 @@ func resourceComputeVpnTunnel() *schema.Resource {
Default: 2, Default: 2,
ForceNew: true, 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{ "detailed_status": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
@ -82,6 +89,15 @@ func resourceComputeVpnTunnelCreate(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("Only IKE version 1 or 2 supported, not %d", ikeVersion) 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) vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
vpnTunnel := &compute.VpnTunnel{ vpnTunnel := &compute.VpnTunnel{
@ -90,6 +106,7 @@ func resourceComputeVpnTunnelCreate(d *schema.ResourceData, meta interface{}) er
SharedSecret: sharedSecret, SharedSecret: sharedSecret,
TargetVpnGateway: targetVpnGateway, TargetVpnGateway: targetVpnGateway,
IkeVersion: int64(ikeVersion), IkeVersion: int64(ikeVersion),
LocalTrafficSelector: localTrafficSelectors,
} }
if v, ok := d.GetOk("description"); ok { if v, ok := d.GetOk("description"); ok {

View File

@ -120,7 +120,12 @@ the type is "local-ssd", in which case scratch must be true).
The `network_interface` block supports: 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 * `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 accessed via the Internet. Omit to ensure that the instance is not accessible from the Internet

View File

@ -26,8 +26,17 @@ The following arguments are supported:
* `name` - (Required) A unique name for the resource, required by GCE. * `name` - (Required) A unique name for the resource, required by GCE.
Changing this forces a new resource to be created. Changing this forces a new resource to be created.
* `ipv4_range` - (Required) The IPv4 address range that machines in this * `ipv4_range` - (Optional) The IPv4 address range that machines in this
network are assigned to, represented as a CIDR block. 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 ## Attributes Reference

View File

@ -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.