@@ -18,6 +18,7 @@ import (
18
18
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cloudapi/cloudapiserver"
19
19
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/slice"
20
20
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
21
+ "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
21
22
)
22
23
23
24
func resourceCubeServer () * schema.Resource {
@@ -223,7 +224,7 @@ func resourceCubeServer() *schema.Resource {
223
224
Optional : true ,
224
225
Computed : true ,
225
226
Description : "Sets the power state of the cube server. Possible values: `RUNNING` or `SUSPENDED`." ,
226
- ValidateDiagFunc : validation .ToDiagFunc (validation .StringInSlice ([]string {cloudapiserver .VMStateStart , cloudapiserver .CubeVMStateStop }, true )),
227
+ ValidateDiagFunc : validation .ToDiagFunc (validation .StringInSlice ([]string {constant .VMStateStart , constant .CubeVMStateStop }, true )),
227
228
},
228
229
"nic" : {
229
230
Type : schema .TypeList ,
@@ -374,14 +375,8 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
374
375
server := ionoscloud.Server {
375
376
Properties : & ionoscloud.ServerProperties {},
376
377
}
377
- volume := ionoscloud.VolumeProperties {}
378
378
379
- var sshKeyPath []interface {}
380
- var publicKeys []string
381
- var image , imageAlias , imageInput string
382
- var isSnapshot bool
383
- var diags diag.Diagnostics
384
- var password , licenceType string
379
+ var image , imageAlias string
385
380
386
381
dcId := d .Get ("datacenter_id" ).(string )
387
382
@@ -396,75 +391,9 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
396
391
server .Properties .AvailabilityZone = & vStr
397
392
}
398
393
399
- serverType := cloudapiserver . CubeServerType
394
+ serverType := constant . CubeType
400
395
server .Properties .Type = & serverType
401
396
402
- volumeType := d .Get ("volume.0.disk_type" ).(string )
403
- volume .Type = & volumeType
404
-
405
- if v , ok := d .GetOk ("volume.0.image_password" ); ok {
406
- vStr := v .(string )
407
- volume .ImagePassword = & vStr
408
- if err := d .Set ("image_password" , password ); err != nil {
409
- diags := diag .FromErr (err )
410
- return diags
411
- }
412
- }
413
-
414
- if v , ok := d .GetOk ("image_password" ); ok {
415
- password = v .(string )
416
- volume .ImagePassword = & password
417
- }
418
-
419
- if v , ok := d .GetOk ("volume.0.licence_type" ); ok {
420
- licenceType = v .(string )
421
- volume .LicenceType = & licenceType
422
- }
423
-
424
- if v , ok := d .GetOk ("volume.0.availability_zone" ); ok {
425
- vStr := v .(string )
426
- volume .AvailabilityZone = & vStr
427
- }
428
-
429
- if v , ok := d .GetOk ("volume.0.name" ); ok {
430
- vStr := v .(string )
431
- volume .Name = & vStr
432
- }
433
-
434
- if v , ok := d .GetOk ("volume.0.bus" ); ok {
435
- vStr := v .(string )
436
- volume .Bus = & vStr
437
- }
438
-
439
- if v , ok := d .GetOk ("volume.0.backup_unit_id" ); ok {
440
- vStr := v .(string )
441
- volume .BackupunitId = & vStr
442
- }
443
-
444
- if v , ok := d .GetOk ("volume.0.user_data" ); ok {
445
- vStr := v .(string )
446
- volume .UserData = & vStr
447
- }
448
-
449
- if v , ok := d .GetOk ("volume.0.ssh_key_path" ); ok {
450
- sshKeyPath = v .([]interface {})
451
- if err := d .Set ("ssh_key_path" , v .([]interface {})); err != nil {
452
- diags := diag .FromErr (err )
453
- return diags
454
- }
455
- } else if v , ok := d .GetOk ("ssh_key_path" ); ok {
456
- sshKeyPath = v .([]interface {})
457
- if err := d .Set ("ssh_key_path" , v .([]interface {})); err != nil {
458
- diags := diag .FromErr (err )
459
- return diags
460
- }
461
- } else {
462
- if err := d .Set ("ssh_key_path" , [][]string {}); err != nil {
463
- diags := diag .FromErr (err )
464
- return diags
465
- }
466
- }
467
-
468
397
if _ , ok := d .GetOk ("boot_cdrom" ); ok {
469
398
resId := d .Get ("boot_cdrom" ).(string )
470
399
server .Properties .BootCdrom = & ionoscloud.ResourceReference {
@@ -477,36 +406,17 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
477
406
return diags
478
407
}
479
408
480
- if len (sshKeyPath ) != 0 {
481
- for _ , path := range sshKeyPath {
482
- log .Printf ("[DEBUG] Reading file %s" , path )
483
- publicKey , err := utils .ReadPublicKey (path .(string ))
484
- if err != nil {
485
- diags := diag .FromErr (fmt .Errorf ("error fetching sshkey from file (%s) %w" , path , err ))
486
- return diags
487
- }
488
- publicKeys = append (publicKeys , publicKey )
489
- }
490
- if len (publicKeys ) > 0 {
491
- volume .SshKeys = & publicKeys
492
- }
493
- }
494
-
495
- if v , ok := d .GetOk ("image_name" ); ok {
496
- imageInput = v .(string )
497
- }
498
-
499
- if imageInput != "" {
500
- image , imageAlias , isSnapshot , diags = checkImage (ctx , client , imageInput , password , licenceType , dcId , sshKeyPath )
501
- if diags != nil {
502
- return diags
503
- }
504
- }
505
-
506
- if isSnapshot == true && (volume .ImagePassword != nil && * volume .ImagePassword != "" || len (publicKeys ) > 0 ) {
507
- diags := diag .FromErr (fmt .Errorf ("you can't pass 'image_password' and/or 'ssh keys' when creating a volume from a snapshot" ))
409
+ var err error
410
+ var volume * ionoscloud.VolumeProperties
411
+ volume , err = getVolumeData (d , "volume.0." , constant .CubeType )
412
+ if err != nil {
413
+ diags := diag .FromErr (err )
508
414
return diags
509
415
}
416
+ image , imageAlias , err = getImage (ctx , client , d , * volume )
417
+ if err != nil {
418
+ return diag .FromErr (err )
419
+ }
510
420
511
421
if image != "" {
512
422
volume .Image = & image
@@ -524,7 +434,7 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
524
434
Volumes : & ionoscloud.AttachedVolumes {
525
435
Items : & []ionoscloud.Volume {
526
436
{
527
- Properties : & volume ,
437
+ Properties : volume ,
528
438
},
529
439
},
530
440
},
@@ -537,7 +447,6 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
537
447
nic := ionoscloud.Nic {
538
448
Properties : & ionoscloud.NicProperties {},
539
449
}
540
- var err error
541
450
if _ , ok := d .GetOk ("nic" ); ok {
542
451
nic , err = cloudapinic .GetNicFromSchema (d , "nic.0." )
543
452
if err != nil {
@@ -660,7 +569,7 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
660
569
ss := cloudapiserver.Service {Client : client , Meta : meta , D : d }
661
570
initialState := initialState .(string )
662
571
663
- if strings .EqualFold (initialState , cloudapiserver .CubeVMStateStop ) {
572
+ if strings .EqualFold (initialState , constant .CubeVMStateStop ) {
664
573
err := ss .Stop (ctx , dcId , d .Id (), serverType )
665
574
if err != nil {
666
575
return diag .FromErr (err )
@@ -830,14 +739,14 @@ func resourceCubeServerUpdate(ctx context.Context, d *schema.ResourceData, meta
830
739
diags := diag .FromErr (fmt .Errorf ("could not retrieve server vmState: %w" , err ))
831
740
return diags
832
741
}
833
- if strings .EqualFold (currentVmState , cloudapiserver .CubeVMStateStop ) && ! d .HasChange ("vm_state" ) {
742
+ if strings .EqualFold (currentVmState , constant .CubeVMStateStop ) && ! d .HasChange ("vm_state" ) {
834
743
diags := diag .FromErr (fmt .Errorf ("cannot update a suspended Cube Server, must change the state to RUNNING first" ))
835
744
return diags
836
745
}
837
746
838
747
// Unsuspend a Cube server first, before applying other changes
839
- if d .HasChange ("vm_state" ) && strings .EqualFold (currentVmState , cloudapiserver .CubeVMStateStop ) {
840
- err := ss .Start (ctx , dcId , d .Id (), cloudapiserver . CubeServerType )
748
+ if d .HasChange ("vm_state" ) && strings .EqualFold (currentVmState , constant .CubeVMStateStop ) {
749
+ err := ss .Start (ctx , dcId , d .Id (), constant . CubeType )
841
750
if err != nil {
842
751
diags := diag .FromErr (err )
843
752
return diags
@@ -1058,10 +967,10 @@ func resourceCubeServerUpdate(ctx context.Context, d *schema.ResourceData, meta
1058
967
}
1059
968
1060
969
// Suspend a Cube server last, after applying other changes
1061
- if d .HasChange ("vm_state" ) && strings .EqualFold (currentVmState , cloudapiserver .VMStateStart ) {
970
+ if d .HasChange ("vm_state" ) && strings .EqualFold (currentVmState , constant .VMStateStart ) {
1062
971
_ , newVmState := d .GetChange ("vm_state" )
1063
- if strings .EqualFold (newVmState .(string ), cloudapiserver .CubeVMStateStop ) {
1064
- err := ss .Stop (ctx , dcId , d .Id (), cloudapiserver . CubeServerType )
972
+ if strings .EqualFold (newVmState .(string ), constant .CubeVMStateStop ) {
973
+ err := ss .Stop (ctx , dcId , d .Id (), constant . CubeType )
1065
974
if err != nil {
1066
975
diags := diag .FromErr (err )
1067
976
return diags
0 commit comments