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

Commit fc876ed

Browse files
authored
Add monitors set command (#355)
## Why? See galasa-dev/projectmanagement#2067 ## Changes - Added `galasactl monitors set` command with a `--is-enabled` flag to enable/disable a monitor with the given name in a Galasa service
1 parent a75c4ff commit fc876ed

File tree

10 files changed

+666
-5
lines changed

10 files changed

+666
-5
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,26 @@ The run "C1234" can be cancelled using the following command:
457457
galasactl runs cancel --name C1234
458458
```
459459

460+
## monitors set
461+
462+
This command can be used to update a monitor in the Galasa service. The name of the monitor to be enabled must be provided using the `--name` flag.
463+
464+
### Examples
465+
466+
To enable a monitor named "myCustomMonitor":
467+
468+
```
469+
galasactl monitors set --name myCustomMonitor --is-enabled true
470+
```
471+
472+
To disable a monitor named "myCustomMonitor":
473+
474+
```
475+
galasactl monitors set --name myCustomMonitor --is-enabled false
476+
```
477+
478+
For a complete list of supported parameters see [here](./docs/generated/galasactl_monitors_set.md).
479+
460480
## monitors get
461481

462482
This command can be used to get the details of monitors, like resource cleanup monitors, that are available in the Galasa service.

docs/generated/errors-list.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,13 @@ The `galasactl` tool can generate the following errors:
222222
- GAL1223E: Failed to get monitors. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
223223
- GAL1224E: Galasa Monitor named {} is not known on the Galasa service.
224224
- GAL1225E: Invalid monitor name provided. The name provided with the --name flag cannot be empty and must only contain characters in the following ranges: 'a'-'z', 'A'-'Z', '0'-'9', '-' (dash), '_' (underscore).
225-
- GAL1226E: Internal failure. Contents of gzip could be read, but not decoded. New gzip reader failed: file: {} error: {}
226-
- GAL1227E: Internal failure. Contents of gzip could not be decoded. {} error: {}
227-
- GAL1228E: Internal failure. Contents of gzip could not be encoded and compressed. {} error: {}
228-
- GAL1229E: Internal failure. Contents of gzip could not be flushed while encoding and compressing. {} error: {}
229-
- GAL1230E: Internal failure. Gzip file could not be closed while encoding and compressing. {} error: {}
225+
- GAL1226E: Invalid '--is-enabled' value provided. Supported values are 'true' and 'false'. Check your command parameters and try again.
226+
- GAL1227E: Failed to update a monitor named '{}'. Sending the put request to the Galasa service failed. Cause is {}
227+
- GAL1228E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server.
228+
- GAL1229E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {}
229+
- GAL1230E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}'
230+
- GAL1231E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server are: '{}'
231+
- GAL1232E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
230232
- GAL2000W: Warning: Maven configuration file settings.xml should contain a reference to a Galasa repository so that the galasa OBR can be resolved. The official release repository is '{}', and 'pre-release' repository is '{}'
231233
- GAL2501I: Downloaded {} artifacts to folder '{}'
232234

docs/generated/galasactl_monitors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ The parent command for operations to manipulate monitors in the Galasa service
2626

2727
* [galasactl](galasactl.md) - CLI for Galasa
2828
* [galasactl monitors get](galasactl_monitors_get.md) - Get monitors from the Galasa service
29+
* [galasactl monitors set](galasactl_monitors_set.md) - Update a monitor in the Galasa service
2930

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## galasactl monitors set
2+
3+
Update a monitor in the Galasa service
4+
5+
### Synopsis
6+
7+
Updates a monitor with the given name in the Galasa service
8+
9+
```
10+
galasactl monitors set [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Displays the options for the 'monitors set' command.
17+
--is-enabled string A boolean flag that determines whether the given monitor should be enabled or disabled. Supported values are 'true' and 'false'.
18+
--name string A mandatory flag that identifies the monitor to be manipulated by name.
19+
```
20+
21+
### Options inherited from parent commands
22+
23+
```
24+
-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
25+
--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.
26+
-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.
27+
--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)
28+
--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)
29+
```
30+
31+
### SEE ALSO
32+
33+
* [galasactl monitors](galasactl_monitors.md) - Manage monitors in the Galasa service
34+

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_GET = "monitors get"
44+
COMMAND_NAME_MONITORS_SET = "monitors set"
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 monitorsGetCommand spi.GalasaCommand
445+
var monitorsSetCommand spi.GalasaCommand
444446

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

447449
if err == nil {
448450
monitorsGetCommand, err = NewMonitorsGetCommand(factory, monitorsCommand, commsFlagSet)
451+
monitorsSetCommand, err = NewMonitorsSetCommand(factory, monitorsCommand, commsFlagSet)
449452
}
450453

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

456460
return err

pkg/cmd/monitorsSet.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 MonitorsSetCmdValues struct {
20+
isEnabledStr string
21+
}
22+
23+
type MonitorsSetCommand struct {
24+
values *MonitorsSetCmdValues
25+
cobraCommand *cobra.Command
26+
}
27+
28+
// ------------------------------------------------------------------------------------------------
29+
// Constructors methods
30+
// ------------------------------------------------------------------------------------------------
31+
func NewMonitorsSetCommand(
32+
factory spi.Factory,
33+
monitorsSetCommand spi.GalasaCommand,
34+
commsFlagSet GalasaFlagSet,
35+
) (spi.GalasaCommand, error) {
36+
37+
cmd := new(MonitorsSetCommand)
38+
39+
err := cmd.init(factory, monitorsSetCommand, commsFlagSet)
40+
return cmd, err
41+
}
42+
43+
// ------------------------------------------------------------------------------------------------
44+
// Public methods
45+
// ------------------------------------------------------------------------------------------------
46+
func (cmd *MonitorsSetCommand) Name() string {
47+
return COMMAND_NAME_MONITORS_SET
48+
}
49+
50+
func (cmd *MonitorsSetCommand) CobraCommand() *cobra.Command {
51+
return cmd.cobraCommand
52+
}
53+
54+
func (cmd *MonitorsSetCommand) Values() interface{} {
55+
return cmd.values
56+
}
57+
58+
// ------------------------------------------------------------------------------------------------
59+
// Private methods
60+
// ------------------------------------------------------------------------------------------------
61+
func (cmd *MonitorsSetCommand) init(factory spi.Factory, monitorsCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
62+
var err error
63+
64+
cmd.values = &MonitorsSetCmdValues{}
65+
cmd.cobraCommand, err = cmd.createCobraCmd(factory, monitorsCommand, commsFlagSet.Values().(*CommsFlagSetValues))
66+
67+
return err
68+
}
69+
70+
func (cmd *MonitorsSetCommand) createCobraCmd(
71+
factory spi.Factory,
72+
monitorsCommand spi.GalasaCommand,
73+
commsFlagSetValues *CommsFlagSetValues,
74+
) (*cobra.Command, error) {
75+
76+
var err error
77+
78+
monitorsCommandValues := monitorsCommand.Values().(*MonitorsCmdValues)
79+
monitorsSetCobraCmd := &cobra.Command{
80+
Use: "set",
81+
Short: "Update a monitor in the Galasa service",
82+
Long: "Updates a monitor with the given name in the Galasa service",
83+
Aliases: []string{COMMAND_NAME_MONITORS_SET},
84+
RunE: func(cobraCommand *cobra.Command, args []string) error {
85+
return cmd.executeMonitorsSet(factory, monitorsCommand.Values().(*MonitorsCmdValues), commsFlagSetValues)
86+
},
87+
}
88+
89+
addMonitorNameFlag(monitorsSetCobraCmd, true, monitorsCommandValues)
90+
isEnabledFlag := "is-enabled"
91+
92+
monitorsSetCobraCmd.Flags().StringVar(&cmd.values.isEnabledStr, isEnabledFlag, "", "A boolean flag that determines whether the given monitor should be enabled or disabled. Supported values are 'true' and 'false'.")
93+
94+
monitorsSetCobraCmd.MarkFlagsOneRequired(
95+
isEnabledFlag,
96+
)
97+
98+
monitorsCommand.CobraCommand().AddCommand(monitorsSetCobraCmd)
99+
100+
return monitorsSetCobraCmd, err
101+
}
102+
103+
func (cmd *MonitorsSetCommand) executeMonitorsSet(
104+
factory spi.Factory,
105+
monitorsCmdValues *MonitorsCmdValues,
106+
commsFlagSetValues *CommsFlagSetValues,
107+
) error {
108+
109+
var err error
110+
// Operations on the file system will all be relative to the current folder.
111+
fileSystem := factory.GetFileSystem()
112+
113+
err = utils.CaptureLog(fileSystem, commsFlagSetValues.logFileName)
114+
if err == nil {
115+
commsFlagSetValues.isCapturingLogs = true
116+
117+
log.Println("Galasa CLI - Update monitors in the Galasa service")
118+
119+
env := factory.GetEnvironment()
120+
121+
var galasaHome spi.GalasaHome
122+
galasaHome, err = utils.NewGalasaHome(fileSystem, env, commsFlagSetValues.CmdParamGalasaHomePath)
123+
if err == nil {
124+
125+
var commsClient api.APICommsClient
126+
commsClient, err = api.NewAPICommsClient(
127+
commsFlagSetValues.bootstrap,
128+
commsFlagSetValues.maxRetries,
129+
commsFlagSetValues.retryBackoffSeconds,
130+
factory,
131+
galasaHome,
132+
)
133+
134+
if err == nil {
135+
136+
byteReader := factory.GetByteReader()
137+
138+
setMonitorsFunc := func(apiClient *galasaapi.APIClient) error {
139+
return monitors.SetMonitor(
140+
monitorsCmdValues.name,
141+
cmd.values.isEnabledStr,
142+
apiClient,
143+
byteReader,
144+
)
145+
}
146+
err = commsClient.RunAuthenticatedCommandWithRateLimitRetries(setMonitorsFunc)
147+
}
148+
}
149+
}
150+
151+
return err
152+
}

pkg/cmd/monitorsSet_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 TestCommandListContainsMonitorsSetCommand(t *testing.T) {
16+
/// Given...
17+
factory := utils.NewMockFactory()
18+
commands, _ := NewCommandCollection(factory)
19+
20+
// When...
21+
monitorsCommand, err := commands.GetCommand(COMMAND_NAME_MONITORS_SET)
22+
assert.Nil(t, err)
23+
24+
// Then...
25+
assert.NotNil(t, monitorsCommand)
26+
assert.Equal(t, COMMAND_NAME_MONITORS_SET, monitorsCommand.Name())
27+
assert.NotNil(t, monitorsCommand.Values())
28+
assert.IsType(t, &MonitorsSetCmdValues{}, monitorsCommand.Values())
29+
}
30+
31+
func TestMonitorsSetHelpFlagSetCorrectly(t *testing.T) {
32+
// Given...
33+
factory := utils.NewMockFactory()
34+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_MONITORS_SET, factory, t)
35+
36+
var args []string = []string{"monitors", "set", "--help"}
37+
38+
// When...
39+
err := commandCollection.Execute(args)
40+
41+
// Then...
42+
checkOutput("Updates a monitor with the given name in the Galasa service", "", factory, t)
43+
44+
assert.Nil(t, err)
45+
}
46+
47+
func TestMonitorsSetWithNameAndIsEnabledFlagsReturnsOk(t *testing.T) {
48+
// Given...
49+
factory := utils.NewMockFactory()
50+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_MONITORS_SET, factory, t)
51+
52+
var args []string = []string{"monitors", "set", "--name", "myMonitor", "--is-enabled", "true"}
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 TestMonitorsSetNoFlagsReturnsErrorMessage(t *testing.T) {
65+
// Given...
66+
factory := utils.NewMockFactory()
67+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_MONITORS_SET, factory, t)
68+
69+
var args []string = []string{"monitors", "set"}
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+
81+
func TestMonitorsSetWithNameFlagOnlyReturnsErrorMessage(t *testing.T) {
82+
// Given...
83+
factory := utils.NewMockFactory()
84+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_MONITORS_SET, factory, t)
85+
86+
var args []string = []string{"monitors", "set", "--name", "myCustomMonitor"}
87+
88+
// When...
89+
err := commandCollection.Execute(args)
90+
91+
// Then...
92+
assert.NotNil(t, err)
93+
94+
// Check what the user saw is reasonable.
95+
checkOutput("", `Error: at least one of the flags in the group [is-enabled] is required`, factory, t)
96+
}
97+

pkg/errors/errorMessage.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ var (
430430
// Getting a single monitor by name...
431431
GALASA_ERROR_MONITOR_NAME_NOT_FOUND = NewMessageType("GAL1224E: Galasa Monitor named %v is not known on the Galasa service.", 1224, STACK_TRACE_NOT_WANTED)
432432
GALASA_ERROR_INVALID_MONITOR_NAME = NewMessageType("GAL1225E: Invalid monitor name provided. The name provided with the --name flag cannot be empty and must only contain characters in the following ranges: 'a'-'z', 'A'-'Z', '0'-'9', '-' (dash), '_' (underscore).", 1225, STACK_TRACE_NOT_WANTED)
433+
434+
// When updating a monitor...
435+
GALASA_ERROR_INVALID_IS_ENABLED_FLAG = NewMessageType("GAL1226E: Invalid '--is-enabled' value provided. Supported values are 'true' and 'false'. Check your command parameters and try again.", 1226, STACK_TRACE_NOT_WANTED)
436+
GALASA_ERROR_UPDATE_MONITOR_REQUEST_FAILED = NewMessageType("GAL1227E: Failed to update a monitor named '%s'. Sending the put request to the Galasa service failed. Cause is %v", 1227, STACK_TRACE_NOT_WANTED)
437+
GALASA_ERROR_UPDATE_MONITOR_NO_RESPONSE_CONTENT = NewMessageType("GAL1228E: Failed to update a monitor named '%s'. Unexpected http status code %v received from the server.", 1228, STACK_TRACE_NOT_WANTED)
438+
GALASA_ERROR_UPDATE_MONITOR_RESPONSE_BODY_UNREADABLE = NewMessageType("GAL1229E: Failed to update a monitor named '%s'. Unexpected http status code %v received from the server. Error details from the server could not be read. Cause: %s", 1229, STACK_TRACE_NOT_WANTED)
439+
GALASA_ERROR_UPDATE_MONITOR_UNPARSEABLE_CONTENT = NewMessageType("GAL1230E: Failed to update a monitor named '%s'. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1230, STACK_TRACE_NOT_WANTED)
440+
GALASA_ERROR_UPDATE_MONITOR_SERVER_REPORTED_ERROR = NewMessageType("GAL1231E: Failed to update a monitor named '%s'. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1231, STACK_TRACE_NOT_WANTED)
441+
GALASA_ERROR_UPDATE_MONITOR_EXPLANATION_NOT_JSON = NewMessageType("GAL1232E: Failed to update a monitor named '%s'. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1232, STACK_TRACE_NOT_WANTED)
433442

434443
// Warnings...
435444
GALASA_WARNING_MAVEN_NO_GALASA_OBR_REPO = NewMessageType("GAL2000W: Warning: Maven configuration file settings.xml should contain a reference to a Galasa repository so that the galasa OBR can be resolved. The official release repository is '%s', and 'pre-release' repository is '%s'", 2000, STACK_TRACE_WANTED)

0 commit comments

Comments
 (0)