2015-03-12 00:17:47 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2018-07-17 23:55:56 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
|
|
|
"github.com/hashicorp/terraform/internal/backend/remote"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 19:40:40 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2018-10-31 16:45:03 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
backendInit "github.com/hashicorp/terraform/internal/backend/init"
|
2015-03-12 00:17:47 +01:00
|
|
|
)
|
|
|
|
|
2018-07-17 23:55:56 +02:00
|
|
|
func dataSourceRemoteStateGetSchema() providers.Schema {
|
|
|
|
return providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"backend": {
|
2020-11-06 09:51:07 +01:00
|
|
|
Type: cty.String,
|
|
|
|
Description: "The remote backend to use, e.g. `remote` or `http`.",
|
|
|
|
DescriptionKind: configschema.StringMarkdown,
|
|
|
|
Required: true,
|
2018-07-17 23:55:56 +02:00
|
|
|
},
|
|
|
|
"config": {
|
2020-11-06 09:51:07 +01:00
|
|
|
Type: cty.DynamicPseudoType,
|
|
|
|
Description: "The configuration of the remote backend. " +
|
|
|
|
"Although this is optional, most backends require " +
|
|
|
|
"some configuration.\n\n" +
|
|
|
|
"The object can use any arguments that would be valid " +
|
|
|
|
"in the equivalent `terraform { backend \"<TYPE>\" { ... } }` " +
|
|
|
|
"block.",
|
|
|
|
DescriptionKind: configschema.StringMarkdown,
|
|
|
|
Optional: true,
|
2018-07-17 23:55:56 +02:00
|
|
|
},
|
|
|
|
"defaults": {
|
2020-11-06 09:51:07 +01:00
|
|
|
Type: cty.DynamicPseudoType,
|
|
|
|
Description: "Default values for outputs, in case " +
|
|
|
|
"the state file is empty or lacks a required output.",
|
|
|
|
DescriptionKind: configschema.StringMarkdown,
|
|
|
|
Optional: true,
|
2018-07-17 23:55:56 +02:00
|
|
|
},
|
|
|
|
"outputs": {
|
2020-11-06 09:51:07 +01:00
|
|
|
Type: cty.DynamicPseudoType,
|
|
|
|
Description: "An object containing every root-level " +
|
|
|
|
"output in the remote state.",
|
|
|
|
DescriptionKind: configschema.StringMarkdown,
|
|
|
|
Computed: true,
|
2018-07-17 23:55:56 +02:00
|
|
|
},
|
|
|
|
"workspace": {
|
2020-11-06 09:51:07 +01:00
|
|
|
Type: cty.String,
|
|
|
|
Description: "The Terraform workspace to use, if " +
|
|
|
|
"the backend supports workspaces.",
|
|
|
|
DescriptionKind: configschema.StringMarkdown,
|
|
|
|
Optional: true,
|
2018-07-17 23:55:56 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-03-12 00:17:47 +01:00
|
|
|
}
|
|
|
|
|
2019-05-04 00:38:39 +02:00
|
|
|
func dataSourceRemoteStateValidate(cfg cty.Value) tfdiags.Diagnostics {
|
2018-07-17 23:55:56 +02:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
2019-05-04 00:38:39 +02:00
|
|
|
// Getting the backend implicitly validates the configuration for it,
|
|
|
|
// but we can only do that if it's all known already.
|
|
|
|
if cfg.GetAttr("config").IsWhollyKnown() && cfg.GetAttr("backend").IsKnown() {
|
2020-05-07 02:54:51 +02:00
|
|
|
_, _, moreDiags := getBackend(cfg)
|
2019-05-04 00:38:39 +02:00
|
|
|
diags = diags.Append(moreDiags)
|
|
|
|
} else {
|
|
|
|
// Otherwise we'll just type-check the config object itself.
|
|
|
|
configTy := cfg.GetAttr("config").Type()
|
|
|
|
if configTy != cty.DynamicPseudoType && !(configTy.IsObjectType() || configTy.IsMapType()) {
|
|
|
|
diags = diags.Append(tfdiags.AttributeValue(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid backend configuration",
|
|
|
|
"The configuration must be an object value.",
|
|
|
|
cty.GetAttrPath("config"),
|
|
|
|
))
|
|
|
|
}
|
2016-09-04 00:38:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-04 00:38:39 +02:00
|
|
|
{
|
|
|
|
defaultsTy := cfg.GetAttr("defaults").Type()
|
|
|
|
if defaultsTy != cty.DynamicPseudoType && !(defaultsTy.IsObjectType() || defaultsTy.IsMapType()) {
|
|
|
|
diags = diags.Append(tfdiags.AttributeValue(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid default values",
|
|
|
|
"Defaults must be given in an object value.",
|
|
|
|
cty.GetAttrPath("defaults"),
|
|
|
|
))
|
|
|
|
}
|
2015-03-12 00:17:47 +01:00
|
|
|
}
|
|
|
|
|
2019-05-04 00:38:39 +02:00
|
|
|
return diags
|
|
|
|
}
|
2018-03-21 02:43:02 +01:00
|
|
|
|
2019-05-04 00:38:39 +02:00
|
|
|
func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
2018-07-17 23:55:56 +02:00
|
|
|
|
2020-05-07 02:54:51 +02:00
|
|
|
b, cfg, moreDiags := getBackend(d)
|
2019-05-04 00:38:39 +02:00
|
|
|
diags = diags.Append(moreDiags)
|
2020-05-07 02:54:51 +02:00
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
return cty.NilVal, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
configureDiags := b.Configure(cfg)
|
|
|
|
if configureDiags.HasErrors() {
|
|
|
|
diags = diags.Append(configureDiags.Err())
|
2018-07-17 23:55:56 +02:00
|
|
|
return cty.NilVal, diags
|
2018-08-29 14:41:56 +02:00
|
|
|
}
|
2018-07-17 23:55:56 +02:00
|
|
|
|
2019-05-04 00:38:39 +02:00
|
|
|
newState := make(map[string]cty.Value)
|
|
|
|
newState["backend"] = d.GetAttr("backend")
|
|
|
|
newState["config"] = d.GetAttr("config")
|
2017-02-22 20:37:56 +01:00
|
|
|
|
2020-06-24 19:41:51 +02:00
|
|
|
workspaceVal := d.GetAttr("workspace")
|
|
|
|
// This attribute is not computed, so we always have to store the state
|
|
|
|
// value, even if we implicitly use a default.
|
|
|
|
newState["workspace"] = workspaceVal
|
2018-07-17 23:55:56 +02:00
|
|
|
|
2020-06-24 19:41:51 +02:00
|
|
|
workspaceName := backend.DefaultStateName
|
|
|
|
if !workspaceVal.IsNull() {
|
2019-05-04 00:38:39 +02:00
|
|
|
workspaceName = workspaceVal.AsString()
|
2017-12-05 19:18:28 +01:00
|
|
|
}
|
2018-03-09 16:22:18 +01:00
|
|
|
|
2019-05-04 00:38:39 +02:00
|
|
|
state, err := b.StateMgr(workspaceName)
|
2017-02-22 20:37:56 +01:00
|
|
|
if err != nil {
|
2018-07-17 23:55:56 +02:00
|
|
|
diags = diags.Append(tfdiags.AttributeValue(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Error loading state error",
|
|
|
|
fmt.Sprintf("error loading the remote state: %s", err),
|
|
|
|
cty.Path(nil).GetAttr("backend"),
|
|
|
|
))
|
|
|
|
return cty.NilVal, diags
|
2017-02-22 20:37:56 +01:00
|
|
|
}
|
2018-07-17 23:55:56 +02:00
|
|
|
|
2015-03-12 00:17:47 +01:00
|
|
|
if err := state.RefreshState(); err != nil {
|
2018-07-17 23:55:56 +02:00
|
|
|
diags = diags.Append(err)
|
|
|
|
return cty.NilVal, diags
|
2015-03-12 00:17:47 +01:00
|
|
|
}
|
2016-06-09 14:45:55 +02:00
|
|
|
|
2018-07-17 23:55:56 +02:00
|
|
|
outputs := make(map[string]cty.Value)
|
2016-07-07 21:37:57 +02:00
|
|
|
|
2018-07-17 23:55:56 +02:00
|
|
|
if defaultsVal := d.GetAttr("defaults"); !defaultsVal.IsNull() {
|
|
|
|
newState["defaults"] = defaultsVal
|
|
|
|
it := defaultsVal.ElementIterator()
|
|
|
|
for it.Next() {
|
|
|
|
k, v := it.Element()
|
|
|
|
outputs[k.AsString()] = v
|
|
|
|
}
|
2018-08-29 02:27:59 +02:00
|
|
|
} else {
|
|
|
|
newState["defaults"] = cty.NullVal(cty.DynamicPseudoType)
|
2017-09-13 18:34:00 +02:00
|
|
|
}
|
|
|
|
|
2016-07-07 21:37:57 +02:00
|
|
|
remoteState := state.State()
|
2018-10-02 02:10:12 +02:00
|
|
|
if remoteState == nil {
|
|
|
|
diags = diags.Append(tfdiags.AttributeValue(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Unable to find remote state",
|
|
|
|
"No stored state was found for the given workspace in the given backend.",
|
|
|
|
cty.Path(nil).GetAttr("workspace"),
|
|
|
|
))
|
|
|
|
newState["outputs"] = cty.EmptyObjectVal
|
|
|
|
return cty.ObjectVal(newState), diags
|
|
|
|
}
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
mod := remoteState.RootModule()
|
|
|
|
if mod != nil { // should always have a root module in any valid state
|
|
|
|
for k, os := range mod.OutputValues {
|
|
|
|
outputs[k] = os.Value
|
2018-07-17 23:55:56 +02:00
|
|
|
}
|
2015-04-02 07:49:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-17 23:55:56 +02:00
|
|
|
newState["outputs"] = cty.ObjectVal(outputs)
|
2017-09-13 18:34:00 +02:00
|
|
|
|
2018-07-17 23:55:56 +02:00
|
|
|
return cty.ObjectVal(newState), diags
|
2015-03-12 00:17:47 +01:00
|
|
|
}
|
2019-05-04 00:38:39 +02:00
|
|
|
|
2020-05-07 02:54:51 +02:00
|
|
|
func getBackend(cfg cty.Value) (backend.Backend, cty.Value, tfdiags.Diagnostics) {
|
2019-05-04 00:38:39 +02:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
backendType := cfg.GetAttr("backend").AsString()
|
|
|
|
|
|
|
|
// Don't break people using the old _local syntax - but note warning above
|
|
|
|
if backendType == "_local" {
|
|
|
|
log.Println(`[INFO] Switching old (unsupported) backend "_local" to "local"`)
|
|
|
|
backendType = "local"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the client to access our remote state
|
|
|
|
log.Printf("[DEBUG] Initializing remote state backend: %s", backendType)
|
2020-05-07 19:48:45 +02:00
|
|
|
f := getBackendFactory(backendType)
|
2019-05-04 00:38:39 +02:00
|
|
|
if f == nil {
|
|
|
|
diags = diags.Append(tfdiags.AttributeValue(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid backend configuration",
|
|
|
|
fmt.Sprintf("There is no backend type named %q.", backendType),
|
|
|
|
cty.Path(nil).GetAttr("backend"),
|
|
|
|
))
|
2020-05-07 02:54:51 +02:00
|
|
|
return nil, cty.NilVal, diags
|
2019-05-04 00:38:39 +02:00
|
|
|
}
|
|
|
|
b := f()
|
|
|
|
|
|
|
|
config := cfg.GetAttr("config")
|
|
|
|
if config.IsNull() {
|
|
|
|
// We'll treat this as an empty configuration and see if the backend's
|
|
|
|
// schema and validation code will accept it.
|
|
|
|
config = cty.EmptyObjectVal
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Type().IsMapType() { // The code below expects an object type, so we'll convert
|
|
|
|
config = cty.ObjectVal(config.AsValueMap())
|
|
|
|
}
|
|
|
|
|
|
|
|
schema := b.ConfigSchema()
|
|
|
|
// Try to coerce the provided value into the desired configuration type.
|
|
|
|
configVal, err := schema.CoerceValue(config)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(tfdiags.AttributeValue(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid backend configuration",
|
|
|
|
fmt.Sprintf("The given configuration is not valid for backend %q: %s.", backendType,
|
|
|
|
tfdiags.FormatError(err)),
|
|
|
|
cty.Path(nil).GetAttr("config"),
|
|
|
|
))
|
2020-05-07 02:54:51 +02:00
|
|
|
return nil, cty.NilVal, diags
|
2019-05-04 00:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
newVal, validateDiags := b.PrepareConfig(configVal)
|
|
|
|
diags = diags.Append(validateDiags)
|
|
|
|
if validateDiags.HasErrors() {
|
2020-05-07 02:54:51 +02:00
|
|
|
return nil, cty.NilVal, diags
|
2019-05-04 00:38:39 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 20:18:04 +01:00
|
|
|
// If this is the enhanced remote backend, we want to disable the version
|
|
|
|
// check, because this is a read-only operation
|
|
|
|
if rb, ok := b.(*remote.Remote); ok {
|
|
|
|
rb.IgnoreVersionConflict()
|
|
|
|
}
|
|
|
|
|
2020-05-07 02:54:51 +02:00
|
|
|
return b, newVal, diags
|
2019-05-04 00:38:39 +02:00
|
|
|
}
|
2020-05-07 19:48:45 +02:00
|
|
|
|
|
|
|
// overrideBackendFactories allows test cases to control the set of available
|
|
|
|
// backends to allow for more self-contained tests. This should never be set
|
|
|
|
// in non-test code.
|
|
|
|
var overrideBackendFactories map[string]backend.InitFn
|
|
|
|
|
|
|
|
func getBackendFactory(backendType string) backend.InitFn {
|
|
|
|
if len(overrideBackendFactories) > 0 {
|
|
|
|
// Tests may override the set of backend factories.
|
|
|
|
return overrideBackendFactories[backendType]
|
|
|
|
}
|
|
|
|
|
|
|
|
return backendInit.Backend(backendType)
|
|
|
|
}
|