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