Skip to content

Commit

Permalink
fix: Add --location for Regional APIs. Fix API-queried regional compl…
Browse files Browse the repository at this point in the history
…etions (#468)

* change: remove old functionality

* change: remove old GetServerURL workarounds

* feat: withRegionalFlags decorator

* feat: withCompletions option

* change: remove old dns completer workarounds

* revert: don't apply the new funcs yet

* add comments, remove print statements

* change: allow users to force a location for completions

* WithRegionalFlags allow force custom location but warn

* replace: dns zone completions

* replace: dns record completions

* clouadpi ipblock completion in a dns command

* replace: dnssec completions

* fix: use regional API flags for Logging API

* fix: stop testing credentials if the URL is overriden

* replace: logging pipeline completions

* replace: logging logs completions

* make cdn a regional command

* feat: cdn completions

* better constant names

* replace: MariaDB add regional support

* replace: mariadb backups regional completions

* doc: update docs

* goimports

* test: fix config tests
  • Loading branch information
avirtopeanu-ionos authored Dec 10, 2024
1 parent ba04678 commit f3a0f01
Show file tree
Hide file tree
Showing 102 changed files with 635 additions and 636 deletions.
4 changes: 3 additions & 1 deletion commands/cdn/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cdn

import (
"github.com/ionos-cloud/ionosctl/v6/commands/cdn/distribution"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/spf13/cobra"
)
Expand All @@ -15,5 +16,6 @@ func Command() *core.Command {
},
}
cmd.AddCommand(distribution.Command())
return cmd

return core.WithRegionalFlags(cmd, constants.CDNApiRegionalURL, constants.CDNLocations)
}
1 change: 1 addition & 0 deletions commands/cdn/completer/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package completer

import (
"context"

"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
cdn "github.com/ionos-cloud/sdk-go-cdn"
Expand Down
3 changes: 2 additions & 1 deletion commands/cdn/distribution/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/pkg/pointer"
"github.com/ionos-cloud/ionosctl/v6/pkg/uuidgen"
cdn "github.com/ionos-cloud/sdk-go-cdn"
"github.com/spf13/viper"
"os"
)

func Create() *core.Command {
Expand Down
16 changes: 9 additions & 7 deletions commands/cdn/distribution/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package distribution
import (
"context"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/pkg/confirm"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
cdn "github.com/ionos-cloud/sdk-go-cdn"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/ionos-cloud/ionosctl/v6/internal/core"
Expand Down Expand Up @@ -41,12 +41,14 @@ func Delete() *core.Command {
InitClient: true,
})

cmd.AddStringFlag(constants.FlagCDNDistributionID, constants.FlagIdShort, "", "The ID of the distribution you want to retrieve", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagCDNDistributionID, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.DistributionsProperty(func(r cdn.Distribution) string {
return *r.Id
}), cobra.ShellCompDirectiveNoFileComp
})
cmd.AddStringFlag(constants.FlagCDNDistributionID, constants.FlagIdShort, "", "The ID of the distribution you want to delete",
core.RequiredFlagOption(),
core.WithCompletion(func() []string {
return completer.DistributionsProperty(func(r cdn.Distribution) string {
return *r.Id
})
}, constants.CDNApiRegionalURL),
)
cmd.AddBoolFlag(constants.ArgAll, constants.ArgAllShort, false, "Delete all records if set", core.RequiredFlagOption())

cmd.Command.SilenceUsage = true
Expand Down
20 changes: 11 additions & 9 deletions commands/cdn/distribution/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package distribution
import (
"context"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
cdn "github.com/ionos-cloud/sdk-go-cdn"
"github.com/spf13/cobra"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
cdn "github.com/ionos-cloud/sdk-go-cdn"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -52,12 +51,15 @@ func FindByID() *core.Command {
},
InitClient: true,
})
cmd.AddStringFlag(constants.FlagCDNDistributionID, constants.FlagIdShort, "", "The ID of the distribution you want to retrieve", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagCDNDistributionID, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.DistributionsProperty(func(r cdn.Distribution) string {
return *r.Id
}), cobra.ShellCompDirectiveNoFileComp
})

