Skip to content

Commit

Permalink
fix: pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
avirtopeanu-ionos committed Jan 8, 2025
1 parent 0a67327 commit b72a267
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 49 deletions.
12 changes: 6 additions & 6 deletions commands/dbaas/mongo/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
ionoscloud "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
mongo "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ var (
defaultCols = allCols[0:9]
)

func Clusters(fs ...Filter) (ionoscloud.ClusterList, error) {
func Clusters(fs ...Filter) (mongo.ClusterList, error) {
req := client.Must().MongoClient.ClustersApi.ClustersGet(context.Background())

for _, f := range fs {
Expand All @@ -67,15 +67,15 @@ func Clusters(fs ...Filter) (ionoscloud.ClusterList, error) {

clusters, _, err := req.Execute()
if err != nil {
return ionoscloud.ClusterList{}, fmt.Errorf("failed getting clusters: %w", err)
return mongo.ClusterList{}, fmt.Errorf("failed getting clusters: %w", err)
}
return clusters, err
}

type Filter func(ionoscloud.ApiClustersGetRequest) ionoscloud.ApiClustersGetRequest
type Filter func(mongo.ApiClustersGetRequest) mongo.ApiClustersGetRequest

func FilterPaginationFlags(c *core.CommandConfig) Filter {
return func(req ionoscloud.ApiClustersGetRequest) ionoscloud.ApiClustersGetRequest {
return func(req mongo.ApiClustersGetRequest) mongo.ApiClustersGetRequest {
if f := core.GetFlagName(c.NS, constants.FlagMaxResults); viper.IsSet(f) {
req = req.Limit(viper.GetInt32(f))
}
Expand All @@ -87,7 +87,7 @@ func FilterPaginationFlags(c *core.CommandConfig) Filter {
}

func FilterNameFlags(c *core.CommandConfig) Filter {
return func(req ionoscloud.ApiClustersGetRequest) ionoscloud.ApiClustersGetRequest {
return func(req mongo.ApiClustersGetRequest) mongo.ApiClustersGetRequest {
if f := core.GetFlagName(c.NS, constants.FlagName); viper.IsSet(f) {
req = req.FilterName(viper.GetString(f))
}
Expand Down
20 changes: 10 additions & 10 deletions commands/dbaas/mongo/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/pkg/convbytes"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
"github.com/ionos-cloud/ionosctl/v6/pkg/pointer"
ionoscloud "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
"github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -89,7 +89,7 @@ func ClusterCreateCmd() *core.Command {
return nil
},
CmdRun: func(c *core.CommandConfig) error {
cluster := ionoscloud.CreateClusterProperties{}
cluster := mongo.CreateClusterProperties{}
if fn := core.GetFlagName(c.NS, constants.FlagEdition); viper.IsSet(fn) {
cluster.Edition = pointer.From(viper.GetString(fn))
}
Expand Down Expand Up @@ -125,15 +125,15 @@ func ClusterCreateCmd() *core.Command {
cluster.Shards = pointer.From(viper.GetInt32(fn))
}

cluster.MaintenanceWindow = &ionoscloud.MaintenanceWindow{}
cluster.MaintenanceWindow = &mongo.MaintenanceWindow{}
if fn := core.GetFlagName(c.NS, constants.FlagMaintenanceDay); viper.GetString(fn) != "" {
cluster.MaintenanceWindow.DayOfTheWeek = mongo.DayOfTheWeek(viper.GetString(fn))
}
if fn := core.GetFlagName(c.NS, constants.FlagMaintenanceTime); viper.GetString(fn) != "" {
cluster.MaintenanceWindow.Time = viper.GetString(fn)
}

cluster.Connections = make([]ionoscloud.Connection, 1)
cluster.Connections = make([]mongo.Connection, 1)
if fn := core.GetFlagName(c.NS, constants.FlagCidr); viper.IsSet(fn) {
cluster.Connections[0].CidrList =
viper.GetStringSlice(fn)
Expand All @@ -151,15 +151,15 @@ func ClusterCreateCmd() *core.Command {
cluster.Backup = nil
if fn := core.GetFlagName(c.NS, flagBackupLocation); viper.IsSet(fn) {
if cluster.Backup == nil {
cluster.Backup = &ionoscloud.BackupProperties{}
cluster.Backup = &mongo.BackupProperties{}
}
cluster.Backup.Location = pointer.From(viper.GetString(fn))
}

cluster.BiConnector = nil
if fn := core.GetFlagName(c.NS, flagBiconnector); viper.IsSet(fn) {
if cluster.BiConnector == nil {
cluster.BiConnector = &ionoscloud.BiConnectorProperties{}
cluster.BiConnector = &mongo.BiConnectorProperties{}
}
hostAndPort := viper.GetString(fn)
host, port, err := net.SplitHostPort(hostAndPort)
Expand All @@ -179,7 +179,7 @@ func ClusterCreateCmd() *core.Command {
cluster.Cores = pointer.From(viper.GetInt32(fn))
}
if fn := core.GetFlagName(c.NS, constants.FlagStorageType); viper.IsSet(fn) && viper.GetString(fn) != "" {
cluster.StorageType = (*ionoscloud.StorageType)(pointer.From(viper.GetString(fn)))
cluster.StorageType = (*mongo.StorageType)(pointer.From(viper.GetString(fn)))
}
if fn := core.GetFlagName(c.NS, constants.FlagStorageSize); viper.IsSet(fn) && viper.GetString(fn) != "" {
sizeInt64 := convbytes.StrToUnit(viper.GetString(fn), convbytes.MB)
Expand All @@ -191,7 +191,7 @@ func ClusterCreateCmd() *core.Command {
}

createdCluster, _, err := client.Must().MongoClient.ClustersApi.ClustersPost(context.Background()).CreateClusterRequest(
ionoscloud.CreateClusterRequest{Properties: &cluster},
mongo.CreateClusterRequest{Properties: &cluster},
).Execute()
if err != nil {
return fmt.Errorf("failed creating cluster: %w", err)
Expand Down Expand Up @@ -230,7 +230,7 @@ func ClusterCreateCmd() *core.Command {
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
names := functional.Fold(ts, func(acc []string, t ionoscloud.TemplateResponse) []string {
names := functional.Fold(ts, func(acc []string, t mongo.TemplateResponse) []string {
if t.Properties == nil || t.Properties.Name == nil {
return acc
}
Expand Down Expand Up @@ -378,7 +378,7 @@ func validateOrInferEditionByTemplate(c *core.PreCommandConfig) error {
// Intentionally don't wrap this error since the deeper error would kind of say the same thing
return err
}
template, err := templates.Find(func(x ionoscloud.TemplateResponse) bool {
template, err := templates.Find(func(x mongo.TemplateResponse) bool {
return *x.Id == tmplId
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion commands/dbaas/mongo/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func deleteAll(c *core.CommandConfig) error {
return err
}

return functional.ApplyAndAggregateErrors(*xs.GetItems(), func(x sdkgo.ClusterResponse) error {
return functional.ApplyAndAggregateErrors(xs.GetItems(), func(x sdkgo.ClusterResponse) error {
yes := confirm.FAsk(c.Command.Command.InOrStdin(), confirmStringForCluster(x), viper.GetBool(constants.ArgForce))
if yes {
_, _, delErr := client.Must().MongoClient.ClustersApi.ClustersDelete(c.Context, *x.Id).Execute()
Expand Down
4 changes: 2 additions & 2 deletions commands/dbaas/mongo/cluster/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
ionoscloud "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
mongo "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ func ClusterRestoreCmd() *core.Command {

_, err := client.Must().MongoClient.RestoresApi.ClustersRestorePost(context.Background(), clusterId).
CreateRestoreRequest(
ionoscloud.CreateRestoreRequest{
mongo.CreateRestoreRequest{
SnapshotId: &snapshotId,
},
).Execute()
Expand Down
33 changes: 14 additions & 19 deletions commands/dbaas/mongo/cluster/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/ionos-cloud/ionosctl/v6/commands/dbaas/mongo/completer"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
ionoscloud "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
"github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
"github.com/spf13/cobra"
)

Expand All @@ -34,7 +34,7 @@ func ClusterUpdateCmd() *core.Command {
return c.Command.Command.MarkFlagRequired(constants.FlagClusterId)
},
CmdRun: func(c *core.CommandConfig) error {
cluster := ionoscloud.PatchClusterProperties{}
cluster := mongo.PatchClusterProperties{}
if fn := core.GetFlagName(c.NS, constants.FlagEdition); viper.IsSet(fn) {
cluster.Edition = pointer.From(viper.GetString(fn))
}
Expand Down Expand Up @@ -64,43 +64,38 @@ func ClusterUpdateCmd() *core.Command {
cluster.Shards = pointer.From(viper.GetInt32(fn))
}

cluster.MaintenanceWindow = &ionoscloud.MaintenanceWindow{}
cluster.MaintenanceWindow = &mongo.MaintenanceWindow{}
if fn := core.GetFlagName(c.NS, constants.FlagMaintenanceDay); viper.IsSet(fn) {
cluster.MaintenanceWindow.DayOfTheWeek = (*ionoscloud.DayOfTheWeek)(pointer.From(
viper.GetString(fn)))
cluster.MaintenanceWindow.DayOfTheWeek = mongo.DayOfTheWeek(viper.GetString(fn))
}
if fn := core.GetFlagName(c.NS, constants.FlagMaintenanceTime); viper.IsSet(fn) {
cluster.MaintenanceWindow.Time = pointer.From(
viper.GetString(fn))
cluster.MaintenanceWindow.Time = viper.GetString(fn)
}

cluster.Connections = pointer.From(make([]ionoscloud.Connection, 1))
cluster.Connections = make([]mongo.Connection, 1)
if fn := core.GetFlagName(c.NS, constants.FlagCidr); viper.IsSet(fn) {
(*cluster.Connections)[0].CidrList = pointer.From(
viper.GetStringSlice(fn))
cluster.Connections[0].CidrList = viper.GetStringSlice(fn)
}
if fn := core.GetFlagName(c.NS, constants.FlagDatacenterId); viper.IsSet(fn) {
(*cluster.Connections)[0].DatacenterId = pointer.From(
viper.GetString(fn))
cluster.Connections[0].DatacenterId = viper.GetString(fn)
}
if fn := core.GetFlagName(c.NS, constants.FlagLanId); viper.IsSet(fn) {
(*cluster.Connections)[0].LanId = pointer.From(
viper.GetString(fn))
cluster.Connections[0].LanId = viper.GetString(fn)
}

// backup flags
cluster.Backup = nil
if fn := core.GetFlagName(c.NS, flagBackupLocation); viper.IsSet(fn) {
if cluster.Backup == nil {
cluster.Backup = &ionoscloud.BackupProperties{}
cluster.Backup = &mongo.BackupProperties{}
}
cluster.Backup.Location = pointer.From(viper.GetString(fn))
}

cluster.BiConnector = nil
if fn := core.GetFlagName(c.NS, flagBiconnector); viper.IsSet(fn) {
if cluster.BiConnector == nil {
cluster.BiConnector = &ionoscloud.BiConnectorProperties{}
cluster.BiConnector = &mongo.BiConnectorProperties{}
}
hostAndPort := viper.GetString(fn)
host, port, err := net.SplitHostPort(hostAndPort)
Expand All @@ -114,7 +109,7 @@ func ClusterUpdateCmd() *core.Command {

if fn := core.GetFlagName(c.NS, flagBiconnectorEnabled); viper.IsSet(fn) {
if cluster.BiConnector == nil {
cluster.BiConnector = &ionoscloud.BiConnectorProperties{}
cluster.BiConnector = &mongo.BiConnectorProperties{}
}
cluster.BiConnector.Enabled = pointer.From(viper.GetBool(fn))
}
Expand All @@ -124,7 +119,7 @@ func ClusterUpdateCmd() *core.Command {
cluster.Cores = pointer.From(viper.GetInt32(fn))
}
if fn := core.GetFlagName(c.NS, constants.FlagStorageType); viper.IsSet(fn) {
cluster.StorageType = (*ionoscloud.StorageType)(pointer.From(viper.GetString(fn)))
cluster.StorageType = (*mongo.StorageType)(pointer.From(viper.GetString(fn)))
}
if fn := core.GetFlagName(c.NS, constants.FlagStorageSize); viper.IsSet(fn) {
sizeInt64 := convbytes.StrToUnit(viper.GetString(fn), convbytes.MB)
Expand All @@ -137,7 +132,7 @@ func ClusterUpdateCmd() *core.Command {

clusterId := viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId))
createdCluster, _, err := client.Must().MongoClient.ClustersApi.ClustersPatch(context.Background(),
clusterId).PatchClusterRequest(ionoscloud.PatchClusterRequest{Properties: &cluster}).Execute()
clusterId).PatchClusterRequest(mongo.PatchClusterRequest{Properties: &cluster}).Execute()
if err != nil {
return fmt.Errorf("failed creating cluster: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions commands/dbaas/mongo/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/pkg/functional"
ionoscloud "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
mongo "github.com/ionos-cloud/sdk-go-bundle/products/dbaas/mongo/v2"
"github.com/spf13/cobra"
)

Expand All @@ -33,7 +33,7 @@ var (
)

// List retrieves a list of templates, optionally filtered by a given funcs
func List(filters ...func(x ionoscloud.TemplateResponse) bool) ([]ionoscloud.TemplateResponse, error) {
func List(filters ...func(x mongo.TemplateResponse) bool) ([]mongo.TemplateResponse, error) {
xs, _, err := client.Must().MongoClient.TemplatesApi.TemplatesGet(context.Background()).Execute()
if err != nil {
return nil, fmt.Errorf("failed getting templates: %w", err)
Expand All @@ -52,16 +52,16 @@ func List(filters ...func(x ionoscloud.TemplateResponse) bool) ([]ionoscloud.Tem
}

// Find returns the first template for which found() returns true
func Find(found func(x ionoscloud.TemplateResponse) bool) (ionoscloud.TemplateResponse, error) {
func Find(found func(x mongo.TemplateResponse) bool) (mongo.TemplateResponse, error) {
filteredTemplates, err := List(found)
if err != nil {
return ionoscloud.TemplateResponse{}, err
return mongo.TemplateResponse{}, err
}

if len(filteredTemplates) > 0 {
return filteredTemplates[0], nil
}
return ionoscloud.TemplateResponse{}, fmt.Errorf("no matching template found")
return mongo.TemplateResponse{}, fmt.Errorf("no matching template found")
}

// Resolve resolves nameOrId to the ID of the template.
Expand All @@ -83,7 +83,7 @@ func Resolve(nameOrId string) (string, error) {
// It's a name

// Why doesn't the API have a FindByID or something :(
templateMatchingWholeWordIgnoreCase, err := Find(func(x ionoscloud.TemplateResponse) bool {
templateMatchingWholeWordIgnoreCase, err := Find(func(x mongo.TemplateResponse) bool {
if x.Properties == nil || x.Properties.Name == nil {
return false
}
Expand Down
8 changes: 4 additions & 4 deletions commands/dbaas/mongo/user/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func deleteAll(c *core.CommandConfig, clusterId string) error {
return err
}

return functional.ApplyAndAggregateErrors(*xs.GetItems(), func(x sdkgo.User) error {
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf("delete user %s", *x.Properties.Username), viper.GetBool(constants.ArgForce))
return functional.ApplyAndAggregateErrors(xs.GetItems(), func(x sdkgo.User) error {
yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf("delete user %s", x.Properties.Username), viper.GetBool(constants.ArgForce))
if !yes {
return fmt.Errorf("user %s skipped by confirmation check", *x.Properties.Username)
return fmt.Errorf("user %s skipped by confirmation check", x.Properties.Username)
}
_, _, delErr := client.Must().MongoClient.UsersApi.ClustersUsersDelete(c.Context, clusterId, *x.Properties.Username).Execute()
_, _, delErr := client.Must().MongoClient.UsersApi.ClustersUsersDelete(c.Context, clusterId, x.Properties.Username).Execute()
if delErr != nil {
return fmt.Errorf("failed deleting one of the resources: %w", delErr)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/dbaas/mongo/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func listAll(c *core.CommandConfig) error {
var lsConverted []map[string]interface{}
var multiErr error

for _, c := range *clusters.GetItems() {
for _, c := range clusters.GetItems() {
l, _, err := client.Must().MongoClient.UsersApi.ClustersUsersGet(context.Background(), *c.Id).Execute()
if err != nil {
multiErr = errors.Join(multiErr, fmt.Errorf("failed listing users of cluster %s: %w", *c.Properties.DisplayName, err))
Expand Down

0 comments on commit b72a267

Please sign in to comment.