-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for creation eCloud virtual machine console sessions (#50)
Co-authored-by: Lee Spottiswood <lee.spottiswood@ukfast.co.uk>
- Loading branch information
Showing
7 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ecloud | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/pkg/browser" | ||
"github.com/ukfast/cli/internal/pkg/factory" | ||
"github.com/ukfast/cli/internal/pkg/output" | ||
"github.com/ukfast/sdk-go/pkg/service/ecloud" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func ecloudVirtualMachineConsoleRootCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "consolesession", | ||
Short: "sub-commands relating to virtual machine Consoles", | ||
} | ||
|
||
// Child commands | ||
cmd.AddCommand(ecloudVirtualMachineConsoleSessionCreateCmd(f)) | ||
|
||
return cmd | ||
} | ||
|
||
func ecloudVirtualMachineConsoleSessionCreateCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create <virtualmachine: id>", | ||
Short: "Creates a virtual machine console session", | ||
Long: "This command creates a virtual machine console session", | ||
Example: "ukfast ecloud vm consolesession create 123", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("Missing virtual machine") | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return ecloudVirtualMachineConsoleSessionCreate(f.NewClient().ECloudService(), cmd, args) | ||
}, | ||
} | ||
|
||
cmd.Flags().Bool("browser", false, "Indicates session should be opened in default browser") | ||
|
||
return cmd | ||
} | ||
|
||
func ecloudVirtualMachineConsoleSessionCreate(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { | ||
vmID, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
return fmt.Errorf("Invalid virtual machine ID [%s]", args[0]) | ||
} | ||
|
||
console, err := service.CreateVirtualMachineConsoleSession(vmID) | ||
if err != nil { | ||
return fmt.Errorf("Error creating virtual machine console session: %s", err) | ||
} | ||
|
||
openBrowser, _ := cmd.Flags().GetBool("browser") | ||
if openBrowser { | ||
return browser.OpenURL(console.URL) | ||
} | ||
|
||
return output.CommandOutput(cmd, OutputECloudConsoleSessionsProvider([]ecloud.ConsoleSession{console})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ecloud | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
gomock "github.com/golang/mock/gomock" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/ukfast/cli/test/mocks" | ||
"github.com/ukfast/sdk-go/pkg/service/ecloud" | ||
) | ||
|
||
func Test_ecloudVirtualMachineConsoleSessionCreateCmd_Args(t *testing.T) { | ||
t.Run("ValidArgs_NoError", func(t *testing.T) { | ||
err := ecloudVirtualMachineConsoleSessionCreateCmd(nil).Args(nil, []string{"123"}) | ||
|
||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("MissingVirtualMachine_Error", func(t *testing.T) { | ||
err := ecloudVirtualMachineConsoleSessionCreateCmd(nil).Args(nil, []string{}) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Missing virtual machine", err.Error()) | ||
}) | ||
} | ||
|
||
func Test_ecloudVirtualMachineConsoleSessionCreate(t *testing.T) { | ||
t.Run("CreateSuccess_NoError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
service.EXPECT().CreateVirtualMachineConsoleSession(123).Return(ecloud.ConsoleSession{}, nil).Times(1) | ||
|
||
err := ecloudVirtualMachineConsoleSessionCreate(service, &cobra.Command{}, []string{"123"}) | ||
|
||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("InvalidVirtualMachineID_ReturnsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
err := ecloudVirtualMachineConsoleSessionCreate(service, &cobra.Command{}, []string{"abc"}) | ||
|
||
assert.Equal(t, "Invalid virtual machine ID [abc]", err.Error()) | ||
}) | ||
|
||
t.Run("GetVirtualMachineConsoleSessionError_OutputsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
service.EXPECT().CreateVirtualMachineConsoleSession(123).Return(ecloud.ConsoleSession{}, errors.New("test error 1")).Times(1) | ||
|
||
err := ecloudVirtualMachineConsoleSessionCreate(service, &cobra.Command{}, []string{"123"}) | ||
|
||
assert.Equal(t, "Error creating virtual machine console session: test error 1", err.Error()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.