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
+
1
15
package google
2
16
3
17
import (
4
18
"fmt"
5
19
"log"
6
20
"strconv"
21
+ "time"
7
22
8
23
"github.com/hashicorp/terraform/helper/schema"
9
- "google.golang.org/api/compute/v1"
24
+ compute "google.golang.org/api/compute/v1"
10
25
)
11
26
12
27
func resourceComputeTargetHttpProxy () * schema.Resource {
13
28
return & schema.Resource {
14
29
Create : resourceComputeTargetHttpProxyCreate ,
15
30
Read : resourceComputeTargetHttpProxyRead ,
16
- Delete : resourceComputeTargetHttpProxyDelete ,
17
31
Update : resourceComputeTargetHttpProxyUpdate ,
32
+ Delete : resourceComputeTargetHttpProxyDelete ,
18
33
19
34
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 ),
21
42
},
22
43
23
44
Schema : map [string ]* schema.Schema {
24
- "name" : & schema. Schema {
45
+ "name" : {
25
46
Type : schema .TypeString ,
26
47
Required : true ,
27
48
ForceNew : true ,
28
49
},
29
-
30
- "url_map" : & schema.Schema {
50
+ "url_map" : {
31
51
Type : schema .TypeString ,
32
52
Required : true ,
33
- DiffSuppressFunc : compareSelfLinkRelativePaths ,
53
+ DiffSuppressFunc : compareSelfLinkOrResourceName ,
34
54
},
35
-
36
- "description" : & schema.Schema {
55
+ "description" : {
37
56
Type : schema .TypeString ,
38
57
Optional : true ,
39
58
ForceNew : true ,
40
59
},
41
-
42
- "proxy_id" : & schema.Schema {
60
+ "creation_timestamp" : {
43
61
Type : schema .TypeString ,
44
62
Computed : true ,
45
63
},
46
-
47
- "project" : & schema.Schema {
64
+ "proxy_id" : {
65
+ Type : schema .TypeInt ,
66
+ Computed : true ,
67
+ },
68
+ "project" : {
48
69
Type : schema .TypeString ,
49
70
Optional : true ,
50
71
Computed : true ,
51
72
ForceNew : true ,
52
73
},
53
-
54
- "self_link" : & schema.Schema {
74
+ "self_link" : {
55
75
Type : schema .TypeString ,
56
76
Computed : true ,
57
77
},
@@ -67,32 +87,91 @@ func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface
67
87
return err
68
88
}
69
89
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
73
101
}
74
102
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 ,
77
107
}
78
108
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 )
82
116
if err != nil {
83
117
return fmt .Errorf ("Error creating TargetHttpProxy: %s" , err )
84
118
}
85
119
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 )
87
129
if err != nil {
88
130
return err
89
131
}
90
132
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
+ }
92
142
93
143
return resourceComputeTargetHttpProxyRead (d , meta )
94
144
}
95
145
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
+
96
175
func resourceComputeTargetHttpProxyUpdate (d * schema.ResourceData , meta interface {}) error {
97
176
config := meta .(* Config )
98
177
@@ -101,75 +180,143 @@ func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface
101
180
return err
102
181
}
103
182
104
- d .Partial (true )
183
+ var url string
184
+ var res map [string ]interface {}
185
+ op := & compute.Operation {}
105
186
106
187
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 )
111
193
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
113
199
}
114
200
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" )
116
207
if err != nil {
117
208
return err
118
209
}
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
+ }
119
214
120
- d .SetPartial ("url_map" )
121
- }
215
+ err = Convert (res , op )
216
+ if err != nil {
217
+ return err
218
+ }
122
219
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
+ }
124
228
125
229
return resourceComputeTargetHttpProxyRead (d , meta )
126
230
}
127
231
128
- func resourceComputeTargetHttpProxyRead (d * schema.ResourceData , meta interface {}) error {
232
+ func resourceComputeTargetHttpProxyDelete (d * schema.ResourceData , meta interface {}) error {
129
233
config := meta .(* Config )
130
234
131
235
project , err := getProject (d , config )
132
236
if err != nil {
133
237
return err
134
238
}
135
239
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}}" )
138
241
if err != nil {
139
- return handleNotFoundError ( err , d , fmt . Sprintf ( "Target HTTP Proxy %q" , d . Get ( "name" ).( string )))
242
+ return err
140
243
}
141
244
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
+ }
148
264
149
265
return nil
150
266
}
151
267
152
- func resourceComputeTargetHttpProxyDelete (d * schema.ResourceData , meta interface {}) error {
268
+ func resourceComputeTargetHttpProxyImport (d * schema.ResourceData , meta interface {}) ([] * schema. ResourceData , error ) {
153
269
config := meta .(* Config )
270
+ parseImportId ([]string {"projects/(?P<project>[^/]+)/global/targetHttpProxies/(?P<name>[^/]+)" , "(?P<project>[^/]+)/(?P<name>[^/]+)" , "(?P<name>[^/]+)" }, d , config )
154
271
155
- project , err := getProject (d , config )
272
+ // Replace import id for the resource id
273
+ id , err := replaceVars (d , config , "{{name}}" )
156
274
if err != nil {
157
- return err
275
+ return nil , fmt . Errorf ( "Error constructing id: %s" , err )
158
276
}
277
+ d .SetId (id )
159
278
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.
166
296
}
297
+ return v
298
+ }
299
+
300
+ func flattenComputeTargetHttpProxyName (v interface {}) interface {} {
301
+ return v
302
+ }
167
303
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 )
169
318
if err != nil {
170
- return err
319
+ return nil , fmt . Errorf ( "Invalid value for url_map: %s" , err )
171
320
}
172
-
173
- d .SetId ("" )
174
- return nil
321
+ return f .RelativeLink (), nil
175
322
}
0 commit comments