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

Commit 72e42b3

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

13 files changed

+775
-1
lines changed

docs/generated/errors-list.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ The `galasactl` tool can generate the following errors:
214214
- GAL1215E: An attempt to update a user '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}'
215215
- GAL1216E: An attempt to update a user '{}' failed. Unexpected http status code {} received from the server. Error details from the server are: '{}'
216216
- GAL1217E: An attempt to update a user '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
217-
- GAL1225E: Failed to open file '{}' cause: {}. Check that this file exists, and that you have read permissions.
217+
- GAL1218E: Failed to update a monitor named '{}'. Sending the put request to the Galasa service failed. Cause is {}
218+
- GAL1219E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server.
219+
- GAL1220E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {}
220+
- GAL1221E: 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: '{}'
221+
- GAL1222E: Failed to update a monitor named '{}'. Unexpected http status code {} received from the server. Error details from the server are: '{}'
222+
- GAL1223E: 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.
223+
- GAL1224E: Galasa Monitor named {} is not known on the Galasa service.
224+
- 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).
218225
- GAL1226E: Internal failure. Contents of gzip could be read, but not decoded. New gzip reader failed: file: {} error: {}
219226
- GAL1227E: Internal failure. Contents of gzip could not be decoded. {} error: {}
220227
- GAL1228E: Internal failure. Contents of gzip could not be encoded and compressed. {} error: {}

docs/generated/galasactl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A tool for controlling Galasa resources using the command-line.
1818

