Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generated/errors-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ The `galasactl` tool can generate the following errors:
- GAL1238E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}'
- GAL1239E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are: '{}'
- GAL1240E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
- GAL1241E: The test stream could not be deleted by name because it was not found by the Galasa service. Try listing streams using 'galasactl streams get' to identify the one you wish to delete
- GAL1242E: Failed to test stream from database by stream name.
- 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 '{}'
- GAL2501I: Downloaded {} artifacts to folder '{}'

Expand Down
1 change: 1 addition & 0 deletions docs/generated/galasactl_streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ Parent command for managing test streams in a Galasa service
### SEE ALSO

* [galasactl](galasactl.md) - CLI for Galasa
* [galasactl streams delete](galasactl_streams_delete.md) - Deletes a test stream by name
* [galasactl streams get](galasactl_streams_get.md) - Gets a list of test streams

33 changes: 33 additions & 0 deletions docs/generated/galasactl_streams_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## galasactl streams delete

Deletes a test stream by name

### Synopsis

Deletes a single test stream by the given name from the API Server

```
galasactl streams delete [flags]
```

### Options

```
-h, --help Displays the options for the 'streams delete' command.
--name string An optional field indicating the name of a test stream
```

### Options inherited from parent commands

```
-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
--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.
-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.
--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)
--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)
```

### SEE ALSO

* [galasactl streams](galasactl_streams.md) - Manages test streams in a Galasa service

13 changes: 12 additions & 1 deletion pkg/cmd/commandCollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const (
COMMAND_NAME_ROLES_GET = "roles get"
COMMAND_NAME_STREAMS = "streams"
COMMAND_NAME_STREAMS_GET = "streams get"
COMMAND_NAME_STREAMS_DELETE = "streams delete"
)

