github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/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  	Meta
    13  	StateMeta
    14  }
    15  
    16  func (c *StateRmCommand) Run(args []string) int {
    17  	args = c.Meta.process(args, true)
    18  
    19  	cmdFlags := c.Meta.flagSet("state show")
    20  	cmdFlags.StringVar(&c.Meta.backupPath, "backup", "-", "backup")
    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  	state, err := c.StateMeta.State(&c.Meta)
    28  	if err != nil {
    29  		c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
    30  		return cli.RunResultHelp
    31  	}
    32  
    33  	stateReal := state.State()
    34  	if stateReal == nil {
    35  		c.Ui.Error(fmt.Sprintf(errStateNotFound))
    36  		return 1
    37  	}
    38  
    39  	if err := stateReal.Remove(args...); err != nil {
    40  		c.Ui.Error(fmt.Sprintf(errStateRm, err))
    41  		return 1
    42  	}
    43  
    44  	if err := state.WriteState(stateReal); err != nil {
    45  		c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
    46  		return 1
    47  	}
    48  
    49  	if err := state.PersistState(); err != nil {
    50  		c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
    51  		return 1
    52  	}
    53  
    54  	c.Ui.Output("Item removal successful.")
    55  	return 0
    56  }
    57  
    58  func (c *StateRmCommand) Help() string {
    59  	helpText := `
    60  Usage: terraform state rm [options] ADDRESS...
    61  
    62    Remove one or more items from the Terraform state.
    63  
    64    This command removes one or more items from the Terraform state based
    65    on the address given. You can view and list the available resources
    66    with "terraform state list".
    67  
    68    This command creates a timestamped backup of the state on every invocation.
    69    This can't be disabled. Due to the destructive nature of this command,
    70    the backup is ensured by Terraform for safety reasons.
    71  
    72  Options:
    73  
    74    -backup=PATH        Path where Terraform should write the backup
    75                        state. This can't be disabled. If not set, Terraform
    76                        will write it to the same path as the statefile with
    77                        a backup extension. This backup will be made in addition
    78                        to the timestamped backup.
    79  
    80    -state=statefile    Path to a Terraform state file to use to look
    81                        up Terraform-managed resources. By default it will
    82                        use the state "terraform.tfstate" if it exists.
    83  
    84  `
    85  	return strings.TrimSpace(helpText)
    86  }
    87  
    88  func (c *StateRmCommand) Synopsis() string {
    89  	return "Remove an item from the state"
    90  }
    91  
    92  const errStateRm = `Error removing items from the state: %s
    93  
    94  The state was not saved. No items were removed from the persisted
    95  state. No backup was created since no modification occurred. Please
    96  resolve the issue above and try again.`
    97  
    98  const errStateRmPersist = `Error saving the state: %s
    99  
   100  The state was not saved. No items were removed from the persisted
   101  state. No backup was created since no modification occurred. Please
   102  resolve the issue above and try again.`