1919
* [galasactl auth](galasactl_auth.md) - Manages authentication with a Galasa ecosystem
2020
* [galasactl local](galasactl_local.md) - Manipulate local system
21+
* [galasactl monitors](galasactl_monitors.md) - Manage monitors in the Galasa service
2122
* [galasactl project](galasactl_project.md) - Manipulate local project source code
2223
* [galasactl properties](galasactl_properties.md) - Manages properties in an ecosystem
2324
* [galasactl resources](galasactl_resources.md) - Manages resources in an ecosystem
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## galasactl monitors
2+
3+
Manage monitors in the Galasa service
4+
5+
### Synopsis
6+
7+
The parent command for operations to manipulate monitors in the Galasa service
8+
9+
### Options
10+
11+
```
12+
-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
13+
-h, --help Displays the options for the 'monitors' command.
14+
--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)
15+
--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)
16+
```
17+
18+
### Options inherited from parent commands
19+
20+
```
21+
--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.
22+
-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.
23+
```
24+
25+
### SEE ALSO
26+
27+
* [galasactl](galasactl.md) - CLI for Galasa
28+
* [galasactl monitors enable](galasactl_monitors_enable.md) - Enable a monitor in the Galasa service
29+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## galasactl monitors enable
2+
3+
Enable a monitor in the Galasa service
4+
5+
### Synopsis
6+
7+
Enables a given monitor in the Galasa service
8+
9+
```
10+
galasactl monitors enable [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Displays the options for the 'monitors enable' 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+

pkg/cmd/commandCollection.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const (
3939
COMMAND_NAME_PROJECT_CREATE = "project create"
4040
COMMAND_NAME_LOCAL = "local"
4141
COMMAND_NAME_LOCAL_INIT = "local init"
42+
COMMAND_NAME_MONITORS = "monitors"
43+
COMMAND_NAME_MONITORS_ENABLE = "monitors enable"
4244
COMMAND_NAME_PROPERTIES = "properties"
4345
COMMAND_NAME_PROPERTIES_GET = "properties get"
4446
COMMAND_NAME_PROPERTIES_SET = "properties set"
@@ -137,6 +139,10 @@ func (commands *commandCollectionImpl) init(factory spi.Factory) error {
137139
err = commands.addLocalCommands(factory, rootCommand)
138140
}
139141

142+
if err == nil {
143+
err = commands.addMonitorsCommands(factory, rootCommand, commsFlagSet)
144+
}
145+
140146
if err == nil {
141147
err = commands.addProjectCommands(factory, rootCommand)
142148
}
@@ -430,6 +436,26 @@ func (commands *commandCollectionImpl) addSecretsCommands(factory spi.Factory, r
430436
return err
431437
}
432438

439+
func (commands *commandCollectionImpl) addMonitorsCommands(factory spi.Factory, rootCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
440+
441+
var err error
442+
var monitorsCommand spi.GalasaCommand
443+
var monitorsEnableCommand spi.GalasaCommand
444+
445+
monitorsCommand, err = NewMonitorsCmd(rootCommand, commsFlagSet)
446+
447+
if err == nil {
448+
monitorsEnableCommand, err = NewMonitorsEnableCommand(factory, monitorsCommand, commsFlagSet)
449+
}
450+
451+
if err == nil {
452+
commands.commandMap[monitorsCommand.Name()] = monitorsCommand
453+
commands.commandMap[monitorsEnableCommand.Name()] = monitorsEnableCommand
454+
}
455+
456+
return err
457+
}
458+
433459
func (commands *commandCollectionImpl) addUsersCommands(factory spi.Factory, rootCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
434460

435461
var err error

pkg/cmd/monitors.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright contributors to the Galasa project
3+
*
4+
* SPDX-License-Identifier: EPL-2.0
5+
*/
6+
package cmd
7+
8+
import (
9+
"github.com/galasa-dev/cli/pkg/spi"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
type MonitorsCmdValues struct {
14+
name string
15+
}
16+
17+
type MonitorsCommand struct {
18+
cobraCommand *cobra.Command
19+
values *MonitorsCmdValues
20+
}
21+
22+
// ------------------------------------------------------------------------------------------------
23+
// Constructors
24+
// ------------------------------------------------------------------------------------------------
25+
26+
func NewMonitorsCmd(rootCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) (spi.GalasaCommand, error) {
27+
cmd := new(MonitorsCommand)
28+
err := cmd.init(rootCommand, commsFlagSet)
29+
return cmd, err
30+
}
31+
32+
// ------------------------------------------------------------------------------------------------
33+
// Public functions
34+
// ------------------------------------------------------------------------------------------------
35+
36+
func (cmd *MonitorsCommand) Name() string {
37+
return COMMAND_NAME_MONITORS
38+
}
39+
40+
func (cmd *MonitorsCommand) CobraCommand() *cobra.Command {
41+
return cmd.cobraCommand
42+
}
43+
44+
func (cmd *MonitorsCommand) Values() interface{} {
45+
return cmd.values
46+
}
47+
48+
// ------------------------------------------------------------------------------------------------
49+
// Private functions
50+
// ------------------------------------------------------------------------------------------------
51+
52+
func (cmd *MonitorsCommand) init(rootCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
53+
54+
var err error
55+
56+
cmd.values = &MonitorsCmdValues{}
57+
cmd.cobraCommand, err = cmd.createCobraCommand(rootCommand, commsFlagSet)
58+
59+
return err
60+
}
61+
62+
func (cmd *MonitorsCommand) createCobraCommand(rootCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) (*cobra.Command, error) {
63+
64+
var err error
65+
66+
monitorsCobraCmd := &cobra.Command{
67+
Use: "monitors",
68+
Short: "Manage monitors in the Galasa service",
69+
Long: "The parent command for operations to manipulate monitors in the Galasa service",
70+
}
71+
72+
monitorsCobraCmd.PersistentFlags().AddFlagSet(commsFlagSet.Flags())
73+
rootCommand.CobraCommand().AddCommand(monitorsCobraCmd)
74+
75+
return monitorsCobraCmd, err
76+
}
77+
78+
func addMonitorNameFlag(cmd *cobra.Command, isMandatory bool, monitorsCmdValues *MonitorsCmdValues) {
79+
80+
flagName := "name"
81+
var description string
82+
if isMandatory {
83+
description = "A mandatory flag that identifies the monitor to be manipulated by name."
84+
} else {
85+
description = "An optional flag that identifies the monitor to be retrieved by name."
86+
}
87+
88+
cmd.Flags().StringVar(&monitorsCmdValues.name, flagName, "", description)
89+
90+
if isMandatory {
91+
cmd.MarkFlagRequired(flagName)
92+
}
93+
}

pkg/cmd/monitorsEnable.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 MonitorsEnableCmdValues struct {
20+
}
21+
22+
type MonitorsEnableCommand struct {
23+
values *MonitorsEnableCmdValues
24+
cobraCommand *cobra.Command
25+
}
26+
27+
// ------------------------------------------------------------------------------------------------
28+
// Constructors methods
29+
// ------------------------------------------------------------------------------------------------
30+
func NewMonitorsEnableCommand(
31+
factory spi.Factory,
32+
monitorsEnableCommand spi.GalasaCommand,
33+
commsFlagSet GalasaFlagSet,
34+
) (spi.GalasaCommand, error) {
35+
36+
cmd := new(MonitorsEnableCommand)
37+
38+
err := cmd.init(factory, monitorsEnableCommand, commsFlagSet)
39+
return cmd, err
40+
}
41+
42+
// ------------------------------------------------------------------------------------------------
43+
// Public methods
44+
// ------------------------------------------------------------------------------------------------
45+
func (cmd *MonitorsEnableCommand) Name() string {
46+
return COMMAND_NAME_MONITORS_ENABLE
47+
}
48+
49+
func (cmd *MonitorsEnableCommand) CobraCommand() *cobra.Command {
50+
return cmd.cobraCommand
51+
}
52+
53+
func (cmd *MonitorsEnableCommand) Values() interface{} {
54+
return cmd.values
55+
}
56+
57+
// ------------------------------------------------------------------------------------------------
58+
// Private methods
59+
// ------------------------------------------------------------------------------------------------
60+
func (cmd *MonitorsEnableCommand) init(factory spi.Factory, monitorsCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
61+
var err error
62+
63+
cmd.values = &MonitorsEnableCmdValues{}
64+
cmd.cobraCommand, err = cmd.createCobraCmd(factory, monitorsCommand, commsFlagSet.Values().(*CommsFlagSetValues))
65+
66+
return err
67+
}
68+
69+
func (cmd *MonitorsEnableCommand) 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+
monitorsEnableCobraCmd := &cobra.Command{
79+
Use: "enable",
80+
Short: "Enable a monitor in the Galasa service",
81+
Long: "Enables a given monitor in the Galasa service",
82+
Aliases: []string{COMMAND_NAME_MONITORS_ENABLE},
83+
RunE: func(cobraCommand *cobra.Command, args []string) error {
84+
return cmd.executeMonitorsEnable(factory, monitorsCommand.Values().(*MonitorsCmdValues), commsFlagSetValues)
85+
},
86+
}
87+
88+
addMonitorNameFlag(monitorsEnableCobraCmd, true, monitorsCommandValues)
89+
90+
monitorsCommand.CobraCommand().AddCommand(monitorsEnableCobraCmd)
91+
92+
return monitorsEnableCobraCmd, err
93+
}
94+
95+
func (cmd *MonitorsEnableCommand) executeMonitorsEnable(
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 - Enable 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+
enableMonitorsFunc := func(apiClient *galasaapi.APIClient) error {
131+
return monitors.EnableMonitor(monitorsCmdValues.name, apiClient, byteReader)
132+
}
133+
err = commsClient.RunAuthenticatedCommandWithRateLimitRetries(enableMonitorsFunc)
134+
}
135+
}
136+
}
137+
138+
return err
139+
}

0 commit comments

Comments
 (0)