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
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
backendlocal "github.com/hashicorp/terraform/backend/local"
|
2016-05-11 02:03:58 +02:00
|
|
|
"github.com/hashicorp/terraform/state"
|
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-03-01 16:10:47 +01:00
|
|
|
type StateMeta struct{}
|
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.
|
2016-05-11 18:16:48 +02:00
|
|
|
func (c *StateMeta) State(m *Meta) (state.State, error) {
|
2017-01-19 05:50:45 +01:00
|
|
|
// Load the backend
|
|
|
|
b, err := m.Backend(nil)
|
2016-05-11 18:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-01 16:10:47 +01:00
|
|
|
env := m.Env()
|
2017-01-19 05:50:45 +01:00
|
|
|
// Get the state
|
2017-02-28 19:13:03 +01:00
|
|
|
s, err := b.State(env)
|
2017-01-19 05:50:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a local backend
|
|
|
|
localRaw, err := m.Backend(&BackendOpts{ForceLocal: true})
|
|
|
|
if err != nil {
|
|
|
|
// This should never fail
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
localB := localRaw.(*backendlocal.Local)
|
2017-03-01 01:18:16 +01:00
|
|
|
_, stateOutPath, _ := localB.StatePaths(env)
|
2017-02-22 19:11:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
|
2016-05-11 18:16:48 +02:00
|
|
|
// 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(
|
2016-11-22 21:39:00 +01:00
|
|
|
"%s.%d%s",
|
2017-02-22 19:11:26 +01:00
|
|
|
stateOutPath,
|
2016-05-11 18:16:48 +02:00
|
|
|
time.Now().UTC().Unix(),
|
|
|
|
DefaultBackupExtension)
|
|
|
|
|
|
|
|
// Wrap it for backups
|
|
|
|
s = &state.BackupState{
|
|
|
|
Real: s,
|
|
|
|
Path: backupPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, 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.`
|