github.com/opentofu/opentofu@v1.7.1/internal/legacy/tofu/resource_provisioner.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package tofu
     7  
     8  import (
     9  	"github.com/opentofu/opentofu/internal/configs/configschema"
    10  	"github.com/opentofu/opentofu/internal/provisioners"
    11  )
    12  
    13  // ResourceProvisioner is an interface that must be implemented by any
    14  // resource provisioner: the thing that initializes resources in
    15  // a OpenTofu configuration.
    16  type ResourceProvisioner interface {
    17  	// GetConfigSchema returns the schema for the provisioner type's main
    18  	// configuration block. This is called prior to Validate to enable some
    19  	// basic structural validation to be performed automatically and to allow
    20  	// the configuration to be properly extracted from potentially-ambiguous
    21  	// configuration file formats.
    22  	GetConfigSchema() (*configschema.Block, error)
    23  
    24  	// Validate is called once at the beginning with the raw
    25  	// configuration (no interpolation done) and can return a list of warnings
    26  	// and/or errors.
    27  	//
    28  	// This is called once per resource.
    29  	//
    30  	// This should not assume any of the values in the resource configuration
    31  	// are valid since it is possible they have to be interpolated still.
    32  	// The primary use case of this call is to check that the required keys
    33  	// are set and that the general structure is correct.
    34  	Validate(*ResourceConfig) ([]string, []error)
    35  
    36  	// Apply runs the provisioner on a specific resource and returns an error.
    37  	// Instead of a diff, the ResourceConfig is provided since provisioners
    38  	// only run after a resource has been newly created.
    39  	Apply(UIOutput, *InstanceState, *ResourceConfig) error
    40  
    41  	// Stop is called when the provisioner should halt any in-flight actions.
    42  	//
    43  	// This can be used to make a nicer Ctrl-C experience for OpenTofu.
    44  	// Even if this isn't implemented to do anything (just returns nil),
    45  	// OpenTofu will still cleanly stop after the currently executing
    46  	// graph node is complete. However, this API can be used to make more
    47  	// efficient halts.
    48  	//
    49  	// Stop doesn't have to and shouldn't block waiting for in-flight actions
    50  	// to complete. It should take any action it wants and return immediately
    51  	// acknowledging it has received the stop request. OpenTofu core will
    52  	// automatically not make any further API calls to the provider soon
    53  	// after Stop is called (technically exactly once the currently executing
    54  	// graph nodes are complete).
    55  	//
    56  	// The error returned, if non-nil, is assumed to mean that signaling the
    57  	// stop somehow failed and that the user should expect potentially waiting
    58  	// a longer period of time.
    59  	Stop() error
    60  }
    61  
    62  // ResourceProvisionerCloser is an interface that provisioners that can close
    63  // connections that aren't needed anymore must implement.
    64  type ResourceProvisionerCloser interface {
    65  	Close() error
    66  }
    67  
    68  // ResourceProvisionerFactory is a function type that creates a new instance
    69  // of a resource provisioner.
    70  type ResourceProvisionerFactory func() (ResourceProvisioner, error)
    71  
    72  // ProvisionerFactory is a function type that creates a new instance
    73  // of a provisioners.Interface.
    74  type ProvisionerFactory = provisioners.Factory