Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 6ea46e2

Browse files
committed
Add monitors disable command
Signed-off-by: Eamonn Mansour <47121388+eamansour@users.noreply.github.com>
1 parent 72e42b3 commit 6ea46e2

11 files changed

+483
-9
lines changed

docs/generated/galasactl_monitors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ The parent command for operations to manipulate monitors in the Galasa service
2525
### SEE ALSO
2626

2727
* [galasactl](galasactl.md) - CLI for Galasa
28+
* [galasactl monitors disable](galasactl_monitors_disable.md) - Disable a monitor in the Galasa service
2829
* [galasactl monitors enable](galasactl_monitors_enable.md) - Enable a monitor in the Galasa service
2930

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## galasactl monitors disable
2+
3+
Disable a monitor in the Galasa service
4+
5+
### Synopsis
6+
7+
Disables a monitor with the given name in the Galasa service
8+
9+
```
10+
galasactl monitors disable [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Displays the options for the 'monitors disable' command.
17+
--name string A mandatory flag that identifies the monitor to be manipulated by name.
18+
```
19+
20+
### Options inherited from parent commands
21+
22+
```
23+
-b, --bootstrap string Bootstrap URL. Should start with 'http://' or 'file://'. If it starts with neither, it is assumed to be a fully-qualified path. If missing, it defaults to use the 'bootstrap.properties' file in your GALASA_HOME. Example: http://example.com/bootstrap, file:///user/myuserid/.galasa/bootstrap.properties , file://C:/Users/myuserid/.galasa/bootstrap.properties
24+
--galasahome string Path to a folder where Galasa will read and write files and configuration settings. The default is '${HOME}/.galasa'. This overrides the GALASA_HOME environment variable which may be set instead.
25+
-l, --log string File to which log information will be sent. Any folder referred to must exist. An existing file will be overwritten. Specify "-" to log to stderr. Defaults to not logging.
26+
--rate-limit-retries int The maximum number of retries that should be made when requests to the Galasa Service fail due to rate limits being exceeded. Must be a whole number. Defaults to 3 retries (default 3)
27+
--rate-limit-retry-backoff-secs float The amount of time in seconds to wait before retrying a command if it failed due to rate limits being exceeded. Defaults to 1 second. (default 1)
28+
```
29+
30+
### SEE ALSO
31+
32+
* [galasactl monitors](galasactl_monitors.md) - Manage monitors in the Galasa service
33+

docs/generated/galasactl_monitors_enable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Enable a monitor in the Galasa service
44

55
### Synopsis
66

7-
Enables a given monitor in the Galasa service
7+
Enables a monitor with the given name in the Galasa service
88

