2015-03-11 18:17:47 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2018-07-17 16:55:56 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
"github.com/hashicorp/terraform/providers"
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2018-10-31 10:45:03 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
backendInit "github.com/hashicorp/terraform/backend/init"
|
2015-03-11 18:17:47 -05:00
|
|
|
)
|
|
|
|
|
2018-07-17 16:55:56 -05:00
|
|
|
func dataSourceRemoteStateGetSchema() providers.Schema {
|
|
|
|
return providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"backend": {
|
|
|
|
Type: cty.String,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"config": {
|
|
|
|
Type: cty.DynamicPseudoType,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"defaults": {
|
|
|
|
Type: cty.DynamicPseudoType,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"outputs": {
|
|
|
|
Type: cty.DynamicPseudoType,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"workspace": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-03-11 18:17:47 -05:00
|
|
|
}
|
|
|
|
|
2019-05-03 17:38:39 -05:00
|
|
|
func dataSourceRemoteStateValidate(cfg cty.Value) tfdiags.Diagnostics {
|
2018-07-17 16:55:56 -05:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
2019-05-03 17:38:39 -05: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() {
|
|
|
|
_, moreDiags := getBackend(cfg)
|
|
|
|
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-03 17:38:43 -05:00
|
|
|
}
|
|
|
|
|
2019-05-03 17:38:39 -05: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-11 18:17:47 -05:00
|
|
|
}
|
|
|
|
|
2019-05-03 17:38:39 -05:00
|
|
|
return diags
|
|
|
|
}
|
2018-03-20 20:43:02 -05:00
|
|
|
|
2019-05-03 17:38:39 -05:00
|
|
|
func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
2018-07-17 16:55:56 -05:00
|
|
|
|
2019-05-03 17:38:39 -05:00
|
|
|
b, moreDiags := getBackend(d)
|
|
|
|
diags = diags.Append(moreDiags)
|
|
|
|
if diags.HasErrors() {
|
2018-07-17 16:55:56 -05:00
|
|
|
return cty.NilVal, diags
|
2018-08-29 07:41:56 -05:00
|
|
|
}
|
2018-07-17 16:55:56 -05:00
|
|
|
|
2019-05-03 17:38:39 -05:00
|
|
|
newState := make(map[string]cty.Value)
|
|
|
|
newState["backend"] = d.GetAttr("backend")
|
|
|
|
newState["config"] = d.GetAttr("config")
|
2017-02-22 13:37:56 -06:00
|
|
|
|
2019-05-03 17:38:39 -05:00
|
|
|
workspaceName := backend.DefaultStateName
|
2018-07-17 16:55:56 -05:00
|
|
|
|
|
|
|
if workspaceVal := d.GetAttr("workspace"); !workspaceVal.IsNull() {
|
|
|
|
newState["workspace"] = workspaceVal
|
2019-05-03 17:38:39 -05:00
|
|
|
workspaceName = workspaceVal.AsString()
|
2017-12-05 12:18:28 -06:00
|
|
|
}
|
2018-03-09 09:22:18 -06:00
|
|
|
|
2019-05-03 17:38:39 -05:00
|
|
|
newState["workspace"] = cty.StringVal(workspaceName)
|
2018-12-20 16:03:19 -06:00
|
|
|
|
2019-05-03 17:38:39 -05:00
|
|
|
state, err := b.StateMgr(workspaceName)
|
2017-02-22 13:37:56 -06:00
|
|
|
if err != nil {
|
2018-07-17 16:55:56 -05: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 13:37:56 -06:00
|
|
|
}
|
2018-07-17 16:55:56 -05:00
|
|
|
|
2015-03-11 18:17:47 -05:00
|
|
|
if err := state.RefreshState(); err != nil {
|
2018-07-17 16:55:56 -05:00
|
|
|
diags = diags.Append(err)
|
|
|
|
return cty.NilVal, diags
|
2015-03-11 18:17:47 -05:00
|
|
|
}
|
2016-06-09 07:45:55 -05:00
|
|
|
|
2018-07-17 16:55:56 -05:00
|
|
|
outputs := make(map[string]cty.Value)
|
2016-07-07 14:37:57 -05:00
|
|
|
|
2018-07-17 16:55:56 -05: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-28 19:27:59 -05:00
|
|
|
} else {
|
|
|
|
newState["defaults"] = cty.NullVal(cty.DynamicPseudoType)
|
2017-09-13 11:34:00 -05:00
|
|
|
}
|
|
|
|
|
2016-07-07 14:37:57 -05:00
|
|
|
remoteState := state.State()
|
2018-10-01 19:10:12 -05: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 16:24:45 -05: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 16:55:56 -05:00
|
|
|
}
|
2015-04-02 00:49:05 -05:00
|
|
|
}
|
|
|
|
|
2018-07-17 16:55:56 -05:00
|
|
|
newState["outputs"] = cty.ObjectVal(outputs)
|
2017-09-13 11:34:00 -05:00
|
|
|
|
2018-07-17 16:55:56 -05:00
|
|
|
return cty.ObjectVal(newState), diags
|
2015-03-11 18:17:47 -05:00
|
|
|
}
|
2019-05-03 17:38:39 -05:00
|
|
|
|
|
|
|
func getBackend(cfg cty.Value) (backend.Backend, tfdiags.Diagnostics) {
|
|
|
|
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)
|
|
|
|
f := backendInit.Backend(backendType)
|
|
|
|
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"),
|
|
|
|
))
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
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"),
|
|
|
|
))
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
newVal, validateDiags := b.PrepareConfig(configVal)
|
|
|
|
diags = diags.Append(validateDiags)
|
|
|
|
if validateDiags.HasErrors() {
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
configVal = newVal
|
|
|
|
|
|
|
|
configureDiags := b.Configure(configVal)
|
|
|
|
if configureDiags.HasErrors() {
|
|
|
|
diags = diags.Append(configureDiags.Err())
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, diags
|
|
|
|
}
|