Skip to content
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

add kafka cluster resource operations #465

Open
wants to merge 1 commit 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
34 changes: 34 additions & 0 deletions commands/kafka/cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cluster

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"
"github.com/spf13/cobra"
)

var (
allCols = []string{"Id", "Name", "Version", "Size", "DatacenterId", "LanId", "BrokerAddresses", "State"}
defaultCols = []string{"Id", "Name", "Version", "Size", "DatacenterId", "LanId", "BrokerAddresses", "State"}
)

func Command() *core.Command {
cmd := &core.Command{
Command: &cobra.Command{
Use: "cluster",
Short: "The sub-commands of 'ionosctl kafka cluster' allow you to manage kafka clusters",
Aliases: []string{"cl"},
TraverseChildren: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
location, _ := cmd.Flags().GetString(constants.FlagLocation)
changeLocation(client.Must().KafkaClient, location)
},

after moving Lcation as a PersistentFlag on this command

}
cmd.Command.PersistentFlags().StringSlice(constants.ArgCols, nil, tabheaders.ColsMessage(allCols))
_ = cmd.Command.RegisterFlagCompletionFunc(constants.ArgCols, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return allCols, cobra.ShellCompDirectiveNoFileComp
})

cmd.AddCommand(List())
cmd.AddCommand(FindByID())
cmd.AddCommand(Delete())
cmd.AddCommand(Create())
return cmd
}
139 changes: 139 additions & 0 deletions commands/kafka/cluster/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package cluster

import (
"context"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/completer"
cloudapiv6completer "github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/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"
"github.com/ionos-cloud/ionosctl/v6/pkg/pointer"
kafka "github.com/ionos-cloud/sdk-go-kafka"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Create() *core.Command {
cmd := core.NewCommand(context.Background(), nil, core.CommandBuilder{
Namespace: "kafka",
Resource: "cluster",
Verb: "create",
Aliases: []string{"c", "post"},
ShortDesc: "Create a kafka cluster. Wiki: https://docs.ionos.com/cloud/data-analytics/kafka/api-howtos/create-kafka",
Example: "ionosctl kafka cl create --name my-cluster --version 1.0 --size S --location de/txl --datacenter-id DATACENTER_ID --lan-id LAN_ID --broker-addresses 127.0.0.1,127.0.0.2",
PreCmdRun: func(c *core.PreCommandConfig) error {
if err := core.CheckRequiredFlags(c.Command, c.NS,
constants.FlagName, constants.FlagVersion, constants.FlagSize, constants.FlagLocation,
constants.FlagDatacenterId, constants.FlagLanId, constants.FlagKafkaBrokerAddresses); err != nil {
return err
}

return nil
},
CmdRun: func(c *core.CommandConfig) error {
input := &kafka.Cluster{}
if err := setPropertiesFromFlags(c, input); err != nil {
return err
}

changeLocation(client.Must().Kafka, viper.GetString(core.GetFlagName(c.NS, constants.FlagLocation)))
res, _, err := client.Must().Kafka.ClustersApi.ClustersPost(context.Background()).
ClusterCreate(kafka.ClusterCreate{
Properties: input,
}).Execute()
if err != nil {
return err
}

return printCluster(c, res)
},
InitClient: true,
})

cmd.Command.SilenceUsage = true
cmd.Command.Flags().SortFlags = false
return addClusterCreateFlags(cmd)
}

func changeLocation(client *kafka.APIClient, location string) {
cfg := client.GetConfig()
cfg.Servers = kafka.ServerConfigurations{
{
URL: locationToURL[location],
},
}
return
}

func addClusterCreateFlags(cmd *core.Command) *core.Command {
cmd.AddStringFlag(constants.FlagName, "", "", "The name of the kafka cluster", core.RequiredFlagOption())
cmd.AddStringFlag(constants.FlagVersion, "", "", "The version of the kafka cluster", core.RequiredFlagOption())
cmd.AddSetFlag(constants.FlagSize, "", "", []string{"XS", "S", "M", "L", "XL"}, "The size of the kafka cluster", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagSize, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"XS", "S", "M", "L", "XL"}, cobra.ShellCompDirectiveNoFileComp
})

cmd.AddStringFlag(constants.FlagLocation, "", "", "The location of the kafka cluster", core.RequiredFlagOption())
Copy link
Contributor

