Skip to content

Fix snapshot schedule UUID #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/provider/hypercore_vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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. <br>" +
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 4 additions & 6 deletions internal/utils/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type VM struct {
tags *[]string
vcpu *int32
memory *int64
snapshotScheduleUUID *string
snapshotScheduleUUID string
powerState *string
strictAffinity bool
preferredNodeUUID string
Expand All @@ -88,7 +88,7 @@ func GetVMStruct(
_tags *[]string,
_vcpu *int32,
_memory *int64,
_snapshotScheduleUUID *string,
_snapshotScheduleUUID string,
_powerState *string,
_strictAffinity bool,
_preferredNodeUUID string,
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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"]
Expand Down
Loading