Skip to content

Implement hypercore_vm_snapshot #16

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 3 commits into from
Apr 2, 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
1 change: 1 addition & 0 deletions docs/data-sources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Read-Only:
- `disks` (Attributes List) List of disks (see [below for nested schema](#nestedatt--vms--disks))
- `name` (String)
- `power_state` (String)
- `snapshot_schedule_uuid` (String) UUID of the applied snapshot schedule for creating automated snapshots
- `uuid` (String)

<a id="nestedatt--vms--affinity_strategy"></a>
Expand Down
6 changes: 4 additions & 2 deletions docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ resource "hypercore_vm" "myvm" {
name = local.vm_name
description = "some description"

vcpu = 4
memory = 4096 # MiB
vcpu = 4
memory = 4096 # MiB
snapshot_schedule_uuid = data.hypercore_vm.clone_source_vm.vms.0.snapshot_schedule_uuid

clone = {
source_vm_uuid = data.hypercore_vm.clone_source_vm.vms.0.uuid
Expand Down Expand Up @@ -63,6 +64,7 @@ output "vm_uuid" {
- `description` (String) Description of this VM
- `group` (String) Group/tag to create this VM in
- `memory` (Number) Memory (RAM) size in `MiB`: If the cloned VM was already created <br>and it's memory was modified, the cloned VM will be rebooted (either gracefully or forcefully)
- `snapshot_schedule_uuid` (String) UUID of the snapshot schedule to create automatic snapshots
- `vcpu` (Number) Number of CPUs on this VM. If the cloned VM was already created and it's <br>`VCPU` was modified, the cloned VM will be rebooted (either gracefully or forcefully)

### Read-Only
Expand Down
58 changes: 58 additions & 0 deletions docs/resources/vm_snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "hypercore_vm_snapshot Resource - hypercore"
subcategory: ""
description: |-
Hypercore VM snapshot resource to manage VM snapshots
---

# hypercore_vm_snapshot (Resource)

Hypercore VM snapshot resource to manage VM snapshots

## Example Usage

```terraform
locals {
vm_name = "example-vm-one"
another_vm_name = "example-vm-two"
}

data "hypercore_vm" "example-vm-one" {
name = local.vm_name
}

data "hypercore_vm" "example-vm-two" {
name = local.another_vm_name
}

resource "hypercore_vm_snapshot" "snapshot" {
vm_uuid = data.hypercore_vm.example-vm-one.vms.0.uuid
label = "my-snapshot"
}

resource "hypercore_vm_snapshot" "imported-snapshot" {
vm_uuid = data.hypercore_vm.example-vm-two.vms.0.uuid
}

import {
to = hypercore_vm_snapshot.imported-snapshot
id = "24ab2255-ca77-49ec-bc96-f469cec3affb"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `vm_uuid` (String) VM UUID of which we want to create a snapshot.

### Optional

- `label` (String) Snapshot label.

### Read-Only

- `id` (String) VM snapshot identifier
- `type` (String) Snapshot type. Can be: USER, AUTOMATED, SUPPORT
88 changes: 88 additions & 0 deletions docs/resources/vm_snapshot_schedule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "hypercore_vm_snapshot_schedule Resource - hypercore"
subcategory: ""
description: |-
Hypercore VM snapshot schedule resource to manage VM snapshots
---

# hypercore_vm_snapshot_schedule (Resource)

Hypercore VM snapshot schedule resource to manage VM snapshots

## Example Usage

```terraform
locals {
vm_name = "example-vm-one"
another_vm_name = "example-vm-two"
}

data "hypercore_vm" "example-vm-one" {
name = local.vm_name
}

data "hypercore_vm" "example-vm-two" {
name = local.another_vm_name
}

resource "hypercore_vm_snapshot_schedule" "example-schedule" {
name = "my-schedule"
rules = [
{
name = "first-example-rule",
start_timestamp = "2023-02-01 00:00:00",
frequency = "FREQ=MINUTELY;INTERVAL=1",
local_retention_seconds = 300
},
{
name = "second-example-rule",
start_timestamp = "2023-02-01 00:00:00",
frequency = "FREQ=MINUTELY;INTERVAL=1",
local_retention_seconds = 300
}
]
}

resource "hypercore_vm_snapshot_schedule" "example-schedule-no-rules" {
name = "my-schedule-without-rules"
}

resource "hypercore_vm_snapshot_schedule" "example-schedule-imported" {
name = "my-imported-schedule"
}

import {
to = hypercore_vm_snapshot_schedule.example-schedule-imported
id = "69b21f14-6bb6-4dd5-a6bc-6dec9bd59c96"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Snapshot schedule name.

### Optional

- `rules` (Attributes List) Scheduled snapshot rules. (see [below for nested schema](#nestedatt--rules))

### Read-Only

- `id` (String) Snapshot schedule identifier

<a id="nestedatt--rules"></a>
### Nested Schema for `rules`

Required:

- `frequency` (String) Frequency based on RFC-2445 (FREQ=MINUTELY;INTERVAL=5)
- `local_retention_seconds` (Number) Number of seconds before snapshots are removed
- `name` (String) Rule name
- `start_timestamp` (String) Local timezone timestamp (2010-01-01 00:00:00) of when a snapshot is to be taken

Optional:

- `remote_retention_seconds` (Number) Number of seconds before snapshots are removed. If not set, it'll be the same as `local_retention_seconds`
5 changes: 3 additions & 2 deletions examples/resources/hypercore_vm/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ resource "hypercore_vm" "myvm" {
name = local.vm_name
description = "some description"

vcpu = 4
memory = 4096 # MiB
vcpu = 4
memory = 4096 # MiB
snapshot_schedule_uuid = data.hypercore_vm.clone_source_vm.vms.0.snapshot_schedule_uuid

clone = {
source_vm_uuid = data.hypercore_vm.clone_source_vm.vms.0.uuid
Expand Down
26 changes: 26 additions & 0 deletions examples/resources/hypercore_vm_snapshot/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
locals {
vm_name = "example-vm-one"
another_vm_name = "example-vm-two"
}

data "hypercore_vm" "example-vm-one" {
name = local.vm_name
}

data "hypercore_vm" "example-vm-two" {
name = local.another_vm_name
}

resource "hypercore_vm_snapshot" "snapshot" {
vm_uuid = data.hypercore_vm.example-vm-one.vms.0.uuid
label = "my-snapshot"
}

resource "hypercore_vm_snapshot" "imported-snapshot" {
vm_uuid = data.hypercore_vm.example-vm-two.vms.0.uuid
}

import {
to = hypercore_vm_snapshot.imported-snapshot
id = "24ab2255-ca77-49ec-bc96-f469cec3affb"
}
43 changes: 43 additions & 0 deletions examples/resources/hypercore_vm_snapshot_schedule/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
locals {
vm_name = "example-vm-one"
another_vm_name = "example-vm-two"
}

data "hypercore_vm" "example-vm-one" {
name = local.vm_name
}

data "hypercore_vm" "example-vm-two" {
name = local.another_vm_name
}

resource "hypercore_vm_snapshot_schedule" "example-schedule" {
name = "my-schedule"
rules = [
{
name = "first-example-rule",
start_timestamp = "2023-02-01 00:00:00",
frequency = "FREQ=MINUTELY;INTERVAL=1",
local_retention_seconds = 300
},
{
name = "second-example-rule",
start_timestamp = "2023-02-01 00:00:00",
frequency = "FREQ=MINUTELY;INTERVAL=1",
local_retention_seconds = 300
}
]
}

resource "hypercore_vm_snapshot_schedule" "example-schedule-no-rules" {
name = "my-schedule-without-rules"
}

resource "hypercore_vm_snapshot_schedule" "example-schedule-imported" {
name = "my-imported-schedule"
}

import {
to = hypercore_vm_snapshot_schedule.example-schedule-imported
id = "69b21f14-6bb6-4dd5-a6bc-6dec9bd59c96"
}
40 changes: 23 additions & 17 deletions internal/provider/hypercore_vm_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ type hypercoreVMsDataSourceModel struct {

// hypercoreVMModel maps VM schema data.
type hypercoreVMModel struct {
UUID types.String `tfsdk:"uuid"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
PowerState types.String `tfsdk:"power_state"`
VCPU types.Int32 `tfsdk:"vcpu"`
Memory types.Int64 `tfsdk:"memory"`
Tags []types.String `tfsdk:"tags"`
Disks []HypercoreDiskModel `tfsdk:"disks"`
UUID types.String `tfsdk:"uuid"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
PowerState types.String `tfsdk:"power_state"`
VCPU types.Int32 `tfsdk:"vcpu"`
Memory types.Int64 `tfsdk:"memory"`
SnapshotScheduleUUID types.String `tfsdk:"snapshot_schedule_uuid"`
Tags []types.String `tfsdk:"tags"`
Disks []HypercoreDiskModel `tfsdk:"disks"`
// TODO nics
AffinityStrategy AffinityStrategyModel `tfsdk:"affinity_strategy"`
}
Expand Down Expand Up @@ -92,6 +93,10 @@ func (d *hypercoreVMDataSource) Schema(_ context.Context, _ datasource.SchemaReq
MarkdownDescription: "Memory (RAM) size in MiB",
Optional: true,
},
"snapshot_schedule_uuid": schema.StringAttribute{
MarkdownDescription: "UUID of the applied snapshot schedule for creating automated snapshots",
Computed: true,
},
"description": schema.StringAttribute{
Computed: true,
},
Expand Down Expand Up @@ -226,15 +231,16 @@ func (d *hypercoreVMDataSource) Read(ctx context.Context, req datasource.ReadReq
memory_B := utils.AnyToInteger64(vm["mem"])
memory_MiB := memory_B / 1024 / 1024
hypercoreVMState := hypercoreVMModel{
UUID: types.StringValue(utils.AnyToString(vm["uuid"])),
Name: types.StringValue(utils.AnyToString(vm["name"])),
VCPU: types.Int32Value(int32(utils.AnyToInteger64(vm["numVCPU"]))),
Memory: types.Int64Value(memory_MiB),
Description: types.StringValue(utils.AnyToString(vm["description"])),
PowerState: types.StringValue(utils.AnyToString(vm["state"])), // TODO convert (stopped vs SHUTOFF)
Tags: tags_String,
AffinityStrategy: affinityStrategy,
Disks: disks,
UUID: types.StringValue(utils.AnyToString(vm["uuid"])),
Name: types.StringValue(utils.AnyToString(vm["name"])),
VCPU: types.Int32Value(int32(utils.AnyToInteger64(vm["numVCPU"]))),
Memory: types.Int64Value(memory_MiB),
SnapshotScheduleUUID: types.StringValue(utils.AnyToString(vm["snapshotScheduleUUID"])),
Description: types.StringValue(utils.AnyToString(vm["description"])),
PowerState: types.StringValue(utils.AnyToString(vm["state"])), // TODO convert (stopped vs SHUTOFF)
Tags: tags_String,
AffinityStrategy: affinityStrategy,
Disks: disks,
}
state.Vms = append(state.Vms, hypercoreVMState)
}
Expand Down
26 changes: 18 additions & 8 deletions internal/provider/hypercore_vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ type HypercoreVMResource struct {

// HypercoreVMResourceModel describes the resource data model.
type HypercoreVMResourceModel struct {
Group types.String `tfsdk:"group"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
VCPU types.Int32 `tfsdk:"vcpu"`
Memory types.Int64 `tfsdk:"memory"`
Clone CloneModel `tfsdk:"clone"`
AffinityStrategy AffinityStrategyModel `tfsdk:"affinity_strategy"`
Id types.String `tfsdk:"id"`
Group types.String `tfsdk:"group"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
VCPU types.Int32 `tfsdk:"vcpu"`
Memory types.Int64 `tfsdk:"memory"`
SnapshotScheduleUUID types.String `tfsdk:"snapshot_schedule_uuid"`
Clone CloneModel `tfsdk:"clone"`
AffinityStrategy AffinityStrategyModel `tfsdk:"affinity_strategy"`
Id types.String `tfsdk:"id"`
}

type CloneModel struct {
Expand Down Expand Up @@ -90,6 +91,10 @@ func (r *HypercoreVMResource) Schema(ctx context.Context, req resource.SchemaReq
"and it's memory was modified, the cloned VM will be rebooted (either gracefully or forcefully)",
Optional: true,
},
"snapshot_schedule_uuid": schema.StringAttribute{
MarkdownDescription: "UUID of the snapshot schedule to create automatic snapshots",
Optional: true,
},
"clone": schema.ObjectAttribute{
MarkdownDescription: "" +
"Clone options if the VM is being created as a clone. The `source_vm_uuid` is the UUID of the VM used for cloning, <br>" +
Expand Down Expand Up @@ -207,6 +212,7 @@ func (r *HypercoreVMResource) Create(ctx context.Context, req resource.CreateReq
tags,
data.VCPU.ValueInt32Pointer(),
data.Memory.ValueInt64Pointer(),
data.SnapshotScheduleUUID.ValueStringPointer(),
nil,
data.AffinityStrategy.StrictAffinity.ValueBool(),
data.AffinityStrategy.PreferredNodeUUID.ValueString(),
Expand Down Expand Up @@ -278,6 +284,7 @@ func (r *HypercoreVMResource) Read(ctx context.Context, req resource.ReadRequest
// uiState TODO
data.VCPU = types.Int32Value(int32(utils.AnyToInteger64(hc3_vm["numVCPU"])))
data.Memory = types.Int64Value(utils.AnyToInteger64(hc3_vm["mem"]) / 1024 / 1024)
data.SnapshotScheduleUUID = types.StringValue(utils.AnyToString(hc3_vm["snapshotScheduleUUID"]))

affinityStrategy := utils.AnyToMap(hc3_vm["affinityStrategy"])
data.AffinityStrategy.StrictAffinity = types.BoolValue(utils.AnyToBool(affinityStrategy["strictAffinity"]))
Expand Down Expand Up @@ -335,6 +342,9 @@ func (r *HypercoreVMResource) Update(ctx context.Context, req resource.UpdateReq
if data_state.VCPU != data.VCPU {
updatePayload["numVCPU"] = data.VCPU.ValueInt32()
}
if data_state.SnapshotScheduleUUID != data.SnapshotScheduleUUID {
updatePayload["snapshotScheduleUUID"] = data.SnapshotScheduleUUID.ValueString()
}

affinityStrategy := map[string]any{}
if data_state.AffinityStrategy.StrictAffinity != data.AffinityStrategy.StrictAffinity {
Expand Down
1 change: 1 addition & 0 deletions internal/provider/hypercore_vm_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ resource "hypercore_vm" "test" {
vcpu = 4
memory = 4096
description = "testtf-vm-description"
snapshot_schedule_uuid = ""
// power_state = %[3]q
clone = {
source_vm_uuid = %[2]q
Expand Down
Loading
Loading