Skip to content

Commit 471fa00

Browse files
committed
feat: added connector start and stop commands
1 parent 3111dba commit 471fa00

File tree

8 files changed

+301
-1
lines changed

8 files changed

+301
-1
lines changed

docs/commands/rhoas_connector.md

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_connector_start.md

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_connector_stop.md

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/connector/connector.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/describe"
99
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/list"
1010
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/namespace"
11+
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/start"
12+
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/stop"
1113
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/update"
1214
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/use"
1315

@@ -32,6 +34,8 @@ func NewConnectorsCommand(f *factory.Factory) *cobra.Command {
3234
cluster.NewConnectorClusterCommand(f),
3335
namespace.NewNameSpaceCommand(f),
3436
use.NewUseCommand(f),
37+
start.NewStartCommand(f),
38+
stop.NewStopCommand(f),
3539
// Hidden for the users and docs at the moment
3640
create.NewCreateCommand(f),
3741
delete.NewDeleteCommand(f),

pkg/cmd/connector/start/start.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package start
2+
3+
import (
4+
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil"
5+
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
6+
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
7+
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
8+
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
9+
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
type options struct {
14+
outputFormat string
15+
f *factory.Factory
16+
connectorID string
17+
}
18+
19+
// NewStartCommand creates a new command to start a connector
20+
func NewStartCommand(f *factory.Factory) *cobra.Command {
21+
22+
opts := &options{
23+
f: f,
24+
}
25+
26+
cmd := &cobra.Command{
27+
Use: "start",
28+
Short: f.Localizer.MustLocalize("connector.start.cmd.shortDescription"),
29+
Long: f.Localizer.MustLocalize("connector.start.cmd.longDescription"),
30+
Example: f.Localizer.MustLocalize("connector.start.cmd.example"),
31+
Hidden: false,
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
34+
validOutputFormats := flagutil.ValidOutputFormats
35+
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
36+
return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
37+
}
38+
39+
return runUpdateCommand(opts)
40+
},
41+
}
42+
43+
flags := connectorcmdutil.NewFlagSet(cmd, f)
44+
flags.AddConnectorID(&opts.connectorID)
45+
flags.AddOutput(&opts.outputFormat)
46+
47+
return cmd
48+
49+
}
50+
51+
func runUpdateCommand(opts *options) error {
52+
f := opts.f
53+
54+
var conn connection.Connection
55+
conn, err := f.Connection(connection.DefaultConfigSkipMasAuth)
56+
if err != nil {
57+
return err
58+
}
59+
60+
if opts.connectorID == "" {
61+
connectorInstance, instance_err := contextutil.GetCurrentConnectorInstance(&conn, f)
62+
if instance_err != nil {
63+
return instance_err
64+
}
65+
66+
opts.connectorID = *connectorInstance.Id
67+
}
68+
69+
api := conn.API()
70+
71+
patch := make(map[string]interface{})
72+
patch["desired_state"] = "ready"
73+
a := api.ConnectorsMgmt().ConnectorsApi.PatchConnector(f.Context, opts.connectorID).Body(patch)
74+
75+
response, httpRes, err := a.Execute()
76+
if httpRes != nil {
77+
defer httpRes.Body.Close()
78+
}
79+
80+
if err != nil {
81+
return err
82+
}
83+
84+
if err = dump.Formatted(f.IOStreams.Out, opts.outputFormat, response); err != nil {
85+
return err
86+
}
87+
88+
f.Logger.Info(f.Localizer.MustLocalize("connector.update.info.success"))
89+
90+
return nil
91+
}

