Files
opentf/internal/engine/plugins/plugins.go
2025-12-11 11:33:31 -05:00

129 lines
5.4 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugins
import (
"context"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/lang/eval"
"github.com/opentofu/opentofu/internal/plugins"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
)
type Plugins interface {
Providers
Provisioners
}
// Providers is implemented by callers of this package to provide access
// to the providers needed by a configuration without this package needing
// to know anything about how provider plugins work, or whether plugins are
// even being used.
type Providers interface {
eval.ProvidersSchema
// ValidateProviderConfig runs provider-specific logic to check whether
// the given configuration is valid. Returns at least one error diagnostic
// if the configuration is not valid, and may also return warning
// diagnostics regardless of whether the configuration is valid.
//
// The given config value is guaranteed to be an object conforming to
// the schema returned by a previous call to ProviderConfigSchema for
// the same provider.
ValidateProviderConfig(ctx context.Context, provider addrs.Provider, configVal cty.Value) tfdiags.Diagnostics
// ValidateResourceConfig runs provider-specific logic to check whether
// the given configuration is valid. Returns at least one error diagnostic
// if the configuration is not valid, and may also return warning
// diagnostics regardless of whether the configuration is valid.
//
// The given config value is guaranteed to be an object conforming to
// the schema returned by a previous call to ResourceTypeSchema for
// the same resource type.
ValidateResourceConfig(ctx context.Context, provider addrs.Provider, mode addrs.ResourceMode, typeName string, configVal cty.Value) tfdiags.Diagnostics
// NewConfiguredProvider starts a _configured_ instance of the given
// provider using the given configuration value.
//
// The evaluation system itself makes no use of configured providers, but
// higher-level processes wrapping it (e.g. the plan and apply engines)
// need to use configured providers for actions related to resources, etc,
// and so this is for their benefit to help ensure that they are definitely
// creating a configured instance of the same provider that other methods
// would be using to return schema information and validation results.
//
// It's the caller's responsibility to ensure that the given configuration
// value is valid according to the provider's schema and validation rules.
// That's usually achieved by taking a value provided by the evaluation
// system, which would then have already been processed using the results
// from [Providers.ProviderConfigSchema] and
// [Providers.ValidateProviderConfig]. If the returned diagnostics contains
// errors then the [providers.Configured] result is invalid and must not be
// used.
NewConfiguredProvider(ctx context.Context, provider addrs.AbsProviderInstanceCorrect, configVal cty.Value) (providers.Configured, tfdiags.Diagnostics)
Close(ctx context.Context) error
}
type Provisioners interface {
eval.ProvisionersSchema
}
type newRuntimePlugins struct {
providers plugins.ProviderManager
provisioners plugins.ProvisionerManager
}
var _ Providers = (*newRuntimePlugins)(nil)
var _ Provisioners = (*newRuntimePlugins)(nil)
func NewRuntimePlugins(manager plugins.PluginManager) Plugins {
return &newRuntimePlugins{
providers: manager,
provisioners: manager,
}
}
// NewConfiguredProvider implements Providers.
func (n *newRuntimePlugins) NewConfiguredProvider(ctx context.Context, provider addrs.AbsProviderInstanceCorrect, configVal cty.Value) (providers.Configured, tfdiags.Diagnostics) {
diags := n.providers.ConfigureProvider(ctx, provider, configVal)
configured := n.providers.ConfiguredProvider(provider)
return configured, diags
}
// ProviderConfigSchema implements Providers.
func (n *newRuntimePlugins) ProviderConfigSchema(ctx context.Context, provider addrs.Provider) (*providers.Schema, tfdiags.Diagnostics) {
return n.providers.ProviderConfigSchema(ctx, provider)
}
// ResourceTypeSchema implements Providers.
func (n *newRuntimePlugins) ResourceTypeSchema(ctx context.Context, provider addrs.Provider, mode addrs.ResourceMode, typeName string) (*providers.Schema, tfdiags.Diagnostics) {
return n.providers.ResourceTypeSchema(ctx, provider, mode, typeName)
}
// ValidateProviderConfig implements Providers.
func (n *newRuntimePlugins) ValidateProviderConfig(ctx context.Context, provider addrs.Provider, configVal cty.Value) tfdiags.Diagnostics {
return n.providers.ValidateProviderConfig(ctx, provider, configVal)
}
// ValidateResourceConfig implements Providers.
func (n *newRuntimePlugins) ValidateResourceConfig(ctx context.Context, provider addrs.Provider, mode addrs.ResourceMode, typeName string, configVal cty.Value) tfdiags.Diagnostics {
return n.providers.ValidateResourceConfig(ctx, provider, mode, typeName, configVal)
}
// ProvisionerConfigSchema implements Provisioners.
func (n *newRuntimePlugins) ProvisionerConfigSchema(ctx context.Context, typeName string) (*configschema.Block, tfdiags.Diagnostics) {
schema, err := n.provisioners.ProvisionerSchema(typeName)
if err != nil {
return nil, tfdiags.Diagnostics{}.Append(err)
}
return schema, nil
}