From 232d798189c2e2f675ff9413498f0df4dca96f29 Mon Sep 17 00:00:00 2001 From: Erin Krengel Date: Mon, 27 Jan 2020 10:35:34 -0800 Subject: [PATCH] Add remove all to policy (#3792) --- CHANGELOG.md | 4 ++++ cmd/policy_enable.go | 23 +++++++---------------- cmd/policy_rm.go | 17 ++++++++++++----- pkg/backend/httpstate/client/client.go | 23 ++++++++++++++++++++--- pkg/backend/httpstate/policypack.go | 4 ++-- tests/integration/policy/policy_test.go | 6 +++++- 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5e18c9b1..d07146a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ CHANGELOG optional inputs than manually converting to pointer types. [#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 / latest` instead of a `--latest` flag. + ## 1.9.0 (2020-01-22) - Publish python types for PEP 561 [#3704](https://github.com/pulumi/pulumi/pull/3704) diff --git a/cmd/policy_enable.go b/cmd/policy_enable.go index bd9f60c7a..e0a455f7a 100644 --- a/cmd/policy_enable.go +++ b/cmd/policy_enable.go @@ -23,19 +23,21 @@ import ( "github.com/spf13/cobra" ) +const latestKeyword = "latest" + type policyEnableArgs struct { policyGroup string - latest bool } func newPolicyEnableCmd() *cobra.Command { args := policyEnableArgs{} var cmd = &cobra.Command{ - Use: "enable / [version]", - Args: cmdutil.RangeArgs(1, 2), + Use: "enable / ", + Args: cmdutil.ExactArgs(2), 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 { // Obtain current PolicyPack, tied to the Pulumi service backend. policyPack, err := requirePolicyPack(cliArgs[0]) @@ -43,17 +45,9 @@ func newPolicyEnableCmd() *cobra.Command { 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. var version *int - if len(cliArgs) > 1 { + if cliArgs[1] != latestKeyword { v, err := strconv.Atoi(cliArgs[1]) if err != nil { return errors.Wrapf(err, "Could not parse version (should be an integer)") @@ -71,8 +65,5 @@ func newPolicyEnableCmd() *cobra.Command { &args.policyGroup, "policy-group", "", "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 } diff --git a/cmd/policy_rm.go b/cmd/policy_rm.go index 60e2f886e..4e0641f80 100644 --- a/cmd/policy_rm.go +++ b/cmd/policy_rm.go @@ -23,9 +23,12 @@ import ( "github.com/spf13/cobra" ) +const allKeyword = "all" + func newPolicyRmCmd() *cobra.Command { + var cmd = &cobra.Command{ - Use: "rm / ", + Use: "rm / ", Args: cmdutil.ExactArgs(2), Short: "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 } - version, err := strconv.Atoi(cliArgs[1]) - if err != nil { - return errors.Wrapf(err, "Could not parse version (should be an integer)") + var version *int + if cliArgs[1] != allKeyword { + 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. return policyPack.Remove(commandContext(), backend.PolicyPackOperation{ - Version: &version, Scopes: cancellationScopes}) + Version: version, Scopes: cancellationScopes}) }), } diff --git a/pkg/backend/httpstate/client/client.go b/pkg/backend/httpstate/client/client.go index 7200a86d2..e8849ec6e 100644 --- a/pkg/backend/httpstate/client/client.go +++ b/pkg/backend/httpstate/client/client.go @@ -122,8 +122,14 @@ func updatePolicyGroupPath(orgName, policyGroup string) string { "/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 -// 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 { return fmt.Sprintf( "/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 } -// RemovePolicyPack removes a `PolicyPack` from the Pulumi organization. -func (pc *Client) RemovePolicyPack(ctx context.Context, orgName string, +// RemovePolicyPack removes all versions of a `PolicyPack` from the Pulumi organization. +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 { path := deletePolicyPackVersionPath(orgName, policyPackName, version) diff --git a/pkg/backend/httpstate/policypack.go b/pkg/backend/httpstate/policypack.go index 9a67f0cd4..35d22617f 100644 --- a/pkg/backend/httpstate/policypack.go +++ b/pkg/backend/httpstate/policypack.go @@ -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 { 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" diff --git a/tests/integration/policy/policy_test.go b/tests/integration/policy/policy_test.go index 0f913a55d..f3e1d7f8c 100644 --- a/tests/integration/policy/policy_test.go +++ b/tests/integration/policy/policy_test.go @@ -35,6 +35,9 @@ func TestPolicy(t *testing.T) { e.ImportDirectory("test_policy_pack") e.RunCommand("yarn", "install") os.Setenv("TEST_POLICY_PACK", policyPackName) + + // Publish the Policy Pack twice. + e.RunCommand("pulumi", "policy", "publish", orgName) e.RunCommand("pulumi", "policy", "publish", orgName) // 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") // 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", "rm", fmt.Sprintf("%s/%s", orgName, policyPackName), "1") + e.RunCommand("pulumi", "policy", "rm", fmt.Sprintf("%s/%s", orgName, policyPackName), "all") } type policyPacksJSON struct {