From e9f1a50ccedcfc6dbe21788bceda5514a1abec81 Mon Sep 17 00:00:00 2001 From: Justin Cinkelj Date: Fri, 9 May 2025 00:09:03 +0200 Subject: [PATCH 1/2] Test snapshot_schedule_uuid Signed-off-by: Justin Cinkelj --- ...m_resource__snapshot_schedule__acc_test.go | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 internal/provider/tests/acceptance/hypercore_vm_resource__snapshot_schedule__acc_test.go diff --git a/internal/provider/tests/acceptance/hypercore_vm_resource__snapshot_schedule__acc_test.go b/internal/provider/tests/acceptance/hypercore_vm_resource__snapshot_schedule__acc_test.go new file mode 100644 index 0000000..14d2a76 --- /dev/null +++ b/internal/provider/tests/acceptance/hypercore_vm_resource__snapshot_schedule__acc_test.go @@ -0,0 +1,102 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package acceptance + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccHypercoreVMResourceSnapshotSchedule(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Create and Read testing + { + Config: testConfig_NoSnapshotScheduleUUID("testtf-vm"), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("hypercore_vm.test", "description", "testtf-vm-description"), + resource.TestCheckResourceAttr("hypercore_vm.test", "memory", "4096"), + resource.TestCheckResourceAttr("hypercore_vm.test", "group", "testtf"), + resource.TestCheckNoResourceAttr("hypercore_vm.test", "clone"), + resource.TestCheckResourceAttr("hypercore_vm.test", "name", "testtf-vm"), + resource.TestCheckResourceAttr("hypercore_vm.test", "vcpu", "4"), + resource.TestCheckResourceAttr("hypercore_vm.test", "snapshot_schedule_uuid", ""), + // resource.TestCheckResourceAttr("hypercore_vm.test", "power_state", requested_power_state), + ), + }, + // TODO make ImportState test pass again. + /* + // ImportState testing + { + ResourceName: "hypercore_vm.test", + ImportState: true, + ImportStateVerify: true, + // This is not normally necessary, but is here because this + // example code does not have an actual upstream service. + // Once the Read method is able to refresh information from + // the upstream service, this can be removed. + ImportStateVerifyIgnore: []string{ + "id", + // TODO do not ignore below attributes + "name", + "description", + "group", + "vcpu", + "memory", + "disk_size", + "clone.source_vm_uuid", + "clone.user_data", + "clone.meta_data", + "power_state", + }, + }, + */ + // Update and Read testing + { + Config: testConfig_NoSnapshotScheduleUUID("testtf-vm"), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("hypercore_vm.test", "name", "testtf-vm"), + resource.TestCheckResourceAttr("hypercore_vm.test", "description", "testtf-vm-description"), + resource.TestCheckResourceAttr("hypercore_vm.test", "group", "testtf"), + resource.TestCheckNoResourceAttr("hypercore_vm.test", "clone"), + resource.TestCheckResourceAttr("hypercore_vm.test", "vcpu", "4"), + resource.TestCheckResourceAttr("hypercore_vm.test", "memory", "4096"), + resource.TestCheckResourceAttr("hypercore_vm.test", "snapshot_schedule_uuid", ""), + // resource.TestCheckResourceAttr("hypercore_vm.test", "power_state", requested_power_state), + ), + }, + // Delete testing automatically occurs in TestCase + }, + }) +} + +// func testConfig_EmptySnapshotScheduleUUID(vm_name string) string { +// return fmt.Sprintf(` +// resource "hypercore_vm" "test" { +// name = %[1]q +// group = "testtf" +// vcpu = 4 +// memory = 4096 +// description = "testtf-vm-description" +// snapshot_schedule_uuid = "" +// } +// `, vm_name) +// } + +func testConfig_NoSnapshotScheduleUUID(vm_name string) string { + return fmt.Sprintf(` +resource "hypercore_vm" "test" { + name = %[1]q + group = "testtf" + vcpu = 4 + memory = 4096 + description = "testtf-vm-description" + // snapshot_schedule_uuid = "" +} +`, vm_name) +} From 532fca9843e7151bb628ce283510fac12fcdba55 Mon Sep 17 00:00:00 2001 From: Justin Cinkelj Date: Fri, 9 May 2025 00:12:17 +0200 Subject: [PATCH 2/2] fix snapshot_schedule_uuid Fixes #43 Signed-off-by: Justin Cinkelj --- internal/provider/hypercore_vm_resource.go | 5 ++++- internal/utils/vm.go | 10 ++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/provider/hypercore_vm_resource.go b/internal/provider/hypercore_vm_resource.go index 4d1169e..d82da03 100644 --- a/internal/provider/hypercore_vm_resource.go +++ b/internal/provider/hypercore_vm_resource.go @@ -17,6 +17,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -117,6 +118,8 @@ The provider will currently try to shutdown VM only before VM delete.`, "snapshot_schedule_uuid": schema.StringAttribute{ MarkdownDescription: "UUID of the snapshot schedule to create automatic snapshots", Optional: true, + Computed: true, + Default: stringdefault.StaticString(""), }, "import": schema.SingleNestedAttribute{ MarkdownDescription: "Options for importing a VM through a SMB server or some other HTTP location.
" + @@ -228,7 +231,7 @@ func getVMStruct(data *HypercoreVMResourceModel, vmDescription *string, vmTags * vmTags, data.VCPU.ValueInt32Pointer(), data.Memory.ValueInt64Pointer(), - data.SnapshotScheduleUUID.ValueStringPointer(), + data.SnapshotScheduleUUID.ValueString(), nil, data.AffinityStrategy.StrictAffinity.ValueBool(), data.AffinityStrategy.PreferredNodeUUID.ValueString(), diff --git a/internal/utils/vm.go b/internal/utils/vm.go index c57b804..a1f2633 100644 --- a/internal/utils/vm.go +++ b/internal/utils/vm.go @@ -65,7 +65,7 @@ type VM struct { tags *[]string vcpu *int32 memory *int64 - snapshotScheduleUUID *string + snapshotScheduleUUID string powerState *string strictAffinity bool preferredNodeUUID string @@ -88,7 +88,7 @@ func GetVMStruct( _tags *[]string, _vcpu *int32, _memory *int64, - _snapshotScheduleUUID *string, + _snapshotScheduleUUID string, _powerState *string, _strictAffinity bool, _preferredNodeUUID string, @@ -478,7 +478,7 @@ func (vc *VM) BuildUpdatePayload(changedParams map[string]bool) map[string]any { updatePayload["numVCPU"] = *vc.vcpu } if changed, ok := changedParams["snapshotScheduleUUID"]; ok && changed { - updatePayload["snapshotScheduleUUID"] = *vc.snapshotScheduleUUID + updatePayload["snapshotScheduleUUID"] = vc.snapshotScheduleUUID } affinityStrategy := map[string]any{} @@ -576,9 +576,7 @@ func (vc *VM) GetChangedParams(ctx context.Context, vmFromClient map[string]any) changedParams["powerState"] = desiredPowerState != vmFromClient["state"] } } - if vc.snapshotScheduleUUID != nil { - changedParams["snapshotScheduleUUID"] = *vc.snapshotScheduleUUID != vmFromClient["snapshotScheduleUUID"] - } + changedParams["snapshotScheduleUUID"] = vc.snapshotScheduleUUID != vmFromClient["snapshotScheduleUUID"] hc3AffinityStrategy := AnyToMap(vmFromClient["affinityStrategy"]) changedParams["strictAffinity"] = vc.strictAffinity != hc3AffinityStrategy["strictAffinity"]