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