Skip to content

Commit

Permalink
feat(objectstorage): add support for managed object storage names (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta authored Apr 4, 2024
1 parent a751f9b commit db3d235
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add managed object storage name to `show` and `list` command outputs.
- Add completions for managed object storages and allow using managed object storage name (in addition to its UUID) as a positional argument.

## [3.6.0] - 2024-03-07

### Added
Expand Down
5 changes: 5 additions & 0 deletions internal/commands/objectstorage/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
Expand All @@ -24,6 +26,9 @@ func DeleteCommand() commands.Command {

type deleteCommand struct {
*commands.BaseCommand
resolver.CachingObjectStorage
completion.ObjectStorage

deleteUsers config.OptionalBoolean
deletePolicies config.OptionalBoolean
}
Expand Down
2 changes: 2 additions & 0 deletions internal/commands/objectstorage/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (c *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
for _, objectstorage := range objectstorages {
rows = append(rows, output.TableRow{
objectstorage.UUID,
objectstorage.Name,
objectstorage.Region,
objectstorage.ConfiguredStatus,
objectstorage.OperationalState,
Expand All @@ -43,6 +44,7 @@ func (c *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
Output: output.Table{
Columns: []output.TableColumn{
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
{Key: "name", Header: "Name"},
{Key: "region", Header: "Region"},
{Key: "configured_status", Header: "Configured status", Format: format.ObjectStorageConfiguredStatus},
{Key: "operational_state", Header: "Operational state", Format: format.ObjectStorageOperationalState},
Expand Down
5 changes: 5 additions & 0 deletions internal/commands/objectstorage/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package objectstorage

import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/labels"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)
Expand All @@ -22,6 +24,8 @@ func ShowCommand() commands.Command {

type showCommand struct {
*commands.BaseCommand
resolver.CachingObjectStorage
completion.ObjectStorage
}

// Execute implements commands.MultipleArgumentCommand
Expand Down Expand Up @@ -119,6 +123,7 @@ func (c *showCommand) Execute(exec commands.Executor, uuid string) (output.Outpu
Title: "Overview:",
Rows: []output.DetailRow{
{Title: "UUID:", Value: objectStorage.UUID, Colour: ui.DefaultUUUIDColours},
{Title: "Name:", Value: objectStorage.Name},
{Title: "Region:", Value: objectStorage.Region},
{Title: "Configured status:", Value: objectStorage.ConfiguredStatus, Format: format.ObjectStorageConfiguredStatus},
{Title: "Operational state:", Value: objectStorage.OperationalState, Format: format.ObjectStorageOperationalState},
Expand Down
29 changes: 29 additions & 0 deletions internal/completion/objectstorage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package completion

import (
"context"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/cobra"
)

// ObjectStorage implements argument completion for gateways, by uuid or name.
type ObjectStorage struct{}

// make sure ObjectStorage implements the interface
var _ Provider = ObjectStorage{}

// CompleteArgument implements completion.Provider
func (s ObjectStorage) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
objectstorages, err := svc.GetManagedObjectStorages(ctx, &request.GetManagedObjectStoragesRequest{})
if err != nil {
return None(toComplete)
}
var vals []string
for _, objsto := range objectstorages {
vals = append(vals, objsto.UUID, objsto.Name)
}

return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
}
42 changes: 42 additions & 0 deletions internal/resolver/objectstorage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package resolver

import (
"context"

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

// CachingObjectStorage implements resolver for ObjectStorages, caching the results
type CachingObjectStorage struct{}

// make sure we implement the ResolutionProvider interface
var _ ResolutionProvider = CachingObjectStorage{}

// Get implements ResolutionProvider.Get
func (s CachingObjectStorage) Get(ctx context.Context, svc internal.AllServices) (Resolver, error) {
objectstorages, err := svc.GetManagedObjectStorages(ctx, &request.GetManagedObjectStoragesRequest{})
if err != nil {
return nil, err
}
return func(arg string) (uuid string, err error) {
rv := ""
for _, objsto := range objectstorages {
if MatchArgWithWhitespace(arg, objsto.Name) || objsto.UUID == arg {
if rv != "" {
return "", AmbiguousResolutionError(arg)
}
rv = objsto.UUID
}
}
if rv != "" {
return rv, nil
}
return "", NotFoundError(arg)
}, nil
}

// PositionalArgumentHelp implements resolver.ResolutionProvider
func (s CachingObjectStorage) PositionalArgumentHelp() string {
return helpUUIDTitle
}

0 comments on commit db3d235

Please sign in to comment.