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