From 620a909fc71e02f4435cbd57a28b765bdcfc7624 Mon Sep 17 00:00:00 2001 From: Jack Delahunt Date: Mon, 18 Jul 2022 12:53:03 +0100 Subject: [PATCH] feat: added name flag to connector delete --- docs/commands/rhoas_connector.md | 2 + docs/commands/rhoas_connector_delete.md | 40 ++++++++++ docs/commands/rhoas_connector_list.md | 40 ++++++++++ pkg/cmd/connector/connectorcmdutil/flagset.go | 15 ++++ pkg/cmd/connector/delete/delete.go | 80 ++++++++++++++++--- pkg/cmd/connector/list/list.go | 2 +- .../localize/locales/en/cmd/connectors.toml | 3 + 7 files changed, 170 insertions(+), 12 deletions(-) create mode 100644 docs/commands/rhoas_connector_delete.md create mode 100644 docs/commands/rhoas_connector_list.md diff --git a/docs/commands/rhoas_connector.md b/docs/commands/rhoas_connector.md index 9b57c5ad1..c99cf8c86 100644 --- a/docs/commands/rhoas_connector.md +++ b/docs/commands/rhoas_connector.md @@ -51,6 +51,8 @@ rhoas connector stop * [rhoas](rhoas.md) - RHOAS CLI * [rhoas connector cluster](rhoas_connector_cluster.md) - Create, delete, and list Connectors clusters +* [rhoas connector delete](rhoas_connector_delete.md) - Delete a Connectors instance +* [rhoas connector list](rhoas_connector_list.md) - List of Connectors instances * [rhoas connector namespace](rhoas_connector_namespace.md) - Connectors namespace commands * [rhoas connector start](rhoas_connector_start.md) - Start a Connectors instance * [rhoas connector stop](rhoas_connector_stop.md) - Stop a Connectors instance diff --git a/docs/commands/rhoas_connector_delete.md b/docs/commands/rhoas_connector_delete.md new file mode 100644 index 000000000..68a535e49 --- /dev/null +++ b/docs/commands/rhoas_connector_delete.md @@ -0,0 +1,40 @@ +## rhoas connector delete + +Delete a Connectors instance + +### Synopsis + +Delete a Connectors instance by specifing its ID. Use the "connector list" command to see a list of all Connectors instances and their ID values. + +``` +rhoas connector delete [flags] +``` + +### Examples + +``` +# Delete a Connectors instance with ID c9b71ucotd37bufoamkg +rhoas connector delete --id=c9b71ucotd37bufoamkg + +``` + +### Options + +``` + --id string The ID for the Connectors instance + --name string The name for the Connectors instance + -o, --output string Specify the output format. Choose from: "json", "yaml", "yml" + -y, --yes Skip confirmation of this action +``` + +### Options inherited from parent commands + +``` + -h, --help Show help for a command + -v, --verbose Enable verbose mode +``` + +### SEE ALSO + +* [rhoas connector](rhoas_connector.md) - Connectors instance commands + diff --git a/docs/commands/rhoas_connector_list.md b/docs/commands/rhoas_connector_list.md new file mode 100644 index 000000000..1540a8df5 --- /dev/null +++ b/docs/commands/rhoas_connector_list.md @@ -0,0 +1,40 @@ +## rhoas connector list + +List of Connectors instances + +### Synopsis + +List of Connectors instances. The returned list includes the ID value for each Connectors instance. + + +``` +rhoas connector list [flags] +``` + +### Examples + +``` +# List of Connectors instance +rhoas connector list + +``` + +### Options + +``` + --limit int Page limit (default 10) + -o, --output string Specify the output format. Choose from: "json", "yaml", "yml" + --page int Page number (default 1) +``` + +### Options inherited from parent commands + +``` + -h, --help Show help for a command + -v, --verbose Enable verbose mode +``` + +### SEE ALSO + +* [rhoas connector](rhoas_connector.md) - Connectors instance commands + diff --git a/pkg/cmd/connector/connectorcmdutil/flagset.go b/pkg/cmd/connector/connectorcmdutil/flagset.go index 36c85e680..6d1e3df00 100644 --- a/pkg/cmd/connector/connectorcmdutil/flagset.go +++ b/pkg/cmd/connector/connectorcmdutil/flagset.go @@ -35,3 +35,18 @@ func (fs *FlagSet) AddConnectorID(ruleType *string) *flagutil.FlagOptions { return flagutil.WithFlagOptions(fs.cmd, flagName) } + +// AddConnectorName adds a flag for specifying the connector name +func (fs *FlagSet) AddConnectorName(ruleType *string) *flagutil.FlagOptions { + flagName := "name" + + fs.StringVar( + ruleType, + flagName, + "", + fs.factory.Localizer.MustLocalize("connector.common.flag.name.description"), + ) + + return flagutil.WithFlagOptions(fs.cmd, flagName) + +} diff --git a/pkg/cmd/connector/delete/delete.go b/pkg/cmd/connector/delete/delete.go index 531db3d08..3e0525105 100644 --- a/pkg/cmd/connector/delete/delete.go +++ b/pkg/cmd/connector/delete/delete.go @@ -8,13 +8,17 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" + "github.com/redhat-developer/app-services-cli/pkg/shared/connectorutil" + "github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client" "github.com/spf13/cobra" ) type options struct { id string + name string outputFormat string f *factory.Factory @@ -32,7 +36,6 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { Short: f.Localizer.MustLocalize("connector.delete.cmd.shortDescription"), Long: f.Localizer.MustLocalize("connector.delete.cmd.longDescription"), Example: f.Localizer.MustLocalize("connector.delete.cmd.example"), - Hidden: true, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { validOutputFormats := flagutil.ValidOutputFormats @@ -40,13 +43,18 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...) } + if opts.name != "" && opts.id != "" { + return opts.f.Localizer.MustLocalizeError("service.error.idAndNameCannotBeUsed") + } + return runDelete(opts) }, } flags := connectorcmdutil.NewFlagSet(cmd, f) flags.AddOutput(&opts.outputFormat) - _ = flags.AddConnectorID(&opts.id).Required() + _ = flags.AddConnectorID(&opts.id) + _ = flags.AddConnectorName(&opts.name) flags.AddYes(&opts.skipConfirm) return cmd @@ -55,6 +63,40 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { func runDelete(opts *options) error { f := opts.f + var conn connection.Connection + conn, err := f.Connection() + if err != nil { + return err + } + + api := conn.API() + connectorMgmt := api.ConnectorsMgmt() + + var connector *connectormgmtclient.Connector + + if opts.id != "" { + connector, err = connectorutil.GetConnectorByID(&connectorMgmt, opts.id, f) + if err != nil { + return err + } + } + + if opts.name != "" { + connector, err = connectorutil.GetConnectorByName(&connectorMgmt, opts.name, f) + if err != nil { + return err + } + } + + if connector == nil { + connector, err = contextutil.GetCurrentConnectorInstance(&conn, f) + if err != nil { + return err + } + } + + opts.id = connector.GetId() + if !opts.skipConfirm { confirm, promptErr := promptConfirmDelete(opts) if promptErr != nil { @@ -66,15 +108,7 @@ func runDelete(opts *options) error { } } - var conn connection.Connection - conn, err := f.Connection() - if err != nil { - return err - } - - api := conn.API() - - a := api.ConnectorsMgmt().ConnectorsApi.DeleteConnector(f.Context, opts.id) + a := connectorMgmt.ConnectorsApi.DeleteConnector(f.Context, opts.id) _, httpRes, err := a.Execute() if httpRes != nil { @@ -87,6 +121,30 @@ func runDelete(opts *options) error { f.Logger.Info(icon.SuccessPrefix(), f.Localizer.MustLocalize("connector.delete.info.success")) + svcContext, err := f.ServiceContext.Load() + if err != nil { + return err + } + + currCtx, err := contextutil.GetCurrentContext(svcContext, f.Localizer) + if err != nil { + return err + } + + // this is not the current instance, our work here is done + if currCtx.ConnectorID != connector.GetId() { + return nil + } + + // the connector that was deleted is set as the user's current cluster + // since it was deleted it should be removed from the context + currCtx.ConnectorID = "" + svcContext.Contexts[svcContext.CurrentContext] = *currCtx + + if err := opts.f.ServiceContext.Save(svcContext); err != nil { + return err + } + return nil } diff --git a/pkg/cmd/connector/list/list.go b/pkg/cmd/connector/list/list.go index 3afcec93f..397a897fa 100644 --- a/pkg/cmd/connector/list/list.go +++ b/pkg/cmd/connector/list/list.go @@ -86,7 +86,7 @@ func runList(opts *options) error { return nil } - if response.Size == 0 && opts.outputFormat == "" { + if response.Size == 0 && opts.outputFormat == "" { f.Logger.Info(f.Localizer.MustLocalize("connector.common.log.info.noResults")) return nil } diff --git a/pkg/core/localize/locales/en/cmd/connectors.toml b/pkg/core/localize/locales/en/cmd/connectors.toml index bc1171340..3f7bf6fb6 100644 --- a/pkg/core/localize/locales/en/cmd/connectors.toml +++ b/pkg/core/localize/locales/en/cmd/connectors.toml @@ -239,6 +239,9 @@ rhoas connector describe --id=c980124otd37bufiemj0 [connector.common.flag.id.description] one = 'The ID for the Connectors instance' +[connector.common.flag.name.description] +one = 'The name for the Connectors instance' + [connector.describe.info.success] one = 'The Connectors instance details were returned successfully'