@avirtopeanu-ionos avirtopeanu-ionos Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add this as a persistent flag on the kafka command? instead of having it on each command

Also, some users can't use completions due to the nature of their environment so we should add the possible values to the helptext of the flag

slices.Collect(maps.Keys(locationToURL))

_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagLocation, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var locations []string
for k := range locationToURL {
locations = append(locations, k)
}

return locations, cobra.ShellCompDirectiveNoFileComp
})
cmd.AddStringFlag(constants.FlagDatacenterId, "", "", "The ID of the datacenter", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagDatacenterId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.DataCentersIds(), cobra.ShellCompDirectiveNoFileComp
})

cmd.AddStringFlag(constants.FlagLanId, "", "", "The ID of the LAN", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagLanId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cloudapiv6completer.LansIds(cmd.Flag(constants.FlagDatacenterId).Value.String()), cobra.ShellCompDirectiveNoFileComp
})

cmd.AddStringSliceFlag(constants.FlagKafkaBrokerAddresses, "", []string{}, "The list of broker addresses", core.RequiredFlagOption())
return cmd
}

func setPropertiesFromFlags(c *core.CommandConfig, p *kafka.Cluster) error {
p.Name = pointer.From(viper.GetString(core.GetFlagName(c.NS, constants.FlagName)))
p.Version = pointer.From(viper.GetString(core.GetFlagName(c.NS, constants.FlagVersion)))
p.Size = pointer.From(viper.GetString(core.GetFlagName(c.NS, constants.FlagSize)))

p.Connections = &[]kafka.KafkaClusterConnection{{
DatacenterId: pointer.From(viper.GetString(core.GetFlagName(c.NS, constants.FlagDatacenterId))),
LanId: pointer.From(viper.GetString(core.GetFlagName(c.NS, constants.FlagLanId))),
BrokerAddresses: pointer.From(viper.GetStringSlice(core.GetFlagName(c.NS, constants.FlagKafkaBrokerAddresses))),
}}

return nil
}

func printCluster(c *core.CommandConfig, d kafka.ClusterRead) error {
cols, _ := c.Command.Command.Flags().GetStringSlice(constants.ArgCols)
out, err := jsontabwriter.GenerateOutput("", jsonpaths.KafkaCluster, d,
tabheaders.GetHeadersAllDefault(defaultCols, cols))
if err != nil {
return err
}

fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}

var locationToURL = map[string]string{
"de/fra": "https://kafka.de-fra.ionos.com",
"de/txl": "https://kafka.de-txl.ionos.com",
// other locations not yet available. will be added in the future.
// "es/vit": "https://kafka.es-vit.ionos.com",
// "gb/lhr": "https://kafka.gb-lhr.ionos.com",
// "us/ewr": "https://kafka.us-ewr.ionos.com",
// "us/las": "https://kafka.us-las.ionos.com",
// "us/mci": "https://kafka.us-mci.ionos.com",
// "fr/par": "https://kafka.fr-par.ionos.com",
}
95 changes: 95 additions & 0 deletions commands/kafka/cluster/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cluster

import (
"context"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/kafka/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"
kafka "github.com/ionos-cloud/sdk-go-kafka"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/ionos-cloud/ionosctl/v6/internal/core"
)

func Delete() *core.Command {
cmd := core.NewCommand(context.Background(), nil, core.CommandBuilder{
Namespace: "kafka",
Resource: "cluster",
Verb: "delete",
Aliases: []string{"del", "d"},
ShortDesc: "Delete a cluster",
Example: `ionosctl kafka cl delete --cluster-id ID`,
PreCmdRun: func(c *core.PreCommandConfig) error {
if err := core.CheckRequiredFlagsSets(c.Command, c.NS,
[]string{constants.FlagClusterId, constants.FlagLocation}, []string{constants.ArgAll, constants.FlagLocation}); err != nil {
return err
}

return nil
},
CmdRun: func(c *core.CommandConfig) error {
changeLocation(client.Must().Kafka, viper.GetString(core.GetFlagName(c.NS, constants.FlagLocation)))
if all := viper.GetBool(core.GetFlagName(c.NS, constants.ArgAll)); all {
return deleteAll(c)
}

return deleteSingle(c, viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId)))
},
InitClient: true,
})

