Skip to content

Commit 430ed48

Browse files
sl1pm4tjen20
authored andcommitted
Update google resources where necessary to make use of subnetworks, update som docs
1 parent aedc5ba commit 430ed48

9 files changed

Lines changed: 142 additions & 21 deletions

builtin/providers/google/provider.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,11 @@ func validateCredentials(v interface{}, k string) (warnings []string, errors []e
145145

146146
return
147147
}
148+
149+
func getRegionFromZone(zone string) string {
150+
if zone != "" && len(zone) > 2 {
151+
region := zone[:len(zone)-2]
152+
return region
153+
}
154+
return ""
155+
}

builtin/providers/google/resource_compute_instance.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,13 @@ func resourceComputeInstance() *schema.Resource {
111111
Schema: map[string]*schema.Schema{
112112
"network": &schema.Schema{
113113
Type: schema.TypeString,
114-
Required: true,
114+
Optional: true,
115+
ForceNew: true,
116+
},
117+
118+
"subnetwork": &schema.Schema{
119+
Type: schema.TypeString,
120+
Optional: true,
115121
ForceNew: true,
116122
},
117123

@@ -445,17 +451,36 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
445451
prefix := fmt.Sprintf("network_interface.%d", i)
446452
// Load up the name of this network_interfac
447453
networkName := d.Get(prefix + ".network").(string)
448-
network, err := config.clientCompute.Networks.Get(
449-
config.Project, networkName).Do()
450-
if err != nil {
451-
return fmt.Errorf(
452-
"Error referencing network '%s': %s",
453-
networkName, err)
454+
subnetworkName := d.Get(prefix + ".subnetwork").(string)
455+
var networkLink, subnetworkLink string
456+
457+
if networkName != "" && subnetworkName != "" {
458+
return fmt.Errorf("Cannot specify both network and subnetwork values.")
459+
} else if networkName != "" {
460+
network, err := config.clientCompute.Networks.Get(
461+
config.Project, networkName).Do()
462+
if err != nil {
463+
return fmt.Errorf(
464+
"Error referencing network '%s': %s",
465+
networkName, err)
466+
}
467+
networkLink = network.SelfLink
468+
} else {
469+
region := getRegionFromZone(d.Get("zone").(string))
470+
subnetwork, err := config.clientCompute.Subnetworks.Get(
471+
config.Project, region, subnetworkName).Do()
472+
if err != nil {
473+
return fmt.Errorf(
474+
"Error referencing subnetwork '%s' in region '%s': %s",
475+
subnetworkName, region, err)
476+
}
477+
subnetworkLink = subnetwork.SelfLink
454478
}
455479

456480
// Build the networkInterface
457481
var iface compute.NetworkInterface
458-
iface.Network = network.SelfLink
482+
iface.Network = networkLink
483+
iface.Subnetwork = subnetworkLink
459484

460485
// Handle access_config structs
461486
accessConfigsCount := d.Get(prefix + ".access_config.#").(int)

builtin/providers/google/resource_compute_instance_template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ func resourceComputeInstanceTemplate() *schema.Resource {
141141
ForceNew: true,
142142
},
143143

144+
"subnetwork": &schema.Schema{
145+
Type: schema.TypeString,
146+
Optional: true,
147+
ForceNew: true,
148+
},
149+
144150
"access_config": &schema.Schema{
145151
Type: schema.TypeList,
146152
Optional: true,
@@ -337,9 +343,12 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) (error, []*compute.
337343
source += v.(string)
338344
}
339345

346+
subnetworkLink := d.Get("subnetwork").(string)
347+
340348
// Build the networkInterface
341349
var iface compute.NetworkInterface
342350
iface.Network = source
351+
iface.Subnetwork = subnetworkLink
343352

344353
accessConfigsCount := d.Get(prefix + ".access_config.#").(int)
345354
iface.AccessConfigs = make([]*compute.AccessConfig, accessConfigsCount)

builtin/providers/google/resource_compute_network.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ func resourceComputeNetwork() *schema.Resource {
2424
},
2525

2626
"ipv4_range": &schema.Schema{
27-
Type: schema.TypeString,
28-
Optional: true,
29-
ForceNew: true,
27+
Type: schema.TypeString,
28+
Optional: true,
29+
ForceNew: true,
30+
Deprecated: "Please use custom subnetworks instead",
3031
},
3132

3233
"gateway_ipv4": &schema.Schema{

builtin/providers/google/resource_compute_subnetwork.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func resourceComputeSubnetwork() *schema.Resource {
3434
ForceNew: true,
3535
},
3636

37-
"ipCidrRange": &schema.Schema{
37+
"ip_cidr_range": &schema.Schema{
3838
Type: schema.TypeString,
3939
Required: true,
4040
ForceNew: true,
@@ -70,7 +70,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e
7070
subnetwork := &compute.Subnetwork{
7171
Name: d.Get("name").(string),
7272
Description: d.Get("description").(string),
73-
IpCidrRange: d.Get("ipCidrRange").(string),
73+
IpCidrRange: d.Get("ip_cidr_range").(string),
7474
Network: d.Get("network").(string),
7575
}
7676
region := d.Get("region").(string)

builtin/providers/google/resource_compute_vpn_tunnel.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ func resourceComputeVpnTunnel() *schema.Resource {
5555
Default: 2,
5656
ForceNew: true,
5757
},
58+
"local_traffic_selector": &schema.Schema{
59+
Type: schema.TypeSet,
60+
Optional: true,
61+
ForceNew: true,
62+
Elem: &schema.Schema{Type: schema.TypeString},
63+
Set: schema.HashString,
64+
},
5865
"detailed_status": &schema.Schema{
5966
Type: schema.TypeString,
6067
Computed: true,
@@ -82,14 +89,24 @@ func resourceComputeVpnTunnelCreate(d *schema.ResourceData, meta interface{}) er
8289
return fmt.Errorf("Only IKE version 1 or 2 supported, not %d", ikeVersion)
8390
}
8491

92+
// Build up the list of sources
93+
var localTrafficSelectors []string
94+
if v := d.Get("local_traffic_selector").(*schema.Set); v.Len() > 0 {
95+
localTrafficSelectors = make([]string, v.Len())
96+
for i, v := range v.List() {
97+
localTrafficSelectors[i] = v.(string)
98+
}
99+
}
100+
85101
vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute)
86102

87103
vpnTunnel := &compute.VpnTunnel{
88-
Name: name,
89-
PeerIp: peerIp,
90-
SharedSecret: sharedSecret,
91-
TargetVpnGateway: targetVpnGateway,
92-
IkeVersion: int64(ikeVersion),
104+
Name: name,
105+
PeerIp: peerIp,
106+
SharedSecret: sharedSecret,
107+
TargetVpnGateway: targetVpnGateway,
108+
IkeVersion: int64(ikeVersion),
109+
LocalTrafficSelector: localTrafficSelectors,
93110
}
94111

95112
if v, ok := d.GetOk("description"); ok {

website/source/docs/providers/google/r/compute_instance.html.markdown

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ the type is "local-ssd", in which case scratch must be true).
120120

121121
The `network_interface` block supports:
122122

123-
* `network` - (Required) The name of the network to attach this interface to.
123+
* `network` - (Optional) The name of the network to attach this interface to. Either
124+
`network` or `subnetwork` must be provided.
125+
126+
* `subnetwork` - (Optional) the name of the subnetwork to attach this interface to. The subnetwork
127+
must exist in the same region this instance is to be created in. Either `network`
128+
or `subnetwork` must be provided.
124129

125130
* `access_config` - (Optional) Access configurations, i.e. IPs via which this instance can be
126131
accessed via the Internet. Omit to ensure that the instance is not accessible from the Internet

website/source/docs/providers/google/r/compute_network.html.markdown

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@ The following arguments are supported:
2626
* `name` - (Required) A unique name for the resource, required by GCE.
2727
Changing this forces a new resource to be created.
2828

29-
* `ipv4_range` - (Required) The IPv4 address range that machines in this
30-
network are assigned to, represented as a CIDR block.
29+
* `ipv4_range` - (Optional) The IPv4 address range that machines in this
30+
network are assigned to, represented as a CIDR block. If not
31+
set, an auto or custom subnetted network will be created, depending
32+
on the value of `auto_create_subnetworks` attribute.
33+
34+
* `auto_create_subnetworks` - (Optional) If set to true, this network
35+
will be created in auto subnet mode, and Google will create a
36+
subnet for each region automatically.
37+
If set to false, and `ipv4_range` is not set, a custom subnetted
38+
network will be created that can support `google_compute_subnetwork`
39+
resources.
3140

3241
## Attributes Reference
3342

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_compute_subnetwork"
4+
sidebar_current: "docs-google-compute-subnetwork"
5+
description: |-
6+
Manages a subnetwork within GCE.
7+
---
8+
9+
# google\_compute\_subnetwork
10+
11+
Manages a subnetwork within GCE.
12+
13+
## Example Usage
14+
15+
```
16+
resource "google_compute_subnetwork" "default-us-east1" {
17+
name = "default-us-east1"
18+
ip_cidr_range = "10.0.0.0/16"
19+
network = "${google_compute_network.default.self_link}"
20+
region = "us-east1"
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
The following arguments are supported:
27+
28+
* `name` - (Required) A unique name for the resource, required by GCE.
29+
Changing this forces a new resource to be created.
30+
31+
* `network` - (Required) A link to the parent network of this subnetwork.
32+
The parent network must have been created in custom subnet mode.
33+
34+
* `ip_cidr_range` - (Required) The IP address range that machines in this
35+
network are assigned to, represented as a CIDR block.
36+
37+
* `region` - (Required) The region this subnetwork will be created in.
38+
39+
* `description` - (Optional) Description of this subnetwork.
40+
41+
## Attributes Reference
42+
43+
The following attributes are exported:
44+
45+
* `name` - The name of the resource.
46+
* `ip_cidr_range` - The CIDR block of this network.
47+
* `gateway_address` - The IP address of the gateway.

0 commit comments

Comments
 (0)