cmd.AddStringFlag(constants.FlagCDNDistributionID, constants.FlagIdShort, "", "The ID of the distribution you want to retrieve",
core.RequiredFlagOption(),
core.WithCompletion(func() []string {
return completer.DistributionsProperty(func(r cdn.Distribution) string {
return *r.Id
})
}, constants.CDNApiRegionalURL),
)
cmd.Command.SilenceUsage = true
cmd.Command.Flags().SortFlags = false
return cmd
Expand Down
1 change: 1 addition & 0 deletions commands/cdn/distribution/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package distribution
import (
"context"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
Expand Down
15 changes: 9 additions & 6 deletions commands/cdn/distribution/routingrules/routing_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routingrules
import (
"context"
"fmt"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
Expand Down Expand Up @@ -78,12 +79,14 @@ func GetDistributionRoutingRules() *core.Command {
},
InitClient: true,
})
cmd.AddStringFlag(constants.FlagCDNDistributionID, constants.FlagIdShort, "", "The ID of the distribution you want to retrieve", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagCDNDistributionID, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.DistributionsProperty(func(r cdn.Distribution) string {
return *r.Id
}), cobra.ShellCompDirectiveNoFileComp
})
cmd.AddStringFlag(constants.FlagCDNDistributionID, constants.FlagIdShort, "", "The ID of the distribution",
core.RequiredFlagOption(),
core.WithCompletion(func() []string {
return completer.DistributionsProperty(func(r cdn.Distribution) string {
return *r.Id
})
}, constants.CDNApiRegionalURL),
)
cmd.Command.SilenceUsage = true
cmd.Command.Flags().SortFlags = false
return cmd
Expand Down
17 changes: 9 additions & 8 deletions commands/cdn/distribution/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package distribution
import (
"context"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/spf13/cobra"

"github.com/ionos-cloud/ionosctl/v6/commands/cdn/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
Expand Down Expand Up @@ -49,12 +48,14 @@ func Update() *core.Command {
InitClient: true,
})

cmd.AddStringFlag(constants.FlagCDNDistributionID, constants.FlagIdShort, "", "The ID of the CDN distribution", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagCDNDistributionID, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.DistributionsProperty(func(r cdn.Distribution) string {
return *r.Id
}), cobra.ShellCompDirectiveNoFileComp
})
cmd.AddStringFlag(constants.FlagCDNDistributionID, constants.FlagIdShort, "", "The ID of the distribution you want to update",
core.RequiredFlagOption(),
core.WithCompletion(func() []string {
return completer.DistributionsProperty(func(r cdn.Distribution) string {
return *r.Id
})
}, constants.CDNApiRegionalURL),
)
cmd.Command.SilenceUsage = true
cmd.Command.Flags().SortFlags = false

Expand Down
9 changes: 9 additions & 0 deletions commands/dbaas/mariadb/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ionos-cloud/ionosctl/v6/commands/dbaas/mariadb/cluster"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
ionoscloud "github.com/ionos-cloud/sdk-go-dbaas-mariadb"

"github.com/ionos-cloud/ionosctl/v6/internal/client"
Expand Down Expand Up @@ -73,3 +74,11 @@ func FilterPaginationFlags(c *core.CommandConfig) Filter {
return req
}
}

func BackupsProperty[V any](f func(c ionoscloud.BackupResponse) V, fs ...Filter) []V {
recs, err := Backups(fs...)
if err != nil {
return nil
}
return functional.Map(*recs.Items, f)
}
16 changes: 14 additions & 2 deletions commands/dbaas/mariadb/backup/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

ionoscloud "github.com/ionos-cloud/sdk-go-dbaas-mariadb"

"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
Expand Down Expand Up @@ -46,7 +48,17 @@ func Get() *core.Command {
InitClient: true,
})

cmd.AddStringFlag(constants.FlagBackupId, "", "", "The ID of the Backup to be retrieved")

cmd.AddStringFlag(constants.FlagBackupId, "", "", "The ID of the Backup to be retrieved",
core.RequiredFlagOption(),
core.WithCompletion(
func() []string {
return BackupsProperty(func(c ionoscloud.BackupResponse) string {
if c.Id == nil {
return ""
}
return *c.Id + "\t" + fmt.Sprintf("(%d MiB)", *c.Properties.Size)
})
}, constants.MariaDBApiRegionalURL),
)
return cmd
}
22 changes: 12 additions & 10 deletions commands/dbaas/mariadb/backup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
ionoscloud "github.com/ionos-cloud/sdk-go-dbaas-mariadb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -58,15 +57,18 @@ func List() *core.Command {
InitClient: true,
})

