Skip to content

Commit

Permalink
feat(kafka): add --wait flag to perform synchronous Kafka creation (#960
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Enda committed Aug 24, 2021
1 parent e240f70 commit 5594411
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 35 deletions.
10 changes: 5 additions & 5 deletions pkg/cmd/kafka/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/redhat-developer/app-services-cli/pkg/connection"
kafkacmdutil "github.com/redhat-developer/app-services-cli/pkg/kafka/cmdutil"
"github.com/redhat-developer/app-services-cli/pkg/localize"

"github.com/redhat-developer/app-services-cli/pkg/iostreams"
Expand Down Expand Up @@ -48,16 +49,12 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command {
Short: opts.localizer.MustLocalize("kafka.delete.cmd.shortDescription"),
Long: opts.localizer.MustLocalize("kafka.delete.cmd.longDescription"),
Example: opts.localizer.MustLocalize("kafka.delete.cmd.example"),
Args: cobra.RangeArgs(0, 1),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if !opts.IO.CanPrompt() && !opts.force {
return flag.RequiredWhenNonInteractiveError("yes")
}

if len(args) > 0 {
opts.name = args[0]
}

if opts.name != "" && opts.id != "" {
return errors.New(opts.localizer.MustLocalize("service.error.idAndNameCannotBeUsed"))
}
Expand All @@ -84,6 +81,9 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command {

cmd.Flags().StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("kafka.delete.flag.id"))
cmd.Flags().BoolVarP(&opts.force, "yes", "y", false, opts.localizer.MustLocalize("kafka.delete.flag.yes"))
cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.describe.flag.name"))

kafkacmdutil.RegisterNameFlagCompletionFunc(cmd, f)

return cmd
}
Expand Down
14 changes: 4 additions & 10 deletions pkg/cmd/kafka/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flags"
"github.com/redhat-developer/app-services-cli/pkg/connection"
"github.com/redhat-developer/app-services-cli/pkg/iostreams"
kafkacmdutil "github.com/redhat-developer/app-services-cli/pkg/kafka/cmdutil"
"github.com/redhat-developer/app-services-cli/pkg/localize"

"github.com/redhat-developer/app-services-cli/pkg/cmd/flag"

"github.com/redhat-developer/app-services-cli/internal/config"
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory"
"github.com/redhat-developer/app-services-cli/pkg/cmdutil"
"github.com/redhat-developer/app-services-cli/pkg/dump"
"github.com/redhat-developer/app-services-cli/pkg/kafka"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
Expand Down Expand Up @@ -48,21 +48,13 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {
Short: opts.localizer.MustLocalize("kafka.describe.cmd.shortDescription"),
Long: opts.localizer.MustLocalize("kafka.describe.cmd.longDescription"),
Example: opts.localizer.MustLocalize("kafka.describe.cmd.example"),
Args: cobra.RangeArgs(0, 1),
// Dynamic completion of the Kafka name
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cmdutil.FilterValidKafkas(f, toComplete)
},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
validOutputFormats := flagutil.ValidOutputFormats
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
return flag.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
}

if len(args) > 0 {
opts.name = args[0]
}

if opts.name != "" && opts.id != "" {
return errors.New(opts.localizer.MustLocalize("service.error.idAndNameCannotBeUsed"))
}
Expand All @@ -89,7 +81,9 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {

cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", opts.localizer.MustLocalize("kafka.common.flag.output.description"))
cmd.Flags().StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("kafka.describe.flag.id"))
cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.describe.flag.id"))

kafkacmdutil.RegisterNameFlagCompletionFunc(cmd, f)
flagutil.EnableOutputFlagCompletion(cmd)

return cmd
Expand Down
22 changes: 10 additions & 12 deletions pkg/cmd/kafka/topic/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

type Options struct {
topicName string
name string
kafkaID string
outputFormat string

Expand All @@ -51,16 +51,8 @@ func NewDescribeTopicCommand(f *factory.Factory) *cobra.Command {
Short: opts.localizer.MustLocalize("kafka.topic.describe.cmd.shortDescription"),
Long: opts.localizer.MustLocalize("kafka.topic.describe.cmd.longDescription"),
Example: opts.localizer.MustLocalize("kafka.topic.describe.cmd.example"),
Args: cobra.ExactValidArgs(1),
// dynamic completion of topic names
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cmdutil.FilterValidTopicNameArgs(f, toComplete)
},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) > 0 {
opts.topicName = args[0]
}

