entitycore

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package entitycore provides a shared embedded core for Plugin Framework entities: ResourceBase centralizes resource.ResourceWithConfigure Configure wiring, the Metadata method required by resource.Resource, and stores the configured *clients.ProviderClientFactory for use via ResourceBase.Client. Data sources use envelope generics.

Data source patterns

Plugin Framework data sources use **envelope generics** — NewKibanaDataSource or NewElasticsearchDataSource — which eliminate Read orchestration boilerplate. The constructor owns config decode, scoped client resolution, and state persistence. The concrete package provides only a schema factory (without connection blocks), a model that embeds KibanaConnectionField or ElasticsearchConnectionField, and a pure read function that performs the entity-specific API call and model mapping.

Example envelope data source:

type myModel struct {
    entitycore.KibanaConnectionField
    ID types.String `tfsdk:"id"`
}

func readMyEntity(ctx context.Context, client *clients.KibanaScopedClient, model myModel) (myModel, diag.Diagnostics) {
    // API call and model population …
    return model, nil
}

func NewDataSource() datasource.DataSource {
    return entitycore.NewKibanaDataSource[myModel](
        entitycore.ComponentKibana,
        "my_entity",
        getDataSourceSchema, // func(ctx context.Context) datasource.Schema, without kibana_connection block
        readMyEntity,
    )
}

Resource patterns

Resources have three patterns:

  1. **Struct-based embedding** — embed *ResourceBase and implement resource.Resource directly. This is the right choice when Create and Update flows diverge significantly from a uniform shape.

  2. **Elasticsearch resource envelope** — use NewElasticsearchResource for Elasticsearch-backed resources whose Create and Update flows match a common shape: decode plan, resolve the scoped client from the connection block, run a mutating API call using the plan-safe write identity from [ElasticsearchResourceModel.GetResourceID], and persist the callback's returned model. The model must satisfy ElasticsearchResourceModel (value-receiver GetID for composite state ID, GetResourceID for the write key such as name or username, and GetElasticsearchConnection). Supply a schema factory (without elasticsearch_connection block), read and delete callbacks, and required create and update callbacks (ElasticsearchCreateFunc, ElasticsearchUpdateFunc); pass the same function for both when behavior matches. The envelope injects the connection block, parses composite IDs for Read and Delete only, resolves the client, and owns state persistence. It does not implement ImportState; concrete resources add that when needed. Resources that still override Create or Update (for example when the update path needs Config or prior state in addition to Plan) may pass PlaceholderElasticsearchWriteCallbacks until their logic is migrated into envelope callbacks. Constructor shape and callback types are defined on NewElasticsearchResource in resource_envelope.go.

  3. **Kibana resource envelope** — use NewKibanaResource for Kibana-backed resources whose Create, Read, Update, and Delete flows match a common shape. The model must satisfy KibanaResourceModel (value-receiver GetID for composite or plain state ID, GetResourceID for the write key such as name or API-assigned UUID, GetSpaceID for the Kibana space, and GetKibanaConnection). Supply a schema factory (without kibana_connection block), read and delete callbacks, and required create and update callbacks (KibanaCreateFunc, KibanaUpdateFunc). The envelope injects the kibana_connection block, resolves resource identity via composite-ID-or-fallback for Read, Update, and Delete, validates spaceID for Create, resolves the scoped Kibana client, and owns state persistence. Create callbacks receive the plan model (and can call plan.GetResourceID() for user-ID resources); Update callbacks receive both plan and prior state. It does not implement ImportState; concrete resources add that when needed. Resources that override Create or Update may pass PlaceholderKibanaWriteCallbacks until their logic is migrated into envelope callbacks. Constructor shape and callback types are defined on NewKibanaResource in kibana_resource_envelope.go.

Component is a typed Terraform resource type-name namespace segment (for example "elasticsearch", "kibana"). It is not a client-resolution kind: the same API family can use different component strings for Terraform naming, such as APM resources using the "apm" segment while calling Kibana APIs.

The resourceName argument to NewResourceBase is the final literal suffix segment in the Terraform type name, joined without normalization. Callers must preserve existing spellings for compatibility (for example "agentbuilder_tool" versus "agent_builder_tool").

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewElasticsearchDataSource

