Skip to content

Commit df568b1

Browse files
modular-magicianrosbo
authored andcommitted
Release generated GlobalAddress (#1379)
1 parent 949b33a commit df568b1

4 files changed

+228
-53
lines changed
+159-35
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,76 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Magic Modules and manual
8+
// changes will be clobbered when the file is regenerated.
9+
//
10+
// Please read more about how to change this file in
11+
// .github/CONTRIBUTING.md.
12+
//
13+
// ----------------------------------------------------------------------------
14+
115
package google
216

317
import (
418
"fmt"
519
"log"
20+
"time"
621

722
"github.com/hashicorp/terraform/helper/schema"
823
"github.com/hashicorp/terraform/helper/validation"
9-
10-
"google.golang.org/api/compute/v1"
24+
compute "google.golang.org/api/compute/v1"
1125
)
1226

1327
func resourceComputeGlobalAddress() *schema.Resource {
1428
return &schema.Resource{
1529
Create: resourceComputeGlobalAddressCreate,
1630
Read: resourceComputeGlobalAddressRead,
1731
Delete: resourceComputeGlobalAddressDelete,
32+
1833
Importer: &schema.ResourceImporter{
19-
State: schema.ImportStatePassthrough,
34+
State: resourceComputeGlobalAddressImport,
35+
},
36+
37+
Timeouts: &schema.ResourceTimeout{
38+
Create: schema.DefaultTimeout(240 * time.Second),
39+
Delete: schema.DefaultTimeout(240 * time.Second),
2040
},
2141

2242
Schema: map[string]*schema.Schema{
23-
"name": &schema.Schema{
43+
"name": {
2444
Type: schema.TypeString,
2545
Required: true,
2646
ForceNew: true,
2747
},
28-
29-
"ip_version": &schema.Schema{
48+
"description": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
ForceNew: true,
52+
},
53+
"ip_version": {
3054
Type: schema.TypeString,
3155
Optional: true,
3256
ForceNew: true,
3357
ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV6", ""}, false),
3458
},
35-
36-
"project": &schema.Schema{
59+
"address": {
3760
Type: schema.TypeString,
38-
Optional: true,
3961
Computed: true,
40-
ForceNew: true,
4162
},
42-
43-
"address": &schema.Schema{
63+
"creation_timestamp": {
4464
Type: schema.TypeString,
4565
Computed: true,
4666
},
47-
48-
"self_link": &schema.Schema{
67+
"project": {
68+
Type: schema.TypeString,
69+
Optional: true,
70+
Computed: true,
71+
ForceNew: true,
72+
},
73+
"self_link": {
4974
Type: schema.TypeString,
5075
Computed: true,
5176
},
@@ -61,25 +86,59 @@ func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}
6186
return err
6287
}
6388

64-
// Build the address parameter
65-
addr := &compute.Address{
66-
Name: d.Get("name").(string),
67-
IpVersion: d.Get("ip_version").(string),
89+
descriptionProp, err := expandComputeGlobalAddressDescription(d.Get("description"), d, config)
90+
if err != nil {
91+
return err
92+
}
93+
nameProp, err := expandComputeGlobalAddressName(d.Get("name"), d, config)
94+
if err != nil {
95+
return err
96+
}
97+
ipVersionProp, err := expandComputeGlobalAddressIpVersion(d.Get("ip_version"), d, config)
98+
if err != nil {
99+
return err
68100
}
69101

70-
op, err := config.clientCompute.GlobalAddresses.Insert(project, addr).Do()
102+
obj := map[string]interface{}{
103+
"description": descriptionProp,
104+
"name": nameProp,
105+
"ipVersion": ipVersionProp,
106+
}
107+
108+
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses")
71109
if err != nil {
72-
return fmt.Errorf("Error creating address: %s", err)
110+
return err
73111
}
74112

75-
// It probably maybe worked, so store the ID now
76-
d.SetId(addr.Name)
113+
log.Printf("[DEBUG] Creating new GlobalAddress: %#v", obj)
114+
res, err := Post(config, url, obj)
115+
if err != nil {
116+
return fmt.Errorf("Error creating GlobalAddress: %s", err)
117+
}
77118

78-
err = computeSharedOperationWait(config.clientCompute, op, project, "Creating Global Address")
119+
// Store the ID now
120+
id, err := replaceVars(d, config, "{{name}}")
121+
if err != nil {
122+
return fmt.Errorf("Error constructing id: %s", err)
123+
}
124+
d.SetId(id)
125+
126+
op := &compute.Operation{}
127+
err = Convert(res, op)
79128
if err != nil {
80129
return err
81130
}
82131

132+
waitErr := computeOperationWaitTime(
133+
config.clientCompute, op, project, "Creating GlobalAddress",
134+
int(d.Timeout(schema.TimeoutCreate).Minutes()))
135+
136+
if waitErr != nil {
137+
// The resource didn't actually create
138+
d.SetId("")
139+
return waitErr
140+
}
141+
83142
return resourceComputeGlobalAddressRead(d, meta)
84143
}
85144

@@ -91,16 +150,23 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{})
91150
return err
92151
}
93152

94-
addr, err := config.clientCompute.GlobalAddresses.Get(project, d.Id()).Do()
153+
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}")
154+
if err != nil {
155+
return err
156+
}
157+
158+
res, err := Get(config, url)
95159
if err != nil {
96-
return handleNotFoundError(err, d, fmt.Sprintf("Global Address %q", d.Get("name").(string)))
160+
return handleNotFoundError(err, d, fmt.Sprintf("ComputeGlobalAddress %q", d.Id()))
97161
}
98162

99-
d.Set("name", addr.Name)
100-
d.Set("ip_version", addr.IpVersion)
101-
d.Set("address", addr.Address)
163+
d.Set("address", flattenComputeGlobalAddressAddress(res["address"]))
164+
d.Set("creation_timestamp", flattenComputeGlobalAddressCreationTimestamp(res["creationTimestamp"]))
165+
d.Set("description", flattenComputeGlobalAddressDescription(res["description"]))
166+
d.Set("name", flattenComputeGlobalAddressName(res["name"]))
167+
d.Set("ip_version", flattenComputeGlobalAddressIpVersion(res["ipVersion"]))
168+
d.Set("self_link", res["selfLink"])
102169
d.Set("project", project)
103-
d.Set("self_link", ConvertSelfLinkToV1(addr.SelfLink))
104170

105171
return nil
106172
}
@@ -113,18 +179,76 @@ func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}
113179
return err
114180
}
115181

