[CLI] Add a confirmation prompt when deleting a policy pack (#6034)

Fixes: #4292
This commit is contained in:
Paul Stack 2021-01-04 22:37:46 +00:00 committed by GitHub
parent 7979fe3ff8
commit 21de78291f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 7 deletions

View file

@ -11,6 +11,9 @@ CHANGELOG
- [CLI] Allow `pulumi console` to accept a stack name
[#6031](https://github.com/pulumi/pulumi/pull/6031)
- [CLI] Add a confirmation promt when using `pulumi policy rm`
[#6034](https://github.com/pulumi/pulumi/pull/6034)
- [CLI] Ensure errors with the Pulumi credentials file
give the user some information on how to resolve the problem

View file

@ -15,38 +15,61 @@
package main
import (
"fmt"
"github.com/pulumi/pulumi/pkg/v2/backend"
"github.com/pulumi/pulumi/pkg/v2/backend/display"
"github.com/pulumi/pulumi/sdk/v2/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v2/go/common/util/result"
"github.com/spf13/cobra"
)
const allKeyword = "all"
func newPolicyRmCmd() *cobra.Command {
var yes bool
var cmd = &cobra.Command{
Use: "rm <org-name>/<policy-pack-name> <all|version>",
Args: cmdutil.ExactArgs(2),
Short: "Removes a Policy Pack from a Pulumi organization",
Long: "Removes a Policy Pack from a Pulumi organization. " +
"The Policy Pack must be disabled from all Policy Groups before it can be removed.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, cliArgs []string) error {
Run: cmdutil.RunResultFunc(func(cmd *cobra.Command, args []string) result.Result {
yes = yes || skipConfirmations()
// Obtain current PolicyPack, tied to the Pulumi service backend.
policyPack, err := requirePolicyPack(cliArgs[0])
policyPack, err := requirePolicyPack(args[0])
if err != nil {
return err
return result.FromError(err)
}
var version *string
if cliArgs[1] != allKeyword {
version = &cliArgs[1]
if args[1] != allKeyword {
version = &args[1]
}
opts := display.Options{
Color: cmdutil.GetGlobalColorization(),
}
prompt := fmt.Sprintf("This will permanently remove the '%s' policy!", args[0])
if !yes && !confirmPrompt(prompt, args[0], opts) {
fmt.Println("confirmation declined")
return result.Bail()
}
// Attempt to remove the Policy Pack.
return policyPack.Remove(commandContext(), backend.PolicyPackOperation{
err = policyPack.Remove(commandContext(), backend.PolicyPackOperation{
VersionTag: version, Scopes: cancellationScopes})
if err != nil {
return result.FromError(err)
}
return nil
}),
}
cmd.PersistentFlags().BoolVarP(
&yes, "yes", "y", false,
"Skip confirmation prompts, and proceed with removal anyway")
return cmd
}