Add remove all to policy (#3792)

This commit is contained in:
Erin Krengel 2020-01-27 10:35:34 -08:00 committed by GitHub
parent a5fea292a3
commit 232d798189
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 27 deletions

View file

@ -19,6 +19,10 @@ CHANGELOG
optional inputs than manually converting to pointer types. optional inputs than manually converting to pointer types.
[#3806](https://github.com/pulumi/pulumi/pull/3806) [#3806](https://github.com/pulumi/pulumi/pull/3806)
- Add ability to specify all versions when removing a Policy Pack.
- Breaking change to Policy command: Change enable command to use `pulumi policy enable <org-name>/<policy-pack-name> latest` instead of a `--latest` flag.
## 1.9.0 (2020-01-22) ## 1.9.0 (2020-01-22)
- Publish python types for PEP 561 - Publish python types for PEP 561
[#3704](https://github.com/pulumi/pulumi/pull/3704) [#3704](https://github.com/pulumi/pulumi/pull/3704)

View file

@ -23,19 +23,21 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const latestKeyword = "latest"
type policyEnableArgs struct { type policyEnableArgs struct {
policyGroup string policyGroup string
latest bool
} }
func newPolicyEnableCmd() *cobra.Command { func newPolicyEnableCmd() *cobra.Command {
args := policyEnableArgs{} args := policyEnableArgs{}
var cmd = &cobra.Command{ var cmd = &cobra.Command{
Use: "enable <org-name>/<policy-pack-name> [version]", Use: "enable <org-name>/<policy-pack-name> <latest|version>",
Args: cmdutil.RangeArgs(1, 2), Args: cmdutil.ExactArgs(2),
Short: "Enable a Policy Pack for a Pulumi organization", Short: "Enable a Policy Pack for a Pulumi organization",
Long: "Enable a Policy Pack for a Pulumi organization. Version or latest flag must be specified.", Long: "Enable a Policy Pack for a Pulumi organization. " +
"Can specify latest to enable the latest version of the Policy Pack or a specific version number.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, cliArgs []string) error { Run: cmdutil.RunFunc(func(cmd *cobra.Command, cliArgs []string) error {
// Obtain current PolicyPack, tied to the Pulumi service backend. // Obtain current PolicyPack, tied to the Pulumi service backend.
policyPack, err := requirePolicyPack(cliArgs[0]) policyPack, err := requirePolicyPack(cliArgs[0])
@ -43,17 +45,9 @@ func newPolicyEnableCmd() *cobra.Command {
return err return err
} }
// Make sure that a version or latest is specified. Having both or neither
// specified would make this an ambiguous request.
if len(cliArgs) < 2 && !args.latest {
return errors.New("must specify a version or the --latest flag")
} else if len(cliArgs) == 2 && args.latest {
return errors.New("cannot specify both a version and the --latest flag")
}
// Parse version if it's specified. // Parse version if it's specified.
var version *int var version *int
if len(cliArgs) > 1 { if cliArgs[1] != latestKeyword {
v, err := strconv.Atoi(cliArgs[1]) v, err := strconv.Atoi(cliArgs[1])
if err != nil { if err != nil {
return errors.Wrapf(err, "Could not parse version (should be an integer)") return errors.Wrapf(err, "Could not parse version (should be an integer)")
@ -71,8 +65,5 @@ func newPolicyEnableCmd() *cobra.Command {
&args.policyGroup, "policy-group", "", &args.policyGroup, "policy-group", "",
"The Policy Group for which the Policy Pack will be enabled; if not specified, the default Policy Group is used") "The Policy Group for which the Policy Pack will be enabled; if not specified, the default Policy Group is used")
cmd.PersistentFlags().BoolVarP(
&args.latest, "latest", "l", false, "Enable the latest version of the Policy Pack")
return cmd return cmd
} }

View file

@ -23,9 +23,12 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const allKeyword = "all"
func newPolicyRmCmd() *cobra.Command { func newPolicyRmCmd() *cobra.Command {
var cmd = &cobra.Command{ var cmd = &cobra.Command{
Use: "rm <org-name>/<policy-pack-name> <version>", Use: "rm <org-name>/<policy-pack-name> <all|version>",
Args: cmdutil.ExactArgs(2), Args: cmdutil.ExactArgs(2),
Short: "Removes a Policy Pack from a Pulumi organization", Short: "Removes a Policy Pack from a Pulumi organization",
Long: "Removes a Policy Pack from a Pulumi organization. " + Long: "Removes a Policy Pack from a Pulumi organization. " +
@ -37,14 +40,18 @@ func newPolicyRmCmd() *cobra.Command {
return err return err
} }
version, err := strconv.Atoi(cliArgs[1]) var version *int
if err != nil { if cliArgs[1] != allKeyword {
return errors.Wrapf(err, "Could not parse version (should be an integer)") v, err := strconv.Atoi(cliArgs[1])
if err != nil {
return errors.Wrapf(err, "Could not parse version (should be an integer)")
}
version = &v
} }
// Attempt to remove the Policy Pack. // Attempt to remove the Policy Pack.
return policyPack.Remove(commandContext(), backend.PolicyPackOperation{ return policyPack.Remove(commandContext(), backend.PolicyPackOperation{
Version: &version, Scopes: cancellationScopes}) Version: version, Scopes: cancellationScopes})
}), }),
} }

View file

@ -122,8 +122,14 @@ func updatePolicyGroupPath(orgName, policyGroup string) string {
"/api/orgs/%s/policygroups/%s", orgName, policyGroup) "/api/orgs/%s/policygroups/%s", orgName, policyGroup)
} }
// deletePolicyPackPath returns the path for an API call to the Pulumi service to delete
// all versions of a Policy Pack from a Pulumi organization.
func deletePolicyPackPath(orgName, policyPackName string) string {
return fmt.Sprintf("/api/orgs/%s/policypacks/%s", orgName, policyPackName)
}
// deletePolicyPackVersionPath returns the path for an API call to the Pulumi service to delete // deletePolicyPackVersionPath returns the path for an API call to the Pulumi service to delete
// a Policy Pack from a Pulumi organization. // a version of a Policy Pack from a Pulumi organization.
func deletePolicyPackVersionPath(orgName, policyPackName string, version int) string { func deletePolicyPackVersionPath(orgName, policyPackName string, version int) string {
return fmt.Sprintf( return fmt.Sprintf(
"/api/orgs/%s/policypacks/%s/versions/%d", orgName, policyPackName, version) "/api/orgs/%s/policypacks/%s/versions/%d", orgName, policyPackName, version)
@ -625,8 +631,19 @@ func (pc *Client) DisablePolicyPack(ctx context.Context, orgName string, policyG
return nil return nil
} }
// RemovePolicyPack removes a `PolicyPack` from the Pulumi organization. // RemovePolicyPack removes all versions of a `PolicyPack` from the Pulumi organization.
func (pc *Client) RemovePolicyPack(ctx context.Context, orgName string, func (pc *Client) RemovePolicyPack(ctx context.Context, orgName string, policyPackName string) error {
path := deletePolicyPackPath(orgName, policyPackName)
err := pc.restCall(ctx, http.MethodDelete, path, nil, nil, nil)
if err != nil {
return errors.Wrapf(err, "Request to remove policy pack failed")
}
return nil
}
// RemovePolicyPackByVersion removes a specific version of a `PolicyPack` from
// the Pulumi organization.
func (pc *Client) RemovePolicyPackByVersion(ctx context.Context, orgName string,
policyPackName string, version int) error { policyPackName string, version int) error {
path := deletePolicyPackVersionPath(orgName, policyPackName, version) path := deletePolicyPackVersionPath(orgName, policyPackName, version)

View file

@ -184,9 +184,9 @@ func (pack *cloudPolicyPack) Disable(ctx context.Context, policyGroup string, op
func (pack *cloudPolicyPack) Remove(ctx context.Context, op backend.PolicyPackOperation) error { func (pack *cloudPolicyPack) Remove(ctx context.Context, op backend.PolicyPackOperation) error {
if op.Version == nil { if op.Version == nil {
return errors.New("remove requires the version be specified") return pack.cl.RemovePolicyPack(ctx, pack.ref.orgName, string(pack.ref.name))
} }
return pack.cl.RemovePolicyPack(ctx, pack.ref.orgName, string(pack.ref.name), *op.Version) return pack.cl.RemovePolicyPackByVersion(ctx, pack.ref.orgName, string(pack.ref.name), *op.Version)
} }
const npmPackageDir = "package" const npmPackageDir = "package"

View file

@ -35,6 +35,9 @@ func TestPolicy(t *testing.T) {
e.ImportDirectory("test_policy_pack") e.ImportDirectory("test_policy_pack")
e.RunCommand("yarn", "install") e.RunCommand("yarn", "install")
os.Setenv("TEST_POLICY_PACK", policyPackName) os.Setenv("TEST_POLICY_PACK", policyPackName)
// Publish the Policy Pack twice.
e.RunCommand("pulumi", "policy", "publish", orgName)
e.RunCommand("pulumi", "policy", "publish", orgName) e.RunCommand("pulumi", "policy", "publish", orgName)
// Check the policy ls commands. // Check the policy ls commands.
@ -51,10 +54,11 @@ func TestPolicy(t *testing.T) {
e.RunCommand("pulumi", "policy", "disable", fmt.Sprintf("%s/%s", orgName, policyPackName), "--version=1") e.RunCommand("pulumi", "policy", "disable", fmt.Sprintf("%s/%s", orgName, policyPackName), "--version=1")
// Enable and Disable without specifying the version number. // Enable and Disable without specifying the version number.
e.RunCommand("pulumi", "policy", "enable", fmt.Sprintf("%s/%s", orgName, policyPackName), "--latest=true") e.RunCommand("pulumi", "policy", "enable", fmt.Sprintf("%s/%s", orgName, policyPackName), "latest")
e.RunCommand("pulumi", "policy", "disable", fmt.Sprintf("%s/%s", orgName, policyPackName)) e.RunCommand("pulumi", "policy", "disable", fmt.Sprintf("%s/%s", orgName, policyPackName))
e.RunCommand("pulumi", "policy", "rm", fmt.Sprintf("%s/%s", orgName, policyPackName), "1") e.RunCommand("pulumi", "policy", "rm", fmt.Sprintf("%s/%s", orgName, policyPackName), "1")
e.RunCommand("pulumi", "policy", "rm", fmt.Sprintf("%s/%s", orgName, policyPackName), "all")
} }
type policyPacksJSON struct { type policyPacksJSON struct {