if opts.outputFormat != "" {
if err = flag.ValidateOutput(opts.outputFormat); err != nil {
return err
Expand Down Expand Up @@ -88,6 +80,12 @@ func NewDescribeTopicCommand(f *factory.Factory) *cobra.Command {

cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", opts.localizer.MustLocalize("kafka.topic.common.flag.output.description"))

cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.topic.common.flag.output.description"))
_ = cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cmdutil.FilterValidTopicNameArgs(f, toComplete)
})
_ = cmd.MarkFlagRequired("name")

flagutil.EnableOutputFlagCompletion(cmd)

return cmd
Expand All @@ -106,14 +104,14 @@ func runCmd(opts *Options) error {

// fetch the topic
topicResponse, httpRes, err := api.TopicsApi.
GetTopic(context.Background(), opts.topicName).
GetTopic(context.Background(), opts.name).
Execute()
if err != nil {
if httpRes == nil {
return err
}

topicNameTmplPair := localize.NewEntry("TopicName", opts.topicName)
topicNameTmplPair := localize.NewEntry("TopicName", opts.name)
kafkaNameTmplPair := localize.NewEntry("InstanceName", kafkaInstance.GetName())
operationTmplPair := localize.NewEntry("Operation", "delete")

Expand Down
9 changes: 4 additions & 5 deletions pkg/cmd/kafka/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (

"github.com/redhat-developer/app-services-cli/pkg/connection"
"github.com/redhat-developer/app-services-cli/pkg/iostreams"
kafkacmdutil "github.com/redhat-developer/app-services-cli/pkg/kafka/cmdutil"
"github.com/redhat-developer/app-services-cli/pkg/localize"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"

"github.com/redhat-developer/app-services-cli/pkg/cmdutil"

"github.com/redhat-developer/app-services-cli/pkg/kafka"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,9 +47,6 @@ func NewUseCommand(f *factory.Factory) *cobra.Command {
Long: opts.localizer.MustLocalize("kafka.use.cmd.longDescription"),
Example: opts.localizer.MustLocalize("kafka.use.cmd.example"),
Args: cobra.NoArgs,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cmdutil.FilterValidKafkas(f, toComplete)
},
RunE: func(cmd *cobra.Command, args []string) error {
if opts.id == "" && opts.name == "" {
if !opts.IO.CanPrompt() {
Expand All @@ -69,6 +65,9 @@ func NewUseCommand(f *factory.Factory) *cobra.Command {

cmd.Flags().StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("kafka.use.flag.id"))
cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.use.flag.name"))

kafkacmdutil.RegisterNameFlagCompletionFunc(cmd, f)

return cmd
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/kafka/cmdutil/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kafkacmdutil

import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory"
"github.com/redhat-developer/app-services-cli/pkg/cmdutil"
"github.com/spf13/cobra"
)
// RegisterNameFlagCompletionFunc adds dynamic completion for the --name flag
func RegisterNameFlagCompletionFunc(cmd *cobra.Command, f *factory.Factory) error {
return cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cmdutil.FilterValidKafkas(f, toComplete)
})
}
6 changes: 5 additions & 1 deletion pkg/localize/locales/en/cmd/kafka_delete.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ $ rhoas kafka delete --id=1iSY6RQ3JKI8Q0OTmjQFd3ocFRg

[kafka.delete.flag.id]
description = 'Description for the --id flag'
one = 'Unique ID of the Kafka instance you want to delete (if not provided, the current Kafka instance will be deleted)'
one = 'Unique ID of the Kafka instance you want to delete'

[kafka.describe.flag.name]
description = 'Description for the --name flag'
one = 'Name of the Kafka instance you want to delete'

[kafka.delete.flag.yes]
description = 'Description for the --yes flag'
Expand Down
6 changes: 5 additions & 1 deletion pkg/localize/locales/en/cmd/kafka_describe.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ $ rhoas kafka describe -o yaml

[kafka.describe.flag.id]
description = 'Description for the --id flag'
one = 'Unique ID of the Kafka instance you want to view (if not provided, the current Kafka instance will be displayed)'
one = 'Unique ID of the Kafka instance you want to view'

[kafka.describe.flag.name]
description = 'Description for the --name flag'
one = 'Name of the Kafka instance you want to view'
3 changes: 3 additions & 0 deletions pkg/localize/locales/en/cmd/kafka_topic_describe.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ one = '''
Print detailed configuration information for a Kafka topic.
'''

[kafka.topic.describe.flag.name]
one = 'Name of the Kafka topic you want to view'

[kafka.topic.describe.cmd.example]
one = '''
# describe a topic
Expand Down
2 changes: 1 addition & 1 deletion pkg/localize/locales/en/service.crud.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ In order to be able to create a new instance, you must first review and accept t
'''

[service.error.idAndNameCannotBeUsed]
one = 'name argument and --id flag cannot be used at the same time'
one = '--name and --id flag cannot be used at the same time'

0 comments on commit 5594411

Please sign in to comment.