Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): add installation delete options/flags to relevant commands (#…
…1189) * feat(*): add installation delete options/flags to relevant commands Signed-off-by: Vaughn Dice <vadice@microsoft.com> * Apply suggestions from code review Co-authored-by: Carolyn Van Slyck <me@carolynvanslyck.com> * incorporate add'l review feedback Signed-off-by: Vaughn Dice <vadice@microsoft.com> * add'l review feedback Signed-off-by: Vaughn Dice <vadice@microsoft.com> * refactor the parent/dep uninstall scenarios Signed-off-by: Vaughn Dice <vadice@microsoft.com> * update dependencies_test.go Signed-off-by: Vaughn Dice <vadice@microsoft.com> * use BundleAction interface to enable action-specific options when executing dependencies Signed-off-by: Vaughn Dice <vadice@microsoft.com> * ref(tests/dependencies_test.go): set mysql dep namespace param as well Signed-off-by: Vaughn Dice <vadice@microsoft.com> * fix cmd example spacing Signed-off-by: Vaughn Dice <vadice@microsoft.com> * add comments around uninstallOpts when empty Signed-off-by: Vaughn Dice <vadice@microsoft.com> * remove ToActionArgs from BundleAction interface Signed-off-by: Vaughn Dice <vadice@microsoft.com> Co-authored-by: Carolyn Van Slyck <me@carolynvanslyck.com>
- Loading branch information
Showing
22 changed files
with
633 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: "porter installations delete" | ||
slug: porter_installations_delete | ||
url: /cli/porter_installations_delete/ | ||
--- | ||
## porter installations delete | ||
|
||
Delete an installation | ||
|
||
### Synopsis | ||
|
||
Deletes all records and outputs associated with an installation | ||
|
||
``` | ||
porter installations delete [INSTALLATION] [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
porter installation delete | ||
porter installation delete wordpress | ||
porter installation delete --force | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
--force Force a delete the installation, regardless of last completed action | ||
-h, --help help for delete | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
--debug Enable debug logging | ||
--debug-plugins Enable plugin debug logging | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [porter installations](/cli/porter_installations/) - Installation commands | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package porter | ||
|
||
import ( | ||
"fmt" | ||
|
||
"get.porter.sh/porter/pkg/context" | ||
claims "github.com/cnabio/cnab-go/claim" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const installationDeleteTmpl = "deleting installation records for %s...\n" | ||
|
||
var ( | ||
// ErrUnsafeInstallationDelete warns the user that deletion of an unsuccessfully uninstalled installation is unsafe | ||
ErrUnsafeInstallationDelete = errors.New("it is unsafe to delete an installation when the last action wasn't a successful uninstall") | ||
|
||
// ErrUnsafeInstallationDeleteRetryForce presents the ErrUnsafeInstallationDelete error and provides a retry option of --force | ||
ErrUnsafeInstallationDeleteRetryForce = fmt.Errorf("%s; if you are sure it should be deleted, retry the last command with the --force flag", ErrUnsafeInstallationDelete) | ||
) | ||
|
||
// DeleteOptions represent options for Porter's installation delete command | ||
type DeleteOptions struct { | ||
sharedOptions | ||
Force bool | ||
} | ||
|
||
// Validate prepares for an installation delete action and validates the args/options. | ||
func (o *DeleteOptions) Validate(args []string, cxt *context.Context) error { | ||
// Ensure only one argument exists (installation name) if args length non-zero | ||
err := o.sharedOptions.validateInstallationName(args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return o.sharedOptions.defaultBundleFiles(cxt) | ||
} | ||
|
||
// DeleteInstallation handles deletion of an installation | ||
func (p *Porter) DeleteInstallation(opts DeleteOptions) error { | ||
err := p.applyDefaultOptions(&opts.sharedOptions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
installation, err := p.Claims.ReadInstallationStatus(opts.Name) | ||
if err != nil { | ||
return errors.Wrapf(err, "unable to read status for installation %s", opts.Name) | ||
} | ||
|
||
claim, err := installation.GetLastClaim() | ||
if err != nil { | ||
return errors.Wrapf(err, "unable to read most recent record for installation %s", opts.Name) | ||
} | ||
|
||
if (claim.Action != claims.ActionUninstall || installation.GetLastStatus() != claims.StatusSucceeded) && !opts.Force { | ||
return ErrUnsafeInstallationDeleteRetryForce | ||
} | ||
|
||
fmt.Fprintf(p.Out, installationDeleteTmpl, opts.Name) | ||
return p.Claims.DeleteInstallation(opts.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package porter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cnabio/cnab-go/bundle" | ||
"github.com/cnabio/cnab-go/claim" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDeleteInstallation(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
lastAction string | ||
lastActionStatus string | ||
force bool | ||
installationRemains bool | ||
wantError string | ||
}{ | ||
{ | ||
name: "not yet installed", | ||
wantError: "unable to read status for installation test: Installation does not exist", | ||
}, { | ||
name: "last action not uninstall; no --force", | ||
lastAction: "install", | ||
lastActionStatus: claim.StatusSucceeded, | ||
installationRemains: true, | ||
wantError: ErrUnsafeInstallationDeleteRetryForce.Error(), | ||
}, { | ||
name: "last action failed uninstall; no --force", | ||
lastAction: "uninstall", | ||
lastActionStatus: claim.StatusFailed, | ||
installationRemains: true, | ||
wantError: ErrUnsafeInstallationDeleteRetryForce.Error(), | ||
}, { | ||
name: "last action not uninstall; --force", | ||
lastAction: "install", | ||
lastActionStatus: claim.StatusSucceeded, | ||
force: true, | ||
}, { | ||
name: "last action failed uninstall; --force", | ||
lastAction: "uninstall", | ||
lastActionStatus: claim.StatusFailed, | ||
force: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
p := NewTestPorter(t) | ||
p.TestConfig.SetupPorterHome() | ||
|
||
var err error | ||
|
||
// Create test claim | ||
if tc.lastAction != "" { | ||
c := p.TestClaims.CreateClaim("test", tc.lastAction, bundle.Bundle{}, nil) | ||
_ = p.TestClaims.CreateResult(c, tc.lastActionStatus) | ||
} | ||
|
||
opts := DeleteOptions{} | ||
opts.Name = "test" | ||
opts.Force = tc.force | ||
|
||
err = p.DeleteInstallation(opts) | ||
if tc.wantError != "" { | ||
require.EqualError(t, err, tc.wantError) | ||
} else { | ||
require.NoError(t, err, "expected DeleteInstallation to succeed") | ||
} | ||
|
||
_, err = p.Claims.ReadInstallation("test") | ||
if tc.installationRemains { | ||
require.NoError(t, err, "expected installation to exist") | ||
} else { | ||
require.EqualError(t, err, "Installation does not exist") | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.