@@ -15,8 +15,10 @@ type TaintCommand struct {
1515func (c * TaintCommand ) Run (args []string ) int {
1616 args = c .Meta .process (args , false )
1717
18+ var allowMissing bool
1819 var module string
1920 cmdFlags := c .Meta .flagSet ("taint" )
21+ cmdFlags .BoolVar (& allowMissing , "allow-missing" , false , "module" )
2022 cmdFlags .StringVar (& module , "module" , "" , "module" )
2123 cmdFlags .StringVar (& c .Meta .statePath , "state" , DefaultStateFilename , "path" )
2224 cmdFlags .StringVar (& c .Meta .stateOutPath , "state-out" , "" , "path" )
@@ -33,7 +35,13 @@ func (c *TaintCommand) Run(args []string) int {
3335 cmdFlags .Usage ()
3436 return 1
3537 }
38+
3639 name := args [0 ]
40+ if module == "" {
41+ module = "root"
42+ } else {
43+ module = "root." + module
44+ }
3745
3846 // Get the state that we'll be modifying
3947 state , err := c .State ()
@@ -45,6 +53,10 @@ func (c *TaintCommand) Run(args []string) int {
4553 // Get the actual state structure
4654 s := state .State ()
4755 if s .Empty () {
56+ if allowMissing {
57+ return c .allowMissingExit (name , module )
58+ }
59+
4860 c .Ui .Error (fmt .Sprintf (
4961 "The state is empty. The most common reason for this is that\n " +
5062 "an invalid state file path was given or Terraform has never\n " +
@@ -54,14 +66,13 @@ func (c *TaintCommand) Run(args []string) int {
5466 }
5567
5668 // Get the proper module we want to taint
57- if module == "" {
58- module = "root"
59- } else {
60- module = "root." + module
61- }
6269 modPath := strings .Split (module , "." )
6370 mod := s .ModuleByPath (modPath )
6471 if mod == nil {
72+ if allowMissing {
73+ return c .allowMissingExit (name , module )
74+ }
75+
6576 c .Ui .Error (fmt .Sprintf (
6677 "The module %s could not be found. There is nothing to taint." ,
6778 module ))
@@ -70,6 +81,10 @@ func (c *TaintCommand) Run(args []string) int {
7081
7182 // If there are no resources in this module, it is an error
7283 if len (mod .Resources ) == 0 {
84+ if allowMissing {
85+ return c .allowMissingExit (name , module )
86+ }
87+
7388 c .Ui .Error (fmt .Sprintf (
7489 "The module %s has no resources. There is nothing to taint." ,
7590 module ))
@@ -79,6 +94,10 @@ func (c *TaintCommand) Run(args []string) int {
7994 // Get the resource we're looking for
8095 rs , ok := mod .Resources [name ]
8196 if ! ok {
97+ if allowMissing {
98+ return c .allowMissingExit (name , module )
99+ }
100+
82101 c .Ui .Error (fmt .Sprintf (
83102 "The resource %s couldn't be found in the module %s." ,
84103 name ,
@@ -116,6 +135,9 @@ Usage: terraform taint [options] name
116135
117136Options:
118137
138+ -allow-missing If specified, the command will succeed (exit code 0)
139+ even if the resource is missing.
140+
119141 -backup=path Path to backup the existing state file before
120142 modifying. Defaults to the "-state-out" path with
121143 ".backup" extension. Set to "-" to disable backup.
@@ -139,3 +161,11 @@ Options:
139161func (c * TaintCommand ) Synopsis () string {
140162 return "Manually mark a resource for recreation"
141163}
164+
165+ func (c * TaintCommand ) allowMissingExit (name , module string ) int {
166+ c .Ui .Output (fmt .Sprintf (
167+ "The resource %s in the module %s was not found, but\n " +
168+ "-allow-missing is set, so we're exiting successfully." ,
169+ name , module ))
170+ return 0
171+ }
0 commit comments