pkg/cmd/connector/stop/stop.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package stop
2+
3+
import (
4+
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil"
5+
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
6+
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
7+
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
8+
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
9+
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
type options struct {
14+
outputFormat string
15+
f *factory.Factory
16+
connectorID string
17+
}
18+
19+
// NewStopCommand creates a new command to start a connector
20+
func NewStopCommand(f *factory.Factory) *cobra.Command {
21+
22+
opts := &options{
23+
f: f,
24+
}
25+
26+
cmd := &cobra.Command{
27+
Use: "stop",
28+
Short: f.Localizer.MustLocalize("connector.stop.cmd.shortDescription"),
29+
Long: f.Localizer.MustLocalize("connector.stop.cmd.longDescription"),
30+
Example: f.Localizer.MustLocalize("connector.stop.cmd.example"),
31+
Hidden: false,
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
34+
validOutputFormats := flagutil.ValidOutputFormats
35+
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
36+
return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
37+
}
38+
39+
return runUpdateCommand(opts)
40+
},
41+
}
42+
43+
flags := connectorcmdutil.NewFlagSet(cmd, f)
44+
flags.AddConnectorID(&opts.connectorID)
45+
flags.AddOutput(&opts.outputFormat)
46+
47+
return cmd
48+
49+
}
50+
51+
func runUpdateCommand(opts *options) error {
52+
f := opts.f
53+
54+
var conn connection.Connection
55+
conn, err := f.Connection(connection.DefaultConfigSkipMasAuth)
56+
if err != nil {
57+
return err
58+
}
59+
60+
if opts.connectorID == "" {
61+
connectorInstance, instance_err := contextutil.GetCurrentConnectorInstance(&conn, f)
62+
if instance_err != nil {
63+
return instance_err
64+
}
65+
66+
opts.connectorID = *connectorInstance.Id
67+
}
68+
69+
api := conn.API()
70+
71+
patch := make(map[string]interface{})
72+
patch["desired_state"] = "stopped"
73+
a := api.ConnectorsMgmt().ConnectorsApi.PatchConnector(f.Context, opts.connectorID).Body(patch)
74+
75+
response, httpRes, err := a.Execute()
76+
if httpRes != nil {
77+
defer httpRes.Body.Close()
78+
}
79+
80+
if err != nil {
81+
return err
82+
}
83+
84+
if err = dump.Formatted(f.IOStreams.Out, opts.outputFormat, response); err != nil {
85+
return err
86+
}
87+
88+
f.Logger.Info(f.Localizer.MustLocalize("connector.update.info.success"))
89+
90+
return nil
91+
}

pkg/core/localize/locales/en/cmd/connectors.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,36 @@ rhoas connector create --file=myconnector.json
143143
cat myconnector.json | rhoas connector create
144144
'''
145145

146+
[connector.stop.cmd.shortDescription]
147+
one = 'Stop a Connectors instance'
148+
149+
[connector.stop.cmd.longDescription]
150+
one = 'Stop a Connectors instance, pass an id or use the instance in the current context'
151+
152+
[connector.stop.cmd.example]
153+
one = '''
154+
# Stop a Connectors instance
155+
rhoas connector stop
156+
157+
# Stop a Connectors instance
158+
rhoas connector stop --id=IJD76DUH675234
159+
'''
160+
161+
[connector.start.cmd.shortDescription]
162+
one = 'Start a Connectors instance'
163+
164+
[connector.start.cmd.longDescription]
165+
one = 'Start a Connectors instance, pass an id or use the instance in the current context'
166+
167+
[connector.start.cmd.example]
168+
one = '''
169+
# Start a Connectors instance
170+
rhoas connector start
171+
172+
# Start a Connectors instance
173+
rhoas connector start --id=IJD76DUH675234
174+
'''
175+
146176
[connector.create.info.success]
147177
one = 'Successfully created the Connectors instance'
148178

pkg/shared/contextutil/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func GetCurrentConnectorInstance(conn *connection.Connection, f *factory.Factory
130130
func GetConnectorForServiceConfig(currCtx *servicecontext.ServiceConfig, conn *connection.Connection, f *factory.Factory) (*connectormgmtclient.Connector, error) {
131131

132132
if currCtx.ConnectorID == "" {
133-
return nil, f.Localizer.MustLocalizeError("context.common.error.noConnecterID")
133+
return nil, f.Localizer.MustLocalizeError("context.common.error.noConnectorID")
134134
}
135135

136136
connectorInstance, _, err := (*conn).API().ConnectorsMgmt().ConnectorsApi.GetConnector(f.Context, currCtx.ConnectorID).Execute()

0 commit comments

Comments
 (0)