cmd.AddStringFlag(constants.FlagClusterId, constants.FlagIdShort, "", "Optionally limit shown backups to those of a certain cluster")
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cluster.ClustersProperty(func(c ionoscloud.ClusterResponse) string {
if c.Id == nil {
return ""
}
return *c.Id
}), cobra.ShellCompDirectiveNoFileComp
})
cmd.AddStringFlag(constants.FlagClusterId, constants.FlagIdShort, "", "Optionally limit shown backups to those of a certain cluster",
core.RequiredFlagOption(),
core.WithCompletion(
func() []string {
return cluster.ClustersProperty(func(c ionoscloud.ClusterResponse) string {
if c.Id == nil {
return ""
}
return *c.Id
})
}, constants.MariaDBApiRegionalURL),
)
cmd.AddInt32Flag(constants.FlagMaxResults, constants.FlagMaxResultsShort, 0, constants.DescMaxResults)
cmd.AddInt32Flag(constants.FlagOffset, "", 0, "Skip a certain number of results")

Expand Down
21 changes: 12 additions & 9 deletions commands/dbaas/mariadb/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ ionosctl db mar c d --all --name <name>`,
InitClient: true,
})

cmd.AddStringFlag(constants.FlagClusterId, constants.FlagIdShort, "", "The unique ID of the cluster", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return ClustersProperty(func(c sdkgo.ClusterResponse) string {
if c.Id == nil {
return ""
}
return *c.Id
}), cobra.ShellCompDirectiveNoFileComp
})
cmd.AddStringFlag(constants.FlagClusterId, constants.FlagIdShort, "", "The unique ID of the cluster",
core.RequiredFlagOption(),
core.WithCompletion(
func() []string {
return ClustersProperty(func(c sdkgo.ClusterResponse) string {
if c.Id == nil {
return ""
}
return *c.Id
})
}, constants.MariaDBApiRegionalURL),
)
cmd.AddBoolFlag(constants.ArgAll, constants.ArgAllShort, false, "Delete all mariadb clusters")
cmd.AddBoolFlag(constants.FlagName, "", false, "When deleting all clusters, filter the clusters by a name")
cmd.AddStringSliceFlag(constants.ArgCols, "", nil, tabheaders.ColsMessage(allCols))
Expand Down
22 changes: 12 additions & 10 deletions commands/dbaas/mariadb/cluster/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
ionoscloud "github.com/ionos-cloud/sdk-go-dbaas-mariadb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -50,15 +49,18 @@ func Get() *core.Command {
InitClient: true,
})

cmd.AddStringFlag(constants.FlagClusterId, constants.FlagIdShort, "", "The unique ID of the cluster", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return ClustersProperty(func(c ionoscloud.ClusterResponse) string {
if c.Id == nil {
return ""
}
return *c.Id
}), cobra.ShellCompDirectiveNoFileComp
})
cmd.AddStringFlag(constants.FlagClusterId, constants.FlagIdShort, "", "The unique ID of the cluster",
core.RequiredFlagOption(),
core.WithCompletion(
func() []string {
return ClustersProperty(func(c ionoscloud.ClusterResponse) string {
if c.Id == nil {
return ""
}
return *c.Id
})
}, constants.MariaDBApiRegionalURL),
)

cmd.Command.SilenceUsage = true

Expand Down
4 changes: 3 additions & 1 deletion commands/dbaas/mariadb/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mariadb
import (
"github.com/ionos-cloud/ionosctl/v6/commands/dbaas/mariadb/backup"
"github.com/ionos-cloud/ionosctl/v6/commands/dbaas/mariadb/cluster"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/spf13/cobra"
)
Expand All @@ -18,5 +19,6 @@ func Root() *core.Command {
}
cmd.AddCommand(cluster.Root())
cmd.AddCommand(backup.Root())
return cmd

return core.WithRegionalFlags(cmd, constants.MariaDBApiRegionalURL, constants.MariaDBLocations)
}
2 changes: 1 addition & 1 deletion commands/dns/completer/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"

"github.com/ionos-cloud/sdk-go-dns"
ionoscloud "github.com/ionos-cloud/sdk-go-dns"
"github.com/spf13/viper"
)

Expand Down
5 changes: 3 additions & 2 deletions commands/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
reverse_record "github.com/ionos-cloud/ionosctl/v6/commands/dns/reverse-record"
secondary_zones "github.com/ionos-cloud/ionosctl/v6/commands/dns/secondary-zones"
"github.com/ionos-cloud/ionosctl/v6/commands/dns/zone"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/spf13/cobra"
)

func DNSCommand() *core.Command {
func Root() *core.Command {
cmd := &core.Command{
Command: &cobra.Command{
Use: "dns",
Expand All @@ -26,5 +27,5 @@ func DNSCommand() *core.Command {
cmd.AddCommand(dnssec.Root())
cmd.AddCommand(secondary_zones.Root())

return cmd
return core.WithRegionalFlags(cmd, constants.DNSApiRegionalURL, constants.DNSLocations)
}
Loading

0 comments on commit f3a0f01

Please sign in to comment.