Skip to content

Commit 9653fe6

Browse files
rosbomodular-magician
authored andcommitted
Generate google_target_http_proxy using MagicModule
1 parent 31a5c9d commit 9653fe6

File tree

2 files changed

+267
-79
lines changed

2 files changed

+267
-79
lines changed
+206-59
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,77 @@
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"
620
"strconv"
21+
"time"
722

823
"github.com/hashicorp/terraform/helper/schema"
9-
"google.golang.org/api/compute/v1"
24+
compute "google.golang.org/api/compute/v1"
1025
)
1126

1227
func resourceComputeTargetHttpProxy() *schema.Resource {
1328
return &schema.Resource{
1429
Create: resourceComputeTargetHttpProxyCreate,
1530
Read: resourceComputeTargetHttpProxyRead,
16-
Delete: resourceComputeTargetHttpProxyDelete,
1731
Update: resourceComputeTargetHttpProxyUpdate,
32+
Delete: resourceComputeTargetHttpProxyDelete,
1833

1934
Importer: &schema.ResourceImporter{
20-
State: schema.ImportStatePassthrough,
35+
State: resourceComputeTargetHttpProxyImport,
36+
},
37+
38+
Timeouts: &schema.ResourceTimeout{
39+
Create: schema.DefaultTimeout(240 * time.Second),
40+
Update: schema.DefaultTimeout(240 * time.Second),
41+
Delete: schema.DefaultTimeout(240 * time.Second),
2142
},
2243

2344
Schema: map[string]*schema.Schema{
24-
"name": &schema.Schema{
45+
"name": {
2546
Type: schema.TypeString,
2647
Required: true,
2748
ForceNew: true,
2849
},
29-
30-
"url_map": &schema.Schema{
50+
"url_map": {
3151
Type: schema.TypeString,
3252
Required: true,
33-
DiffSuppressFunc: compareSelfLinkRelativePaths,
53+
DiffSuppressFunc: compareSelfLinkOrResourceName,
3454
},
35-
36-
"description": &schema.Schema{
55+
"description": {
3756
Type: schema.TypeString,
3857
Optional: true,
3958
ForceNew: true,
4059
},
41-
42-
"proxy_id": &schema.Schema{
60+
"creation_timestamp": {
4361
Type: schema.TypeString,
4462
Computed: true,
4563
},
46-
47-
"project": &schema.Schema{
64+
"proxy_id": {
65+
Type: schema.TypeInt,
66+
Computed: true,
67+
},
68+
"project": {
4869
Type: schema.TypeString,
4970
Optional: true,
5071
Computed: true,
5172
ForceNew: true,
5273
},
53-
54-
"self_link": &schema.Schema{
74+
"self_link": {
5575
Type: schema.TypeString,
5676
Computed: true,
5777
},
@@ -67,32 +87,91 @@ func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface
6787
return err
6888
}
6989

70-
proxy := &compute.TargetHttpProxy{
71-
Name: d.Get("name").(string),
72-
UrlMap: d.Get("url_map").(string),
90+
descriptionProp, err := expandComputeTargetHttpProxyDescription(d.Get("description"), d, config)
91+
if err != nil {
92+
return err
93+
}
94+
nameProp, err := expandComputeTargetHttpProxyName(d.Get("name"), d, config)
95+
if err != nil {
96+
return err
97+
}
98+
urlMapProp, err := expandComputeTargetHttpProxyUrlMap(d.Get("url_map"), d, config)
99+
if err != nil {
100+
return err
73101
}
74102

75-
if v, ok := d.GetOk("description"); ok {
76-
proxy.Description = v.(string)
103+
obj := map[string]interface{}{
104+
"description": descriptionProp,
105+
"name": nameProp,
106+
"urlMap": urlMapProp,
77107
}
78108

79-
log.Printf("[DEBUG] TargetHttpProxy insert request: %#v", proxy)
80-
op, err := config.clientCompute.TargetHttpProxies.Insert(
81-
project, proxy).Do()
109+
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpProxies")
110+
if err != nil {
111+
return err
112+
}
113+
114+
log.Printf("[DEBUG] Creating new TargetHttpProxy: %#v", obj)
115+
res, err := Post(config, url, obj)
82116
if err != nil {
83117
return fmt.Errorf("Error creating TargetHttpProxy: %s", err)
84118
}
85119

86-
err = computeOperationWait(config.clientCompute, op, project, "Creating Target Http Proxy")
120+
// Store the ID now
121+
id, err := replaceVars(d, config, "{{name}}")
122+
if err != nil {
123+
return fmt.Errorf("Error constructing id: %s", err)
124+
}
125+
d.SetId(id)
126+
127+
op := &compute.Operation{}
128+
err = Convert(res, op)
87129
if err != nil {
88130
return err
89131
}
90132

91-
d.SetId(proxy.Name)
133+
waitErr := computeOperationWaitTime(
134+
config.clientCompute, op, project, "Creating TargetHttpProxy",
135+
int(d.Timeout(schema.TimeoutCreate).Minutes()))
136+
137+
if waitErr != nil {
138+
// The resource didn't actually create
139+
d.SetId("")
140+
return waitErr
141+
}
92142

93143
return resourceComputeTargetHttpProxyRead(d, meta)
94144
}
95145

146+
func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}) error {
147+
config := meta.(*Config)
148+
149+
project, err := getProject(d, config)
150+
if err != nil {
151+
return err
152+
}
153+
154+
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpProxies/{{name}}")
155+
if err != nil {
156+
return err
157+
}
158+
159+
res, err := Get(config, url)
160+
if err != nil {
161+
return handleNotFoundError(err, d, fmt.Sprintf("ComputeTargetHttpProxy %q", d.Id()))
162+
}
163+
164+
d.Set("creation_timestamp", flattenComputeTargetHttpProxyCreationTimestamp(res["creationTimestamp"]))
165+
d.Set("description", flattenComputeTargetHttpProxyDescription(res["description"]))
166+
d.Set("proxy_id", flattenComputeTargetHttpProxyProxyId(res["id"]))
167+
d.Set("name", flattenComputeTargetHttpProxyName(res["name"]))
168+
d.Set("url_map", flattenComputeTargetHttpProxyUrlMap(res["urlMap"]))
169+
d.Set("self_link", res["selfLink"])
170+
d.Set("project", project)
171+
172+
return nil
173+
}
174+
96175
func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface{}) error {
97176
config := meta.(*Config)
98177

@@ -101,75 +180,143 @@ func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface
101180
return err
102181
}
103182

104-
d.Partial(true)
183+
var url string
184+
var res map[string]interface{}
185+
op := &compute.Operation{}
105186

106187
if d.HasChange("url_map") {
107-
url_map := d.Get("url_map").(string)
108-
url_map_ref := &compute.UrlMapReference{UrlMap: url_map}
109-
op, err := config.clientCompute.TargetHttpProxies.SetUrlMap(
110-
project, d.Id(), url_map_ref).Do()
188+
descriptionProp, err := expandComputeTargetHttpProxyDescription(d.Get("description"), d, config)
189+
if err != nil {
190+
return err
191+
}
192+
nameProp, err := expandComputeTargetHttpProxyName(d.Get("name"), d, config)
111193
if err != nil {
112-
return fmt.Errorf("Error updating target: %s", err)
194+
return err
195+
}
196+
urlMapProp, err := expandComputeTargetHttpProxyUrlMap(d.Get("url_map"), d, config)
197+
if err != nil {
198+
return err
113199
}
114200

115-
err = computeOperationWait(config.clientCompute, op, project, "Updating Target Http Proxy")
201+
obj := map[string]interface{}{
202+
"description": descriptionProp,
203+
"name": nameProp,
204+
"urlMap": urlMapProp,
205+
}
206+
url, err = replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/targetHttpProxies/{{name}}/setUrlMap")
116207
if err != nil {
117208
return err
118209
}
210+
res, err = sendRequest(config, "POST", url, obj)
211+
if err != nil {
212+
return fmt.Errorf("Error updating TargetHttpProxy %q: %s", d.Id(), err)
213+
}
119214

120-
d.SetPartial("url_map")
121-
}
215+
err = Convert(res, op)
216+
if err != nil {
217+
return err
218+
}
122219

123-
d.Partial(false)
220+
err = computeOperationWaitTime(
221+
config.clientCompute, op, project, "Updating TargetHttpProxy",
222+
int(d.Timeout(schema.TimeoutUpdate).Minutes()))
223+
224+
if err != nil {
225+
return err
226+
}
227+
}
124228

125229
return resourceComputeTargetHttpProxyRead(d, meta)
126230
}
127231

128-
func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}) error {
232+
func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error {
129233
config := meta.(*Config)
130234

131235
project, err := getProject(d, config)
132236
if err != nil {
133237
return err
134238
}
135239

136-
proxy, err := config.clientCompute.TargetHttpProxies.Get(
137-
project, d.Id()).Do()
240+
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpProxies/{{name}}")
138241
if err != nil {
139-
return handleNotFoundError(err, d, fmt.Sprintf("Target HTTP Proxy %q", d.Get("name").(string)))
242+
return err
140243
}
141244

142-
d.Set("self_link", proxy.SelfLink)
143-
d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10))
144-
d.Set("description", proxy.Description)
145-
d.Set("url_map", proxy.UrlMap)
146-
d.Set("name", proxy.Name)
147-
d.Set("project", project)
245+
log.Printf("[DEBUG] Deleting TargetHttpProxy %q", d.Id())
246+
res, err := Delete(config, url)
247+
if err != nil {
248+
return fmt.Errorf("Error deleting TargetHttpProxy %q: %s", d.Id(), err)
249+
}
250+
251+
op := &compute.Operation{}
252+
err = Convert(res, op)
253+
if err != nil {
254+
return err
255+
}
256+
257+
err = computeOperationWaitTime(
258+
config.clientCompute, op, project, "Deleting TargetHttpProxy",
259+
int(d.Timeout(schema.TimeoutDelete).Minutes()))
260+
261+
if err != nil {
262+
return err
263+
}
148264

149265
return nil
150266
}
151267

152-
func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error {
268+
func resourceComputeTargetHttpProxyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
153269
config := meta.(*Config)
270+
parseImportId([]string{"projects/(?P<project>[^/]+)/global/targetHttpProxies/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config)
154271

155-
project, err := getProject(d, config)
272+
// Replace import id for the resource id
273+
id, err := replaceVars(d, config, "{{name}}")
156274
if err != nil {
157-
return err
275+
return nil, fmt.Errorf("Error constructing id: %s", err)
158276
}
277+
d.SetId(id)
159278

160-
// Delete the TargetHttpProxy
161-
log.Printf("[DEBUG] TargetHttpProxy delete request")
162-
op, err := config.clientCompute.TargetHttpProxies.Delete(
163-
project, d.Id()).Do()
164-
if err != nil {
165-
return fmt.Errorf("Error deleting TargetHttpProxy: %s", err)
279+
return []*schema.ResourceData{d}, nil
280+
}
281+
282+
func flattenComputeTargetHttpProxyCreationTimestamp(v interface{}) interface{} {
283+
return v
284+
}
285+
286+
func flattenComputeTargetHttpProxyDescription(v interface{}) interface{} {
287+
return v
288+
}
289+
290+
func flattenComputeTargetHttpProxyProxyId(v interface{}) interface{} {
291+
// Handles the string fixed64 format
292+
if strVal, ok := v.(string); ok {
293+
if intVal, err := strconv.Atoi(strVal); err == nil {
294+
return intVal
295+
} // let terraform core handle it if we can't convert the string to an int.
166296
}
297+
return v
298+
}
299+
300+
func flattenComputeTargetHttpProxyName(v interface{}) interface{} {
301+
return v
302+
}
167303

168-
err = computeOperationWait(config.clientCompute, op, project, "Deleting Target Http Proxy")
304+
func flattenComputeTargetHttpProxyUrlMap(v interface{}) interface{} {
305+
return v
306+
}
307+
308+
func expandComputeTargetHttpProxyDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
309+
return v, nil
310+
}
311+
312+
func expandComputeTargetHttpProxyName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
313+
return v, nil
314+
}
315+
316+
func expandComputeTargetHttpProxyUrlMap(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
317+
f, err := parseGlobalFieldValue("urlMaps", v.(string), "project", d, config, true)
169318
if err != nil {
170-
return err
319+
return nil, fmt.Errorf("Invalid value for url_map: %s", err)
171320
}
172-
173-
d.SetId("")
174-
return nil
321+
return f.RelativeLink(), nil
175322
}

0 commit comments

Comments
 (0)