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
- ValidateFunc : validation .StringInSlice ([]string {"IPV4" , "IPV6" , "" }, false ),
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" : {
49
68
Type : schema .TypeString ,
69
+ Optional : true ,
50
70
Computed : true ,
71
+ ForceNew : true ,
51
72
},
52
73
},
53
74
}
@@ -61,25 +82,59 @@ func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}
61
82
return err
62
83
}
63
84
64
- // Build the address parameter
65
- addr := & compute.Address {
66
- Name : d .Get ("name" ).(string ),
67
- IpVersion : d .Get ("ip_version" ).(string ),
85
+ descriptionProp , err := expandComputeGlobalAddressDescription (d .Get ("description" ), d , config )
86
+ if err != nil {
87
+ return err
88
+ }
89
+ nameProp , err := expandComputeGlobalAddressName (d .Get ("name" ), d , config )
90
+ if err != nil {
91
+ return err
92
+ }
93
+ ipVersionProp , err := expandComputeGlobalAddressIpVersion (d .Get ("ip_version" ), d , config )
94
+ if err != nil {
95
+ return err
96
+ }
97
+
98
+ obj := map [string ]interface {}{
99
+ "description" : descriptionProp ,
100
+ "name" : nameProp ,
101
+ "ipVersion" : ipVersionProp ,
68
102
}
69
103
70
- op , err := config . clientCompute . GlobalAddresses . Insert ( project , addr ). Do ( )
104
+ url , err := replaceVars ( d , config , "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses" )
71
105
if err != nil {
72
- return fmt . Errorf ( "Error creating address: %s" , err )
106
+ return err
73
107
}
74
108
75
- // It probably maybe worked, so store the ID now
76
- d .SetId (addr .Name )
109
+ log .Printf ("[DEBUG] Creating new GlobalAddress: %#v" , obj )
110
+ res , err := Post (config , url , obj )
111
+ if err != nil {
112
+ return fmt .Errorf ("Error creating GlobalAddress: %s" , err )
113
+ }
77
114
78
- err = computeSharedOperationWait (config .clientCompute , op , project , "Creating Global Address" )
115
+ // Store the ID now
116
+ id , err := replaceVars (d , config , "{{name}}" )
117
+ if err != nil {
118
+ return fmt .Errorf ("Error constructing id: %s" , err )
119
+ }
120
+ d .SetId (id )
121
+
122
+ op := & compute.Operation {}
123
+ err = Convert (res , op )
79
124
if err != nil {
80
125
return err
81
126
}
82
127
128
+ waitErr := computeOperationWaitTime (
129
+ config .clientCompute , op , project , "Creating GlobalAddress" ,
130
+ int (d .Timeout (schema .TimeoutCreate ).Minutes ()))
131
+
132
+ if waitErr != nil {
133
+ // The resource didn't actually create
134
+ d .SetId ("" )
135
+ return waitErr
136
+ }
137
+
83
138
return resourceComputeGlobalAddressRead (d , meta )
84
139
}
85
140
@@ -91,16 +146,23 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{})
91
146
return err
92
147
}
93
148
94
- addr , err := config . clientCompute . GlobalAddresses . Get ( project , d . Id ()). Do ( )
149
+ url , err := replaceVars ( d , config , "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}" )
95
150
if err != nil {
96
- return handleNotFoundError ( err , d , fmt . Sprintf ( "Global Address %q" , d . Get ( "name" ).( string )))
151
+ return err
97
152
}
98
153
99
- d .Set ("name" , addr .Name )
100
- d .Set ("ip_version" , addr .IpVersion )
101
- d .Set ("address" , addr .Address )
154
+ res , err := Get (config , url )
155
+ if err != nil {
156
+ return handleNotFoundError (err , d , fmt .Sprintf ("ComputeGlobalAddress %q" , d .Id ()))
157
+ }
158
+
159
+ d .Set ("address" , flattenComputeGlobalAddressAddress (res ["address" ]))
160
+ d .Set ("creation_timestamp" , flattenComputeGlobalAddressCreationTimestamp (res ["creationTimestamp" ]))
161
+ d .Set ("description" , flattenComputeGlobalAddressDescription (res ["description" ]))
162
+ d .Set ("name" , flattenComputeGlobalAddressName (res ["name" ]))
163
+ d .Set ("ip_version" , flattenComputeGlobalAddressIpVersion (res ["ipVersion" ]))
164
+ d .Set ("self_link" , res ["selfLink" ])
102
165
d .Set ("project" , project )
103
- d .Set ("self_link" , ConvertSelfLinkToV1 (addr .SelfLink ))
104
166
105
167
return nil
106
168
}
@@ -113,18 +175,76 @@ func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}
113
175
return err
114
176
}
115
177
116
- // Delete the address
117
- log .Printf ("[DEBUG] address delete request" )
118
- op , err := config .clientCompute .GlobalAddresses .Delete (project , d .Id ()).Do ()
178
+ url , err := replaceVars (d , config , "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}" )
179
+ if err != nil {
180
+ return err
181
+ }
182
+
183
+ log .Printf ("[DEBUG] Deleting GlobalAddress %q" , d .Id ())
184
+ res , err := Delete (config , url )
185
+ if err != nil {
186
+ return fmt .Errorf ("Error deleting GlobalAddress %q: %s" , d .Id (), err )
187
+ }
188
+
189
+ op := & compute.Operation {}
190
+ err = Convert (res , op )
119
191
if err != nil {
120
- return fmt . Errorf ( "Error deleting address: %s" , err )
192
+ return err
121
193
}
122
194
123
- err = computeSharedOperationWait (config .clientCompute , op , project , "Deleting Global Address" )
195
+ err = computeOperationWaitTime (
196
+ config .clientCompute , op , project , "Deleting GlobalAddress" ,
197
+ int (d .Timeout (schema .TimeoutDelete ).Minutes ()))
198
+
124
199
if err != nil {
125
200
return err
126
201
}
127
202
128
- d .SetId ("" )
129
203
return nil
130
204
}
205
+
206
+ func resourceComputeGlobalAddressImport (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
207
+ config := meta .(* Config )
208
+ parseImportId ([]string {"projects/(?P<project>[^/]+)/global/addresses/(?P<name>[^/]+)" , "(?P<project>[^/]+)/(?P<name>[^/]+)" , "(?P<name>[^/]+)" }, d , config )
209
+
210
+ // Replace import id for the resource id
211
+ id , err := replaceVars (d , config , "{{name}}" )
212
+ if err != nil {
213
+ return nil , fmt .Errorf ("Error constructing id: %s" , err )
214
+ }
215
+ d .SetId (id )
216
+
217
+ return []* schema.ResourceData {d }, nil
218
+ }
219
+
220
+ func flattenComputeGlobalAddressAddress (v interface {}) interface {} {
221
+ return v
222
+ }
223
+
224
+ func flattenComputeGlobalAddressCreationTimestamp (v interface {}) interface {} {
225
+ return v
226
+ }
227
+
228
+ func flattenComputeGlobalAddressDescription (v interface {}) interface {} {
229
+ return v
230
+ }
231
+
232
+ func flattenComputeGlobalAddressName (v interface {}) interface {} {
233
+ return v
234
+ }
235
+
236
+ func flattenComputeGlobalAddressIpVersion (v interface {}) interface {} {
237
+ return v
238
+ }
239
+
240
+ func expandComputeGlobalAddressDescription (v interface {}, d * schema.ResourceData , config * Config ) (interface {}, error ) {
241
+ return v , nil
242
+ }
243
+
244
+ func expandComputeGlobalAddressName (v interface {}, d * schema.ResourceData , config * Config ) (interface {}, error ) {
245
+ return v , nil
246
+ }
247
+
248
+ func expandComputeGlobalAddressIpVersion (v interface {}, d * schema.ResourceData , config * Config ) (interface {}, error ) {
249
+ return v , nil
250
+ }
0 commit comments