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