1
1
package ovh
2
2
3
3
import (
4
+ "fmt"
5
+ "net/url"
6
+
4
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8
+ "github.com/ovh/go-ovh/ovh"
5
9
)
6
10
7
11
func dataSourceMeInstallationTemplate () * schema.Resource {
@@ -254,9 +258,7 @@ func dataSourceMeInstallationTemplate() *schema.Resource {
254
258
255
259
func dataSourceMeInstallationTemplateRead (d * schema.ResourceData , meta interface {}) error {
256
260
config := meta .(* Config )
257
- name := d .Get ("template_name" ).(string )
258
-
259
- template , err := getInstallationTemplate (name , config .OVHClient )
261
+ template , err := getInstallationTemplate (d , config .OVHClient )
260
262
if err != nil {
261
263
return err
262
264
}
@@ -272,6 +274,7 @@ func dataSourceMeInstallationTemplateRead(d *schema.ResourceData, meta interface
272
274
return err
273
275
}
274
276
277
+ name := d .Get ("template_name" ).(string )
275
278
d .SetId (name )
276
279
277
280
return nil
@@ -322,3 +325,138 @@ func partialMeInstallationTemplatePartitionSchemesRead(d *schema.ResourceData, m
322
325
323
326
return nil
324
327
}
328
+
329
+ func getPartitionSchemes (template string , client * ovh.Client ) ([]* PartitionScheme , error ) {
330
+ schemes , err := getPartitionSchemeIds (template , client )
331
+ if err != nil {
332
+ return nil , err
333
+ }
334
+
335
+ partitionSchemes := []* PartitionScheme {}
336
+ for _ , scheme := range schemes {
337
+ partitionScheme , err := getPartitionScheme (template , scheme , client )
338
+ if err != nil {
339
+ return nil , err
340
+ }
341
+
342
+ partitionSchemes = append (partitionSchemes , partitionScheme )
343
+ }
344
+
345
+ return partitionSchemes , nil
346
+ }
347
+
348
+ func getPartitionScheme (template , scheme string , client * ovh.Client ) (* PartitionScheme , error ) {
349
+ r := & PartitionScheme {}
350
+
351
+ endpoint := fmt .Sprintf (
352
+ "/me/installationTemplate/%s/partitionScheme/%s" ,
353
+ url .PathEscape (template ),
354
+ url .PathEscape (scheme ),
355
+ )
356
+
357
+ if err := client .Get (endpoint , r ); err != nil {
358
+ return nil , fmt .Errorf ("Error calling GET %s: %s \n " , endpoint , err .Error ())
359
+ }
360
+
361
+ return r , nil
362
+ }
363
+
364
+ func getPartitionSchemeIds (template string , client * ovh.Client ) ([]string , error ) {
365
+ schemes := []string {}
366
+ endpoint := fmt .Sprintf (
367
+ "/me/installationTemplate/%s/partitionScheme" ,
368
+ url .PathEscape (template ),
369
+ )
370
+ err := client .Get (endpoint , & schemes )
371
+
372
+ if err != nil {
373
+ return nil , fmt .Errorf ("Error calling GET %s: %s \n " , endpoint , err .Error ())
374
+ }
375
+ return schemes , nil
376
+ }
377
+
378
+ func getPartitionSchemePartitions (template , scheme string , client * ovh.Client ) ([]* Partition , error ) {
379
+ mountPoints := []string {}
380
+ endpoint := fmt .Sprintf (
381
+ "/me/installationTemplate/%s/partitionScheme/%s/partition" ,
382
+ url .PathEscape (template ),
383
+ url .PathEscape (scheme ),
384
+ )
385
+ err := client .Get (endpoint , & mountPoints )
386
+
387
+ if err != nil {
388
+ return nil , fmt .Errorf ("Error calling GET %s: %s \n " , endpoint , err .Error ())
389
+ }
390
+
391
+ partitions := []* Partition {}
392
+ for _ , mountPoint := range mountPoints {
393
+ partition , err := getPartitionSchemePartition (template , scheme , mountPoint , client )
394
+ if err != nil {
395
+ return nil , err
396
+ }
397
+
398
+ partitions = append (partitions , partition )
399
+ }
400
+
401
+ return partitions , nil
402
+ }
403
+
404
+ func getPartitionSchemePartition (template , scheme , mountPoint string , client * ovh.Client ) (* Partition , error ) {
405
+ r := & Partition {}
406
+
407
+ endpoint := fmt .Sprintf (
408
+ "/me/installationTemplate/%s/partitionScheme/%s/partition/%s" ,
409
+ url .PathEscape (template ),
410
+ url .PathEscape (scheme ),
411
+ url .PathEscape (mountPoint ),
412
+ )
413
+
414
+ if err := client .Get (endpoint , r ); err != nil {
415
+ return nil , fmt .Errorf ("Calling GET %s: %s \n " , endpoint , err .Error ())
416
+ }
417
+
418
+ return r , nil
419
+ }
420
+
421
+ func getPartitionSchemeHardwareRaids (template , scheme string , client * ovh.Client ) ([]* HardwareRaid , error ) {
422
+ names := []string {}
423
+ endpoint := fmt .Sprintf (
424
+ "/me/installationTemplate/%s/partitionScheme/%s/hardwareRaid" ,
425
+ url .PathEscape (template ),
426
+ url .PathEscape (scheme ),
427
+ )
428
+ err := client .Get (endpoint , & names )
429
+
430
+ if err != nil {
431
+ return nil , fmt .Errorf ("Error calling GET %s: %s \n " , endpoint , err .Error ())
432
+ }
433
+
434
+ hardwareRaids := []* HardwareRaid {}
435
+ for _ , name := range names {
436
+ hardwareRaid , err := getPartitionSchemeHardwareRaid (template , scheme , name , client )
437
+ if err != nil {
438
+ return nil , err
439
+ }
440
+
441
+ hardwareRaids = append (hardwareRaids , hardwareRaid )
442
+ }
443
+
444
+ return hardwareRaids , nil
445
+ }
446
+
447
+ func getPartitionSchemeHardwareRaid (template , scheme , name string , client * ovh.Client ) (* HardwareRaid , error ) {
448
+ r := & HardwareRaid {}
449
+
450
+ endpoint := fmt .Sprintf (
451
+ "/me/installationTemplate/%s/partitionScheme/%s/hardwareRaid/%s" ,
452
+ url .PathEscape (template ),
453
+ url .PathEscape (scheme ),
454
+ url .PathEscape (name ),
455
+ )
456
+
457
+ if err := client .Get (endpoint , r ); err != nil {
458
+ return nil , fmt .Errorf ("Error calling %s: %s \n " , endpoint , err .Error ())
459
+ }
460
+
461
+ return r , nil
462
+ }
0 commit comments