99
```
1010
galasactl monitors enable [flags]

pkg/cmd/commandCollection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
COMMAND_NAME_LOCAL_INIT = "local init"
4242
COMMAND_NAME_MONITORS = "monitors"
4343
COMMAND_NAME_MONITORS_ENABLE = "monitors enable"
44+
COMMAND_NAME_MONITORS_DISABLE = "monitors disable"
4445
COMMAND_NAME_PROPERTIES = "properties"
4546
COMMAND_NAME_PROPERTIES_GET = "properties get"
4647
COMMAND_NAME_PROPERTIES_SET = "properties set"
@@ -441,16 +442,19 @@ func (commands *commandCollectionImpl) addMonitorsCommands(factory spi.Factory,
441442
var err error
442443
var monitorsCommand spi.GalasaCommand
443444
var monitorsEnableCommand spi.GalasaCommand
445+
var monitorsDisableCommand spi.GalasaCommand
444446

445447
monitorsCommand, err = NewMonitorsCmd(rootCommand, commsFlagSet)
446448

447449
if err == nil {
448450
monitorsEnableCommand, err = NewMonitorsEnableCommand(factory, monitorsCommand, commsFlagSet)
451+
monitorsDisableCommand, err = NewMonitorsDisableCommand(factory, monitorsCommand, commsFlagSet)
449452
}
450453

451454
if err == nil {
452455
commands.commandMap[monitorsCommand.Name()] = monitorsCommand
453456
commands.commandMap[monitorsEnableCommand.Name()] = monitorsEnableCommand
457+
commands.commandMap[monitorsDisableCommand.Name()] = monitorsDisableCommand
454458
}
455459

456460
return err

pkg/cmd/monitorsDisable.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright contributors to the Galasa project
3+
*
4+
* SPDX-License-Identifier: EPL-2.0
5+
*/
6+
package cmd
7+
8+
import (
9+
"log"
10+
11+
"github.com/galasa-dev/cli/pkg/api"
12+
"github.com/galasa-dev/cli/pkg/galasaapi"
13+
"github.com/galasa-dev/cli/pkg/monitors"
14+
"github.com/galasa-dev/cli/pkg/spi"
15+
"github.com/galasa-dev/cli/pkg/utils"
16+
"github.com/spf13/cobra"
17+
)
18+
19+
type MonitorsDisableCmdValues struct {
20+
}
21+
22+
type MonitorsDisableCommand struct {
23+
values *MonitorsDisableCmdValues
24+
cobraCommand *cobra.Command
25+
}
26+
27+
// ------------------------------------------------------------------------------------------------
28+
// Constructors methods
29+
// ------------------------------------------------------------------------------------------------
30+
func NewMonitorsDisableCommand(
31+
factory spi.Factory,
32+
monitorsDisableCommand spi.GalasaCommand,
33+
commsFlagSet GalasaFlagSet,
34+
) (spi.GalasaCommand, error) {
35+
36+
cmd := new(MonitorsDisableCommand)
37+
38+
err := cmd.init(factory, monitorsDisableCommand, commsFlagSet)
39+
return cmd, err
40+
}
41+
42+
// ------------------------------------------------------------------------------------------------
43+
// Public methods
44+
// ------------------------------------------------------------------------------------------------
45+
func (cmd *MonitorsDisableCommand) Name() string {
46+
return COMMAND_NAME_MONITORS_DISABLE
47+
}
48+
49+
func (cmd *MonitorsDisableCommand) CobraCommand() *cobra.Command {
50+
return cmd.cobraCommand
51+
}
52+
53+
func (cmd *MonitorsDisableCommand) Values() interface{} {
54+
return cmd.values
55+
}
56+
57+
// ------------------------------------------------------------------------------------------------
58+
// Private methods
59+
// ------------------------------------------------------------------------------------------------
60+
func (cmd *MonitorsDisableCommand) init(factory spi.Factory, monitorsCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
61+
var err error
62+
63+
cmd.values = &MonitorsDisableCmdValues{}
64+
cmd.cobraCommand, err = cmd.createCobraCmd(factory, monitorsCommand, commsFlagSet.Values().(*CommsFlagSetValues))
65+
66+
return err
67+
}
68+
69+
func (cmd *MonitorsDisableCommand) createCobraCmd(
70+
factory spi.Factory,
71+
monitorsCommand spi.GalasaCommand,
72+
commsFlagSetValues *CommsFlagSetValues,
73+
) (*cobra.Command, error) {
74+
75+
var err error
76+
77+
monitorsCommandValues := monitorsCommand.Values().(*MonitorsCmdValues)
78+
monitorsDisableCobraCmd := &cobra.Command{
79+
Use: "disable",
80+
Short: "Disable a monitor in the Galasa service",
81+
Long: "Disables a monitor with the given name in the Galasa service",
82+
Aliases: []string{COMMAND_NAME_MONITORS_DISABLE},
83+
RunE: func(cobraCommand *cobra.Command, args []string) error {
84+
return cmd.executeMonitorsDisable(factory, monitorsCommand.Values().(*MonitorsCmdValues), commsFlagSetValues)
85+
},
86+
}
87+
88+
addMonitorNameFlag(monitorsDisableCobraCmd, true, monitorsCommandValues)
89+
90+
monitorsCommand.CobraCommand().AddCommand(monitorsDisableCobraCmd)
91+
92+
return monitorsDisableCobraCmd, err
93+
}
94+
95+
func (cmd *MonitorsDisableCommand) executeMonitorsDisable(
96+
factory spi.Factory,
97+
monitorsCmdValues *MonitorsCmdValues,
98+
commsFlagSetValues *CommsFlagSetValues,
99+
) error {
100+
101+
var err error
102+
// Operations on the file system will all be relative to the current folder.
103+
fileSystem := factory.GetFileSystem()
104+
105+
err = utils.CaptureLog(fileSystem, commsFlagSetValues.logFileName)
106+
if err == nil {
107+
commsFlagSetValues.isCapturingLogs = true
108+
109+
log.Println("Galasa CLI - Disable monitors from the Galasa service")
110+
111+
env := factory.GetEnvironment()
112+
113+
var galasaHome spi.GalasaHome
114+
galasaHome, err = utils.NewGalasaHome(fileSystem, env, commsFlagSetValues.CmdParamGalasaHomePath)
115+
if err == nil {
116+
117+
var commsClient api.APICommsClient
118+
commsClient, err = api.NewAPICommsClient(
119+
commsFlagSetValues.bootstrap,
120+
commsFlagSetValues.maxRetries,
121+
commsFlagSetValues.retryBackoffSeconds,
122+
factory,
123+
galasaHome,
124+
)
125+
126+
if err == nil {
127+
128+
byteReader := factory.GetByteReader()
129+
130+
disableMonitorsFunc := func(apiClient *galasaapi.APIClient) error {
131+
return monitors.DisableMonitor(monitorsCmdValues.name, apiClient, byteReader)
132+
}
133+
err = commsClient.RunAuthenticatedCommandWithRateLimitRetries(disableMonitorsFunc)
134+
}
135+
}
136+
}
137+
138+
return err
139+
}

pkg/cmd/monitorsDisable_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright contributors to the Galasa project
3+
*
4+
* SPDX-License-Identifier: EPL-2.0
5+
*/
6+
package cmd
7+
8+
import (
9+
"testing"
10+
11+
"github.com/galasa-dev/cli/pkg/utils"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestCommandListContainsMonitorsDisableCommand(t *testing.T) {
16+
/// Given...
17+
factory := utils.NewMockFactory()
18+
commands, _ := NewCommandCollection(factory)
19+
20+
// When...
21+
monitorsCommand, err := commands.GetCommand(COMMAND_NAME_MONITORS_DISABLE)
22+
assert.Nil(t, err)
23+
24+
// Then...
25+
assert.NotNil(t, monitorsCommand)
26+
assert.Equal(t, COMMAND_NAME_MONITORS_DISABLE, monitorsCommand.Name())
27+
assert.NotNil(t, monitorsCommand.Values())
28+
assert.IsType(t, &MonitorsDisableCmdValues{}, monitorsCommand.Values())
29+
}
30+
31+
func TestMonitorsDisableHelpFlagSetCorrectly(t *testing.T) {
32+
// Given...
33+
factory := utils.NewMockFactory()
34+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_MONITORS_DISABLE, factory, t)
35+
36+
var args []string = []string{"monitors", "disable", "--help"}
37+
38+
// When...
39+
err := commandCollection.Execute(args)
40+
41+
// Then...
42+
checkOutput("Disables a monitor with the given name in the Galasa service", "", factory, t)
43+
44+
assert.Nil(t, err)
45+
}
46+
47+
func TestMonitorsDisableWithNameFlagReturnsOk(t *testing.T) {
48+
// Given...
49+
factory := utils.NewMockFactory()
50+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_MONITORS_DISABLE, factory, t)
51+
52+
var args []string = []string{"monitors", "disable", "--name", "myMonitor"}
53+
54+
// When...
55+
err := commandCollection.Execute(args)
56+
57+
// Then...
58+
assert.Nil(t, err)
59+
60+
// Check what the user saw is reasonable.
61+
checkOutput("", "", factory, t)
62+
}
63+
64+
func TestMonitorsDisableNoFlagsReturnsErrorMessage(t *testing.T) {
65+
// Given...
66+
factory := utils.NewMockFactory()
67+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_MONITORS_DISABLE, factory, t)
68+
69+
var args []string = []string{"monitors", "disable"}
70+
71+
// When...
72+
err := commandCollection.Execute(args)
73+
74+
// Then...
75+
assert.NotNil(t, err)
76+
77+
// Check what the user saw is reasonable.
78+
checkOutput("", `Error: required flag(s) "name" not set`, factory, t)
79+
}
80+

pkg/cmd/monitorsEnable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (cmd *MonitorsEnableCommand) createCobraCmd(
7878
monitorsEnableCobraCmd := &cobra.Command{
7979
Use: "enable",
8080
Short: "Enable a monitor in the Galasa service",
81-
Long: "Enables a given monitor in the Galasa service",
81+
Long: "Enables a monitor with the given name in the Galasa service",
8282
Aliases: []string{COMMAND_NAME_MONITORS_ENABLE},
8383
RunE: func(cobraCommand *cobra.Command, args []string) error {
8484
return cmd.executeMonitorsEnable(factory, monitorsCommand.Values().(*MonitorsCmdValues), commsFlagSetValues)

pkg/cmd/monitorsEnable_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestMonitorsEnableHelpFlagSetCorrectly(t *testing.T) {
3939
err := commandCollection.Execute(args)
4040

4141
// Then...
42-
checkOutput("Enables a given monitor in the Galasa service", "", factory, t)
42+
checkOutput("Enables a monitor with the given name in the Galasa service", "", factory, t)
4343

4444
assert.Nil(t, err)
4545
}

pkg/monitors/monitorsDisable.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright contributors to the Galasa project
3+
*
4+
* SPDX-License-Identifier: EPL-2.0
5+
*/
6+
7+
package monitors
8+
9+
import (
10+
"log"
11+
"github.com/galasa-dev/cli/pkg/galasaapi"
12+
"github.com/galasa-dev/cli/pkg/spi"
13+
)
14+
15+
func DisableMonitor(
16+
monitorName string,
17+
apiClient *galasaapi.APIClient,
18+
byteReader spi.ByteReader,
19+
) error {
20+
var err error
21+
22+
desiredEnabledState := false
23+
err = setMonitorIsEnabledState(monitorName, desiredEnabledState, apiClient, byteReader)
24+
25+
log.Printf("DisableMonitor exiting. err is %v\n", err)
26+
return err
27+
}

0 commit comments

Comments
 (0)