func NewElasticsearchDataSource[T ElasticsearchDataSourceModel](
	component Component,
	name string,
	schemaFactory func(context.Context) dsschema.Schema,
	readFunc func(context.Context, *clients.ElasticsearchScopedClient, T) (T, diag.Diagnostics),
) datasource.DataSource

NewElasticsearchDataSource returns a datasource.DataSource that wraps the provided schema and read function with automatic elasticsearch_connection block injection, config decode, scoped client resolution, and state persistence.

The concrete model T must embed ElasticsearchConnectionField.

func NewKibanaDataSource

func NewKibanaDataSource[T KibanaDataSourceModel](
	component Component,
	name string,
	schemaFactory func(context.Context) dsschema.Schema,
	readFunc func(context.Context, *clients.KibanaScopedClient, T) (T, diag.Diagnostics),
) datasource.DataSource

NewKibanaDataSource returns a datasource.DataSource that wraps the provided schema and read function with automatic kibana_connection block injection, config decode, scoped client resolution, and state persistence.

The concrete model T must embed KibanaConnectionField so that the connection block can be decoded alongside entity attributes.

Example usage (package doc):

type myModel struct {
    entitycore.KibanaConnectionField
    ID types.String `tfsdk:"id"`
}

func NewDataSource() datasource.DataSource {
    return entitycore.NewKibanaDataSource[myModel](
        entitycore.ComponentKibana,
        "my_entity",
        getDataSourceSchema, // func(ctx context.Context) datasource.Schema, without kibana_connection block
        readMyEntity,
    )
}

func PlaceholderKibanaWriteCallbacks

func PlaceholderKibanaWriteCallbacks[T KibanaResourceModel]() (KibanaCreateFunc[T], KibanaUpdateFunc[T])

PlaceholderKibanaWriteCallbacks returns create and update callbacks that fail if invoked. Use when a concrete resource type still defines its own Create and Update methods that override the envelope so Terraform never calls these placeholders.

Types

type Component

type Component string

Component is a Terraform type-name namespace segment used when building the full resource type name. See package documentation.

const (
	ComponentElasticsearch Component = "elasticsearch"
	ComponentKibana        Component = "kibana"
	ComponentFleet         Component = "fleet"
	ComponentAPM           Component = "apm"
)

Well-known Terraform type-name namespace segments for ResourceBase.Metadata.

type DataSourceVersionRequirement

type DataSourceVersionRequirement struct {
	// MinVersion is the minimum Kibana server version required.
	MinVersion version.Version
	// ErrorMessage is the human-readable detail added to the
	// "Unsupported server version" diagnostic when the server does not
	// satisfy MinVersion.
	ErrorMessage string
}

DataSourceVersionRequirement describes a minimum server version that a Kibana data source model requires before the entity read function is invoked.

type ElasticsearchConnectionField

type ElasticsearchConnectionField struct {
	ElasticsearchConnection types.List `tfsdk:"elasticsearch_connection"`
}

ElasticsearchConnectionField is an embeddable struct that provides the elasticsearch_connection block field for data source models used with NewElasticsearchDataSource.

func (ElasticsearchConnectionField) GetElasticsearchConnection

func (f ElasticsearchConnectionField) GetElasticsearchConnection() types.List

GetElasticsearchConnection returns the elasticsearch_connection block value.

type ElasticsearchCreateFunc

ElasticsearchCreateFunc performs the create after the envelope decodes the plan, checks the write identity, resolves the scoped Elasticsearch client, and passes the planned model. The callback should call the remote create API, set the composite ID on the returned model when readFunc expects to carry it through (e.g. via client.ID()), include any create-only field values, and return the model. The envelope invokes readFunc after a successful callback and sets state from the read result; the callback must not call readFunc.

type ElasticsearchDataSourceModel

type ElasticsearchDataSourceModel interface {
	GetElasticsearchConnection() types.List
}

ElasticsearchDataSourceModel is the type constraint for models passed to NewElasticsearchDataSource. It is satisfied by any struct that embeds ElasticsearchConnectionField (or otherwise provides a GetElasticsearchConnection method).

type ElasticsearchResource

type ElasticsearchResource[T ElasticsearchResourceModel] struct {
	*ResourceBase
	// contains filtered or unexported fields
}

ElasticsearchResource implements resource.Resource and related interfaces for Elasticsearch-backed resources. It embeds *ResourceBase to reuse Configure, Metadata, and Client.

