Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(objectstorage): add command to list available regions #316

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add `gateway plans` command for listing gateway plans.
- Add `objectstorage regions` command for listing managed object storage regions.

### Changed

Expand Down
1 change: 1 addition & 0 deletions internal/commands/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
commands.BuildCommand(objectstorage.DeleteCommand(), objectStorageCommand.Cobra(), conf)
commands.BuildCommand(objectstorage.ListCommand(), objectStorageCommand.Cobra(), conf)
commands.BuildCommand(objectstorage.ShowCommand(), objectStorageCommand.Cobra(), conf)
commands.BuildCommand(objectstorage.RegionsCommand(), objectStorageCommand.Cobra(), conf)

// Network Gateway operations
gatewayCommand := commands.BuildCommand(gateway.BaseGatewayCommand(), rootCmd, conf)
Expand Down
60 changes: 60 additions & 0 deletions internal/commands/objectstorage/regions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package objectstorage

import (
"sort"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)

// RegionsCommand creates the "objectstorage regions" command
func RegionsCommand() commands.Command {
return &regionsCommand{
BaseCommand: commands.New("regions", "List objectstorage regions", "upctl objectstorage regions"),
}
}

type regionsCommand struct {
*commands.BaseCommand
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *regionsCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
regions, err := exec.All().GetManagedObjectStorageRegions(exec.Context(), &request.GetManagedObjectStorageRegionsRequest{})
if err != nil {
return nil, err
}

sort.Slice(regions, func(i, j int) bool {
return regions[i].Name < regions[j].Name
})

rows := []output.TableRow{}
for _, r := range regions {
zones := []string{}
for _, z := range r.Zones {
zones = append(zones, z.Name)
}
sort.Strings(zones)

rows = append(rows, output.TableRow{
r.Name,
r.PrimaryZone,
zones,
})
}

return output.MarshaledWithHumanOutput{
Value: regions,
Output: output.Table{
Columns: []output.TableColumn{
{Key: "name", Header: "Name"},
{Key: "primary_zone", Header: "Primary zone"},
{Key: "zones", Header: "Zones", Format: format.StringSliceSingleLineAnd},
},
Rows: rows,
},
}, nil
}
16 changes: 10 additions & 6 deletions internal/format/stringslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import (
)

func StringSliceOr(val interface{}) (text.Colors, string, error) {
return formatStringSlice(val, "or")
return formatStringSlice(val, "or", false)
}

func StringSliceAnd(val interface{}) (text.Colors, string, error) {
return formatStringSlice(val, "and")
return formatStringSlice(val, "and", false)
}

func formatStringSlice(val interface{}, andOrOr string) (text.Colors, string, error) {
func StringSliceSingleLineAnd(val interface{}) (text.Colors, string, error) {
return formatStringSlice(val, "and", true)
}

func formatStringSlice(val interface{}, andOrOr string, singleLine bool) (text.Colors, string, error) {
if val == nil {
return nil, "", nil
}
Expand All @@ -26,7 +30,7 @@ func formatStringSlice(val interface{}, andOrOr string) (text.Colors, string, er
}

if ifaceSliceVal, ok := toIfaceSlice(val); ok {
return nil, stringSliceString(ifaceSliceVal, andOrOr), nil
return nil, stringSliceString(ifaceSliceVal, andOrOr, singleLine), nil
}

return nil, fmt.Sprintf("%+v", val), nil
Expand Down Expand Up @@ -54,7 +58,7 @@ func maxStringLen(strings []string) int {
return max
}

func stringSliceString(values []interface{}, andOrOr string) string {
func stringSliceString(values []interface{}, andOrOr string, singleLine bool) string {
if len(values) == 0 {
return ""
}
Expand All @@ -69,7 +73,7 @@ func stringSliceString(values []interface{}, andOrOr string) string {
}

whitespace := " "
if maxStringLen(strs) > 15 || len(strs) > 3 {
if !singleLine && (maxStringLen(strs) > 15 || len(strs) > 3) {
whitespace = "\n"
}

Expand Down
Loading