Skip to content

Commit

Permalink
refactor: override fetch if instance exists (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkpattnaik780 authored Feb 22, 2023
1 parent f7cc29e commit 28d9c88
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 74 deletions.
40 changes: 21 additions & 19 deletions pkg/cmd/kafka/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
)

type options struct {
id string
name string
skipConfirm bool
id string
name string
skipConfirm bool
kafkaInstance *kafkamgmtclient.KafkaRequest

IO *iostreams.IOStreams
Connection factory.ConnectionFunc
Expand All @@ -50,7 +51,7 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command {
Long: opts.localizer.MustLocalize("kafka.delete.cmd.longDescription"),
Example: opts.localizer.MustLocalize("kafka.delete.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (err error) {
if !opts.IO.CanPrompt() && !opts.skipConfirm {
return flagutil.RequiredWhenNonInteractiveError("yes")
}
Expand All @@ -63,12 +64,12 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command {
return runDelete(opts)
}

kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f)
opts.kafkaInstance, err = contextutil.GetCurrentKafkaInstance(f)
if err != nil {
return err
}

opts.id = kafkaInstance.GetId()
opts.id = opts.kafkaInstance.GetId()

return runDelete(opts)
},
Expand Down Expand Up @@ -105,20 +106,21 @@ func runDelete(opts *options) error {

api := conn.API()

var response *kafkamgmtclient.KafkaRequest
if opts.name != "" {
response, _, err = kafkautil.GetKafkaByName(opts.Context, api.KafkaMgmt(), opts.name)
if err != nil {
return err
}
} else {
response, _, err = kafkautil.GetKafkaByID(opts.Context, api.KafkaMgmt(), opts.id)
if err != nil {
return err
if opts.kafkaInstance == nil {
if opts.name != "" {
opts.kafkaInstance, _, err = kafkautil.GetKafkaByName(opts.Context, api.KafkaMgmt(), opts.name)
if err != nil {
return err
}
} else {
opts.kafkaInstance, _, err = kafkautil.GetKafkaByID(opts.Context, api.KafkaMgmt(), opts.id)
if err != nil {
return err
}
}
}

kafkaName := response.GetName()
kafkaName := opts.kafkaInstance.GetName()

if !opts.skipConfirm {
promptConfirmName := &survey.Input{
Expand All @@ -139,7 +141,7 @@ func runDelete(opts *options) error {

// delete the Kafka
opts.Logger.Debug(opts.localizer.MustLocalize("kafka.delete.log.debug.deletingKafka"), fmt.Sprintf("\"%s\"", kafkaName))
a := api.KafkaMgmt().DeleteKafkaById(opts.Context, response.GetId())
a := api.KafkaMgmt().DeleteKafkaById(opts.Context, opts.kafkaInstance.GetId())
a = a.Async(true)
_, _, err = a.Execute()

Expand All @@ -151,7 +153,7 @@ func runDelete(opts *options) error {

currentKafka := currCtx.KafkaID
// this is not the current instance, our work here is done
if currentKafka != response.GetId() {
if currentKafka != opts.kafkaInstance.GetId() {
return nil
}

Expand Down
47 changes: 25 additions & 22 deletions pkg/cmd/kafka/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type options struct {
name string
bootstrapServer bool
outputFormat string
kafkaInstance *kafkamgmtclient.KafkaRequest

IO *iostreams.IOStreams
Connection factory.ConnectionFunc
Expand Down Expand Up @@ -51,7 +52,7 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {
Long: opts.localizer.MustLocalize("kafka.describe.cmd.longDescription"),
Example: opts.localizer.MustLocalize("kafka.describe.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (err error) {
validOutputFormats := flagutil.ValidOutputFormats
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
Expand All @@ -65,12 +66,12 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {
return runDescribe(opts)
}

kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f)
opts.kafkaInstance, err = contextutil.GetCurrentKafkaInstance(f)
if err != nil {
return err
}

opts.id = kafkaInstance.GetId()
opts.id = opts.kafkaInstance.GetId()

return runDescribe(opts)
},
Expand Down Expand Up @@ -99,34 +100,36 @@ func runDescribe(opts *options) error {

api := conn.API()

var kafkaInstance *kafkamgmtclient.KafkaRequest
var httpRes *http.Response
if opts.name != "" {
kafkaInstance, httpRes, err = kafkautil.GetKafkaByName(opts.Context, api.KafkaMgmt(), opts.name)
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
return err
}
} else {
kafkaInstance, httpRes, err = kafkautil.GetKafkaByID(opts.Context, api.KafkaMgmt(), opts.id)
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
return err

if opts.kafkaInstance == nil {
if opts.name != "" {
opts.kafkaInstance, httpRes, err = kafkautil.GetKafkaByName(opts.Context, api.KafkaMgmt(), opts.name)
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
return err
}
} else {
opts.kafkaInstance, httpRes, err = kafkautil.GetKafkaByID(opts.Context, api.KafkaMgmt(), opts.id)
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
return err
}
}
}

if opts.bootstrapServer {
if host, ok := kafkaInstance.GetBootstrapServerHostOk(); ok {
if host, ok := opts.kafkaInstance.GetBootstrapServerHostOk(); ok {
fmt.Fprintln(opts.IO.Out, *host)
return nil
}
opts.Logger.Info(opts.localizer.MustLocalize("kafka.describe.bootstrapserver.not.available", localize.NewEntry("Name", kafkaInstance.GetName())))
opts.Logger.Info(opts.localizer.MustLocalize("kafka.describe.bootstrapserver.not.available", localize.NewEntry("Name", opts.kafkaInstance.GetName())))
return nil
}

return dump.Formatted(opts.IO.Out, opts.outputFormat, kafkaInstance)
return dump.Formatted(opts.IO.Out, opts.outputFormat, opts.kafkaInstance)
}
38 changes: 20 additions & 18 deletions pkg/cmd/registry/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
)

type options struct {
id string
name string
force bool
id string
name string
force bool
registryInstance *srsmgmtv1client.Registry

IO *iostreams.IOStreams
Connection factory.ConnectionFunc
Expand All @@ -48,7 +49,7 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command {
Long: f.Localizer.MustLocalize("registry.cmd.delete.longDescription"),
Example: f.Localizer.MustLocalize("registry.cmd.delete.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (err error) {
if !opts.IO.CanPrompt() && !opts.force {
return flagutil.RequiredWhenNonInteractiveError("yes")
}
Expand All @@ -61,12 +62,12 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command {
return runDelete(opts)
}

registryInstance, err := contextutil.GetCurrentRegistryInstance(f)
opts.registryInstance, err = contextutil.GetCurrentRegistryInstance(f)
if err != nil {
return err
}

opts.id = registryInstance.GetId()
opts.id = opts.registryInstance.GetId()

return runDelete(opts)
},
Expand Down Expand Up @@ -98,20 +99,21 @@ func runDelete(opts *options) error {

api := conn.API()

var registry *srsmgmtv1client.Registry
if opts.name != "" {
registry, _, err = serviceregistryutil.GetServiceRegistryByName(opts.Context, api.ServiceRegistryMgmt(), opts.name)
if err != nil {
return err
}
} else {
registry, _, err = serviceregistryutil.GetServiceRegistryByID(opts.Context, api.ServiceRegistryMgmt(), opts.id)
if err != nil {
return err
if opts.registryInstance == nil {
if opts.name != "" {
opts.registryInstance, _, err = serviceregistryutil.GetServiceRegistryByName(opts.Context, api.ServiceRegistryMgmt(), opts.name)
if err != nil {
return err
}
} else {
opts.registryInstance, _, err = serviceregistryutil.GetServiceRegistryByID(opts.Context, api.ServiceRegistryMgmt(), opts.id)
if err != nil {
return err
}
}
}

registryName := registry.GetName()
registryName := opts.registryInstance.GetName()
opts.Logger.Info(opts.localizer.MustLocalize("registry.delete.log.info.deletingService", localize.NewEntry("Name", registryName)))
opts.Logger.Info("")

Expand All @@ -134,7 +136,7 @@ func runDelete(opts *options) error {

opts.Logger.Debug("Deleting Service registry", fmt.Sprintf("\"%s\"", registryName))

a := api.ServiceRegistryMgmt().DeleteRegistry(opts.Context, registry.GetId())
a := api.ServiceRegistryMgmt().DeleteRegistry(opts.Context, opts.registryInstance.GetId())
_, err = a.Execute()

if err != nil {
Expand Down
34 changes: 19 additions & 15 deletions pkg/cmd/registry/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ type options struct {
logger logging.Logger
Context context.Context
ServiceContext servicecontext.IContext

registryInstance *srsmgmtv1.Registry
}

// NewDescribeCommand describes a service instance, either by passing an `--id flag`
// or by using the service instance set in the current context, if any
func NewDescribeCommand(f *factory.Factory) *cobra.Command {

opts := &options{
Connection: f.Connection,
IO: f.IOStreams,
Expand All @@ -49,7 +52,7 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {
Long: f.Localizer.MustLocalize("registry.cmd.describe.longDescription"),
Example: f.Localizer.MustLocalize("registry.cmd.describe.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (err error) {
validOutputFormats := flagutil.ValidOutputFormats
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
Expand All @@ -63,12 +66,12 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {
return runDescribe(opts)
}

registryInstance, err := contextutil.GetCurrentRegistryInstance(f)
opts.registryInstance, err = contextutil.GetCurrentRegistryInstance(f)
if err != nil {
return err
}

opts.id = registryInstance.GetId()
opts.id = opts.registryInstance.GetId()

return runDescribe(opts)
},
Expand All @@ -91,24 +94,25 @@ func runDescribe(opts *options) error {

api := conn.API()

var registry *srsmgmtv1.Registry
if opts.name != "" {
registry, _, err = serviceregistryutil.GetServiceRegistryByName(opts.Context, api.ServiceRegistryMgmt(), opts.name)
if err != nil {
return err
}
} else {
registry, _, err = serviceregistryutil.GetServiceRegistryByID(opts.Context, api.ServiceRegistryMgmt(), opts.id)
if err != nil {
return err
if opts.registryInstance == nil {
if opts.name != "" {
opts.registryInstance, _, err = serviceregistryutil.GetServiceRegistryByName(opts.Context, api.ServiceRegistryMgmt(), opts.name)
if err != nil {
return err
}
} else {
opts.registryInstance, _, err = serviceregistryutil.GetServiceRegistryByID(opts.Context, api.ServiceRegistryMgmt(), opts.id)
if err != nil {
return err
}
}
}

if err := dump.Formatted(opts.IO.Out, opts.outputFormat, registry); err != nil {
if err := dump.Formatted(opts.IO.Out, opts.outputFormat, opts.registryInstance); err != nil {
return err
}

compatibleEndpoints := registrycmdutil.GetCompatibilityEndpoints(registry.GetRegistryUrl())
compatibleEndpoints := registrycmdutil.GetCompatibilityEndpoints(opts.registryInstance.GetRegistryUrl())

opts.logger.Info(opts.localizer.MustLocalize(
"registry.common.log.message.compatibleAPIs",
Expand Down

0 comments on commit 28d9c88

Please sign in to comment.