Skip to content

Commit d86a3b9

Browse files
authored
refactor: volume image retrieval duplicate code (#478)
1 parent d5c79df commit d86a3b9

File tree

11 files changed

+102
-300
lines changed

11 files changed

+102
-300
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.4.13
2+
### Refactor
3+
- Remove duplicate functions for image retrieval (`checkImage`, `resolveImageName`) in `resource_volume.go`
4+
15
## 6.4.12
26
### Features
37
- Add `ionoscloud_server_boot_device_selection` resource for selecting the boot device of `ionoscloud_server`, `ionoscloud_vcpu_server` and `ionoscloud_cube_server` resources

ionoscloud/resource_server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func resourceServer() *schema.Resource {
318318
Optional: true,
319319
Computed: true,
320320
Description: "Sets the power state of the server. Possible values: `RUNNING`, `SHUTOFF` or `SUSPENDED`. SUSPENDED state is only valid for cube. SHUTOFF state is only valid for enterprise",
321-
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{cloudapiserver.VMStateStart, cloudapiserver.VMStateStop, cloudapiserver.CubeVMStateStop}, true)),
321+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{constant.VMStateStart, constant.VMStateStop, constant.CubeVMStateStop}, true)),
322322
},
323323
"nic": {
324324
Type: schema.TypeList,
@@ -754,7 +754,7 @@ func resourceServerCreate(ctx context.Context, d *schema.ResourceData, meta inte
754754
ss := cloudapiserver.Service{Client: client, Meta: meta, D: d}
755755
initialState := initialState.(string)
756756

757-
if !strings.EqualFold(initialState, cloudapiserver.VMStateStart) {
757+
if !strings.EqualFold(initialState, constant.VMStateStart) {
758758
err := ss.Stop(ctx, datacenterId, d.Id(), serverType)
759759
if err != nil {
760760
return diag.FromErr(err)
@@ -835,7 +835,7 @@ func resourceServerUpdate(ctx context.Context, d *schema.ResourceData, meta inte
835835
diags := diag.FromErr(fmt.Errorf("could not retrieve server vmState: %w", err))
836836
return diags
837837
}
838-
if strings.EqualFold(currentVmState, cloudapiserver.CubeVMStateStop) && !d.HasChange("vm_state") {
838+
if strings.EqualFold(currentVmState, constant.CubeVMStateStop) && !d.HasChange("vm_state") {
839839
diags := diag.FromErr(fmt.Errorf("cannot update a suspended Cube Server, must change the state to RUNNING first"))
840840
return diags
841841
}
@@ -1126,8 +1126,8 @@ func resourceServerUpdate(ctx context.Context, d *schema.ResourceData, meta inte
11261126
}
11271127

11281128
_, newVmState := d.GetChange("vm_state")
1129-
if strings.EqualFold(serverType, cloudapiserver.CubeServerType) && strings.EqualFold(newVmState.(string), cloudapiserver.CubeVMStateStop) {
1130-
err := ss.Stop(ctx, dcId, d.Id(), cloudapiserver.CubeServerType)
1129+
if strings.EqualFold(serverType, constant.CubeType) && strings.EqualFold(newVmState.(string), constant.CubeVMStateStop) {
1130+
err := ss.Stop(ctx, dcId, d.Id(), constant.CubeType)
11311131
if err != nil {
11321132
return diag.FromErr(err)
11331133
}

ionoscloud/resource_server_cube.go

Lines changed: 21 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cloudapi/cloudapiserver"
1919
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/slice"
2020
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
21+
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
2122
)
2223

2324
func resourceCubeServer() *schema.Resource {
@@ -223,7 +224,7 @@ func resourceCubeServer() *schema.Resource {
223224
Optional: true,
224225
Computed: true,
225226
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)),
227228
},
228229
"nic": {
229230
Type: schema.TypeList,
@@ -374,14 +375,8 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
374375
server := ionoscloud.Server{
375376
Properties: &ionoscloud.ServerProperties{},
376377
}
377-
volume := ionoscloud.VolumeProperties{}
378378

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
385380

386381
dcId := d.Get("datacenter_id").(string)
387382

@@ -396,75 +391,9 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
396391
server.Properties.AvailabilityZone = &vStr
397392
}
398393

399-
serverType := cloudapiserver.CubeServerType
394+
serverType := constant.CubeType
400395
server.Properties.Type = &serverType
401396

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-
468397
if _, ok := d.GetOk("boot_cdrom"); ok {
469398
resId := d.Get("boot_cdrom").(string)
470399
server.Properties.BootCdrom = &ionoscloud.ResourceReference{
@@ -477,36 +406,17 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
477406
return diags
478407
}
479408

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)
508414
return diags
509415
}
416+
image, imageAlias, err = getImage(ctx, client, d, *volume)
417+
if err != nil {
418+
return diag.FromErr(err)
419+
}
510420

511421
if image != "" {
512422
volume.Image = &image
@@ -524,7 +434,7 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
524434
Volumes: &ionoscloud.AttachedVolumes{
525435
Items: &[]ionoscloud.Volume{
526436
{
527-
Properties: &volume,
437+
Properties: volume,
528438
},
529439
},
530440
},
@@ -537,7 +447,6 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
537447
nic := ionoscloud.Nic{
538448
Properties: &ionoscloud.NicProperties{},
539449
}
540-
var err error
541450
if _, ok := d.GetOk("nic"); ok {
542451
nic, err = cloudapinic.GetNicFromSchema(d, "nic.0.")
543452
if err != nil {
@@ -660,7 +569,7 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
660569
ss := cloudapiserver.Service{Client: client, Meta: meta, D: d}
661570
initialState := initialState.(string)
662571

663-
if strings.EqualFold(initialState, cloudapiserver.CubeVMStateStop) {
572+
if strings.EqualFold(initialState, constant.CubeVMStateStop) {
664573
err := ss.Stop(ctx, dcId, d.Id(), serverType)
665574
if err != nil {
666575
return diag.FromErr(err)
@@ -830,14 +739,14 @@ func resourceCubeServerUpdate(ctx context.Context, d *schema.ResourceData, meta
830739
diags := diag.FromErr(fmt.Errorf("could not retrieve server vmState: %w", err))
831740
return diags
832741
}
833-
if strings.EqualFold(currentVmState, cloudapiserver.CubeVMStateStop) && !d.HasChange("vm_state") {
742+
if strings.EqualFold(currentVmState, constant.CubeVMStateStop) && !d.HasChange("vm_state") {
834743
diags := diag.FromErr(fmt.Errorf("cannot update a suspended Cube Server, must change the state to RUNNING first"))
835744
return diags
836745
}
837746

838747
// 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)
841750
if err != nil {
842751
diags := diag.FromErr(err)
843752
return diags
@@ -1058,10 +967,10 @@ func resourceCubeServerUpdate(ctx context.Context, d *schema.ResourceData, meta
1058967
}
1059968

1060969
// 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) {
1062971
_, 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)
1065974
if err != nil {
1066975
diags := diag.FromErr(err)
1067976
return diags

ionoscloud/resource_server_cube_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
1313
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
14-
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cloudapi/cloudapiserver"
1514
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
1615
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
1716

@@ -175,7 +174,7 @@ func TestAccCubeServerBasic(t *testing.T) {
175174
{
176175
Config: testAccCheckCubeServerSuspend,
177176
Check: resource.ComposeTestCheckFunc(
178-
resource.TestCheckResourceAttr(constant.ServerCubeResource+"."+constant.ServerTestResource, "vm_state", cloudapiserver.CubeVMStateStop),
177+
resource.TestCheckResourceAttr(constant.ServerCubeResource+"."+constant.ServerTestResource, "vm_state", constant.CubeVMStateStop),
179178
resource.TestCheckResourceAttrPair(constant.DataSource+"."+constant.ServerCubeResource+"."+constant.ServerDataSourceById, "vm_state", constant.ServerCubeResource+"."+constant.ServerTestResource, "vm_state"),
180179
),
181180
},
@@ -186,7 +185,7 @@ func TestAccCubeServerBasic(t *testing.T) {
186185
{
187186
Config: testAccCheckCubeServerResume,
188187
Check: resource.ComposeTestCheckFunc(
189-
resource.TestCheckResourceAttr(constant.ServerCubeResource+"."+constant.ServerTestResource, "vm_state", cloudapiserver.VMStateStart),
188+
resource.TestCheckResourceAttr(constant.ServerCubeResource+"."+constant.ServerTestResource, "vm_state", constant.VMStateStart),
190189
resource.TestCheckResourceAttrPair(constant.DataSource+"."+constant.ServerCubeResource+"."+constant.ServerDataSourceById, "vm_state", constant.ServerCubeResource+"."+constant.ServerTestResource, "vm_state"),
191190
),
192191
},

ionoscloud/resource_server_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"testing"
1111

1212
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
13-
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cloudapi/cloudapiserver"
1413
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
1514

1615
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
@@ -229,14 +228,14 @@ func TestAccServerBasic(t *testing.T) {
229228
{
230229
Config: testAccCheckServerConfigShutdown,
231230
Check: resource.ComposeTestCheckFunc(
232-
resource.TestCheckResourceAttr(constant.ServerResource+"."+constant.ServerTestResource, "vm_state", cloudapiserver.VMStateStop),
231+
resource.TestCheckResourceAttr(constant.ServerResource+"."+constant.ServerTestResource, "vm_state", constant.VMStateStop),
233232
resource.TestCheckResourceAttrPair(constant.DataSource+"."+constant.ServerResource+"."+constant.ServerDataSourceById, "vm_state", constant.ServerResource+"."+constant.ServerTestResource, "vm_state"),
234233
),
235234
},
236235
{
237236
Config: testAccCheckServerConfigPowerOn,
238237
Check: resource.ComposeTestCheckFunc(
239-
resource.TestCheckResourceAttr(constant.ServerResource+"."+constant.ServerTestResource, "vm_state", cloudapiserver.VMStateStart),
238+
resource.TestCheckResourceAttr(constant.ServerResource+"."+constant.ServerTestResource, "vm_state", constant.VMStateStart),
240239
resource.TestCheckResourceAttrPair(constant.DataSource+"."+constant.ServerResource+"."+constant.ServerDataSourceById, "vm_state", constant.ServerResource+"."+constant.ServerTestResource, "vm_state"),
241240
),
242241
},

ionoscloud/resource_server_vcpu.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
9-
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cloudapi/cloudapiserver"
109
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
1110
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
1211
)
@@ -212,7 +211,7 @@ func resourceVCPUServer() *schema.Resource {
212211
Optional: true,
213212
Computed: true,
214213
Description: "Sets the power state of the vcpu server. Possible values: `RUNNING` or `SHUTOFF`.",
215-
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{cloudapiserver.VMStateStart, cloudapiserver.VMStateStop}, true)),
214+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{constant.VMStateStart, constant.VMStateStop}, true)),
216215
},
217216
"nic": {
218217
Type: schema.TypeList,

ionoscloud/resource_server_vcpu_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1616
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
17-
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cloudapi/cloudapiserver"
1817
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
1918
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
2019
)
@@ -56,7 +55,7 @@ func TestAccServerVCPUBasic(t *testing.T) {
5655
Config: testAccCheckServerVCPUShutDown,
5756
Check: resource.ComposeTestCheckFunc(
5857
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "name", constant.ServerTestResource),
59-
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "vm_state", cloudapiserver.VMStateStop),
58+
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "vm_state", constant.VMStateStop),
6059
),
6160
},
6261
{
@@ -65,7 +64,7 @@ func TestAccServerVCPUBasic(t *testing.T) {
6564
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "name", constant.ServerTestResource),
6665
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "cores", "2"),
6766
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "ram", "2048"),
68-
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "vm_state", cloudapiserver.VMStateStop),
67+
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "vm_state", constant.VMStateStop),
6968
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "availability_zone", "ZONE_1"),
7069
resource.TestCheckResourceAttrSet(constant.ServerVCPUResource+"."+constant.ServerTestResource, "cpu_family"),
7170
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "type", constant.VCPUType),
@@ -75,7 +74,7 @@ func TestAccServerVCPUBasic(t *testing.T) {
7574
Config: testAccCheckServerVCPUPowerOn,
7675
Check: resource.ComposeTestCheckFunc(
7776
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "name", constant.ServerTestResource),
78-
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "vm_state", cloudapiserver.VMStateStart),
77+
resource.TestCheckResourceAttr(constant.ServerVCPUResource+"."+constant.ServerTestResource, "vm_state", constant.VMStateStart),
7978
),
8079
},
8180
{

0 commit comments

Comments
 (0)