Skip to content

Commit

Permalink
feat: added name flag to connector delete
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdelahunt authored and wtrocki committed Jul 18, 2022
1 parent ee9fed7 commit 620a909
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/commands/rhoas_connector.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions docs/commands/rhoas_connector_delete.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions docs/commands/rhoas_connector_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/cmd/connector/connectorcmdutil/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
80 changes: 69 additions & 11 deletions pkg/cmd/connector/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,21 +36,25 @@ 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
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/core/localize/locales/en/cmd/connectors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 620a909

Please sign in to comment.