|
| 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 | +} |
0 commit comments