Skip to content

Commit

Permalink
add support for creation eCloud virtual machine console sessions (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: Lee Spottiswood <lee.spottiswood@ukfast.co.uk>
  • Loading branch information
0x4c6565 and 0x4c6565 committed Apr 3, 2020
1 parent 940c7c1 commit 9be9ad8
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/ecloud/ecloud_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func ecloudVirtualMachineRootCmd(f factory.ClientFactory) *cobra.Command {
cmd.AddCommand(ecloudVirtualMachineTagRootCmd(f))
cmd.AddCommand(ecloudVirtualMachineDiskRootCmd(f))
cmd.AddCommand(ecloudVirtualMachineTemplateRootCmd(f))
cmd.AddCommand(ecloudVirtualMachineConsoleRootCmd(f))

return cmd
}
Expand Down
68 changes: 68 additions & 0 deletions cmd/ecloud/ecloud_vm_consolesession.go
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}))
}
66 changes: 66 additions & 0 deletions cmd/ecloud/ecloud_vm_consolesession_test.go
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())
})
}
17 changes: 17 additions & 0 deletions cmd/ecloud/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,20 @@ func OutputECloudApplianceParametersProvider(parameters []ecloud.ApplianceParame
}),
)
}

func OutputECloudConsoleSessionsProvider(sessions []ecloud.ConsoleSession) output.OutputHandlerProvider {
return output.NewGenericOutputHandlerProvider(
output.WithData(sessions),
output.WithFieldDataFunc(func() ([]*output.OrderedFields, error) {
var data []*output.OrderedFields
for _, session := range sessions {
fields := output.NewOrderedFields()
fields.Set("url", output.NewFieldValue(session.URL, true))

data = append(data, fields)
}

return data, nil
}),
)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ require (
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.1
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/rhysd/go-github-selfupdate v1.1.0
github.com/ryanuber/go-glob v1.0.0
github.com/spf13/afero v1.2.1
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.3.2
github.com/stretchr/testify v1.3.0
github.com/ukfast/sdk-go v1.3.4
github.com/ukfast/sdk-go v1.3.7
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b // indirect
gopkg.in/go-playground/assert.v1 v1.2.1
)
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rhysd/go-github-selfupdate v1.1.0 h1:+aMomy69YCYxJ6kr13nYIgAJWSB1kHK5M5YpbmjQkWo=
Expand Down Expand Up @@ -93,6 +95,10 @@ github.com/ukfast/go-durationstring v1.0.0 h1:kgPuA7XjLjgLDfkG8j0MpolxcZh/eMdiVo
github.com/ukfast/go-durationstring v1.0.0/go.mod h1:Ci81n51kfxlKUIaLY9cINIKRO94VTqV+iCGbOMTb0V8=
github.com/ukfast/sdk-go v1.3.4 h1:3L+wBtJ4Gk0hie9BzWSeFhQ8cyCRJzwCHDVh88LSbRA=
github.com/ukfast/sdk-go v1.3.4/go.mod h1:VHQi+BbNxVKPu131MHaMFe0UrMoFWbe4LjUqSCt64yg=
github.com/ukfast/sdk-go v1.3.6 h1:T1wQiy3Us4zbJWJrHCPk7TQN6uL1VruU7AJ3/0aSmkQ=
github.com/ukfast/sdk-go v1.3.6/go.mod h1:VHQi+BbNxVKPu131MHaMFe0UrMoFWbe4LjUqSCt64yg=
github.com/ukfast/sdk-go v1.3.7 h1:iY08lE8WcnwI7ZGcLRYUW6JWWf0aX20a+VZ0fm9tsZg=
github.com/ukfast/sdk-go v1.3.7/go.mod h1:VHQi+BbNxVKPu131MHaMFe0UrMoFWbe4LjUqSCt64yg=
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
Expand Down
30 changes: 30 additions & 0 deletions test/mocks/mock_ecloudservice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9be9ad8

Please sign in to comment.