Skip to content

Commit

Permalink
Merge branch 'master' of github.com:exoscale/cli into fix-sks-tain
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]>
  • Loading branch information
pierre-emmanuelJ committed May 8, 2024
2 parents 1360ab9 + d971643 commit 95cc961
Show file tree
Hide file tree
Showing 41 changed files with 829 additions and 2,476 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
### Bug Fixes
- SKS: Fix nodepool taints format parsing #600

## 1.78.0

### Features

- blockstorage: implement updating volume and snapshot labels and names #601

## 1.77.2

### Improvements
Expand Down
6 changes: 3 additions & 3 deletions bucket/exoscale-cli.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"version": "1.77.2",
"version": "1.78.0",
"architecture": {
"64bit": {
"url": "https://github.com/exoscale/cli/releases/download/v1.77.2/exoscale-cli_1.77.2_windows_amd64.zip",
"url": "https://github.com/exoscale/cli/releases/download/v1.78.0/exoscale-cli_1.78.0_windows_amd64.zip",
"bin": [
"exo.exe"
],
"hash": "d224a5a58e11845dfcb2c9d12a9bc9f89b0477eb968426b66bb941a4f76c20d1"
"hash": "073f9f1c689288d0a897bbe49c06f1d5a49efea42401e6c4a45aea46a8d6d07f"
}
},
"homepage": "https://github.com/exoscale/cli",
Expand Down
8 changes: 5 additions & 3 deletions cmd/blockstorage_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type blockStorageCreateCmd struct {

Name string `cli-arg:"#" cli-usage:"NAME"`

Size int64 `cli-usage:"block storage volume size (default: 10)"`
Snapshot string `cli-usage:"block storage volume snapshot NAME|ID"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"block storage zone"`
Size int64 `cli-usage:"block storage volume size (default: 10)"`
Labels map[string]string `cli-flag:"label" cli-usage:"block storage volume labels (format: key=value)"`
Snapshot string `cli-usage:"block storage volume snapshot NAME|ID"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"block storage zone"`
}

func (c *blockStorageCreateCmd) cmdAliases() []string { return gCreateAlias }
Expand Down Expand Up @@ -65,6 +66,7 @@ func (c *blockStorageCreateCmd) cmdRun(_ *cobra.Command, _ []string) error {
req := v3.CreateBlockStorageVolumeRequest{
Name: c.Name,
Size: c.Size,
Labels: c.Labels,
BlockStorageSnapshot: snapshot,
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/blockstorage_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type blockStorageShowOutput struct {
Instance *v3.InstanceTarget `json:"instance"`
Name string `json:"name"`
Size string `json:"size"`
Labels map[string]string `json:"labels"`
State v3.BlockStorageVolumeState `json:"state"`
}

Expand Down Expand Up @@ -82,6 +83,7 @@ func (c *blockStorageShowCmd) cmdRun(cmd *cobra.Command, _ []string) error {
State: volume.State,
Instance: volume.Instance,
BlockStorageSnapshots: volume.BlockStorageSnapshots,
Labels: volume.Labels,
}, nil)
}

Expand Down
10 changes: 6 additions & 4 deletions cmd/blockstorage_snapshot_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ type blockStorageSnapshotCreateCmd struct {

_ bool `cli-cmd:"create"`

Volume string `cli-arg:"#" cli-usage:"<volume NAME|ID>"`
Name string `cli-flag:"name" cli-usage:"block storage volume snapshot name"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"block storage volume snapshot zone"`
Volume string `cli-arg:"#" cli-usage:"<volume NAME|ID>"`
Name string `cli-flag:"name" cli-usage:"block storage volume snapshot name"`
Labels map[string]string `cli-flag:"label" cli-usage:"block storage volume snapshot labels (format: key=value)"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"block storage volume snapshot zone"`
}

func (c *blockStorageSnapshotCreateCmd) cmdAliases() []string { return gCreateAlias }
Expand Down Expand Up @@ -58,7 +59,8 @@ func (c *blockStorageSnapshotCreateCmd) cmdRun(_ *cobra.Command, _ []string) err