cmd.AddStringFlag(constants.FlagClusterId, constants.FlagIdShort, "", "The ID of the cluster you want to retrieve", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
changeLocation(client.Must().Kafka, viper.GetString(core.GetFlagName(cmd.NS, constants.FlagLocation)))
return completer.ClustersProperty(func(r kafka.ClusterRead) string {
return *r.Id
}), cobra.ShellCompDirectiveNoFileComp
})

cmd.AddStringFlag(constants.FlagLocation, "", "", "The datacenter location", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagLocation, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var locations []string
for k := range locationToURL {
locations = append(locations, k)
}

return locations, cobra.ShellCompDirectiveNoFileComp
})
cmd.AddBoolFlag(constants.ArgAll, constants.ArgAllShort, false, "Delete all records if set", core.RequiredFlagOption())

cmd.Command.SilenceUsage = true
cmd.Command.Flags().SortFlags = false

return cmd
}

func deleteAll(c *core.CommandConfig) error {
records, err := completer.Clusters()
if err != nil {
return fmt.Errorf("failed getting all clusters: %w", err)
}

return functional.ApplyAndAggregateErrors(*records.GetItems(), func(d kafka.ClusterRead) error {
return deleteSingle(c, *d.Id)
})
}

func deleteSingle(c *core.CommandConfig, id string) error {
d, _, err := client.Must().Kafka.ClustersApi.ClustersFindById(context.Background(), id).Execute()
if err != nil {
return fmt.Errorf("cluster not found: %w", err)
}

yes := confirm.FAsk(c.Command.Command.InOrStdin(), fmt.Sprintf("Are you sure you want to delete cluster %s with name %s", *d.Id, *d.Properties.Name),
viper.GetBool(constants.ArgForce))
if !yes {
return fmt.Errorf("user cancelled deletion")
}

_, err = client.Must().Kafka.ClustersApi.ClustersDelete(context.Background(), *d.Id).Execute()
return err
}
76 changes: 76 additions & 0 deletions commands/kafka/cluster/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cluster

import (
"context"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/kafka/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"
kafka "github.com/ionos-cloud/sdk-go-kafka"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func FindByID() *core.Command {
cmd := core.NewCommand(context.Background(), nil, core.CommandBuilder{
Namespace: "kafka",
Resource: "cluster",
Verb: "get",
Aliases: []string{"g"},
ShortDesc: "Retrieve a cluster",
Example: "ionosctl kafka cl get --cluster-id ID",
PreCmdRun: func(c *core.PreCommandConfig) error {
if err := core.CheckRequiredFlags(c.Command, c.NS, constants.FlagClusterId, constants.FlagLocation); err != nil {
return err
}

return nil
},
CmdRun: func(c *core.CommandConfig) error {
changeLocation(client.Must().Kafka, viper.GetString(core.GetFlagName(c.NS, constants.FlagLocation)))
clusterID := viper.GetString(core.GetFlagName(c.NS, constants.FlagClusterId))
r, _, err := client.Must().Kafka.ClustersApi.ClustersFindById(context.Background(),
clusterID,
).Execute()
if err != nil {
return err
}

cols, _ := c.Command.Command.Flags().GetStringSlice(constants.ArgCols)
out, err := jsontabwriter.GenerateOutput("", jsonpaths.KafkaCluster, r,
tabheaders.GetHeadersAllDefault(defaultCols, cols))
if err != nil {
return err
}

fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
},
InitClient: true,
})
cmd.AddStringFlag(constants.FlagClusterId, constants.FlagIdShort, "", "The ID of the cluster you want to retrieve", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagClusterId, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
changeLocation(client.Must().Kafka, viper.GetString(core.GetFlagName(cmd.NS, constants.FlagLocation)))
return completer.ClustersProperty(func(r kafka.ClusterRead) string {
return *r.Id
}), cobra.ShellCompDirectiveNoFileComp
})

cmd.AddStringFlag(constants.FlagLocation, "", "", "The datacenter location", core.RequiredFlagOption())
_ = cmd.Command.RegisterFlagCompletionFunc(constants.FlagLocation, func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var locations []string
for k := range locationToURL {
locations = append(locations, k)
}

return locations, cobra.ShellCompDirectiveNoFileComp
})

cmd.Command.SilenceUsage = true
cmd.Command.Flags().SortFlags = false
return cmd
}
Loading
Loading