The envelope owns Schema (with elasticsearch_connection block injection), Create, Read, Update, and Delete. Concrete resources may override Create or Update when their lifecycle does not fit the callback contract, and may choose to implement ImportState.

func NewElasticsearchResource

func NewElasticsearchResource[T ElasticsearchResourceModel](
	component Component,
	name string,
	schemaFactory func(context.Context) rschema.Schema,
	readFunc elasticsearchReadFunc[T],
	deleteFunc elasticsearchDeleteFunc[T],
	createFunc ElasticsearchCreateFunc[T],
	updateFunc ElasticsearchUpdateFunc[T],
) *ElasticsearchResource[T]

NewElasticsearchResource returns an *ElasticsearchResource that owns Schema, Create, Read, Update, and Delete. Concrete resources supply a schema factory (without elasticsearch_connection block), read, delete, create, and update callbacks. All callbacks must be non-nil; otherwise Create or Update surface a configuration error diagnostic instead of invoking the callback.

func (*ElasticsearchResource[T]) Create

Create implements resource.Resource: decode plan, resolve client, invoke the create callback, then persist the returned model.

func (*ElasticsearchResource[T]) Delete

Delete implements resource.Resource with the standard prelude, then delegates to the concrete deleteFunc.

func (*ElasticsearchResource[T]) Read

Read implements resource.Resource with the standard prelude: decode state, parse composite ID, resolve scoped Elasticsearch client, then delegate to the concrete readFunc. When readFunc reports found==true, the returned model is persisted via resp.State.Set; when found==false, the resource is removed from state.

func (*ElasticsearchResource[T]) Schema

Schema implements resource.Resource, injecting the elasticsearch_connection block into the schema returned by the concrete schema factory.

func (*ElasticsearchResource[T]) Update

Update implements resource.Resource with the same prelude as Create.

type ElasticsearchResourceModel

type ElasticsearchResourceModel interface {
	GetID() types.String
	// GetResourceID returns the plan-safe write identity (for example name or
	// username). Create and Update use this instead of GetID because computed
	// id values may be unknown in create plans.
	GetResourceID() types.String
	GetElasticsearchConnection() types.List
}

ElasticsearchResourceModel is the type constraint for models passed to NewElasticsearchResource. Concrete types must provide value-receiver methods GetID, GetResourceID, and GetElasticsearchConnection.

type ElasticsearchUpdateFunc

ElasticsearchUpdateFunc performs the update with the same prelude as ElasticsearchCreateFunc. The callback should call the remote update API, set the composite ID on the returned model when readFunc expects to carry it through, and return it. The envelope invokes readFunc after a successful callback and sets state from the read result; the callback must not call readFunc.

type KibanaConnectionField

type KibanaConnectionField struct {
	KibanaConnection types.List `tfsdk:"kibana_connection"`
}

KibanaConnectionField is an embeddable struct that provides the kibana_connection block field for Kibana entity models used with NewKibanaDataSource or NewKibanaResource.

func (KibanaConnectionField) GetKibanaConnection

func (f KibanaConnectionField) GetKibanaConnection() types.List

GetKibanaConnection returns the kibana_connection block value.

type KibanaCreateFunc

type KibanaCreateFunc[T KibanaResourceModel] func(
	context.Context,
	*clients.KibanaScopedClient,
	string,
	T,
) (T, diag.Diagnostics)

KibanaCreateFunc performs the create after the envelope decodes the plan, validates the space ID, resolves the scoped Kibana client, and passes the planned model. It returns the model to persist in state.

type KibanaDataSourceModel

type KibanaDataSourceModel interface {
	GetKibanaConnection() types.List
}

KibanaDataSourceModel is the type constraint for models passed to NewKibanaDataSource. It is satisfied by any struct that embeds KibanaConnectionField (or otherwise provides a GetKibanaConnection method).

type KibanaResource

type KibanaResource[T KibanaResourceModel] struct {
	*ResourceBase
	// contains filtered or unexported fields
}

KibanaResource implements resource.Resource and related interfaces for Kibana-backed resources. It embeds *ResourceBase to reuse Configure, Metadata, and Client.

The envelope owns Schema (with kibana_connection block injection), Create, Read, Update, and Delete. Concrete resources may override Create or Update when their lifecycle does not fit the callback contract, and may choose to implement ImportState.