op, err := client.CreateBlockStorageSnapshot(ctx, volume.ID,
v3.CreateBlockStorageSnapshotRequest{
Name: c.Name,
Name: c.Name,
Labels: c.Labels,
},
)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/blockstorage_snapshot_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type blockStorageSnapshotShowOutput struct {
Name string `json:"name"`
Size string `json:"size"`
State v3.BlockStorageSnapshotState `json:"state"`
Labels map[string]string `json:"labels"`
}

func (o *blockStorageSnapshotShowOutput) Type() string { return "Block Storage Volume Snapshot" }
Expand Down Expand Up @@ -74,6 +75,7 @@ func (c *blockStorageSnapshotShowCmd) cmdRun(cmd *cobra.Command, _ []string) err
CreatedAT: snapshot.CreatedAT,
State: snapshot.State,
Volume: snapshot.BlockStorageVolume,
Labels: snapshot.Labels,
}, nil)
}

Expand Down
104 changes: 104 additions & 0 deletions cmd/blockstorage_snapshot_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
v3 "github.com/exoscale/egoscale/v3"
)

type blockStorageSnapshotUpdateCmd struct {
cliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"update"`

Name string `cli-arg:"#" cli-usage:"NAME|ID"`
Labels map[string]string `cli-flag:"label" cli-usage:"block storage volume snapshot label (format: key=value), clearing the labels is possible by passing [=]"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"block storage volume snapshot zone"`
Rename string `cli-usage:"rename block storage volume snapshot"`
}

func (c *blockStorageSnapshotUpdateCmd) cmdAliases() []string { return []string{"up"} }

func (c *blockStorageSnapshotUpdateCmd) cmdShort() string {
return "Update a Block Storage Volume Snapshot"
}

func (c *blockStorageSnapshotUpdateCmd) cmdLong() string {
return fmt.Sprintf(`This command updates a Block Storage Volume Snapshot.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&blockStorageShowOutput{}), ", "))
}

func (c *blockStorageSnapshotUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}

func (c *blockStorageSnapshotUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
ctx := gContext
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, c.Zone)
if err != nil {
return err
}

snapshots, err := client.ListBlockStorageSnapshots(ctx)
if err != nil {
return err
}

snapshot, err := snapshots.FindBlockStorageSnapshot(c.Name)
if err != nil {
return err
}

var updated bool
updateReq := v3.UpdateBlockStorageSnapshotRequest{}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Labels)) {
updateReq.Labels = convertIfSpecialEmptyMap(c.Labels)

updated = true
}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Rename)) {
updateReq.Name = &c.Rename

updated = true
}

if updated {
op, err := client.UpdateBlockStorageSnapshot(ctx, snapshot.ID, updateReq)
if err != nil {
return err
}

_, err = client.Wait(ctx, op, v3.OperationStateSuccess)
if err != nil {
return err
}
}

if updated && !globalstate.Quiet {
name := c.Name
if c.Rename != "" {
name = c.Rename
}
return (&blockStorageSnapshotShowCmd{
cliCommandSettings: c.cliCommandSettings,
Name: name,
}).cmdRun(nil, nil)
}

return nil
}

