forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_compute_network.go
213 lines (176 loc) · 5.48 KB
/
resource_compute_network.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package google
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)
func resourceComputeNetwork() *schema.Resource {
return &schema.Resource{
Create: resourceComputeNetworkCreate,
Read: resourceComputeNetworkRead,
Update: resourceComputeNetworkUpdate,
Delete: resourceComputeNetworkDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"auto_create_subnetworks": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"routing_mode": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"gateway_ipv4": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ipv4_range": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
// This needs to remain deprecated until the API is retired
Deprecated: "Please use google_compute_subnetwork resources instead.",
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
//
// Possible modes:
// - 1 Legacy mode - Create a network in the legacy mode. ipv4_range is set. auto_create_subnetworks must not be
// set (enforced by ConflictsWith schema attribute)
// - 2 Distributed Mode - Create a new generation network that supports subnetworks:
// - 2.a - Auto subnet mode - auto_create_subnetworks = true, Google will generate 1 subnetwork per region
// - 2.b - Custom subnet mode - auto_create_subnetworks = false & ipv4_range not set,
//
autoCreateSubnetworks := d.Get("auto_create_subnetworks").(bool)
if autoCreateSubnetworks && d.Get("ipv4_range").(string) != "" {
return fmt.Errorf("ipv4_range can't be set if auto_create_subnetworks is true.")
}
// Build the network parameter
network := &compute.Network{
Name: d.Get("name").(string),
AutoCreateSubnetworks: autoCreateSubnetworks,
Description: d.Get("description").(string),
}
if v, ok := d.GetOk("routing_mode"); ok {
routingConfig := &compute.NetworkRoutingConfig{
RoutingMode: v.(string),
}
network.RoutingConfig = routingConfig
}
if v, ok := d.GetOk("ipv4_range"); ok {
log.Printf("[DEBUG] Setting IPv4Range (%#v) for legacy network mode", v.(string))
network.IPv4Range = v.(string)
} else {
// custom subnet mode, so make sure AutoCreateSubnetworks field is included in request otherwise
// google will create a network in legacy mode.
network.ForceSendFields = []string{"AutoCreateSubnetworks"}
}
log.Printf("[DEBUG] Network insert request: %#v", network)
op, err := config.clientCompute.Networks.Insert(
project, network).Do()
if err != nil {
return fmt.Errorf("Error creating network: %s", err)
}
// It probably maybe worked, so store the ID now
d.SetId(network.Name)
err = computeOperationWait(config.clientCompute, op, project, "Creating Network")
if err != nil {
return err
}
return resourceComputeNetworkRead(d, meta)
}
func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
network, err := config.clientCompute.Networks.Get(
project, d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Network %q", d.Get("name").(string)))
}
routingConfig := network.RoutingConfig
d.Set("routing_mode", routingConfig.RoutingMode)
d.Set("gateway_ipv4", network.GatewayIPv4)
d.Set("ipv4_range", network.IPv4Range)
d.Set("self_link", network.SelfLink)
d.Set("name", network.Name)
d.Set("description", network.Description)
d.Set("auto_create_subnetworks", network.AutoCreateSubnetworks)
d.Set("project", project)
return nil
}
func resourceComputeNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
op, err := config.clientCompute.Networks.Patch(project, d.Id(), &compute.Network{
RoutingConfig: &compute.NetworkRoutingConfig{
RoutingMode: d.Get("routing_mode").(string),
},
}).Do()
if err != nil {
return fmt.Errorf("Error updating network: %s", err)
}
err = computeSharedOperationWait(config.clientCompute, op, project, "UpdateNetwork")
if err != nil {
return err
}
return resourceComputeNetworkRead(d, meta)
}
func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
return deleteComputeNetwork(project, d.Id(), config)
}
func deleteComputeNetwork(project, network string, config *Config) error {
op, err := config.clientCompute.Networks.Delete(
project, network).Do()
if err != nil {
return fmt.Errorf("Error deleting network: %s", err)
}
err = computeOperationWaitTime(config.clientCompute, op, project, "Deleting Network", 10)
if err != nil {
return err
}
return nil
}