github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/command/state_list.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"github.com/mitchellh/cli"
     9  )
    10  
    11  // StateListCommand is a Command implementation that lists the resources
    12  // within a state file.
    13  type StateListCommand struct {
    14  	Meta
    15  }
    16  
    17  func (c *StateListCommand) Run(args []string) int {
    18  	args = c.Meta.process(args, true)
    19  
    20  	cmdFlags := c.Meta.flagSet("state list")
    21  	cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
    22  	if err := cmdFlags.Parse(args); err != nil {
    23  		return cli.RunResultHelp
    24  	}
    25  	args = cmdFlags.Args()
    26  
    27  	// Load the backend
    28  	b, err := c.Backend(nil)
    29  	if err != nil {
    30  		c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
    31  		return 1
    32  	}
    33  
    34  	// Get the state
    35  	state, err := b.State()
    36  	if err != nil {
    37  		c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
    38  		return 1
    39  	}
    40  
    41  	stateReal := state.State()
    42  	if stateReal == nil {
    43  		c.Ui.Error(fmt.Sprintf(errStateNotFound))
    44  		return 1
    45  	}
    46  
    47  	filter := &terraform.StateFilter{State: stateReal}
    48  	results, err := filter.Filter(args...)
    49  	if err != nil {
    50  		c.Ui.Error(fmt.Sprintf(errStateFilter, err))
    51  		return cli.RunResultHelp
    52  	}
    53  
    54  	for _, result := range results {
    55  		if _, ok := result.Value.(*terraform.InstanceState); ok {
    56  			c.Ui.Output(result.Address)
    57  		}
    58  	}
    59  
    60  	return 0
    61  }
    62  
    63  func (c *StateListCommand) Help() string {
    64  	helpText := `
    65  Usage: terraform state list [options] [pattern...]
    66  
    67    List resources in the Terraform state.
    68  
    69    This command lists resources in the Terraform state. The pattern argument
    70    can be used to filter the resources by resource or module. If no pattern
    71    is given, all resources are listed.
    72  
    73    The pattern argument is meant to provide very simple filtering. For
    74    advanced filtering, please use tools such as "grep". The output of this
    75    command is designed to be friendly for this usage.
    76  
    77    The pattern argument accepts any resource targeting syntax. Please
    78    refer to the documentation on resource targeting syntax for more
    79    information.
    80  
    81  Options:
    82  
    83    -state=statefile    Path to a Terraform state file to use to look
    84                        up Terraform-managed resources. By default it will
    85                        use the state "terraform.tfstate" if it exists.
    86  
    87  `
    88  	return strings.TrimSpace(helpText)
    89  }
    90  
    91  func (c *StateListCommand) Synopsis() string {
    92  	return "List resources in the state"
    93  }
    94  
    95  const errStateFilter = `Error filtering state: %[1]s
    96  
    97  Please ensure that all your addresses are formatted properly.`
    98  
    99  const errStateLoadingState = `Error loading the state: %[1]s
   100  
   101  Please ensure that your Terraform state exists and that you've
   102  configured it properly. You can use the "-state" flag to point
   103  Terraform at another state file.`
   104  
   105  const errStateNotFound = `No state file was found!
   106  
   107  State management commands require a state file. Run this command
   108  in a directory where Terraform has been run or use the -state flag
   109  to point the command to a specific state location.`