forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_compute_subnetwork.go
193 lines (154 loc) · 4.31 KB
/
resource_compute_subnetwork.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package google
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)
func resourceComputeSubnetwork() *schema.Resource {
return &schema.Resource{
Create: resourceComputeSubnetworkCreate,
Read: resourceComputeSubnetworkRead,
Delete: resourceComputeSubnetworkDelete,
Schema: map[string]*schema.Schema{
"ip_cidr_range": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"network": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"gateway_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func createSubnetID(s *compute.Subnetwork) string {
return fmt.Sprintf("%s/%s", s.Region, s.Name)
}
func splitSubnetID(id string) (region string, name string) {
parts := strings.Split(id, "/")
region = parts[0]
name = parts[1]
return
}
func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region, err := getRegion(d, config)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
network, err := getNetworkLink(d, config, "network")
if err != nil {
return err
}
// Build the subnetwork parameters
subnetwork := &compute.Subnetwork{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
IpCidrRange: d.Get("ip_cidr_range").(string),
Network: network,
}
log.Printf("[DEBUG] Subnetwork insert request: %#v", subnetwork)
op, err := config.clientCompute.Subnetworks.Insert(
project, region, subnetwork).Do()
if err != nil {
return fmt.Errorf("Error creating subnetwork: %s", err)
}
// It probably maybe worked, so store the ID now. ID is a combination of region + subnetwork
// name because subnetwork names are not unique in a project, per the Google docs:
// "When creating a new subnetwork, its name has to be unique in that project for that region, even across networks.
// The same name can appear twice in a project, as long as each one is in a different region."
// https://cloud.google.com/compute/docs/subnetworks
subnetwork.Region = region
d.SetId(createSubnetID(subnetwork))
err = computeOperationWaitRegion(config, op, project, region, "Creating Subnetwork")
if err != nil {
return err
}
return resourceComputeSubnetworkRead(d, meta)
}
func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region, err := getRegion(d, config)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
subnetwork, err := config.clientCompute.Subnetworks.Get(
project, region, name).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
log.Printf("[WARN] Removing Subnetwork %q because it's gone", name)
// The resource doesn't exist anymore
d.SetId("")
return nil
}
return fmt.Errorf("Error reading subnetwork: %s", err)
}
d.Set("gateway_address", subnetwork.GatewayAddress)
d.Set("self_link", subnetwork.SelfLink)
return nil
}
func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region, err := getRegion(d, config)
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
// Delete the subnetwork
op, err := config.clientCompute.Subnetworks.Delete(
project, region, d.Get("name").(string)).Do()
if err != nil {
return fmt.Errorf("Error deleting subnetwork: %s", err)
}
err = computeOperationWaitRegion(config, op, project, region, "Deleting Subnetwork")
if err != nil {
return err
}
d.SetId("")
return nil
}