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"
20
+ "time"
6
21
7
22
"github.com/hashicorp/terraform/helper/schema"
8
23
"github.com/hashicorp/terraform/helper/validation"
9
-
10
- "google.golang.org/api/compute/v1"
24
+ compute "google.golang.org/api/compute/v1"
11
25
)
12
26
13
27
func resourceComputeGlobalAddress () * schema.Resource {
14
28
return & schema.Resource {
15
29
Create : resourceComputeGlobalAddressCreate ,
16
30
Read : resourceComputeGlobalAddressRead ,
17
31
Delete : resourceComputeGlobalAddressDelete ,
32
+
18
33
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 ),
20
40
},
21
41
22
42
Schema : map [string ]* schema.Schema {
23
- "name" : & schema. Schema {
43
+ "name" : {
24
44
Type : schema .TypeString ,
25
45
Required : true ,
26
46
ForceNew : true ,
27
47
},
28
-
29
- "ip_version" : & schema.Schema {
48
+ "description" : {
49
+ Type : schema .TypeString ,
50
+ Optional : true ,
51
+ ForceNew : true ,
52
+ },
53
+ "ip_version" : {
30
54
Type : schema .TypeString ,
31
55
Optional : true ,
32
56
ForceNew : true ,
33
57
ValidateFunc : validation .StringInSlice ([]string {"IPV4" , "IPV6" , "" }, false ),
34
58
},
35
-
36
- "project" : & schema.Schema {
59
+ "address" : {
37
60
Type : schema .TypeString ,
38
- Optional : true ,
39
61
Computed : true ,
40
- ForceNew : true ,
41
62
},
42
-
43
- "address" : & schema.Schema {
63
+ "creation_timestamp" : {
44
64
Type : schema .TypeString ,
45
65
Computed : true ,
46
66
},
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" : {
49
74
Type : schema .TypeString ,
50
75
Computed : true ,
51
76
},
@@ -61,25 +86,59 @@ func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}
61
86
return err
62
87
}
63
88
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
68
100
}
69
101
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" )
71
109
if err != nil {
72
- return fmt . Errorf ( "Error creating address: %s" , err )
110
+ return err
73
111
}
74
112
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
+ }
77
118
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 )
79
128
if err != nil {
80
129
return err
81
130
}
82
131
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
+
83
142
return resourceComputeGlobalAddressRead (d , meta )
84
143
}
85
144
@@ -91,16 +150,23 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{})
91
150
return err
92
151
}
93
152
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 )
95
159
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 ( )))
97
161
}
98
162
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" ])
102
169
d .Set ("project" , project )
103
- d .Set ("self_link" , ConvertSelfLinkToV1 (addr .SelfLink ))
104
170
105
171
return nil
106
172
}
@@ -113,18 +179,76 @@ func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}
113
179
return err
114
180
}
115
181
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}}" )
119
183
if err != nil {
120
- return fmt . Errorf ( "Error deleting address: %s" , err )
184
+ return err
121
185
}
122
186
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
+
124
203
if err != nil {
125
204
return err
126
205
}
127
206
128
- d .SetId ("" )
129
207
return nil
130
208
}
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
+ }
0 commit comments