Repository: aws-cloudformation/rain
Pull request #62 (2021-05-07 13:16:08)

Enhancement #37
*Issue #, if available:* #37

*Description of changes:* Add -t flag on deploy for termination protection


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.



Patch:
diff --git a/internal/aws/cfn/mock.go b/internal/aws/cfn/mock.go
index 5152a6a7..b22be202 100644
--- a/internal/aws/cfn/mock.go
+++ b/internal/aws/cfn/mock.go
@@ -115,6 +115,7 @@ func DeleteStack(stackName string) error {
 func SetTerminationProtection(stackName string, protectionEnabled bool) error {
 	if s, ok := region().stacks[stackName]; ok {
 		s.stack.EnableTerminationProtection = ptr.Bool(true)
+		return nil
 	}
 
 	return noStackErr
diff --git a/internal/cmd/deploy/deploy.go b/internal/cmd/deploy/deploy.go
index f8133fa1..ba2cb2b6 100644
--- a/internal/cmd/deploy/deploy.go
+++ b/internal/cmd/deploy/deploy.go
@@ -21,6 +21,7 @@ var detach bool
 var yes bool
 var params []string
 var tags []string
+var terminationProtection bool
 
 // Cmd is the deploy command's entrypoint
 var Cmd = &cobra.Command{
@@ -151,6 +152,14 @@ The bucket's name will be of the format rain-artifacts-<AWS account id>-<AWS reg
 				panic(fmt.Errorf("failed deploying stack '%s'", stackName))
 			}
 		}
+
+		// Enable termination protection
+		if terminationProtection {
+			err = cfn.SetTerminationProtection(stackName, true)
+			if err != nil {
+				panic(ui.Errorf(err, "error while enabling termination protection on stack '%s'", stackName))
+			}
+		}
 	},
 }
 
@@ -161,4 +170,5 @@ func init() {
 	Cmd.Flags().BoolVarP(&yes, "yes", "y", false, "Don't ask questions; just deploy.")
 	Cmd.Flags().StringSliceVar(&tags, "tags", []string{}, "Add tags to the stack. Use the format key1=value1,key2=value2.")
 	Cmd.Flags().StringSliceVar(&params, "params", []string{}, "Set parameter values. Use the format key1=value1,key2=value2.")
+	Cmd.Flags().BoolVarP(&terminationProtection, "termination-protection", "t", false, "Enable  termination protection on the stack.")
 }
diff --git a/internal/cmd/rm/rm.go b/internal/cmd/rm/rm.go
index 7b762abc..66b7d197 100644
--- a/internal/cmd/rm/rm.go
+++ b/internal/cmd/rm/rm.go
@@ -31,6 +31,18 @@ var Cmd = &cobra.Command{
 			panic(ui.Errorf(err, "unable to delete stack '%s'", stackName))
 		}
 
+		if !yes {
+			output, _ := ui.GetStackOutput(stack)
+
+			spinner.Pause()
+			fmt.Println(output)
+
+			if !console.Confirm(false, "Are you sure you want to delete this stack?") {
+				panic(fmt.Errorf("user cancelled deletion of stack '%s'", stackName))
+			}
+			spinner.Resume()
+		}
+
 		if *stack.EnableTerminationProtection {
 			spinner.Pause()
 
@@ -47,18 +59,6 @@ var Cmd = &cobra.Command{
 			spinner.Resume()
 		}
 
-		if !yes {
-			output, _ := ui.GetStackOutput(stack)
-
-			spinner.Pause()
-			fmt.Println(output)
-
-			if !console.Confirm(false, "Are you sure you want to delete this stack?") {
-				panic(fmt.Errorf("user cancelled deletion of stack '%s'", stackName))
-			}
-			spinner.Resume()
-		}
-
 		spinner.Pop()
 
 		err = cfn.DeleteStack(stackName)


Test patch:
diff --git a/internal/cmd/deploy/deploy_test.go b/internal/cmd/deploy/deploy_test.go
index b525665e..d045cbf8 100644
--- a/internal/cmd/deploy/deploy_test.go
+++ b/internal/cmd/deploy/deploy_test.go
@@ -25,9 +25,10 @@ func Example_deploy_help() {
 	//   deploy <template> [stack]
 	//
 	// Flags:
-	//   -d, --detach           Once deployment has started, don't wait around for it to finish.
-	//   -h, --help             help for deploy
-	//       --params strings   Set parameter values. Use the format key1=value1,key2=value2.
-	//       --tags strings     Add tags to the stack. Use the format key1=value1,key2=value2.
-	//   -y, --yes              Don't ask questions; just deploy.
+	//   -d, --detach                   Once deployment has started, don't wait around for it to finish.
+	//   -h, --help                     help for deploy
+	//       --params strings           Set parameter values. Use the format key1=value1,key2=value2.
+	//       --tags strings             Add tags to the stack. Use the format key1=value1,key2=value2.
+	//   -t, --termination-protection   Enable  termination protection on the stack.
+	//   -y, --yes                      Don't ask questions; just deploy.
 }
diff --git a/test/func_test.go b/test/func_test.go
index 7bc8d4c5..3b374b8f 100644
--- a/test/func_test.go
+++ b/test/func_test.go
@@ -67,6 +67,7 @@ func TestFlow(t *testing.T) {
 	wrap(t, []string{
 		"deploy",
 		"-y",
+		"-t",
 		"--params",
 		"BucketName=foo",
 		"templates/success.template",