From c7658c86fa9f6448a118b9eef1978ab41f487504 Mon Sep 17 00:00:00 2001 From: joshuabezaleel Date: Sat, 18 Jun 2022 14:51:59 +0700 Subject: [PATCH] Add state and status to list installation --- pkg/porter/list.go | 81 +++++++++++++++---- pkg/porter/testdata/show/expected-output.json | 4 +- pkg/porter/testdata/show/expected-output.yaml | 2 + pkg/storage/installation.go | 5 ++ 4 files changed, 77 insertions(+), 15 deletions(-) diff --git a/pkg/porter/list.go b/pkg/porter/list.go index 84b7465433..bf083ad981 100644 --- a/pkg/porter/list.go +++ b/pkg/porter/list.go @@ -17,6 +17,16 @@ import ( "github.com/pkg/errors" ) +const ( + StateInstalled = "installed" + StateUninstalled = "uninstalled" + StateDefined = "defined" + + StatusInstalling = "installing" + StatusUninstalling = "uninstalling" + StatusUpgrading = "upgrading" +) + // ListOptions represent generic options for use by Porter's list commands type ListOptions struct { printer.PrintOptions @@ -102,6 +112,9 @@ type DisplayInstallation struct { // Status of the installation. Status storage.InstallationStatus `json:"status,omitempty" yaml:"status,omitempty" toml:"status,omitempty"` DisplayInstallationMetadata `json:"_calculated" yaml:"_calculated"` + + DisplayInstallationState string `json:"displayInstallationState,omitempty" yaml:"displayInstallationState,omitempty" toml:"displayInstallationState,omitempty"` + DisplayInstallationStatus string `json:"displayInstallationStatus,omitempty" yaml:"displayInstallationStatus,omitempty" toml:"displayInstallationStatus,omitempty"` } type DisplayInstallationMetadata struct { @@ -111,18 +124,20 @@ type DisplayInstallationMetadata struct { func NewDisplayInstallation(installation storage.Installation) DisplayInstallation { di := DisplayInstallation{ - SchemaType: "Installation", - SchemaVersion: installation.SchemaVersion, - ID: installation.ID, - Name: installation.Name, - Namespace: installation.Namespace, - Uninstalled: installation.Uninstalled, - Bundle: installation.Bundle, - Custom: installation.Custom, - Labels: installation.Labels, - CredentialSets: installation.CredentialSets, - ParameterSets: installation.ParameterSets, - Status: installation.Status, + SchemaType: "Installation", + SchemaVersion: installation.SchemaVersion, + ID: installation.ID, + Name: installation.Name, + Namespace: installation.Namespace, + Uninstalled: installation.Uninstalled, + Bundle: installation.Bundle, + Custom: installation.Custom, + Labels: installation.Labels, + CredentialSets: installation.CredentialSets, + ParameterSets: installation.ParameterSets, + Status: installation.Status, + DisplayInstallationState: setDisplayInstallationState(installation), + DisplayInstallationStatus: setDisplayInstallationStatus(installation), } return di @@ -266,11 +281,49 @@ func (p *Porter) PrintInstallations(ctx context.Context, opts ListOptions) error if !ok { return nil } - return []string{cl.Namespace, cl.Name, tp.Format(cl.Status.Created), tp.Format(cl.Status.Modified), cl.Status.Action, cl.Status.ResultStatus} + return []string{cl.Namespace, cl.Name, cl.Status.BundleVersion, cl.DisplayInstallationState, cl.DisplayInstallationStatus, tp.Format(cl.Status.Modified)} } return printer.PrintTable(p.Out, displayInstallations, row, - "NAMESPACE", "NAME", "CREATED", "MODIFIED", "LAST ACTION", "LAST STATUS") + "NAMESPACE", "NAME", "VERSION", "STATE", "STATUS", "MODIFIED") default: return fmt.Errorf("invalid format: %s", opts.Format) } } + +func setDisplayInstallationState(installation storage.Installation) string { + var state string + + if installation.IsInstalled() { + state = StateInstalled + } else if installation.IsUninstalled() { + state = StateUninstalled + } else if installation.IsDefined() { + state = StateDefined + } + + return state +} + +func setDisplayInstallationStatus(installation storage.Installation) string { + var status string + + switch installation.Status.ResultStatus { + case cnab.StatusSucceeded: + status = cnab.StatusSucceeded + case cnab.StatusFailed: + status = cnab.StatusFailed + case cnab.StatusRunning: + switch installation.Status.Action { + case cnab.ActionInstall: + status = StatusInstalling + case cnab.ActionUninstall: + status = StatusUninstalling + case cnab.ActionUpgrade: + status = StatusUpgrading + default: + status = fmt.Sprintf("running %s", installation.Status.Action) + } + } + + return status +} diff --git a/pkg/porter/testdata/show/expected-output.json b/pkg/porter/testdata/show/expected-output.json index 74e15467aa..8d1ea4d74c 100644 --- a/pkg/porter/testdata/show/expected-output.json +++ b/pkg/porter/testdata/show/expected-output.json @@ -53,5 +53,7 @@ "value": "top-secret" } ] - } + }, + "displayInstallationState": "installed", + "displayInstallationStatus": "succeeded" } diff --git a/pkg/porter/testdata/show/expected-output.yaml b/pkg/porter/testdata/show/expected-output.yaml index 1586f69032..822f1b3ec3 100644 --- a/pkg/porter/testdata/show/expected-output.yaml +++ b/pkg/porter/testdata/show/expected-output.yaml @@ -40,3 +40,5 @@ _calculated: type: unknown sensitive: false value: top-secret +displayInstallationState: installed +displayInstallationStatus: succeeded diff --git a/pkg/storage/installation.go b/pkg/storage/installation.go index 2997e68b0b..57495c9241 100644 --- a/pkg/storage/installation.go +++ b/pkg/storage/installation.go @@ -222,6 +222,11 @@ func (i Installation) IsUninstalled() bool { return i.Status.Uninstalled != nil } +// IsDefined checks if the installation is has already been defined but not installed yet. +func (i Installation) IsDefined() bool { + return i.Status.Installed == nil +} + // OCIReferenceParts is our storage representation of cnab.OCIReference // with the parts explicitly stored separately so that they are queryable. type OCIReferenceParts struct {