2016-03-25 18:17:25 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-05-11 18:16:48 +02:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2016-03-25 18:17:25 +01:00
|
|
|
|
2018-07-04 12:07:44 +02:00
|
|
|
backendLocal "github.com/hashicorp/terraform/backend/local"
|
2016-05-11 02:03:58 +02:00
|
|
|
"github.com/hashicorp/terraform/state"
|
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
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2016-03-25 18:17:25 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateMeta is the meta struct that should be embedded in state subcommands.
|
2017-07-26 19:08:09 +02:00
|
|
|
type StateMeta struct {
|
|
|
|
Meta
|
|
|
|
}
|
2016-03-25 18:17:25 +01:00
|
|
|
|
2017-03-01 16:10:47 +01:00
|
|
|
// State returns the state for this meta. This gets the appropriate state from
|
|
|
|
// the backend, but changes the way that backups are done. This configures
|
|
|
|
// backups to be timestamped rather than just the original state path plus a
|
|
|
|
// backup path.
|
2017-07-26 19:08:09 +02:00
|
|
|
func (c *StateMeta) State() (state.State, error) {
|
2017-06-23 20:41:49 +02:00
|
|
|
var realState state.State
|
2017-07-26 19:08:09 +02:00
|
|
|
backupPath := c.backupPath
|
|
|
|
stateOutPath := c.statePath
|
2016-05-11 18:16:48 +02:00
|
|
|
|
2017-06-23 20:41:49 +02:00
|
|
|
// use the specified state
|
2017-07-26 19:08:09 +02:00
|
|
|
if c.statePath != "" {
|
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
|
|
|
realState = statemgr.NewFilesystem(c.statePath)
|
2017-06-23 20:41:49 +02:00
|
|
|
} else {
|
|
|
|
// Load the backend
|
2018-03-28 00:31:05 +02:00
|
|
|
b, backendDiags := c.Backend(nil)
|
|
|
|
if backendDiags.HasErrors() {
|
|
|
|
return nil, backendDiags.Err()
|
2017-06-23 20:41:49 +02:00
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
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
|
|
|
workspace := c.Workspace()
|
2017-06-23 20:41:49 +02:00
|
|
|
// Get the state
|
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
|
|
|
s, err := b.StateMgr(workspace)
|
2017-06-23 20:41:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a local backend
|
2018-03-28 00:31:05 +02:00
|
|
|
localRaw, backendDiags := c.Backend(&BackendOpts{ForceLocal: true})
|
|
|
|
if backendDiags.HasErrors() {
|
2017-06-23 20:41:49 +02:00
|
|
|
// This should never fail
|
2018-03-28 00:31:05 +02:00
|
|
|
panic(backendDiags.Err())
|
2017-06-23 20:41:49 +02:00
|
|
|
}
|
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
|
|
|
localB := localRaw.(*backendlocal.Local)
|
|
|
|
_, stateOutPath, _ = localB.StatePaths(workspace)
|
2017-06-23 20:41:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
realState = s
|
2017-02-22 19:11:26 +01:00
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2017-06-23 20:41:49 +02:00
|
|
|
// We always backup state commands, so set the back if none was specified
|
|
|
|
// (the default is "-", but some tests bypass the flag parsing).
|
|
|
|
if backupPath == "-" || backupPath == "" {
|
|
|
|
// Determine the backup path. stateOutPath is set to the resulting
|
|
|
|
// file where state is written (cached in the case of remote state)
|
|
|
|
backupPath = fmt.Sprintf(
|
|
|
|
"%s.%d%s",
|
|
|
|
stateOutPath,
|
|
|
|
time.Now().UTC().Unix(),
|
|
|
|
DefaultBackupExtension)
|
|
|
|
}
|
2016-05-11 18:16:48 +02:00
|
|
|
|
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
|
|
|
// If the backend is local (which it should always be, given our asserting
|
|
|
|
// of it above) we can now enable backups for it.
|
|
|
|
if lb, ok := realState.(*statemgr.Filesystem); ok {
|
|
|
|
lb.SetBackupPath(backupPath)
|
2016-05-11 18:16:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 20:41:49 +02:00
|
|
|
return realState, nil
|
2016-05-11 02:03:58 +02:00
|
|
|
}
|
|
|
|
|
2016-03-25 18:17:25 +01:00
|
|
|
// filterInstance filters a single instance out of filter results.
|
|
|
|
func (c *StateMeta) filterInstance(rs []*terraform.StateFilterResult) (*terraform.StateFilterResult, error) {
|
|
|
|
var result *terraform.StateFilterResult
|
|
|
|
for _, r := range rs {
|
|
|
|
if _, ok := r.Value.(*terraform.InstanceState); !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != nil {
|
|
|
|
return nil, errors.New(errStateMultiple)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = r
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const errStateMultiple = `Multiple instances found for the given pattern!
|
|
|
|
|
|
|
|
This command requires that the pattern match exactly one instance
|
|
|
|
of a resource. To view the matched instances, use "terraform state list".
|
|
|
|
Please modify the pattern to match only a single instance.`
|