schema

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReferencesFromState

func ReferencesFromState(attrs []Attribute, state State) []string

ReferencesFromState returns the instance names stored as references in the state, by matching reference attributes from the schema against state keys. Unlike ReferencesOf, this works from serialised state without needing the live struct with populated interface fields.

func ReferencesOf

func ReferencesOf(resource any) []string

ReferencesOf walks a resource config struct and returns the [ResourceInstance.Name] of every non-nil ResourceInstance reference field. The result is the set of dependency names that must be applied before this resource.

func ValidateName

func ValidateName(name string) error

ValidateName checks that name is a valid resource type name or instance label. Names may contain lowercase letters, digits and underscores, and must start with a letter or underscore. Dots and hyphens are not allowed. Names that collide with Terraform/HCL reserved words are rejected.

func ValidateRefs

func ValidateRefs(resource any) error

ValidateRefs walks a resource config struct and validates every ResourceInstance reference field against its struct tags:

  • required:"" — the reference must be non-nil
  • type:"<name>" — the referenced instance's [Resource.Name] must match

It returns a combined error for all failing fields, or nil if valid.

func ValidateRequired

func ValidateRequired(resource any) error

ValidateRequired checks that every field tagged required:"" (without a default:"" tag) has a non-zero value. Reference (interface) fields are skipped — those are handled by ValidateRefs.

Types

type Action

type Action string

Action describes the kind of change planned for a resource.

const (
	ActionCreate  Action = "create"
	ActionUpdate  Action = "update"
	ActionDestroy Action = "destroy"
	ActionNoop    Action = "noop"
)

type Attribute

type Attribute struct {
	// Name is the unique name of the field within the resource schema.
	Name string `json:"name"`

	// Type is the value type (e.g. "string", "int", "bool", "duration",
	// "[]string").
	Type string `json:"type"`

	// Description is a human-readable explanation of the field.
	Description string `json:"description,omitempty"`

	// Required indicates the field must be set by the caller.
	Required bool `json:"required,omitempty"`

	// Default is the value used when the caller does not set the field.
	// It must be assignable to Type.
	Default any `json:"default,omitempty"`

	// Sensitive marks the field as containing secrets that should not
	// appear in logs or plan output.
	Sensitive bool `json:"sensitive,omitempty"`

	// ReadOnly marks the field as computed by the provider. It is not
	// settable by the caller and is populated during [Apply].
	ReadOnly bool `json:"readonly,omitempty"`

	// Reference indicates this attribute is a dependency on another
	// resource instance, resolved by name at decode time.
	Reference bool `json:"reference,omitempty"`
}

Attribute describes a single configuration field within a [Schema].

func AttributesOf

func AttributesOf(resource any) []Attribute

Attributes uses reflection to convert a struct into a slice of Attribute, reading struct tags to determine each field's properties:

  • name:"<name>" — attribute name (required; omitted or name:"-" skips the field)
  • help:"<text>" — human-readable description
  • default:"<value>" — default value (field is optional when present)
  • required:"" — marks the attribute as required (ignored if default is set)
  • type:"<type>" — overrides the inferred type string (e.g. "file", "url")
  • sensitive:"" — marks the field as containing secrets; values are redacted in plan output, logs and state serialisation
  • embed:"" with prefix:"<prefix>" — flatten nested struct, prepending prefix to all child attribute names

type Change

type Change struct {
	// Field is the attribute name.
	Field string

	// Old is the current value (nil on create).
	Old any

	// New is the desired value (nil on destroy).
	New any
}

Change describes a single field-level diff within a Plan.

type CreateResourceInstanceRequest

type CreateResourceInstanceRequest struct {
	Name string `json:"name" arg:"" help:"Instance name as resource.label (e.g. \"httpserver.main\")"`
}

CreateResourceInstanceRequest asks the manager to instantiate a resource.

type CreateResourceInstanceResponse

type CreateResourceInstanceResponse struct {
	Instance InstanceMeta `json:"instance"`
}

CreateResourceInstanceResponse contains the newly created instance metadata.

func (CreateResourceInstanceResponse) String

type DestroyResourceInstanceRequest

type DestroyResourceInstanceRequest struct {
	Name    string `json:"name"`              // instance name
	Cascade bool   `json:"cascade,omitempty"` // also destroy dependents in topological order
}

DestroyResourceInstanceRequest asks the manager to tear down an instance.

type DestroyResourceInstanceResponse

type DestroyResourceInstanceResponse struct {
	Instances []InstanceMeta `json:"instances"`
}

DestroyResourceInstanceResponse confirms destruction and returns the metadata of every destroyed instance (in destruction order).

func (DestroyResourceInstanceResponse) String

type GetResourceInstanceResponse

type GetResourceInstanceResponse struct {
	Instance InstanceMeta `json:"instance"`
}

GetResourceInstanceResponse contains the metadata for a single instance.

func (GetResourceInstanceResponse) String

type InstanceMeta

type InstanceMeta struct {
	Name       string   `json:"name"`
	Resource   string   `json:"resource"`
	ReadOnly   bool     `json:"readonly,omitempty"`
	State      State    `json:"state,omitempty"`
	References []string `json:"references,omitempty"`
}

InstanceMeta describes a resource instance without exposing the full interface.

type ListResourcesRequest

type ListResourcesRequest struct {
	Type *string `json:"type,omitempty" arg:"" optional:"" help:"Resource type name (e.g. \"httpserver\")"` // filter by type name
}

ListResourcesRequest filters the set of resource types to return.

func (ListResourcesRequest) Query

func (r ListResourcesRequest) Query() url.Values

