-
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 retrieving IOPS tiers (#168)
- Loading branch information
Showing
10 changed files
with
414 additions
and
3 deletions.
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
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,64 @@ | ||
package ecloud | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ans-group/cli/internal/pkg/factory" | ||
"github.com/ans-group/cli/internal/pkg/helper" | ||
"github.com/ans-group/cli/internal/pkg/output" | ||
"github.com/ans-group/sdk-go/pkg/service/ecloud" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func ecloudAvailabilityZoneIOPSRootCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "iops", | ||
Short: "sub-commands relating to availability zone IOPS tiers", | ||
Aliases: []string{ | ||
"az", | ||
}, | ||
} | ||
|
||
// Child commands | ||
cmd.AddCommand(ecloudAvailabilityZoneIOPSTierListCmd(f)) | ||
|
||
return cmd | ||
} | ||
|
||
func ecloudAvailabilityZoneIOPSTierListCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists availability zone IOPS tiers", | ||
Long: "This command lists availability zone IOPS tiers", | ||
Example: "ans ecloud availabilityzone iops list", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("Missing availability zone") | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: ecloudCobraRunEFunc(f, ecloudAvailabilityZoneIOPSTierList), | ||
} | ||
|
||
cmd.Flags().String("name", "", "IOPS name for filtering") | ||
|
||
return cmd | ||
} | ||
|
||
func ecloudAvailabilityZoneIOPSTierList(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { | ||
params, err := helper.GetAPIRequestParametersFromFlags(cmd, | ||
helper.NewStringFilterFlagOption("name", "name"), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
zones, err := service.GetAvailabilityZoneIOPSTiers(args[0], params) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving availability zones: %s", err) | ||
} | ||
|
||
return output.CommandOutput(cmd, OutputECloudIOPSTierProvider(zones)) | ||
} |
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" | ||
"testing" | ||
|
||
"github.com/ans-group/cli/internal/pkg/clierrors" | ||
"github.com/ans-group/cli/test/mocks" | ||
"github.com/ans-group/sdk-go/pkg/service/ecloud" | ||
gomock "github.com/golang/mock/gomock" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ecloudAvailabilityZoneIOPSTiersListCmd_Args(t *testing.T) { | ||
t.Run("ValidArgs_NoError", func(t *testing.T) { | ||
err := ecloudAvailabilityZoneIOPSTierListCmd(nil).Args(nil, []string{"fwp-abcdef12"}) | ||
|
||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("InvalidArgs_Error", func(t *testing.T) { | ||
err := ecloudAvailabilityZoneIOPSTierListCmd(nil).Args(nil, []string{}) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Missing availability zone", err.Error()) | ||
}) | ||
} | ||
|
||
func Test_ecloudAvailabilityZoneIOPSTiersList(t *testing.T) { | ||
t.Run("DefaultRetrieve", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
service.EXPECT().GetAvailabilityZoneIOPSTiers("fwp-abcdef12", gomock.Any()).Return([]ecloud.IOPSTier{}, nil).Times(1) | ||
|
||
ecloudAvailabilityZoneIOPSTierList(service, &cobra.Command{}, []string{"fwp-abcdef12"}) | ||
}) | ||
|
||
t.Run("MalformedFlag_ReturnsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
cmd := &cobra.Command{} | ||
cmd.Flags().StringArray("filter", []string{"invalidfilter"}, "") | ||
|
||
err := ecloudAvailabilityZoneIOPSTierList(service, cmd, []string{}) | ||
|
||
assert.IsType(t, &clierrors.ErrInvalidFlagValue{}, err) | ||
}) | ||
|
||
t.Run("GetAvailabilityZonesError_ReturnsError", func(t *testing.T) { | ||
|
||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
service.EXPECT().GetAvailabilityZoneIOPSTiers("fwp-abcdef12", gomock.Any()).Return([]ecloud.IOPSTier{}, errors.New("test error")).Times(1) | ||
|
||
err := ecloudAvailabilityZoneIOPSTierList(service, &cobra.Command{}, []string{"fwp-abcdef12"}) | ||
|
||
assert.Equal(t, "Error retrieving availability zone ports: test error", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package ecloud | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ans-group/cli/internal/pkg/factory" | ||
"github.com/ans-group/cli/internal/pkg/helper" | ||
"github.com/ans-group/cli/internal/pkg/output" | ||
"github.com/ans-group/sdk-go/pkg/service/ecloud" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func ecloudIOPSRootCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "iops", | ||
Short: "sub-commands relating to IOPS tiers", | ||
} | ||
|
||
// Child commands | ||
cmd.AddCommand(ecloudIOPSTierListCmd(f)) | ||
cmd.AddCommand(ecloudIOPSTierShowCmd(f)) | ||
|
||
return cmd | ||
} | ||
|
||
func ecloudIOPSTierListCmd(f factory.ClientFactory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists IOPS tiers", | ||
Long: "This command lists IOPS tiers", | ||
Example: "ans ecloud iops list", | ||
RunE: ecloudCobraRunEFunc(f, ecloudIOPSTierList), | ||
} | ||
|
||
cmd.Flags().String("name", "", "IOPS name for filtering") | ||
|
||
return cmd | ||
} | ||
|
||
func ecloudIOPSTierList(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { | ||
params, err := helper.GetAPIRequestParametersFromFlags(cmd, | ||
helper.NewStringFilterFlagOption("name", "name"), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tiers, err := service.GetIOPSTiers(params) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving IOPS tiers: %s", err) | ||
} | ||
|
||
return output.CommandOutput(cmd, OutputECloudIOPSTierProvider(tiers)) | ||
} | ||
|
||
func ecloudIOPSTierShowCmd(f factory.ClientFactory) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "show <iops: id>...", | ||
Short: "Shows an IOPS tier", | ||
Long: "This command shows one or more IOPS tiers", | ||
Example: "ans ecloud IOPS show iops-abcdef12", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("Missing IOPS tier") | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: ecloudCobraRunEFunc(f, ecloudIOPSTierShow), | ||
} | ||
} | ||
|
||
func ecloudIOPSTierShow(service ecloud.ECloudService, cmd *cobra.Command, args []string) error { | ||
var tiersList []ecloud.IOPSTier | ||
for _, arg := range args { | ||
tiers, err := service.GetIOPSTier(arg) | ||
if err != nil { | ||
output.OutputWithErrorLevelf("Error retrieving IOPS tier [%s]: %s", arg, err) | ||
continue | ||
} | ||
|
||
tiersList = append(tiersList, tiers) | ||
} | ||
|
||
return output.CommandOutput(cmd, OutputECloudIOPSTierProvider(tiersList)) | ||
} |
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,109 @@ | ||
package ecloud | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ans-group/cli/internal/pkg/clierrors" | ||
"github.com/ans-group/cli/test/mocks" | ||
"github.com/ans-group/cli/test/test_output" | ||
"github.com/ans-group/sdk-go/pkg/service/ecloud" | ||
gomock "github.com/golang/mock/gomock" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ecloudIOPSTierList(t *testing.T) { | ||
t.Run("DefaultRetrieve", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
service.EXPECT().GetIOPSTiers(gomock.Any()).Return([]ecloud.IOPSTier{}, nil).Times(1) | ||
|
||
ecloudIOPSTierList(service, &cobra.Command{}, []string{}) | ||
}) | ||
|
||
t.Run("MalformedFlag_ReturnsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
cmd := &cobra.Command{} | ||
cmd.Flags().StringArray("filter", []string{"invalidfilter"}, "") | ||
|
||
err := ecloudIOPSTierList(service, cmd, []string{}) | ||
|
||
assert.IsType(t, &clierrors.ErrInvalidFlagValue{}, err) | ||
}) | ||
|
||
t.Run("GetIOPSTiersError_ReturnsError", func(t *testing.T) { | ||
|
||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
service.EXPECT().GetIOPSTiers(gomock.Any()).Return([]ecloud.IOPSTier{}, errors.New("test error")).Times(1) | ||
|
||
err := ecloudIOPSTierList(service, &cobra.Command{}, []string{}) | ||
|
||
assert.Equal(t, "Error retrieving IOPS tiers: test error", err.Error()) | ||
}) | ||
} | ||
|
||
func Test_ecloudIOPSTierShowCmd_Args(t *testing.T) { | ||
t.Run("ValidArgs_NoError", func(t *testing.T) { | ||
err := ecloudIOPSTierShowCmd(nil).Args(nil, []string{"iops-abcdef12"}) | ||
|
||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("InvalidArgs_Error", func(t *testing.T) { | ||
err := ecloudIOPSTierShowCmd(nil).Args(nil, []string{}) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Missing IOPS", err.Error()) | ||
}) | ||
} | ||
|
||
func Test_ecloudIOPSTierShow(t *testing.T) { | ||
t.Run("SingleIOPS", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
service.EXPECT().GetIOPSTier("iops-abcdef12").Return(ecloud.IOPSTier{}, nil).Times(1) | ||
|
||
ecloudIOPSTierShow(service, &cobra.Command{}, []string{"iops-abcdef12"}) | ||
}) | ||
|
||
t.Run("MultipleIOPSs", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
gomock.InOrder( | ||
service.EXPECT().GetIOPSTier("iops-abcdef12").Return(ecloud.IOPSTier{}, nil), | ||
service.EXPECT().GetIOPSTier("iops-abcdef23").Return(ecloud.IOPSTier{}, nil), | ||
) | ||
|
||
ecloudIOPSTierShow(service, &cobra.Command{}, []string{"iops-abcdef12", "iops-abcdef23"}) | ||
}) | ||
|
||
t.Run("GetIOPSError_OutputsError", func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
service := mocks.NewMockECloudService(mockCtrl) | ||
|
||
service.EXPECT().GetIOPSTier("iops-abcdef12").Return(ecloud.IOPSTier{}, errors.New("test error")) | ||
|
||
test_output.AssertErrorOutput(t, "Error retrieving IOPS tier [iops-abcdef12]: test error\n", func() { | ||
ecloudIOPSTierShow(service, &cobra.Command{}, []string{"iops-abcdef12"}) | ||
}) | ||
}) | ||
} |
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
Oops, something went wrong.