Skip to content

Commit db3d235

Browse files
authored
feat(objectstorage): add support for managed object storage names (#299)
1 parent a751f9b commit db3d235

6 files changed

Lines changed: 88 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add managed object storage name to `show` and `list` command outputs.
13+
- Add completions for managed object storages and allow using managed object storage name (in addition to its UUID) as a positional argument.
14+
1015
## [3.6.0] - 2024-03-07
1116

1217
### Added

internal/commands/objectstorage/delete.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"fmt"
55

66
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
78
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
89
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
10+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
911

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

2527
type deleteCommand struct {
2628
*commands.BaseCommand
29+
resolver.CachingObjectStorage
30+
completion.ObjectStorage
31+
2732
deleteUsers config.OptionalBoolean
2833
deletePolicies config.OptionalBoolean
2934
}

internal/commands/objectstorage/list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func (c *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
3232
for _, objectstorage := range objectstorages {
3333
rows = append(rows, output.TableRow{
3434
objectstorage.UUID,
35+
objectstorage.Name,
3536
objectstorage.Region,
3637
objectstorage.ConfiguredStatus,
3738
objectstorage.OperationalState,
@@ -43,6 +44,7 @@ func (c *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Ou
4344
Output: output.Table{
4445
Columns: []output.TableColumn{
4546
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
47+
{Key: "name", Header: "Name"},
4648
{Key: "region", Header: "Region"},
4749
{Key: "configured_status", Header: "Configured status", Format: format.ObjectStorageConfiguredStatus},
4850
{Key: "operational_state", Header: "Operational state", Format: format.ObjectStorageOperationalState},

internal/commands/objectstorage/show.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package objectstorage
22

33
import (
44
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
5+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
56
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
67
"github.com/UpCloudLtd/upcloud-cli/v3/internal/labels"
78
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
9+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
810
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
911
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
1012
)
@@ -22,6 +24,8 @@ func ShowCommand() commands.Command {
2224

2325
type showCommand struct {
2426
*commands.BaseCommand
27+
resolver.CachingObjectStorage
28+
completion.ObjectStorage
2529
}
2630

2731
// Execute implements commands.MultipleArgumentCommand
@@ -119,6 +123,7 @@ func (c *showCommand) Execute(exec commands.Executor, uuid string) (output.Outpu
119123
Title: "Overview:",
120124
Rows: []output.DetailRow{
121125
{Title: "UUID:", Value: objectStorage.UUID, Colour: ui.DefaultUUUIDColours},
126+
{Title: "Name:", Value: objectStorage.Name},
122127
{Title: "Region:", Value: objectStorage.Region},
123128
{Title: "Configured status:", Value: objectStorage.ConfiguredStatus, Format: format.ObjectStorageConfiguredStatus},
124129
{Title: "Operational state:", Value: objectStorage.OperationalState, Format: format.ObjectStorageOperationalState},
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package completion
2+
3+
import (
4+
"context"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
7+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// ObjectStorage implements argument completion for gateways, by uuid or name.
12+
type ObjectStorage struct{}
13+
14+
// make sure ObjectStorage implements the interface
15+
var _ Provider = ObjectStorage{}
16+
17+
// CompleteArgument implements completion.Provider
18+
func (s ObjectStorage) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
19+
objectstorages, err := svc.GetManagedObjectStorages(ctx, &request.GetManagedObjectStoragesRequest{})
20+
if err != nil {
21+
return None(toComplete)
22+
}
23+
var vals []string
24+
for _, objsto := range objectstorages {
25+
vals = append(vals, objsto.UUID, objsto.Name)
26+
}
27+
28+
return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
29+
}

internal/resolver/objectstorage.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package resolver
2+
3+
import (
4+
"context"
5+
6+
internal "github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
7+
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
8+
)
9+
10+
// CachingObjectStorage implements resolver for ObjectStorages, caching the results
11+
type CachingObjectStorage struct{}
12+
13+
// make sure we implement the ResolutionProvider interface
14+
var _ ResolutionProvider = CachingObjectStorage{}
15+
16+
// Get implements ResolutionProvider.Get
17+
func (s CachingObjectStorage) Get(ctx context.Context, svc internal.AllServices) (Resolver, error) {
18+
objectstorages, err := svc.GetManagedObjectStorages(ctx, &request.GetManagedObjectStoragesRequest{})
19+
if err != nil {
20+
return nil, err
21+
}
22+
return func(arg string) (uuid string, err error) {
23+
rv := ""
24+
for _, objsto := range objectstorages {
25+
if MatchArgWithWhitespace(arg, objsto.Name) || objsto.UUID == arg {
26+
if rv != "" {
27+
return "", AmbiguousResolutionError(arg)
28+
}
29+
rv = objsto.UUID
30+
}
31+
}
32+
if rv != "" {
33+
return rv, nil
34+
}
35+
return "", NotFoundError(arg)
36+
}, nil
37+
}
38+
39+
// PositionalArgumentHelp implements resolver.ResolutionProvider
40+
func (s CachingObjectStorage) PositionalArgumentHelp() string {
41+
return helpUUIDTitle
42+
}

0 commit comments

Comments
 (0)