2016-03-25 12:17:25 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateShowCommand is a Command implementation that shows a single resource.
|
|
|
|
type StateShowCommand struct {
|
2017-03-01 09:10:47 -06:00
|
|
|
Meta
|
2016-03-25 12:17:25 -05:00
|
|
|
StateMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateShowCommand) Run(args []string) int {
|
|
|
|
args = c.Meta.process(args, true)
|
|
|
|
|
|
|
|
cmdFlags := c.Meta.flagSet("state show")
|
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
// Load the backend
|
|
|
|
b, err := c.Backend(nil)
|
2016-03-25 12:17:25 -05:00
|
|
|
if err != nil {
|
2017-01-18 22:50:45 -06:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the state
|
2017-02-28 12:13:03 -06:00
|
|
|
env := c.Env()
|
|
|
|
state, err := b.State(env)
|
2017-01-18 22:50:45 -06:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
|
|
|
return 1
|
2016-03-25 12:17:25 -05:00
|
|
|
}
|
2017-02-21 22:35:43 -06:00
|
|
|
if err := state.RefreshState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2016-03-25 12:17:25 -05:00
|
|
|
|
|
|
|
stateReal := state.State()
|
|
|
|
if stateReal == nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateNotFound))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := &terraform.StateFilter{State: stateReal}
|
|
|
|
results, err := filter.Filter(args...)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateFilter, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-08-12 13:19:42 -05:00
|
|
|
if len(results) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-03-25 12:17:25 -05:00
|
|
|
instance, err := c.filterInstance(results)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2016-08-12 13:19:42 -05:00
|
|
|
|
2016-10-26 16:18:36 -05:00
|
|
|
if instance == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-03-25 12:17:25 -05:00
|
|
|
is := instance.Value.(*terraform.InstanceState)
|
|
|
|
|
|
|
|
// Sort the keys
|
2016-08-12 13:19:42 -05:00
|
|
|
var keys []string
|
2016-03-25 12:17:25 -05:00
|
|
|
for k, _ := range is.Attributes {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
// Build the output
|
2016-08-12 13:19:42 -05:00
|
|
|
var output []string
|
2016-03-25 12:17:25 -05:00
|
|
|
output = append(output, fmt.Sprintf("id | %s", is.ID))
|
|
|
|
for _, k := range keys {
|
2016-03-25 12:26:30 -05:00
|
|
|
if k != "id" {
|
|
|
|
output = append(output, fmt.Sprintf("%s | %s", k, is.Attributes[k]))
|
|
|
|
}
|
2016-03-25 12:17:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output
|
|
|
|
config := columnize.DefaultConfig()
|
|
|
|
config.Glue = " = "
|
|
|
|
c.Ui.Output(columnize.Format(output, config))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateShowCommand) Help() string {
|
|
|
|
helpText := `
|
2016-03-25 16:33:31 -05:00
|
|
|
Usage: terraform state show [options] ADDRESS
|
2016-03-25 12:17:25 -05:00
|
|
|
|
|
|
|
Shows the attributes of a resource in the Terraform state.
|
|
|
|
|
|
|
|
This command shows the attributes of a single resource in the Terraform
|
2016-03-25 16:33:31 -05:00
|
|
|
state. The address argument must be used to specify a single resource.
|
2016-03-25 12:17:25 -05:00
|
|
|
You can view the list of available resources with "terraform state list".
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-state=statefile Path to a Terraform state file to use to look
|
|
|
|
up Terraform-managed resources. By default it will
|
|
|
|
use the state "terraform.tfstate" if it exists.
|
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateShowCommand) Synopsis() string {
|
|
|
|
return "Show a resource in the state"
|
|
|
|
}
|