116-
// Delete the address
117-
log.Printf("[DEBUG] address delete request")
118-
op, err := config.clientCompute.GlobalAddresses.Delete(project, d.Id()).Do()
182+
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}")
119183
if err != nil {
120-
return fmt.Errorf("Error deleting address: %s", err)
184+
return err
121185
}
122186

123-
err = computeSharedOperationWait(config.clientCompute, op, project, "Deleting Global Address")
187+
log.Printf("[DEBUG] Deleting GlobalAddress %q", d.Id())
188+
res, err := Delete(config, url)
189+
if err != nil {
190+
return fmt.Errorf("Error deleting GlobalAddress %q: %s", d.Id(), err)
191+
}
192+
193+
op := &compute.Operation{}
194+
err = Convert(res, op)
195+
if err != nil {
196+
return err
197+
}
198+
199+
err = computeOperationWaitTime(
200+
config.clientCompute, op, project, "Deleting GlobalAddress",
201+
int(d.Timeout(schema.TimeoutDelete).Minutes()))
202+
124203
if err != nil {
125204
return err
126205
}
127206

128-
d.SetId("")
129207
return nil
130208
}
209+
210+
func resourceComputeGlobalAddressImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
211+
config := meta.(*Config)
212+
parseImportId([]string{"projects/(?P<project>[^/]+)/global/addresses/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config)
213+
214+
// Replace import id for the resource id
215+
id, err := replaceVars(d, config, "{{name}}")
216+
if err != nil {
217+
return nil, fmt.Errorf("Error constructing id: %s", err)
218+
}
219+
d.SetId(id)
220+
221+
return []*schema.ResourceData{d}, nil
222+
}
223+
224+
func flattenComputeGlobalAddressAddress(v interface{}) interface{} {
225+
return v
226+
}
227+
228+
func flattenComputeGlobalAddressCreationTimestamp(v interface{}) interface{} {
229+
return v
230+
}
231+
232+
func flattenComputeGlobalAddressDescription(v interface{}) interface{} {
233+
return v
234+
}
235+
236+
func flattenComputeGlobalAddressName(v interface{}) interface{} {
237+
return v
238+
}
239+
240+
func flattenComputeGlobalAddressIpVersion(v interface{}) interface{} {
241+
return v
242+
}
243+
244+
func expandComputeGlobalAddressDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
245+
return v, nil
246+
}
247+
248+
func expandComputeGlobalAddressName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
249+
return v, nil
250+
}
251+
252+
func expandComputeGlobalAddressIpVersion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
253+
return v, nil
254+
}

google/resource_compute_global_address_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,15 @@ func testAccComputeGlobalAddress_basic() string {
144144
return fmt.Sprintf(`
145145
resource "google_compute_global_address" "foobar" {
146146
name = "address-test-%s"
147+
description = "Created for Terraform acceptance testing"
147148
}`, acctest.RandString(10))
148149
}
149150

150151
func testAccComputeGlobalAddress_ipv6() string {
151152
return fmt.Sprintf(`
152153
resource "google_compute_global_address" "foobar" {
153154
name = "address-test-%s"
155+
description = "Created for Terraform acceptance testing"
154156
ip_version = "IPV6"
155157
}`, acctest.RandString(10))
156158
}

google/resource_compute_target_ssl_proxy.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/hashicorp/terraform/helper/schema"
24+
"github.com/hashicorp/terraform/helper/validation"
2425
compute "google.golang.org/api/compute/v1"
2526
)
2627

@@ -67,9 +68,10 @@ func resourceComputeTargetSslProxy() *schema.Resource {
6768
ForceNew: true,
6869
},
6970
"proxy_header": {
70-
Type: schema.TypeString,
71-
Optional: true,
72-
Default: "NONE",
71+
Type: schema.TypeString,
72+
Optional: true,
73+
ValidateFunc: validation.StringInSlice([]string{"NONE", "PROXY_V1", ""}, false),
74+
Default: "NONE",
7375
},
7476
"creation_timestamp": {
7577
Type: schema.TypeString,

0 commit comments

Comments
 (0)