// -----------------------------------------------------------------
Expand Down Expand Up @@ -519,14 +520,24 @@ func (commands *commandCollectionImpl) addStreamsCommands(factory spi.Factory, r
var err error
var streamsCommand spi.GalasaCommand
var streamsGetCommand spi.GalasaCommand
var streamsDeleteCommand spi.GalasaCommand

streamsCommand, err = NewStreamsCommand(rootCommand, commsFlagSet)

if err == nil {

commands.commandMap[streamsCommand.Name()] = streamsCommand
streamsGetCommand, err = NewStreamsGetCommand(factory, streamsCommand, commsFlagSet)

if err == nil {
commands.commandMap[streamsCommand.Name()] = streamsCommand

commands.commandMap[streamsGetCommand.Name()] = streamsGetCommand
streamsDeleteCommand, err = NewStreamsDeleteCommand(factory, streamsCommand, commsFlagSet)

if err == nil {
commands.commandMap[streamsDeleteCommand.Name()] = streamsDeleteCommand
}

}
}

Expand Down
149 changes: 149 additions & 0 deletions pkg/cmd/streamsDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/

package cmd

import (
"log"

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/spi"
"github.com/galasa-dev/cli/pkg/streams"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
)

// Objective: Allow user to do this:
//
// streams delete
type StreamsDeleteCommand struct {
cobraCommand *cobra.Command
}

// ------------------------------------------------------------------------------------------------
// Constructors methods
// ------------------------------------------------------------------------------------------------
func NewStreamsDeleteCommand(
factory spi.Factory,
streamsDeleteCommand spi.GalasaCommand,
commsFlagSet GalasaFlagSet,
) (spi.GalasaCommand, error) {

cmd := new(StreamsDeleteCommand)
err := cmd.init(factory, streamsDeleteCommand, commsFlagSet)
return cmd, err

}

// ------------------------------------------------------------------------------------------------
// Public methods
// ------------------------------------------------------------------------------------------------
func (cmd *StreamsDeleteCommand) Name() string {
return COMMAND_NAME_STREAMS_DELETE
}

func (cmd *StreamsDeleteCommand) CobraCommand() *cobra.Command {
return cmd.cobraCommand
}

func (cmd *StreamsDeleteCommand) Values() interface{} {
return nil
}

// ------------------------------------------------------------------------------------------------
// Private methods
// ------------------------------------------------------------------------------------------------

func (cmd *StreamsDeleteCommand) init(factory spi.Factory, streamsCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {

var err error

cmd.cobraCommand, err = cmd.createCobraCmd(factory, streamsCommand, commsFlagSet)

return err

}

func (cmd *StreamsDeleteCommand) createCobraCmd(
factory spi.Factory,
streamsCommand spi.GalasaCommand,
commsFlagSet GalasaFlagSet,
) (*cobra.Command, error) {

var err error

commsFlagSetValues := commsFlagSet.Values().(*CommsFlagSetValues)
streamsCommandValues := streamsCommand.Values().(*StreamsCmdValues)

streamsDeleteCobraCmd := &cobra.Command{
Use: "delete",
Short: "Deletes a test stream by name",
Long: "Deletes a single test stream by the given name from the API Server",
Aliases: []string{COMMAND_NAME_STREAMS_DELETE},
RunE: func(cobraCommand *cobra.Command, args []string) error {
return cmd.executeStreamsDelete(
factory, streamsCommand.Values().(*StreamsCmdValues), commsFlagSetValues,
)
},
}

addStreamNameFlag(streamsDeleteCobraCmd, false, streamsCommandValues)
streamsCommand.CobraCommand().AddCommand(streamsDeleteCobraCmd)

return streamsDeleteCobraCmd, err

}

func (cmd *StreamsDeleteCommand) executeStreamsDelete(
factory spi.Factory,
streamsCmdValues *StreamsCmdValues,
commsFlagSetValues *CommsFlagSetValues,
) error {

var err error

// Operations on the file system will all be relative to the current folder.
fileSystem := factory.GetFileSystem()
byteReader := factory.GetByteReader()

err = utils.CaptureLog(fileSystem, commsFlagSetValues.logFileName)

if err == nil {

commsFlagSetValues.isCapturingLogs = true

log.Println("Galasa CLI - Delete test stream from the Galasa service")

// Get the ability to query environment variables.
env := factory.GetEnvironment()

var galasaHome spi.GalasaHome
galasaHome, err = utils.NewGalasaHome(fileSystem, env, commsFlagSetValues.CmdParamGalasaHomePath)
if err == nil {

var commsClient api.APICommsClient
commsClient, err = api.NewAPICommsClient(
commsFlagSetValues.bootstrap,
commsFlagSetValues.maxRetries,
commsFlagSetValues.retryBackoffSeconds,
factory,
galasaHome,
)

if err == nil {
deleteStreamFunc := func(apiClient *galasaapi.APIClient) error {
// Call to process the command in a unit-testable way.
return streams.DeleteStream(streamsCmdValues.name, apiClient, byteReader)
}
err = commsClient.RunAuthenticatedCommandWithRateLimitRetries(deleteStreamFunc)
}
}
}

return err

}
62 changes: 62 additions & 0 deletions pkg/cmd/streamsDelete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/

package cmd

import (
"testing"

"github.com/galasa-dev/cli/pkg/utils"
"github.com/stretchr/testify/assert"
)

func TestStreamsDeleteCommandInCommandCollectionHasName(t *testing.T) {

factory := utils.NewMockFactory()
commands, _ := NewCommandCollection(factory)

StreamsDeleteCommand, err := commands.GetCommand(COMMAND_NAME_STREAMS_DELETE)
assert.Nil(t, err)

assert.Equal(t, COMMAND_NAME_STREAMS_DELETE, StreamsDeleteCommand.Name())
assert.NotNil(t, StreamsDeleteCommand.CobraCommand())

}

func TestStreamsDeleteHelpFlagSetCorrectly(t *testing.T) {
// Given...
factory := utils.NewMockFactory()

var args []string = []string{"streams", "delete", "--help"}

// When...
err := Execute(factory, args)

// Then...
// Check what the user saw is reasonable.
checkOutput("Displays the options for the 'streams delete' command.", "", factory, t)

assert.Nil(t, err)
}

func TestStreamsDeleteNamespaceNameFlagsReturnsOk(t *testing.T) {
// Given...
factory := utils.NewMockFactory()
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_STREAMS_DELETE, factory, t)

var args []string = []string{"streams", "delete", "--name", "mystream"}

// When...
err := commandCollection.Execute(args)

// Then...
assert.Nil(t, err)

// Check what the user saw was reasonable
checkOutput("", "", factory, t)

assert.Nil(t, err)
}
2 changes: 2 additions & 0 deletions pkg/errors/errorMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ var (
GALASA_ERROR_GET_STREAMS_UNPARSEABLE_CONTENT = NewMessageType("GAL1238E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1238, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_GET_STREAMS_SERVER_REPORTED_ERROR = NewMessageType("GAL1239E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1239, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_GET_STREAMS_EXPLANATION_NOT_JSON = NewMessageType("GAL1240E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1240, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_STREAMS_NOT_FOUND = NewMessageType("GAL1241E: The test stream could not be deleted by name because it was not found by the Galasa service. Try listing streams using 'galasactl streams get' to identify the one you wish to delete", 1241, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_FAILED_TO_DELETE_STREAM = NewMessageType("GAL1242E: Failed to test stream from database by stream name.", 1242, STACK_TRACE_NOT_WANTED)

// When getting multiple monitors...
GALASA_ERROR_GET_MONITORS_REQUEST_FAILED = NewMessageType("GAL1218E: Failed to get monitors. Sending the get request to the Galasa service failed. Cause is %v", 1218, STACK_TRACE_NOT_WANTED)
Expand Down
86 changes: 86 additions & 0 deletions pkg/streams/streamsDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/

package streams

import (
"context"
"log"
"net/http"

"github.com/galasa-dev/cli/pkg/embedded"
galasaErrors "github.com/galasa-dev/cli/pkg/errors"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/spi"
)

func DeleteStream(streamName string, apiClient *galasaapi.APIClient, byteReader spi.ByteReader) error {

streams, err := getStreamsFromRestApi(streamName, apiClient, byteReader)

if err == nil {

if len(streams) != 0 {
err = deleteStreamFromRestApi(streams[0], apiClient, byteReader)
} else {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_DELETE_STREAMS_NOT_FOUND)
}

}

return err

}

func deleteStreamFromRestApi(
stream galasaapi.Stream,
apiClient *galasaapi.APIClient,
byteReader spi.ByteReader,
) error {

var context context.Context = nil
var resp *http.Response

restApiVersion, err := embedded.GetGalasactlRestApiVersion()

if err == nil {

streamName := stream.Metadata.GetName()
apiCall := apiClient.StreamsAPIApi.DeleteStreamByName(context, streamName).ClientApiVersion(restApiVersion)
resp, err = apiCall.Execute()

if resp != nil {
defer resp.Body.Close()
}

if err != nil {

if resp == nil {

err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_FAILED_TO_DELETE_STREAM, err.Error())

} else {

err = galasaErrors.HttpResponseToGalasaError(
resp,
streamName,
byteReader,
galasaErrors.GALASA_ERROR_GET_STREAMS_NO_RESPONSE_CONTENT,
galasaErrors.GALASA_ERROR_GET_STREAMS_RESPONSE_BODY_UNREADABLE,
galasaErrors.GALASA_ERROR_GET_STREAMS_UNPARSEABLE_CONTENT,
galasaErrors.GALASA_ERROR_GET_STREAMS_SERVER_REPORTED_ERROR,
galasaErrors.GALASA_ERROR_GET_STREAMS_EXPLANATION_NOT_JSON,
)

}

log.Printf("Test stream with name '%s', was deleted OK.\n", streamName)
}
}

return err

}
Loading