type ListResourcesResponse

type ListResourcesResponse struct {
	Provider    string         `json:"provider"`
	Description string         `json:"description,omitempty"`
	Version     string         `json:"version,omitempty"`
	Resources   []ResourceMeta `json:"resources"`
}

ListResourcesResponse contains the resource types the provider can manage.

func (ListResourcesResponse) String

func (r ListResourcesResponse) String() string

type Plan

type Plan struct {
	// Action is the high-level operation (create, update, destroy, noop).
	Action Action

	// Changes lists the individual field-level diffs. It is empty when
	// Action is Noop or Destroy.
	Changes []Change
}

Plan describes the changes that [Resource.Apply] would make.

type Provider

type Provider interface {
	// Name returns the unique name for the provider.
	Name() string

	// Description returns a human-readable summary of the provider.
	Description() string

	// Resources returns the set of resources this provider can manage.
	Resources() []Resource
}

Provider is a named provider that registers one or more resource types.

type Resolver

type Resolver func(name string) ResourceInstance

Resolver maps a resource-instance name (as stored by StateOf) to the live ResourceInstance, or returns nil if it does not exist.

type Resource

type Resource interface {
	// Name returns a unique name for this resource type, used in
	// configuration and plan output.
	Name() string

	// Schema returns the attribute definitions for this resource type,
	// used for validation and documentation.
	Schema() []Attribute

	// New creates a named resource instance.
	// The returned instance is not yet applied; call
	// [ResourceInstance.Apply] to materialise it.
	New(name string) (ResourceInstance, error)
}

Resource describes a kind of resource a provider can manage. It acts as a factory: it advertises the configuration schema and creates new instances.

type ResourceInstance

type ResourceInstance interface {
	// Name returns the logical name of this resource instance
	// (e.g. "main", "api-v1").
	Name() string

	// Resource returns the resource type that created this instance.
	Resource() Resource

	// Validate decodes the incoming [State] using the given [Resolver],
	// checks that the resulting configuration is complete and consistent,
	// and returns the typed configuration value (as any) for use by
	// [Plan] and [Apply].
	Validate(context.Context, State, Resolver) (any, error)

	// Plan computes the difference between the validated configuration
	// (returned by [Validate]) and the instance's current state,
	// returning a set of planned changes without modifying anything.
	Plan(context.Context, any) (Plan, error)

	// Apply materialises the resource using the validated configuration
	// (returned by [Validate]), creating or updating it to match the
	// desired state.
	Apply(context.Context, any) error

	// Destroy tears down the resource and releases its backing
	// infrastructure. It returns an error if the resource cannot be
	// cleanly removed.
	Destroy(context.Context) error

	// Read returns the current live state of the instance. This is
	// used to refresh the manager's cached state and to detect drift
	// from the desired configuration. Implementations may re-read
	// computed fields (e.g. endpoint URLs) from the running infra.
	Read(context.Context) (State, error)

	// References returns the labels of other resources this resource
	// depends on. The runtime must ensure those resources are applied
	// first and destroyed last.
	References() []string
}

ResourceInstance represents a single configured unit of infrastructure with a full create/read/update/delete lifecycle.

type ResourceMeta

type ResourceMeta struct {
	Name       string         `json:"name"`
	Attributes []Attribute    `json:"attributes"`
	Instances  []InstanceMeta `json:"instances"`
}

ResourceMeta describes a resource type without exposing the full interface.

type State

type State map[string]any

State is an opaque snapshot of a resource after it has been applied.

func StateOf

func StateOf(resource any) State

StateOf uses reflection to extract the current field values from a struct and return them as a State map keyed by attribute name. It follows the same struct-tag rules as [Attributes]: fields without a name tag or with name:"-" are skipped, and embedded structs are flattened with their prefix.

Interface fields that implement ResourceInstance (or any interface with a Name() string method) are stored by name. Other interface fields are skipped.

Duration values are stored as their string representation (e.g. "5m0s").

func WritableStateOf

func WritableStateOf(resource any) State

WritableStateOf is like StateOf but excludes readonly fields. It is intended for plan diffs where computed values should not appear.

func (State) Decode

func (s State) Decode(v any, resolve Resolver) error

Decode sets the fields of the struct pointed to by v from the state map, using the same struct-tag conventions as StateOf and [Attributes]. v must be a pointer to a struct. Fields whose keys are absent from the state are left untouched.

Interface fields that hold resource references are resolved using the Resolver. When the resolver is nil, any interface field whose key is present in the state will cause an error. When a resolver returns nil for a field marked required:"", an error is returned.

type UpdateResourceInstanceRequest

type UpdateResourceInstanceRequest struct {
	Attributes State `json:"attributes,omitempty"` // desired attribute values
	Apply      bool  `json:"apply"`                // false = plan only, true = apply changes
}

UpdateResourceInstanceRequest is the unified request for plan or apply. When Apply is false (the default), only a plan is computed. When true, the planned changes are applied. Attributes contains the desired attribute values keyed by attribute name.

type UpdateResourceInstanceResponse

type UpdateResourceInstanceResponse struct {
	Instance InstanceMeta `json:"instance"`
	Plan     Plan         `json:"plan"`
}

UpdateResourceInstanceResponse contains the instance metadata, the computed plan, and (when apply was requested) the resulting state.

func (UpdateResourceInstanceResponse) String

Directories

Path Synopsis
Package schematest provides shared mock implementations of schema.Resource and schema.ResourceInstance for use in tests.
Package schematest provides shared mock implementations of schema.Resource and schema.ResourceInstance for use in tests.

Jump to

Keyboard shortcuts

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