model

package
v0.1.25 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package model holds the types shared by discovery sources and the report.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonical

func Canonical(v any) (string, bool)

Canonical is Scalar for scalars and compact JSON (object keys sorted) for lists and objects, so that any non-null value can be compared. Null is reported as absent.

func DecodeJSON

func DecodeJSON(data []byte, v any) error

DecodeJSON decodes data keeping numbers as json.Number, so identities compare exactly regardless of size. data must hold one JSON value: a second object on the same line (a plugin's error line without its newline) would otherwise be dropped silently.

func DecodeJSONUnique added in v0.1.14

func DecodeJSONUnique(data []byte, v any) error

DecodeJSONUnique is DecodeJSON for input that tforphans does not trust to be unambiguous (plugin lines): a key repeated in one object is an error, since encoding/json would keep the last value and {"done":false,"done":true} would mean done. Keys of the top-level object also may not repeat in another case: they fill struct fields, which encoding/json matches ignoring case ({"type":"a","Type":"b"}). Nested objects decode into maps, where "Env" and "env" are two tags.

func ProviderFamily

func ProviderFamily(addr string) string

ProviderFamily is the name SameProvider compares: namespace/type, with providers that share resource types under one name (google-beta is hashicorp/google).

func ReasonLink(code string) string

ReasonLink points at the public reference. Callers pass internal codes.

func SameProvider

func SameProvider(a, b string) bool

SameProvider reports whether two provider addresses can name the same provider. An unknown address ("") matches any. Hosts that mirror one namespace (the Terraform and OpenTofu registries) are ignored, a short "aws" means hashicorp/aws, and providers known to share resource types and objects (google-beta, the moved hashicorp/github) count as one.

func Scalar

func Scalar(v any) (string, bool)

Scalar returns the canonical string form of a JSON scalar decoded with DecodeJSON. Nulls, objects and arrays are reported as not scalar.

func SortedKeys

func SortedKeys(m map[string]any) []string

SortedKeys returns map keys in a stable order.

func TargetOf added in v0.1.23

func TargetOf(addr string) string

TargetOf reduces a server address (https://user:pw@vault.example:8200/x) to its host and port, "" when it has no usable host. Nothing else of the address is kept.

func ValidTarget added in v0.1.23

func ValidTarget(s string) bool

ValidTarget reports whether s can be a Resource.Target: a host name or address with an optional port. A URL, user name or query could carry a secret and is refused.

Types

type ReasonDetails added in v0.1.15

type ReasonDetails struct {
	ReasonCode  string   `json:"reason_code,omitempty"`
	Action      string   `json:"action,omitempty"`
	MissingKeys []string `json:"missing_keys,omitempty"`
}

ReasonDetails is the stable, additive machine-readable explanation. MissingKeys contains names only, never identity or argument values.

func Reason added in v0.1.15

func Reason(code string, keys ...string) ReasonDetails

Reason attaches an action only when the cause identifies a specific remedy.

type Resource

type Resource struct {
	Type string `json:"type"`
	// AltTypes are other resource types that may manage the same object,
	// such as the type's name in an older provider version or in another
	// provider for the same platform. State under any of them counts.
	AltTypes []string `json:"alt_types,omitempty"`
	// AltIdentity overrides Identity for an alternative type whose state
	// keys the same object differently.
	AltIdentity map[string]map[string]any `json:"-"`
	Identity    map[string]any            `json:"identity"`
	DisplayName string                    `json:"display_name"`
	// BaseName, when set, is the object's own name where DisplayName adds
	// to it ("web" for "web (pve1)"): import addresses and ignore rules by
	// name use it.
	BaseName string `json:"-"`
	// TagName is the object's Name tag, read for adopt -names-from-tags:
	// it names the import address before BaseName and DisplayName.
	TagName string `json:"-"`
	// ScopeName says for people where the object lives: "zone example.com",
	// "project my-project".
	ScopeName string `json:"scope,omitempty"`
	// Target says, without secrets, where the object was found when the
	// source has no scope of its own: the host (and port) of the Vault or
	// Proxmox server. A baseline never calls a finding known when it came
	// from another target.
	Target string `json:"target,omitempty"`
	// ImportID is an optional legacy import ID. When set, generated import
	// blocks use `id` instead of `identity`.
	ImportID string `json:"import_id"`
	// RequiredKeys lists identity attributes that must be present and equal
	// in state for the resource to count as managed. Nil means all keys.
	RequiredKeys []string `json:"-"`
	// AttributeKeys are the RequiredKeys that are also resource attributes,
	// checked against state that has no identity; nil means the primary
	// keys are derived from RequiredKeys (or "id", or all keys).
	AttributeKeys []string `json:"-"`
	// Provider is the local provider name used to list the resource, when it
	// is not implied by the type prefix (import blocks then need it).
	Provider string `json:"provider"`
	// ProviderSource is the address of the provider that listed the
	// resource (registry.terraform.io/hashicorp/aws), "" when unknown (a
	// plugin that does not say). State of another provider never manages
	// it, except under AltTypes.
	ProviderSource string `json:"provider_source"`
	// UndeclaredProvider is the provider address when the root module does
	// not declare the provider at all (only child modules do).
	UndeclaredProvider string `json:"undeclared_provider"`
	// ImportNote is a comment for the resource's import block.
	ImportNote string `json:"-"`
	// Attributes are the resource's attributes that were asked for
	// (-include-attrs, or attr. conditions of ignore rules).
	Attributes map[string]any `json:"attributes,omitempty"`
	// ListAddress is the list block that found it (terraform query only).
	ListAddress string `json:"list_address"`
	// Scope restricts which state instances may manage the object, for
	// providers whose state does not record where an object lives (the
	// Proxmox cluster). Nil means no restriction.
	Scope *Scope `json:"-"`
	// Source is the root module directory or plugin that reported it.
	Source   string `json:"source"`
	Baseline string `json:"baseline,omitempty"`
}

Resource is an object that exists in the real infrastructure, as reported by `terraform query` or by a plugin.

func (Resource) IdentityString

func (r Resource) IdentityString() string

IdentityString renders the identity as "k=v, k=v" for humans.

func (Resource) Key

func (r Resource) Key() string

Key identifies a resource for de-duplication across sources. Names and values are quoted: a value from an API or a plugin may hold any byte, and must not pass for another key.

type ResponseError added in v0.1.14

type ResponseError struct{ Err error }

ResponseError marks a response that could not be decoded or validated. Its message is unchanged so callers can keep their usual error reporting.

func (*ResponseError) Error added in v0.1.14

func (e *ResponseError) Error() string

func (*ResponseError) Unwrap added in v0.1.14

func (e *ResponseError) Unwrap() error

type Scope

type Scope struct {
	// Attrs maps resource types to the state attribute holding the
	// location, such as a Proxmox node.
	Attrs map[string]string
	// Values are the locations of the scanned place (its nodes).
	Values map[string]bool
	// Unverifiable marks objects whose state carries no location at all.
	Unverifiable bool
	// Trusted is the user's statement that every state given belongs to
	// the scanned place. Locations in state only rule instances out (a node
	// the place does not have); they cannot prove that an instance belongs,
	// since node names repeat across clusters. Without Trusted, matches are
	// unverified.
	Trusted bool
	// Also is the scope of another copy of the same object (JoinScope):
	// both must hold.
	Also *Scope
}

Scope tells which state instances can belong to the scanned place.

func JoinScope added in v0.1.11

func JoinScope(a, b *Scope) *Scope

JoinScope returns the scope of an object listed twice: an instance must pass both scopes, and a match is verified only if both say so. The order of the copies does not matter, and neither scope is changed.

Jump to

Keyboard shortcuts

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