github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/providers/provider.go (about)

     1  package providers
     2  
     3  import (
     4  	"github.com/zclconf/go-cty/cty"
     5  
     6  	"github.com/cycloidio/terraform/configs/configschema"
     7  	"github.com/cycloidio/terraform/states"
     8  	"github.com/cycloidio/terraform/tfdiags"
     9  )
    10  
    11  // Interface represents the set of methods required for a complete resource
    12  // provider plugin.
    13  type Interface interface {
    14  	// GetSchema returns the complete schema for the provider.
    15  	GetProviderSchema() GetProviderSchemaResponse
    16  
    17  	// ValidateProviderConfig allows the provider to validate the configuration.
    18  	// The ValidateProviderConfigResponse.PreparedConfig field is unused. The
    19  	// final configuration is not stored in the state, and any modifications
    20  	// that need to be made must be made during the Configure method call.
    21  	ValidateProviderConfig(ValidateProviderConfigRequest) ValidateProviderConfigResponse
    22  
    23  	// ValidateResourceConfig allows the provider to validate the resource
    24  	// configuration values.
    25  	ValidateResourceConfig(ValidateResourceConfigRequest) ValidateResourceConfigResponse
    26  
    27  	// ValidateDataResourceConfig allows the provider to validate the data source
    28  	// configuration values.
    29  	ValidateDataResourceConfig(ValidateDataResourceConfigRequest) ValidateDataResourceConfigResponse
    30  
    31  	// UpgradeResourceState is called when the state loader encounters an
    32  	// instance state whose schema version is less than the one reported by the
    33  	// currently-used version of the corresponding provider, and the upgraded
    34  	// result is used for any further processing.
    35  	UpgradeResourceState(UpgradeResourceStateRequest) UpgradeResourceStateResponse
    36  
    37  	// Configure configures and initialized the provider.
    38  	ConfigureProvider(ConfigureProviderRequest) ConfigureProviderResponse
    39  
    40  	// Stop is called when the provider should halt any in-flight actions.
    41  	//
    42  	// Stop should not block waiting for in-flight actions to complete. It
    43  	// should take any action it wants and return immediately acknowledging it
    44  	// has received the stop request. Terraform will not make any further API
    45  	// calls to the provider after Stop is called.
    46  	//
    47  	// The error returned, if non-nil, is assumed to mean that signaling the
    48  	// stop somehow failed and that the user should expect potentially waiting
    49  	// a longer period of time.
    50  	Stop() error
    51  
    52  	// ReadResource refreshes a resource and returns its current state.
    53  	ReadResource(ReadResourceRequest) ReadResourceResponse
    54  
    55  	// PlanResourceChange takes the current state and proposed state of a
    56  	// resource, and returns the planned final state.
    57  	PlanResourceChange(PlanResourceChangeRequest) PlanResourceChangeResponse
    58  
    59  	// ApplyResourceChange takes the planned state for a resource, which may
    60  	// yet contain unknown computed values, and applies the changes returning
    61  	// the final state.
    62  	ApplyResourceChange(ApplyResourceChangeRequest) ApplyResourceChangeResponse
    63  
    64  	// ImportResourceState requests that the given resource be imported.
    65  	ImportResourceState(ImportResourceStateRequest) ImportResourceStateResponse
    66  
    67  	// ReadDataSource returns the data source's current state.
    68  	ReadDataSource(ReadDataSourceRequest) ReadDataSourceResponse
    69  
    70  	// Close shuts down the plugin process if applicable.
    71  	Close() error
    72  }
    73  
    74  type GetProviderSchemaResponse struct {
    75  	// Provider is the schema for the provider itself.
    76  	Provider Schema
    77  
    78  	// ProviderMeta is the schema for the provider's meta info in a module
    79  	ProviderMeta Schema
    80  
    81  	// ResourceTypes map the resource type name to that type's schema.
    82  	ResourceTypes map[string]Schema
    83  
    84  	// DataSources maps the data source name to that data source's schema.
    85  	DataSources map[string]Schema
    86  
    87  	// Diagnostics contains any warnings or errors from the method call.
    88  	Diagnostics tfdiags.Diagnostics
    89  }
    90  
    91  // Schema pairs a provider or resource schema with that schema's version.
    92  // This is used to be able to upgrade the schema in UpgradeResourceState.
    93  type Schema struct {
    94  	Version int64
    95  	Block   *configschema.Block
    96  }
    97  
    98  type ValidateProviderConfigRequest struct {
    99  	// Config is the raw configuration value for the provider.
   100  	Config cty.Value
   101  }
   102  
   103  type ValidateProviderConfigResponse struct {
   104  	// PreparedConfig is unused and will be removed with support for plugin protocol v5.
   105  	PreparedConfig cty.Value
   106  	// Diagnostics contains any warnings or errors from the method call.
   107  	Diagnostics tfdiags.Diagnostics
   108  }
   109  
   110  type ValidateResourceConfigRequest struct {
   111  	// TypeName is the name of the resource type to validate.
   112  	TypeName string
   113  
   114  	// Config is the configuration value to validate, which may contain unknown
   115  	// values.
   116  	Config cty.Value
   117  }
   118  
   119  type ValidateResourceConfigResponse struct {
   120  	// Diagnostics contains any warnings or errors from the method call.
   121  	Diagnostics tfdiags.Diagnostics
   122  }
   123  
   124  type ValidateDataResourceConfigRequest struct {
   125  	// TypeName is the name of the data source type to validate.
   126  	TypeName string
   127  
   128  	// Config is the configuration value to validate, which may contain unknown
   129  	// values.
   130  	Config cty.Value
   131  }
   132  
   133  type ValidateDataResourceConfigResponse struct {
   134  	// Diagnostics contains any warnings or errors from the method call.
   135  	Diagnostics tfdiags.Diagnostics
   136  }
   137  
   138  type UpgradeResourceStateRequest struct {
   139  	// TypeName is the name of the resource type being upgraded
   140  	TypeName string
   141  
   142  	// Version is version of the schema that created the current state.
   143  	Version int64
   144  
   145  	// RawStateJSON and RawStateFlatmap contiain the state that needs to be
   146  	// upgraded to match the current schema version. Because the schema is
   147  	// unknown, this contains only the raw data as stored in the state.
   148  	// RawStateJSON is the current json state encoding.
   149  	// RawStateFlatmap is the legacy flatmap encoding.
   150  	// Only on of these fields may be set for the upgrade request.
   151  	RawStateJSON    []byte
   152  	RawStateFlatmap map[string]string
   153  }
   154  
   155  type UpgradeResourceStateResponse struct {
   156  	// UpgradedState is the newly upgraded resource state.
   157  	UpgradedState cty.Value
   158  
   159  	// Diagnostics contains any warnings or errors from the method call.
   160  	Diagnostics tfdiags.Diagnostics
   161  }
   162  
   163  type ConfigureProviderRequest struct {
   164  	// Terraform version is the version string from the running instance of
   165  	// terraform. Providers can use TerraformVersion to verify compatibility,
   166  	// and to store for informational purposes.
   167  	TerraformVersion string
   168  
   169  	// Config is the complete configuration value for the provider.
   170  	Config cty.Value
   171  }
   172  
   173  type ConfigureProviderResponse struct {
   174  	// Diagnostics contains any warnings or errors from the method call.
   175  	Diagnostics tfdiags.Diagnostics
   176  }
   177  
   178  type ReadResourceRequest struct {
   179  	// TypeName is the name of the resource type being read.
   180  	TypeName string
   181  
   182  	// PriorState contains the previously saved state value for this resource.
   183  	PriorState cty.Value
   184  
   185  	// Private is an opaque blob that will be stored in state along with the
   186  	// resource. It is intended only for interpretation by the provider itself.
   187  	Private []byte
   188  
   189  	// ProviderMeta is the configuration for the provider_meta block for the
   190  	// module and provider this resource belongs to. Its use is defined by
   191  	// each provider, and it should not be used without coordination with
   192  	// HashiCorp. It is considered experimental and subject to change.
   193  	ProviderMeta cty.Value
   194  }
   195  
   196  type ReadResourceResponse struct {
   197  	// NewState contains the current state of the resource.
   198  	NewState cty.Value
   199  
   200  	// Diagnostics contains any warnings or errors from the method call.
   201  	Diagnostics tfdiags.Diagnostics
   202  
   203  	// Private is an opaque blob that will be stored in state along with the
   204  	// resource. It is intended only for interpretation by the provider itself.
   205  	Private []byte
   206  }
   207  
   208  type PlanResourceChangeRequest struct {
   209  	// TypeName is the name of the resource type to plan.
   210  	TypeName string
   211  
   212  	// PriorState is the previously saved state value for this resource.
   213  	PriorState cty.Value
   214  
   215  	// ProposedNewState is the expected state after the new configuration is
   216  	// applied. This is created by directly applying the configuration to the
   217  	// PriorState. The provider is then responsible for applying any further
   218  	// changes required to create the proposed final state.
   219  	ProposedNewState cty.Value
   220  
   221  	// Config is the resource configuration, before being merged with the
   222  	// PriorState. Any value not explicitly set in the configuration will be
   223  	// null. Config is supplied for reference, but Provider implementations
   224  	// should prefer the ProposedNewState in most circumstances.
   225  	Config cty.Value
   226  
   227  	// PriorPrivate is the previously saved private data returned from the
   228  	// provider during the last apply.
   229  	PriorPrivate []byte
   230  
   231  	// ProviderMeta is the configuration for the provider_meta block for the
   232  	// module and provider this resource belongs to. Its use is defined by
   233  	// each provider, and it should not be used without coordination with
   234  	// HashiCorp. It is considered experimental and subject to change.
   235  	ProviderMeta cty.Value
   236  }
   237  
   238  type PlanResourceChangeResponse struct {
   239  	// PlannedState is the expected state of the resource once the current
   240  	// configuration is applied.
   241  	PlannedState cty.Value
   242  
   243  	// RequiresReplace is the list of the attributes that are requiring
   244  	// resource replacement.
   245  	RequiresReplace []cty.Path
   246  
   247  	// PlannedPrivate is an opaque blob that is not interpreted by terraform
   248  	// core. This will be saved and relayed back to the provider during
   249  	// ApplyResourceChange.
   250  	PlannedPrivate []byte
   251  
   252  	// Diagnostics contains any warnings or errors from the method call.
   253  	Diagnostics tfdiags.Diagnostics
   254  
   255  	// LegacyTypeSystem is set only if the provider is using the legacy SDK
   256  	// whose type system cannot be precisely mapped into the Terraform type
   257  	// system. We use this to bypass certain consistency checks that would
   258  	// otherwise fail due to this imprecise mapping. No other provider or SDK
   259  	// implementation is permitted to set this.
   260  	LegacyTypeSystem bool
   261  }
   262  
   263  type ApplyResourceChangeRequest struct {
   264  	// TypeName is the name of the resource type being applied.
   265  	TypeName string
   266  
   267  	// PriorState is the current state of resource.
   268  	PriorState cty.Value
   269  
   270  	// Planned state is the state returned from PlanResourceChange, and should
   271  	// represent the new state, minus any remaining computed attributes.
   272  	PlannedState cty.Value
   273  
   274  	// Config is the resource configuration, before being merged with the
   275  	// PriorState. Any value not explicitly set in the configuration will be
   276  	// null. Config is supplied for reference, but Provider implementations
   277  	// should prefer the PlannedState in most circumstances.
   278  	Config cty.Value
   279  
   280  	// PlannedPrivate is the same value as returned by PlanResourceChange.
   281  	PlannedPrivate []byte
   282  
   283  	// ProviderMeta is the configuration for the provider_meta block for the
   284  	// module and provider this resource belongs to. Its use is defined by
   285  	// each provider, and it should not be used without coordination with
   286  	// HashiCorp. It is considered experimental and subject to change.
   287  	ProviderMeta cty.Value
   288  }
   289  
   290  type ApplyResourceChangeResponse struct {
   291  	// NewState is the new complete state after applying the planned change.
   292  	// In the event of an error, NewState should represent the most recent
   293  	// known state of the resource, if it exists.
   294  	NewState cty.Value
   295  
   296  	// Private is an opaque blob that will be stored in state along with the
   297  	// resource. It is intended only for interpretation by the provider itself.
   298  	Private []byte
   299  
   300  	// Diagnostics contains any warnings or errors from the method call.
   301  	Diagnostics tfdiags.Diagnostics
   302  
   303  	// LegacyTypeSystem is set only if the provider is using the legacy SDK
   304  	// whose type system cannot be precisely mapped into the Terraform type
   305  	// system. We use this to bypass certain consistency checks that would
   306  	// otherwise fail due to this imprecise mapping. No other provider or SDK
   307  	// implementation is permitted to set this.
   308  	LegacyTypeSystem bool
   309  }
   310  
   311  type ImportResourceStateRequest struct {
   312  	// TypeName is the name of the resource type to be imported.
   313  	TypeName string
   314  
   315  	// ID is a string with which the provider can identify the resource to be
   316  	// imported.
   317  	ID string
   318  }
   319  
   320  type ImportResourceStateResponse struct {
   321  	// ImportedResources contains one or more state values related to the
   322  	// imported resource. It is not required that these be complete, only that
   323  	// there is enough identifying information for the provider to successfully
   324  	// update the states in ReadResource.
   325  	ImportedResources []ImportedResource
   326  
   327  	// Diagnostics contains any warnings or errors from the method call.
   328  	Diagnostics tfdiags.Diagnostics
   329  }
   330  
   331  // ImportedResource represents an object being imported into Terraform with the
   332  // help of a provider. An ImportedObject is a RemoteObject that has been read
   333  // by the provider's import handler but hasn't yet been committed to state.
   334  type ImportedResource struct {
   335  	// TypeName is the name of the resource type associated with the
   336  	// returned state. It's possible for providers to import multiple related
   337  	// types with a single import request.
   338  	TypeName string
   339  
   340  	// State is the state of the remote object being imported. This may not be
   341  	// complete, but must contain enough information to uniquely identify the
   342  	// resource.
   343  	State cty.Value
   344  
   345  	// Private is an opaque blob that will be stored in state along with the
   346  	// resource. It is intended only for interpretation by the provider itself.
   347  	Private []byte
   348  }
   349  
   350  // AsInstanceObject converts the receiving ImportedObject into a
   351  // ResourceInstanceObject that has status ObjectReady.
   352  //
   353  // The returned object does not know its own resource type, so the caller must
   354  // retain the ResourceType value from the source object if this information is
   355  // needed.
   356  //
   357  // The returned object also has no dependency addresses, but the caller may
   358  // freely modify the direct fields of the returned object without affecting
   359  // the receiver.
   360  func (ir ImportedResource) AsInstanceObject() *states.ResourceInstanceObject {
   361  	return &states.ResourceInstanceObject{
   362  		Status:  states.ObjectReady,
   363  		Value:   ir.State,
   364  		Private: ir.Private,
   365  	}
   366  }
   367  
   368  type ReadDataSourceRequest struct {
   369  	// TypeName is the name of the data source type to Read.
   370  	TypeName string
   371  
   372  	// Config is the complete configuration for the requested data source.
   373  	Config cty.Value
   374  
   375  	// ProviderMeta is the configuration for the provider_meta block for the
   376  	// module and provider this resource belongs to. Its use is defined by
   377  	// each provider, and it should not be used without coordination with
   378  	// HashiCorp. It is considered experimental and subject to change.
   379  	ProviderMeta cty.Value
   380  }
   381  
   382  type ReadDataSourceResponse struct {
   383  	// State is the current state of the requested data source.
   384  	State cty.Value
   385  
   386  	// Diagnostics contains any warnings or errors from the method call.
   387  	Diagnostics tfdiags.Diagnostics
   388  }