@@ -5,12 +5,9 @@ import (
5
5
"fmt"
6
6
"log"
7
7
"net/url"
8
- "time"
9
8
10
9
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
12
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
- "github.com/ovh/go-ovh/ovh"
14
11
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
15
12
)
16
13
@@ -25,7 +22,7 @@ func resourceCloudProjectInstance() *schema.Resource {
25
22
Type : schema .TypeString ,
26
23
Required : true ,
27
24
DefaultFunc : schema .EnvDefaultFunc ("OVH_CLOUD_PROJECT_SERVICE" , nil ),
28
- Description : "Service name of the resource representing the id of the cloud project. " ,
25
+ Description : "Service name of the resource representing the id of the cloud project" ,
29
26
ForceNew : true ,
30
27
},
31
28
"region" : {
@@ -134,12 +131,12 @@ func resourceCloudProjectInstance() *schema.Resource {
134
131
Optional : true ,
135
132
ForceNew : true ,
136
133
MaxItems : 1 ,
137
- Description : "Existing SSH Keypair " ,
134
+ Description : "Existing SSH Key pair " ,
138
135
Elem : & schema.Resource {
139
136
Schema : map [string ]* schema.Schema {
140
137
"name" : {
141
138
Type : schema .TypeString ,
142
- Description : "SSH Keypair name" ,
139
+ Description : "SSH Key pair name" ,
143
140
Required : true ,
144
141
},
145
142
},
@@ -150,17 +147,17 @@ func resourceCloudProjectInstance() *schema.Resource {
150
147
Optional : true ,
151
148
ForceNew : true ,
152
149
MaxItems : 1 ,
153
- Description : "Creatting SSH Keypair " ,
150
+ Description : "Add existing SSH Key pair into your Public Cloud project and link it to the instance " ,
154
151
Elem : & schema.Resource {
155
152
Schema : map [string ]* schema.Schema {
156
153
"name" : {
157
154
Type : schema .TypeString ,
158
- Description : "SSH Keypair name" ,
155
+ Description : "SSH Key pair name" ,
159
156
Required : true ,
160
157
},
161
158
"public_key" : {
162
159
Type : schema .TypeString ,
163
- Description : "Group id " ,
160
+ Description : "SSH Public Key " ,
164
161
Required : true ,
165
162
},
166
163
},
@@ -211,12 +208,12 @@ func resourceCloudProjectInstance() *schema.Resource {
211
208
"attached_volumes" : {
212
209
Type : schema .TypeSet ,
213
210
Computed : true ,
214
- Description : " Volumes attached to the instance" ,
211
+ Description : "Volumes attached to the instance" ,
215
212
Elem : & schema.Resource {
216
213
Schema : map [string ]* schema.Schema {
217
214
"id" : {
218
215
Type : schema .TypeString ,
219
- Description : "Volume Id " ,
216
+ Description : "Volume id " ,
220
217
Computed : true ,
221
218
},
222
219
},
@@ -258,107 +255,65 @@ func resourceCloudProjectInstanceCreate(ctx context.Context, d *schema.ResourceD
258
255
params := new (CloudProjectInstanceCreateOpts )
259
256
params .FromResource (d )
260
257
261
- r := & CloudProjectOperation {}
262
-
263
258
endpoint := fmt .Sprintf ("/cloud/project/%s/region/%s/instance" ,
264
259
url .PathEscape (serviceName ),
265
260
url .PathEscape (region ),
266
261
)
267
262
263
+ r := & CloudProjectOperationResponse {}
268
264
if err := config .OVHClient .Post (endpoint , params , r ); err != nil {
269
265
return diag .Errorf ("calling %s with params %v:\n \t %q" , endpoint , params , err )
270
266
}
271
267
272
- err := waitForInstanceCreation (ctx , config .OVHClient , serviceName , r .Id )
268
+ instanceID , err := waitForCloudProjectOperation (ctx , config .OVHClient , serviceName , r .Id )
273
269
if err != nil {
274
270
return diag .Errorf ("timeout instance creation: %s" , err )
275
271
}
276
272
277
- endpointInstance := fmt .Sprintf ("/cloud/project/%s/operation/%s" ,
278
- url .PathEscape (serviceName ),
279
- url .PathEscape (r .Id ),
280
- )
281
-
282
- err = config .OVHClient .GetWithContext (ctx , endpointInstance , r )
283
- if err != nil {
284
- return diag .Errorf ("failed to get instance id: %s" , err )
285
- }
286
-
287
- d .SetId (r .SubOperations [0 ].ResourceId )
273
+ d .SetId (instanceID )
288
274
d .Set ("region" , region )
289
275
290
276
return resourceCloudProjectInstanceRead (ctx , d , meta )
291
277
}
292
278
293
- func waitForInstanceCreation (ctx context.Context , client * ovh.Client , serviceName , operationId string ) error {
294
- stateConf := & retry.StateChangeConf {
295
- Pending : []string {"null" , "in-progress" , "created" , "" },
296
- Target : []string {"completed" },
297
- Refresh : func () (interface {}, string , error ) {
298
- res := & CloudProjectOperation {}
299
- endpoint := fmt .Sprintf ("/cloud/project/%s/operation/%s" ,
300
- url .PathEscape (serviceName ),
301
- url .PathEscape (operationId ),
302
- )
303
- err := client .GetWithContext (ctx , endpoint , res )
304
- if err != nil {
305
- return res , "" , err
306
- }
307
- return res , res .Status , nil
308
- },
309
- Timeout : 360 * time .Second ,
310
- Delay : 10 * time .Second ,
311
- MinTimeout : 3 * time .Second ,
312
- }
313
-
314
- _ , err := stateConf .WaitForStateContext (ctx )
315
- return err
316
- }
317
-
318
279
func resourceCloudProjectInstanceRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
319
280
config := meta .(* Config )
320
281
id := d .Id ()
321
282
region := d .Get ("region" ).(string )
322
283
serviceName := d .Get ("service_name" ).(string )
323
284
324
- r := & CloudProjectInstanceResponse {}
325
-
326
285
endpoint := fmt .Sprintf ("/cloud/project/%s/region/%s/instance/%s" ,
327
286
url .PathEscape (serviceName ),
328
287
url .PathEscape (region ),
329
288
url .PathEscape (id ),
330
289
)
331
290
291
+ r := & CloudProjectInstanceResponse {}
332
292
if err := config .OVHClient .Get (endpoint , r ); err != nil {
333
- return diag .Errorf ("Error calling post %s:\n \t %q" , endpoint , err )
293
+ return diag .Errorf ("Error calling Get %s:\n \t %q" , endpoint , err )
334
294
}
335
295
336
296
d .Set ("flavor_id" , r .FlavorId )
337
297
d .Set ("flavor_name" , r .FlavorName )
338
298
d .Set ("image_id" , r .ImageId )
339
299
d .Set ("region" , r .Region )
340
300
d .Set ("task_state" , r .TaskState )
341
- d .Set ("name" , d .Get ("name" ).(string ))
342
301
d .Set ("id" , r .Id )
343
302
344
- addresses := make ([]map [string ]interface {}, 0 )
345
- if r .Addresses != nil {
346
- for _ , add := range r .Addresses {
347
- address := make (map [string ]interface {})
348
- address ["ip" ] = add .Ip
349
- address ["version" ] = add .Version
350
- addresses = append (addresses , address )
351
- }
303
+ addresses := make ([]map [string ]interface {}, 0 , len (r .Addresses ))
304
+ for _ , add := range r .Addresses {
305
+ address := make (map [string ]interface {})
306
+ address ["ip" ] = add .Ip
307
+ address ["version" ] = add .Version
308
+ addresses = append (addresses , address )
352
309
}
353
310
d .Set ("addresses" , addresses )
354
311
355
- attachedVolumes := make ([]map [string ]interface {}, 0 )
356
- if r .AttachedVolumes != nil {
357
- for _ , att := range r .AttachedVolumes {
358
- attachedVolume := make (map [string ]interface {})
359
- attachedVolume ["id" ] = att .Id
360
- attachedVolumes = append (attachedVolumes , attachedVolume )
361
- }
312
+ attachedVolumes := make ([]map [string ]interface {}, 0 , len (r .AttachedVolumes ))
313
+ for _ , att := range r .AttachedVolumes {
314
+ attachedVolume := make (map [string ]interface {})
315
+ attachedVolume ["id" ] = att .Id
316
+ attachedVolumes = append (attachedVolumes , attachedVolume )
362
317
}
363
318
d .Set ("attached_volumes" , attachedVolumes )
364
319
@@ -380,11 +335,11 @@ func resourceCloudProjectInstanceDelete(ctx context.Context, d *schema.ResourceD
380
335
)
381
336
382
337
if err := config .OVHClient .Delete (endpoint , nil ); err != nil {
383
- return diag .Errorf ("Error calling post %s:\n \t %q" , endpoint , err )
338
+ return diag .Errorf ("Error calling Delete %s:\n \t %q" , endpoint , err )
384
339
}
385
340
386
341
d .SetId ("" )
387
342
388
- log .Printf ("[DEBUG] Deleted Public Cloud %s Gateway %s" , serviceName , id )
343
+ log .Printf ("[DEBUG] Deleted Public Cloud %s Instance %s" , serviceName , id )
389
344
return nil
390
345
}
0 commit comments