func NewKibanaResource

func NewKibanaResource[T KibanaResourceModel](
	component Component,
	name string,
	schemaFactory func(context.Context) rschema.Schema,
	readFunc kibanaReadFunc[T],
	deleteFunc kibanaDeleteFunc[T],
	createFunc KibanaCreateFunc[T],
	updateFunc KibanaUpdateFunc[T],
) *KibanaResource[T]

NewKibanaResource returns an *KibanaResource that owns Schema, Create, Read, Update, and Delete. Concrete resources supply a schema factory (without kibana_connection block), read, delete, create, and update callbacks. All callbacks must be non-nil; otherwise Create or Update surface a configuration error diagnostic instead of invoking the callback.

func (*KibanaResource[T]) Create

Create implements resource.Resource: decode plan, validate spaceID, resolve client, invoke the create callback, then persist the returned model.

func (*KibanaResource[T]) Delete

Delete implements resource.Resource with the standard prelude, then delegates to the concrete deleteFunc.

func (*KibanaResource[T]) Read

Read implements resource.Resource with the standard prelude: decode state, resolve identity via composite-ID-or-fallback, validate resourceID, resolve scoped Kibana client, then delegate to the concrete readFunc. When readFunc reports found==true, the returned model is persisted via resp.State.Set; when found==false, the resource is removed from state.

func (*KibanaResource[T]) Schema

Schema implements resource.Resource, injecting the kibana_connection block into the schema returned by the concrete schema factory.

func (*KibanaResource[T]) Update

Update implements resource.Resource: decode plan and prior state, resolve identity on the plan model, validate resourceID, resolve client, invoke the update callback, then persist the returned model.

type KibanaResourceModel

type KibanaResourceModel interface {
	GetID() types.String
	// GetResourceID returns the plan-safe write identity (for example name or
	// API-assigned UUID). Read, Update, and Delete use this when the state ID
	// is not a composite.
	GetResourceID() types.String
	GetSpaceID() types.String
	GetKibanaConnection() types.List
}

KibanaResourceModel is the type constraint for models passed to NewKibanaResource. Concrete types must provide value-receiver methods GetID, GetResourceID, GetSpaceID, and GetKibanaConnection.

type KibanaUpdateFunc

type KibanaUpdateFunc[T KibanaResourceModel] func(
	context.Context,
	*clients.KibanaScopedClient,
	string,
	string,
	T,
	T,
) (T, diag.Diagnostics)

KibanaUpdateFunc performs the update after the envelope decodes the plan and prior state, resolves the resource identity, resolves the scoped Kibana client, and passes both the plan and prior models. It returns the model to persist in state.

type ResourceBase

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

ResourceBase holds shared Plugin Framework resource wiring: typed naming parts and the provider client factory from Configure. Embed *ResourceBase in concrete resources to reuse Configure, Metadata, and Client.

func NewResourceBase

func NewResourceBase(component Component, resourceName string) *ResourceBase

NewResourceBase returns a ResourceBase for the given namespace segment and literal resource name suffix. resourceName is not normalized; see package documentation.

func (*ResourceBase) Client

Client returns the client factory from the last successful ResourceBase.Configure assignment, or nil if none has been stored yet. A nil *ResourceBase (e.g. a partially constructed embed) returns nil so callers can surface diagnostics instead of panicking.

func (*ResourceBase) Configure

Configure implements resource.ResourceWithConfigure, converting provider data with clients.ConvertProviderDataToFactory and appending diagnostics. If the response has error diagnostics, it returns without assigning a new factory, leaving any prior successful client unchanged (same pattern as resources such as fleet integration and kibana agent builder tool).

func (*ResourceBase) Metadata

Metadata implements the Metadata method of resource.Resource, setting the Terraform type name to "<providerTypeName>_<component>_<resourceName>".

type WithVersionRequirements

type WithVersionRequirements interface {
	GetVersionRequirements() ([]DataSourceVersionRequirement, diag.Diagnostics)
}

WithVersionRequirements is an optional interface that Kibana entity models may implement to declare server version requirements. When a decoded model satisfies this interface, the generic Kibana envelopes evaluate the requirements after scoped client resolution and before invoking the concrete lifecycle callback.

Jump to

Keyboard shortcuts

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