feat: Update configs.StateStore to be able to contain information about how the state store provider has been supplied to Terraform

Sarah French 1 month ago
parent e158548e73
commit 41c544f220

@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform/internal/depsfile"
"github.com/hashicorp/terraform/internal/getproviders/providerreqs"
"github.com/hashicorp/terraform/internal/getproviders/reattach"
"github.com/hashicorp/terraform/internal/getproviders/supplymode"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
)
@ -38,6 +39,11 @@ type StateStore struct {
// and is used in diagnostics
ProviderAddr tfaddr.Provider
// ProviderSupplyMode describes how the provider used for state storage was supplied to Terraform.
// This is needed when handling provider version data; unmanaged and builtin providers have no version data available.
// This value is ultimately recorded in the backend state file alongside the provider version data (which may be null).
ProviderSupplyMode supplymode.ProviderSupplyMode
TypeRange hcl.Range
DeclRange hcl.Range
}
@ -104,6 +110,24 @@ var StateStorageBlockSchema = &hcl.BodySchema{
},
}
func (s *StateStore) SetProviderSupplyMode(isDevOverride bool) {
isReattached, err := reattach.IsProviderReattached(s.ProviderAddr, os.Getenv("TF_REATTACH_PROVIDERS"))
if err != nil {
panic(fmt.Sprintf("Unable to determine if provider %s is reattached while initializing the state store. This is a bug in Terraform and should be reported: %v", s.ProviderAddr.ForDisplay(), err))
}
switch {
case s.ProviderAddr.IsBuiltIn():
s.ProviderSupplyMode = supplymode.ProviderSupplyModeBuiltIn
case isReattached:
s.ProviderSupplyMode = supplymode.ProviderSupplyModeReattached
case isDevOverride:
s.ProviderSupplyMode = supplymode.ProviderSupplyModeDevOverride
default:
// assume provider is managed if nothing indicates otherwise.
s.ProviderSupplyMode = supplymode.ProviderSupplyModeManaged
}
}
// resolveStateStoreProviderType is used to obtain provider source data from required_providers data.
// The only exception is the builtin terraform provider, which we return source data for without using required_providers.
// This code is reused in code for parsing config and modules.

@ -0,0 +1,24 @@
// Copyright IBM Corp. 2014, 2026
// SPDX-License-Identifier: BUSL-1.1
package supplymode
// ProviderSupplyMode describes how a provider is being supplied to Terraform.
type ProviderSupplyMode string
const (
// Unset value.
ProviderSupplyModeUnset ProviderSupplyMode = ""
// The provider is built into the Terraform binary.
ProviderSupplyModeBuiltIn ProviderSupplyMode = "built_in"
// The provider is unmanaged by Terraform, supplied via TF_REATTACH_PROVIDERS by the user/environment.
ProviderSupplyModeReattached ProviderSupplyMode = "reattached"
// The provider is overridden by a development configuration.
ProviderSupplyModeDevOverride ProviderSupplyMode = "dev_override"
// The provider is managed by Terraform. This is the "normal" and most common case.
ProviderSupplyModeManaged ProviderSupplyMode = "managed_by_terraform"
)
Loading…
Cancel
Save