github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/command/state_show.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/cli"
    10  	"github.com/ryanuber/columnize"
    11  )
    12  
    13  // StateShowCommand is a Command implementation that shows a single resource.
    14  type StateShowCommand struct {
    15  	Meta
    16  	StateMeta
    17  }
    18  
    19  func (c *StateShowCommand) Run(args []string) int {
    20  	args = c.Meta.process(args, true)
    21  
    22  	cmdFlags := c.Meta.flagSet("state show")
    23  	cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
    24  	if err := cmdFlags.Parse(args); err != nil {
    25  		return cli.RunResultHelp
    26  	}
    27  	args = cmdFlags.Args()
    28  
    29  	state, err := c.Meta.State()
    30  	if err != nil {
    31  		c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
    32  		return cli.RunResultHelp
    33  	}
    34  
    35  	stateReal := state.State()
    36  	if stateReal == nil {
    37  		c.Ui.Error(fmt.Sprintf(errStateNotFound))
    38  		return 1
    39  	}
    40  
    41  	filter := &terraform.StateFilter{State: stateReal}
    42  	results, err := filter.Filter(args...)
    43  	if err != nil {
    44  		c.Ui.Error(fmt.Sprintf(errStateFilter, err))
    45  		return 1
    46  	}
    47  
    48  	if len(results) == 0 {
    49  		return 0
    50  	}
    51  
    52  	instance, err := c.filterInstance(results)
    53  	if err != nil {
    54  		c.Ui.Error(err.Error())
    55  		return 1
    56  	}
    57  
    58  	if instance == nil {
    59  		return 0
    60  	}
    61  
    62  	is := instance.Value.(*terraform.InstanceState)
    63  
    64  	// Sort the keys
    65  	var keys []string
    66  	for k, _ := range is.Attributes {
    67  		keys = append(keys, k)
    68  	}
    69  	sort.Strings(keys)
    70  
    71  	// Build the output
    72  	var output []string
    73  	output = append(output, fmt.Sprintf("id | %s", is.ID))
    74  	for _, k := range keys {
    75  		if k != "id" {
    76  			output = append(output, fmt.Sprintf("%s | %s", k, is.Attributes[k]))
    77  		}
    78  	}
    79  
    80  	// Output
    81  	config := columnize.DefaultConfig()
    82  	config.Glue = " = "
    83  	c.Ui.Output(columnize.Format(output, config))
    84  	return 0
    85  }
    86  
    87  func (c *StateShowCommand) Help() string {
    88  	helpText := `
    89  Usage: terraform state show [options] ADDRESS
    90  
    91    Shows the attributes of a resource in the Terraform state.
    92  
    93    This command shows the attributes of a single resource in the Terraform
    94    state. The address argument must be used to specify a single resource.
    95    You can view the list of available resources with "terraform state list".
    96  
    97  Options:
    98  
    99    -state=statefile    Path to a Terraform state file to use to look
   100                        up Terraform-managed resources. By default it will
   101                        use the state "terraform.tfstate" if it exists.
   102  
   103  `
   104  	return strings.TrimSpace(helpText)
   105  }
   106  
   107  func (c *StateShowCommand) Synopsis() string {
   108  	return "Show a resource in the state"
   109  }