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