image_file

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: MPL-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package image_file implements the hyperv_image_file resource. Wraps the image_file/{get,new,remove}.ps1 contract via the typed hyperv.Client.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() resource.Resource

New is the framework factory.

Types

type Model

type Model struct {
	ID              pathtype.Path `tfsdk:"id"`
	DestinationPath pathtype.Path `tfsdk:"destination_path"`
	URL             *URLConfig    `tfsdk:"url"`
	LocalPath       pathtype.Path `tfsdk:"local_path"`
	Sha256          types.String  `tfsdk:"sha256"`
	SizeBytes       types.Int64   `tfsdk:"size_bytes"`
	KeepOnDestroy   types.Bool    `tfsdk:"keep_on_destroy"`
}

Model is the tfsdk-bound struct backing the resource state. Field tags align with schema.go attribute names; conversion to/from the typed hyperv.ImageFile DTO lives in resource.go.

Three source modes, discriminated by which of URL / LocalPath is set:

  • URL non-nil => url mode (HttpClient fetch)
  • URL nil, LocalPath non-null => local_path mode (runner streams bytes via Connection.StreamFile)
  • URL nil, LocalPath null => host_path mode (verify only)

URL and LocalPath are mutually exclusive; the ConfigValidator on the resource rejects configs that set both. Both URL and LocalPath carry RequiresReplace at the schema layer, so any mode switch destroys and recreates. The Delete path keys on the same discriminators to gate the host-side remove (host_path mode never removes, since the user attested the file already existed).

DestinationPath uses the pathtype.Path custom type so users can write either `C:/foo` or `C:\foo` without the framework rejecting the apply with "Provider produced inconsistent result after apply" when Hyper-V returns the canonical backslash form. ID mirrors destination_path and is also Path so the same semantic-equality covers the Computed mirror's refresh path. LocalPath uses Path for the same slash-folding reason -- users on macOS / Linux runners typically write forward slashes for the local path even when the destination is a Windows-form path.

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

Resource implements hyperv_image_file.

func (*Resource) ConfigValidators added in v0.2.0

func (r *Resource) ConfigValidators(_ context.Context) []resource.ConfigValidator

ConfigValidators rejects mode-attribute combinations that the wire contract can't honor, surfacing a clear attribute-anchored diagnostic at plan time instead of an opaque cmdlet error at apply time.

func (*Resource) Configure

Configure stashes the typed Hyper-V client built by the provider's Configure pass. Skips when ProviderData is nil (validate-time invocation before the provider has resolved its config).

func (*Resource) Create

Create dispatches on source mode (url, local_path, or host_path) and writes the post-create read shape back to state.

url-mode: the provider fetches via HttpClient and verifies the checksum. ErrChecksumMismatch is surfaced on path.Root("url").AtName("checksum") so the diagnostic anchors to the offending attribute, not the resource.

local_path-mode: the provider streams the runner-side file through the active connection backend, then asks new.ps1 to verify the streamed bytes' SHA against the runner-computed value and atomic-rename. A host-side hash mismatch surfaces ErrChecksumMismatch on local_path (transport corruption rather than user-supplied checksum drift).

host_path-mode: the provider verifies the file already exists at destination_path. ErrNotFound is anchored to destination_path.

func (*Resource) Delete

Delete runs remove.ps1 for url-mode and local_path-mode resources -- both modes mean the provider put the file on the host, so removing it on destroy is the symmetric operation. host_path-mode (URL nil AND LocalPath null) leaves the file alone: the user attested it already existed, so removing on destroy would surprise them.

ErrNotFound from RemoveImageFile is treated as success (the file is already gone, no need to error).

func (*Resource) ImportState

ImportState lets `terraform import hyperv_image_file.foo C:\path\file.vhdx` work by treating the import ID as the destination path. The imported resource lands in host_path mode (no url block) -- importing inherently means "this file already exists on the host." Users can convert to url-mode later by adding the block, which will trigger replacement.

func (*Resource) Metadata

Metadata sets the resource's TF type name.

func (*Resource) ModifyPlan added in v0.2.0

ModifyPlan computes the runner-side SHA-256 and size of `local_path` at plan time and writes them into the planned `sha256` / `size_bytes` attributes. This is what makes content changes to the local file (same path, different bytes) surface as a plan diff -- without it, `UseStateForUnknown` would carry the prior values forward and the framework would either skip the Update entirely or reject the apply with a "Provider produced inconsistent result" check on the Computed attribute that didn't match its planned value.

Both attributes must be updated together: a content change generally changes both, and the framework's post-apply consistency check triggers on either one drifting from plan to apply.

Skipped for url-mode and host_path-mode (LocalPath null/unknown), and during destroy (no plan).

func (*Resource) Read

Read fetches the current shape via get.ps1 and reconciles state.

ErrNotFound -> RemoveResource so Terraform plans recreate. ErrUnauthorized / ErrPSExecution -> AddError so a transient fault doesn't silently drop the resource from state.

func (*Resource) Schema

Schema returns the locked-in schema (see schema.go).

func (*Resource) Update

Update is reached only in local_path-mode when the runner-side file's contents change between applies. ModifyPlan recomputes the SHA from disk; if it differs from state, the framework dispatches Update here (every other user-settable field is RequiresReplace). Re-stream the new bytes and verify host-side hash matches.

For url-mode and host_path-mode, every user-settable field is RequiresReplace, so Update is effectively unreachable in those modes -- pass the plan through to state for the framework's Computed propagation machinery.

type URLConfig

type URLConfig struct {
	URL      types.String `tfsdk:"url"`
	Checksum types.String `tfsdk:"checksum"`
}

URLConfig is the user-supplied URL-mode source configuration. Both fields are required when the block is present; the schema-layer Required flag enforces this without a separate config validator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL