Skip to content

Add Object Storage Tiers support #520

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 45 additions & 2 deletions cmd/objectstorage/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ func NewCmdObjectStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo
return fmt.Errorf("error parsing flag 'label' for object storage create : %v", errLa)
}

tierID, errTID := cmd.Flags().GetInt("tier-id")
if errTID != nil {
return fmt.Errorf("error parsing flag 'tier-id' for object storage create : %v", errTID)
}

o.ClusterID = clusterID
o.Label = label
o.TierID = tierID

os, err := o.create()
if err != nil {
Expand All @@ -118,6 +124,7 @@ func NewCmdObjectStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo

create.Flags().StringP("label", "l", "", "label you want your object storage to have")
create.Flags().IntP("cluster-id", "i", 0, "ID of the cluster in which to create the object storage")
create.Flags().IntP("tier-id", "t", 1, "Tier ID used to create the object storage tiers")
if err := create.MarkFlagRequired("cluster-id"); err != nil {
printer.Error(fmt.Errorf("error marking object storage create 'cluster-id' flag required : %v", err))
os.Exit(1)
Expand Down Expand Up @@ -222,6 +229,26 @@ func NewCmdObjectStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo
},
}

// List Tiers
listTiers := &cobra.Command{
Use: "list-tiers",
Short: "Retrieve a list of all available object storage tiers",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
o.Base.Options = utils.GetPaging(cmd)

tiers, err := o.listTiers()
if err != nil {
return fmt.Errorf("error retrieving object storage tier list : %v", err)
}

data := &ObjectStorageTiersPrinter{Tiers: tiers}
o.Base.Printer.Display(data, nil)

return nil
},
}

cmd.AddCommand(
list,
get,
Expand All @@ -230,6 +257,7 @@ func NewCmdObjectStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo
del,
regenerateKeys,
listClusters,
listTiers,
)

return cmd
Expand All @@ -238,6 +266,7 @@ func NewCmdObjectStorage(base *cli.Base) *cobra.Command { //nolint:gocyclo
type options struct {
Base *cli.Base
ClusterID int
TierID int
Label string
}

Expand All @@ -252,12 +281,21 @@ func (o *options) get() (*govultr.ObjectStorage, error) {
}

func (o *options) create() (*govultr.ObjectStorage, error) {
os, _, err := o.Base.Client.ObjectStorage.Create(o.Base.Context, o.ClusterID, o.Label)
OSreq := &govultr.ObjectStorageReq{
ClusterID: o.ClusterID,
TierID: o.TierID,
Label: o.Label,
}
os, _, err := o.Base.Client.ObjectStorage.Create(o.Base.Context, OSreq)
return os, err
}

func (o *options) update() error {
return o.Base.Client.ObjectStorage.Update(o.Base.Context, o.Base.Args[0], o.Label)
OSreq := &govultr.ObjectStorageReq{
Label: o.Label,
}

return o.Base.Client.ObjectStorage.Update(o.Base.Context, o.Base.Args[0], OSreq)
}

func (o *options) del() error {
Expand All @@ -269,6 +307,11 @@ func (o *options) listClusters() ([]govultr.ObjectStorageCluster, *govultr.Meta,
return clusters, meta, err
}

func (o *options) listTiers() ([]govultr.ObjectStorageTier, error) {
tiers, _, err := o.Base.Client.ObjectStorage.ListTiers(o.Base.Context)
return tiers, err
}

func (o *options) regenerateKeys() (*govultr.S3Keys, error) {
keys, _, err := o.Base.Client.ObjectStorage.RegenerateKeys(o.Base.Context, o.Base.Args[0])
return keys, err
Expand Down
67 changes: 67 additions & 0 deletions cmd/objectstorage/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package objectstorage

import (
"strconv"
"strings"

"github.com/vultr/govultr/v3"
"github.com/vultr/vultr-cli/v3/cmd/printer"
Expand Down Expand Up @@ -210,3 +211,69 @@ func (o *ObjectStorageKeysPrinter) Data() [][]string {
func (o *ObjectStorageKeysPrinter) Paging() [][]string {
return nil
}

// ======================================

// ObjectStorageTiersPrinter ...
type ObjectStorageTiersPrinter struct {
Tiers []govultr.ObjectStorageTier `json:"tiers"`
}

// JSON ...
func (o *ObjectStorageTiersPrinter) JSON() []byte {
return printer.MarshalObject(o, "json")
}

// YAML ...
func (o *ObjectStorageTiersPrinter) YAML() []byte {
return printer.MarshalObject(o, "yaml")
}

// Columns ...
func (o *ObjectStorageTiersPrinter) Columns() [][]string {
return [][]string{0: {
"ID",
"NAME",
"PRICE",
"PRICE BANDWIDTH GB",
"PRICE DISK GB",
"RATE LIMIT BYTES/SEC",
"RATE LIMIT OPS/SEC",
"AVAILABLE REGIONS",
}}
}

// Data ...
func (o *ObjectStorageTiersPrinter) Data() [][]string {
if len(o.Tiers) == 0 {
return [][]string{{
"---", "---", "---", "---", "---", "---", "---", "---",
}}
}

var data [][]string
for _, tier := range o.Tiers {
var locations []string
for _, clusterID := range tier.Locations {
locations = append(locations, clusterID.Region)
}

data = append(data, []string{
strconv.Itoa(tier.ID),
tier.Name,
strconv.FormatFloat(float64(tier.Price), 'f', 2, 32),
strconv.FormatFloat(float64(tier.PriceBandwidthGB), 'f', 2, 32),
strconv.FormatFloat(float64(tier.PriceDiskGB), 'f', 2, 32),
strconv.Itoa(tier.RateLimitBytesSec),
strconv.Itoa(tier.RateLimitOpsSec),
strings.Join(locations, ", "),
})
}

return data
}

// Paging ...
func (o *ObjectStorageTiersPrinter) Paging() [][]string {
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.24
require (
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.19.0
github.com/vultr/govultr/v3 v3.15.0
github.com/vultr/govultr/v3 v3.16.1
golang.org/x/oauth2 v0.27.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/vultr/govultr/v3 v3.15.0 h1:hvc5MqKr9FSYFdK0pAAq8ta/JvQiuYPv6x3LKuDUPDc=
github.com/vultr/govultr/v3 v3.15.0/go.mod h1:WO5RKj6HvR4KNXsnCW1+28ak2UofTNhFC5ytaS/kf78=
github.com/vultr/govultr/v3 v3.16.1 h1:fADa6GDVg+kphNjog05gCxKz6CmMZ7zSourX0e2ezyw=
github.com/vultr/govultr/v3 v3.16.1/go.mod h1:q34Wd76upKmf+vxFMgaNMH3A8BbsPBmSYZUGC8oZa5w=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
Expand Down
Loading