github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/state_rm.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/mitchellh/cli"
     8  )
     9  
    10  // StateRmCommand is a Command implementation that shows a single resource.
    11  type StateRmCommand struct {
    12  	StateMeta
    13  }
    14  
    15  func (c *StateRmCommand) Run(args []string) int {
    16  	args, err := c.Meta.process(args, true)
    17  	if err != nil {
    18  		return 1
    19  	}
    20  
    21  	cmdFlags := c.Meta.flagSet("state show")
    22  	cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup")
    23  	cmdFlags.StringVar(&c.statePath, "state", "", "path")
    24  	if err := cmdFlags.Parse(args); err != nil {
    25  		return cli.RunResultHelp
    26  	}
    27  	args = cmdFlags.Args()
    28  
    29  	if len(args) < 1 {
    30  		c.Ui.Error("At least one resource address is required.")
    31  		return 1
    32  	}
    33  
    34  	state, err := c.State()
    35  	if err != nil {
    36  		c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
    37  		return 1
    38  	}
    39  	if err := state.RefreshState(); err != nil {
    40  		c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
    41  		return 1
    42  	}
    43  
    44  	stateReal := state.State()
    45  	if stateReal == nil {
    46  		c.Ui.Error(fmt.Sprintf(errStateNotFound))
    47  		return 1
    48  	}
    49  
    50  	if err := stateReal.Remove(args...); err != nil {
    51  		c.Ui.Error(fmt.Sprintf(errStateRm, err))
    52  		return 1
    53  	}
    54  
    55  	c.Ui.Output(fmt.Sprintf("%d items removed.", len(args)))
    56  
    57  	if err := state.WriteState(stateReal); err != nil {
    58  		c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
    59  		return 1
    60  	}
    61  
    62  	if err := state.PersistState(); err != nil {
    63  		c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
    64  		return 1
    65  	}
    66  
    67  	c.Ui.Output("Item removal successful.")
    68  	return 0
    69  }
    70  
    71  func (c *StateRmCommand) Help() string {
    72  	helpText := `
    73  Usage: terraform state rm [options] ADDRESS...
    74  
    75    Remove one or more items from the Terraform state.
    76  
    77    This command removes one or more items from the Terraform state based
    78    on the address given. You can view and list the available resources
    79    with "terraform state list".
    80  
    81    This command creates a timestamped backup of the state on every invocation.
    82    This can't be disabled. Due to the destructive nature of this command,
    83    the backup is ensured by Terraform for safety reasons.
    84  
    85  Options:
    86  
    87    -backup=PATH        Path where Terraform should write the backup
    88                        state. This can't be disabled. If not set, Terraform
    89                        will write it to the same path as the statefile with
    90                        a backup extension.
    91  
    92    -state=PATH         Path to the source state file. Defaults to the configured
    93                        backend, or "terraform.tfstate"
    94  
    95  `
    96  	return strings.TrimSpace(helpText)
    97  }
    98  
    99  func (c *StateRmCommand) Synopsis() string {
   100  	return "Remove an item from the state"
   101  }
   102  
   103  const errStateRm = `Error removing items from the state: %s
   104  
   105  The state was not saved. No items were removed from the persisted
   106  state. No backup was created since no modification occurred. Please
   107  resolve the issue above and try again.`
   108  
   109  const errStateRmPersist = `Error saving the state: %s
   110  
   111  The state was not saved. No items were removed from the persisted
   112  state. No backup was created since no modification occurred. Please
   113  resolve the issue above and try again.`