Skip to content
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
100 changes: 84 additions & 16 deletions cmd/base/list_options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package base

import (
"log"
"fmt"
"strings"

serverscom "github.com/serverscom/serverscom-go-client/pkg"
Expand Down Expand Up @@ -36,13 +36,8 @@ func (o *BaseListOptions[T]) AddFlags(cmd *cobra.Command) {
flags.IntVar(&o.perPage, "per-page", 0, "Number of items per page")
flags.IntVar(&o.page, "page", 0, "Page number")
flags.StringVar(&o.sorting, "sorting", "", "Sort field")
flags.StringVar(&o.direction, "direction", "", "Sort direction (ASC or DESC)")
flags.StringVar(&o.direction, "direction", "", "Sort direction (asc, desc)")
flags.BoolVarP(&o.allPages, "all", "A", false, "Get all pages of resources")

flags.String("type", "", "")
if err := flags.MarkHidden("type"); err != nil {
log.Fatal(err)
}
}

// ApplyToCollection applies the options to a collection
Expand All @@ -67,6 +62,19 @@ func (o *BaseListOptions[T]) AllPages() bool {
return o.allPages
}

// HiddenTypeOption adds hidden type flag.
// Used in commands that determine type from sub command rather than user input
type HiddenTypeOption[T any] struct{}

func (o *HiddenTypeOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().String("type", "", "")
_ = cmd.Flags().MarkHidden("type")
}

func (o *HiddenTypeOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
// stub for compatibility with other options
}

// label selector option
type LabelSelectorOption[T any] struct {
labelSelector string
Expand Down Expand Up @@ -127,16 +135,16 @@ func (o *ClusterIDOption[T]) ApplyToCollection(collection serverscom.Collection[
}
}

// status option
type StatusOption[T any] struct {
// invoice status option
type InvoiceStatusOption[T any] struct {
status string
}

func (o *StatusOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.status, "status", "", "Filter results by status")
func (o *InvoiceStatusOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.status, "status", "", "Filter results by status (pending, outstanding, overdue, paid, canceled, reissued)")
}

func (o *StatusOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
func (o *InvoiceStatusOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.status != "" {
collection.SetParam("status", o.status)
}
Expand All @@ -149,7 +157,7 @@ type InvoiceTypeOption[T any] struct {

// use itype instead of type to avoid conflict with baseList 'type' hidden flag which we use for subcommands
func (o *InvoiceTypeOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.typeVal, "itype", "", "Filter results by type")
cmd.Flags().StringVar(&o.typeVal, "type", "", "Filter results by type (invoice, credit_note)")
}

func (o *InvoiceTypeOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
Expand Down Expand Up @@ -223,7 +231,7 @@ type FamilyOption[T any] struct {
}

func (o *FamilyOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.family, "family", "", "Set to 'ipv4' or 'ipv6'")
cmd.Flags().StringVar(&o.family, "family", "", "Filter results by IP family (ipv4, ipv6)")
}

func (o *FamilyOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
Expand All @@ -237,7 +245,7 @@ type InterfaceTypeOption[T any] struct {
}

func (o *InterfaceTypeOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.interfaceType, "interface-type", "", "Type of network interface: public, private, oob")
cmd.Flags().StringVar(&o.interfaceType, "interface-type", "", "Filter results by network interface type (public, private, oob)")
}

func (o *InterfaceTypeOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
Expand All @@ -251,7 +259,7 @@ type DistributionMethodOption[T any] struct {
}

func (o *DistributionMethodOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.distributionMethod, "distribution-method", "", "Distribution method: route or gateway")
cmd.Flags().StringVar(&o.distributionMethod, "distribution-method", "", "Filter results by distribution method (route, gateway)")
}

func (o *DistributionMethodOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
Expand All @@ -273,3 +281,63 @@ func (o *AdditionalOption[T]) ApplyToCollection(collection serverscom.Collection
collection.SetParam("additional", "true")
}
}

// redundancy option
type RedundancyOption[T any] struct {
redundancy bool
}

func (o *RedundancyOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&o.redundancy, "redundancy", false, "Filter uplinks by redundancy (true, false)")
}

func (o *RedundancyOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.redundancy {
collection.SetParam("redundancy", "true")
}
}

// uplink type option
type UplinkTypeOption[T any] struct {
uplinkType string
}

func (o *UplinkTypeOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.uplinkType, "type", "", "Filter uplinks by type (public, private)")
}

func (o *UplinkTypeOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.uplinkType != "" {
collection.SetParam("type", o.uplinkType)
}
}

// operating system id option
type OperatingSystemIDOption[T any] struct {
osID int64
}

func (o *OperatingSystemIDOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().Int64Var(&o.osID, "operating-system-id", 0, "Filter uplinks by operating system ID")
}

func (o *OperatingSystemIDOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.osID != 0 {
collection.SetParam("operating_system_id", fmt.Sprintf("%d", o.osID))
}
}

// bandwidth type option
type BandwidthTypeOption[T any] struct {
bandwidthType string
}

func (o *BandwidthTypeOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.bandwidthType, "type", "", `Filter bandwidth options by type (bytes, bandwidth, unmetered)`)
}

func (o *BandwidthTypeOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.bandwidthType != "" {
collection.SetParam("type", o.bandwidthType)
}
}
1 change: 1 addition & 0 deletions cmd/entities/hosts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func newListCmd(cmdContext *base.CmdContext, hostType *HostTypeCmd) *cobra.Comma

opts := base.NewListOptions(
&hostListOptions{},
&base.HiddenTypeOption[serverscom.Host]{},
&base.LabelSelectorOption[serverscom.Host]{},
&base.SearchPatternOption[serverscom.Host]{},
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/entities/invoices/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func newListCmd(cmdContext *base.CmdContext) *cobra.Command {

opts := base.NewListOptions(
&base.BaseListOptions[serverscom.InvoiceList]{},
&base.StatusOption[serverscom.InvoiceList]{},
&base.InvoiceStatusOption[serverscom.InvoiceList]{},
&base.InvoiceTypeOption[serverscom.InvoiceList]{},
&base.ParentIDOption[serverscom.InvoiceList]{},
&base.CurrencyOption[serverscom.InvoiceList]{},
Expand Down
1 change: 1 addition & 0 deletions cmd/entities/load_balancers/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func newListCmd(cmdContext *base.CmdContext, lbType *LBTypeCmd) *cobra.Command {

opts := base.NewListOptions(
&base.BaseListOptions[serverscom.LoadBalancer]{},
&base.HiddenTypeOption[serverscom.LoadBalancer]{},
&base.LabelSelectorOption[serverscom.LoadBalancer]{},
&base.SearchPatternOption[serverscom.LoadBalancer]{},
&base.LocationIDOption[serverscom.LoadBalancer]{},
Expand Down
1 change: 1 addition & 0 deletions cmd/entities/ssl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func newListCmd(cmdContext *base.CmdContext, sslType *SSLTypeCmd) *cobra.Command

opts := base.NewListOptions(
&base.BaseListOptions[serverscom.SSLCertificate]{},
&base.HiddenTypeOption[serverscom.SSLCertificate]{},
&base.LabelSelectorOption[serverscom.SSLCertificate]{},
&base.SearchPatternOption[serverscom.SSLCertificate]{},
)
Expand Down
57 changes: 57 additions & 0 deletions cmd/entities/uplinkbandwidths/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package uplinkbandwidths

import (
"fmt"
"strconv"

"github.com/serverscom/srvctl/cmd/base"
"github.com/spf13/cobra"
)

func newGetCmd(cmdContext *base.CmdContext) *cobra.Command {
var locationID int64
var serverModelID int64
var uplinkModelID int64

cmd := &cobra.Command{
Use: "get <id>",
Short: "Get a bandwidth option for an uplink model",
Long: "Get a bandwidth option for an uplink model by id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
manager := cmdContext.GetManager()

ctx, cancel := base.SetupContext(cmd, manager)
defer cancel()

base.SetupProxy(cmd, manager)

scClient := cmdContext.GetClient().SetVerbose(manager.GetVerbose(cmd)).GetScClient()

bandwidthID, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("bandwidth id should be integer")
}

model, err := scClient.Locations.GetBandwidthOption(ctx, locationID, serverModelID, uplinkModelID, int64(bandwidthID))
if err != nil {
return err
}

if model != nil {
formatter := cmdContext.GetOrCreateFormatter(cmd)
return formatter.Format(model)
}
return nil
},
}

cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location id (int, required)")
cmd.Flags().Int64Var(&serverModelID, "server-model-id", 0, "Server model id (int, required)")
cmd.Flags().Int64Var(&uplinkModelID, "uplink-model-id", 0, "Uplink model id (int, required)")
_ = cmd.MarkFlagRequired("location-id")
_ = cmd.MarkFlagRequired("server-model-id")
_ = cmd.MarkFlagRequired("uplink-model-id")

return cmd
}
34 changes: 34 additions & 0 deletions cmd/entities/uplinkbandwidths/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uplinkbandwidths

import (
serverscom "github.com/serverscom/serverscom-go-client/pkg"
"github.com/serverscom/srvctl/cmd/base"
"github.com/spf13/cobra"
)

func newListCmd(cmdContext *base.CmdContext) *cobra.Command {
var locationID int64
var serverModelID int64
var uplinkModelID int64

factory := func(verbose bool, args ...string) serverscom.Collection[serverscom.BandwidthOption] {
scClient := cmdContext.GetClient().SetVerbose(verbose).GetScClient()
return scClient.Locations.BandwidthOptions(locationID, serverModelID, uplinkModelID)
}

opts := base.NewListOptions(
&base.BaseListOptions[serverscom.BandwidthOption]{},
&base.BandwidthTypeOption[serverscom.BandwidthOption]{},
)

cmd := base.NewListCmd("list", "uplink-bandwidths", factory, cmdContext, opts...)

cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location ID (required)")
cmd.Flags().Int64Var(&serverModelID, "server-model-id", 0, "Server Model ID (required)")
cmd.Flags().Int64Var(&uplinkModelID, "uplink-model-id", 0, "Uplink Model ID (required)")
_ = cmd.MarkFlagRequired("location-id")
_ = cmd.MarkFlagRequired("server-model-id")
_ = cmd.MarkFlagRequired("uplink-model-id")

return cmd
}
38 changes: 38 additions & 0 deletions cmd/entities/uplinkbandwidths/uplinkbandwidths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package uplinkbandwidths

import (
"log"

serverscom "github.com/serverscom/serverscom-go-client/pkg"
"github.com/serverscom/srvctl/cmd/base"
"github.com/serverscom/srvctl/internal/output/entities"
"github.com/spf13/cobra"
)

func NewCmd(cmdContext *base.CmdContext) *cobra.Command {
uplinkEntity, err := entities.Registry.GetEntityFromValue(serverscom.BandwidthOption{})
if err != nil {
log.Fatal(err)
}
entitiesMap := make(map[string]entities.EntityInterface)
entitiesMap["uplink-bandwidths"] = uplinkEntity
cmd := &cobra.Command{
Use: "uplink-bandwidths",
Short: "Manage uplink bandwidths",
PersistentPreRunE: base.CombinePreRunE(
base.CheckFormatterFlags(cmdContext, entitiesMap),
base.CheckEmptyContexts(cmdContext),
),
Args: base.NoArgs,
Run: base.UsageRun,
}

cmd.AddCommand(
newListCmd(cmdContext),
newGetCmd(cmdContext),
)

base.AddFormatFlags(cmd)

return cmd
}
Loading
Loading