func init() {
cobra.CheckErr(registerCLICommand(blockstorageSnapshotCmd, &blockStorageSnapshotUpdateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}
63 changes: 45 additions & 18 deletions cmd/blockstorage_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type blockStorageUpdateCmd struct {

_ bool `cli-cmd:"update"`

Name string `cli-arg:"#" cli-usage:"NAME|ID"`
Size int64 `cli-usage:"block storage volume size"`
// TODO(pej): Re-enable it when API is up to date on this call.
// Labels map[string]string `cli-flag:"label" cli-usage:"block storage volume label (format: key=value)"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"block storage volume zone"`
Name string `cli-arg:"#" cli-usage:"NAME|ID"`
Size int64 `cli-usage:"block storage volume size"`
Labels map[string]string `cli-flag:"label" cli-usage:"block storage volume label (format: key=value), clearing the labels is possible by passing [=]"`
Zone v3.ZoneName `cli-short:"z" cli-usage:"block storage volume zone"`
Rename string `cli-usage:"rename block storage volume"`
}

func (c *blockStorageUpdateCmd) cmdAliases() []string { return []string{"up"} }
Expand All @@ -39,7 +39,7 @@ func (c *blockStorageUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) err
return cliCommandDefaultPreRun(c, cmd, args)
}

func (c *blockStorageUpdateCmd) cmdRun(_ *cobra.Command, _ []string) error {
func (c *blockStorageUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
ctx := gContext
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, c.Zone)
if err != nil {
Expand All @@ -56,13 +56,10 @@ func (c *blockStorageUpdateCmd) cmdRun(_ *cobra.Command, _ []string) error {
return err
}

if c.Size == 0 {
return nil
}
var resized bool

var updated bool
decorateAsyncOperation(fmt.Sprintf("Updating block storage volume %q...", c.Name), func() {
if c.Size != 0 {
if c.Size > 0 {
decorateAsyncOperation(fmt.Sprintf("Updating block storage volume %q...", c.Name), func() {
_, err = client.ResizeBlockStorageVolume(ctx, volume.ID,
v3.ResizeBlockStorageVolumeRequest{
Size: c.Size,
Expand All @@ -71,17 +68,47 @@ func (c *blockStorageUpdateCmd) cmdRun(_ *cobra.Command, _ []string) error {
if err != nil {
return
}
updated = true
resized = true
})
if err != nil {
return err
}
})
if err != nil {
return err
}

if updated && !globalstate.Quiet {
var updated bool
updateReq := v3.UpdateBlockStorageVolumeRequest{}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Labels)) {
updateReq.Labels = convertIfSpecialEmptyMap(c.Labels)

updated = true
}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Rename)) {
updateReq.Name = &c.Rename

updated = true
}

if updated {
op, err := client.UpdateBlockStorageVolume(ctx, volume.ID, updateReq)
if err != nil {
return err
}

_, err = client.Wait(ctx, op, v3.OperationStateSuccess)
if err != nil {
return err
}
}

if (resized || updated) && !globalstate.Quiet {
name := c.Name
if c.Rename != "" {
name = c.Rename
}
return (&blockStorageShowCmd{
cliCommandSettings: c.cliCommandSettings,
Name: c.Name,
Name: name,
}).cmdRun(nil, nil)
}

Expand Down
16 changes: 16 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ func cliCommandFlagName(c cliCommand, field interface{}) (string, error) {
return "", fmt.Errorf("field not found in struct %s", cv.Type())
}

func convertIfSpecialEmptyMap(m map[string]string) map[string]string {
// since it is not possible to pass an empty map
// with a spf13/pflag https://github.com/spf13/pflag/issues/312
// we use the special value of a map with only
// one empty string key and the empty string value
// as the "empty map"
// this allows users to clear the labels of
// a resource by passing "--label=[=]"
v, ok := m[""]
if ok && v == "" {
return map[string]string{}
}

return m
}

func mustCLICommandFlagName(c cliCommand, field interface{}) string {
v, err := cliCommandFlagName(c, field)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/sks_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *sksUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Description)) {
updateReq.Description = c.Description
updateReq.Description = &c.Description
updated = true
}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.2.0
github.com/aws/smithy-go v1.1.0
github.com/dustin/go-humanize v1.0.1
github.com/exoscale/egoscale v0.102.4-0.20240411151757-e59fc2d046ea
github.com/exoscale/egoscale v0.102.4-0.20240506093113-3ae83713b097
github.com/exoscale/openapi-cli-generator v1.1.0
github.com/fatih/camelcase v1.0.0
github.com/google/uuid v1.4.0
Expand Down Expand Up @@ -148,12 +148,12 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/tools v0.18.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading

0 comments on commit 95cc961

Please sign in to comment.