github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/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  	// Load the backend
    30  	b, err := c.Backend(nil)
    31  	if err != nil {
    32  		c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
    33  		return 1
    34  	}
    35  
    36  	// Get the state
    37  	state, err := b.State()
    38  	if err != nil {
    39  		c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
    40  		return 1
    41  	}
    42  
    43  	stateReal := state.State()
    44  	if stateReal == nil {
    45  		c.Ui.Error(fmt.Sprintf(errStateNotFound))
    46  		return 1
    47  	}
    48  
    49  	filter := &terraform.StateFilter{State: stateReal}
    50  	results, err := filter.Filter(args...)
    51  	if err != nil {
    52  		c.Ui.Error(fmt.Sprintf(errStateFilter, err))
    53  		return 1
    54  	}
    55  
    56  	if len(results) == 0 {
    57  		return 0
    58  	}
    59  
    60  	instance, err := c.filterInstance(results)
    61  	if err != nil {
    62  		c.Ui.Error(err.Error())
    63  		return 1
    64  	}
    65  
    66  	if instance == nil {
    67  		return 0
    68  	}
    69  
    70  	is := instance.Value.(*terraform.InstanceState)
    71  
    72  	// Sort the keys
    73  	var keys []string
    74  	for k, _ := range is.Attributes {
    75  		keys = append(keys, k)
    76  	}
    77  	sort.Strings(keys)
    78  
    79  	// Build the output
    80  	var output []string
    81  	output = append(output, fmt.Sprintf("id | %s", is.ID))
    82  	for _, k := range keys {
    83  		if k != "id" {
    84  			output = append(output, fmt.Sprintf("%s | %s", k, is.Attributes[k]))
    85  		}
    86  	}
    87  
    88  	// Output
    89  	config := columnize.DefaultConfig()
    90  	config.Glue = " = "
    91  	c.Ui.Output(columnize.Format(output, config))
    92  	return 0
    93  }
    94  
    95  func (c *StateShowCommand) Help() string {
    96  	helpText := `
    97  Usage: terraform state show [options] ADDRESS
    98  
    99    Shows the attributes of a resource in the Terraform state.
   100  
   101    This command shows the attributes of a single resource in the Terraform
   102    state. The address argument must be used to specify a single resource.
   103    You can view the list of available resources with "terraform state list".
   104  
   105  Options:
   106  
   107    -state=statefile    Path to a Terraform state file to use to look
   108                        up Terraform-managed resources. By default it will
   109                        use the state "terraform.tfstate" if it exists.
   110  
   111  `
   112  	return strings.TrimSpace(helpText)
   113  }
   114  
   115  func (c *StateShowCommand) Synopsis() string {
   116  	return "Show a resource in the state"
   117  }