From ddd600f62aedf4575f8ed3b8459b52946ba2b4df Mon Sep 17 00:00:00 2001 From: Ramakrishna Pattnaik Date: Thu, 12 Jan 2023 16:29:41 +0530 Subject: [PATCH] fix(connector update): populate kafka url (#1786) --- docs/commands/rhoas_connector_update.md | 13 +++--- pkg/cmd/connector/create/create.go | 14 +++++- pkg/cmd/connector/update/update.go | 44 ++++++++++++++----- .../localize/locales/en/cmd/connectors.toml | 11 +++-- 4 files changed, 60 insertions(+), 22 deletions(-) diff --git a/docs/commands/rhoas_connector_update.md b/docs/commands/rhoas_connector_update.md index 530cd32b9..23b854b16 100644 --- a/docs/commands/rhoas_connector_update.md +++ b/docs/commands/rhoas_connector_update.md @@ -23,20 +23,21 @@ rhoas connector update [flags] ### Examples ``` -# Update a Connectors instance -rhoas connector update --id=my-connector --file=myconnector.json +# Update name of the current Connectors instance +rhoas connector update --name=my-connector -# Update a Connectors instance from stdin -cat myconnector.json | rhoas connector update +# Update Kafka Instance of a Connectors instance by ID +rhoas connector update --kafka-id ce6pg07k09f3rs6us7sg --id ce6tgb1mk0orirpo5i70 ``` ### Options ``` - --kafka-id string ID of the namespace in which you want to deploy the Connectors instance + --id string ID of the Connectors instance to be updated (the default is the instance in current context) + --kafka-id string ID of of the Kafka instance that you want the Connectors instance to use --name string Override the name of the Connectors instance (the default name is the name specified in the connector configuration file) - --namespace-id string ID of of the Kafka instance that you want the Connectors instance to use + --namespace-id string ID of the namespace in which you want to deploy the Connectors instance -o, --output string Specify the output format. Choose from: "json", "yaml", "yml" ``` diff --git a/pkg/cmd/connector/create/create.go b/pkg/cmd/connector/create/create.go index 0363a9987..75cf0ff77 100644 --- a/pkg/cmd/connector/create/create.go +++ b/pkg/cmd/connector/create/create.go @@ -164,9 +164,21 @@ func createServiceAccount(opts *factory.Factory, shortDescription string) (*svca } func setDefaultValuesFromFlags(connector *connectormgmtclient.ConnectorRequest, opts *options) error { + + conn, err := opts.f.Connection() + if err != nil { + return err + } + if opts.kafkaId != "" { + kafkaInstance, _, kafkaErr := kafkautil.GetKafkaByID(opts.f.Context, conn.API().KafkaMgmt(), opts.kafkaId) + if kafkaErr != nil { + return kafkaErr + } + connector.Kafka = connectormgmtclient.KafkaConnectionSettings{ - Id: opts.kafkaId, + Id: opts.kafkaId, + Url: kafkaInstance.GetBootstrapServerHost(), } } diff --git a/pkg/cmd/connector/update/update.go b/pkg/cmd/connector/update/update.go index 6265b96b4..4b58a48fc 100644 --- a/pkg/cmd/connector/update/update.go +++ b/pkg/cmd/connector/update/update.go @@ -2,9 +2,11 @@ package update import ( "encoding/json" + "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" "github.com/redhat-developer/app-services-cli/pkg/shared/connectorutil" "github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" + "github.com/redhat-developer/app-services-cli/pkg/shared/kafkautil" "github.com/spf13/cobra" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump" @@ -20,6 +22,7 @@ type options struct { namespaceID string kafkaID string name string + id string outputFormat string f *factory.Factory @@ -43,13 +46,30 @@ func NewUpdateCommand(f *factory.Factory) *cobra.Command { return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...) } + if opts.id != "" { + return runUpdate(opts) + } + + conn, err := opts.f.Connection() + if err != nil { + return err + } + + connector, err := contextutil.GetCurrentConnectorInstance(&conn, opts.f) + if err != nil { + return err + } + + opts.id = connector.GetId() + return runUpdate(opts) }, } flags := flagutil.NewFlagSet(cmd, f.Localizer) + flags.StringVar(&opts.id, "id", "", f.Localizer.MustLocalize("connector.flag.id.description")) flags.StringVar(&opts.name, "name", "", f.Localizer.MustLocalize("connector.flag.name.description")) - flags.StringVar(&opts.namespaceID, "namespace-id", "", f.Localizer.MustLocalize("connector.flag.kafkaID.description")) - flags.StringVar(&opts.kafkaID, "kafka-id", "", f.Localizer.MustLocalize("connector.flag.namespaceID.description")) + flags.StringVar(&opts.namespaceID, "namespace-id", "", f.Localizer.MustLocalize("connector.flag.namespaceID.description")) + flags.StringVar(&opts.kafkaID, "kafka-id", "", f.Localizer.MustLocalize("connector.flag.kafkaID.description")) flags.AddOutput(&opts.outputFormat) return cmd @@ -63,13 +83,10 @@ func runUpdate(opts *options) error { return err } - api := conn.API() - - connector, err := contextutil.GetCurrentConnectorInstance(&conn, opts.f) - if err != nil || connector == nil { - if connector, err = connectorutil.InteractiveSelect(conn, opts.f); err != nil { - return err - } + connectorsApi := conn.API().ConnectorsMgmt() + connector, err := connectorutil.GetConnectorByID(&connectorsApi, opts.id, opts.f) + if err != nil { + return err } connectorChanged := false @@ -82,7 +99,12 @@ func runUpdate(opts *options) error { connectorChanged = true } if opts.kafkaID != "" { - connector.Kafka.SetId(opts.kafkaID) + kafkaInstance, _, kafkaErr := kafkautil.GetKafkaByID(opts.f.Context, conn.API().KafkaMgmt(), opts.kafkaID) + if kafkaErr != nil { + return kafkaErr + } + connector.Kafka.SetId(kafkaInstance.GetId()) + connector.Kafka.SetUrl(kafkaInstance.GetBootstrapServerHost()) connectorChanged = true } @@ -105,7 +127,7 @@ func runUpdate(opts *options) error { return err } - a := api.ConnectorsMgmt().ConnectorsApi.PatchConnector(opts.f.Context, connector.GetId()) + a := conn.API().ConnectorsMgmt().ConnectorsApi.PatchConnector(opts.f.Context, connector.GetId()) a = a.Body(patchData) updated, httpRes, err := a.Execute() if httpRes != nil { diff --git a/pkg/core/localize/locales/en/cmd/connectors.toml b/pkg/core/localize/locales/en/cmd/connectors.toml index a7ed6f346..3e2371977 100644 --- a/pkg/core/localize/locales/en/cmd/connectors.toml +++ b/pkg/core/localize/locales/en/cmd/connectors.toml @@ -399,11 +399,11 @@ After you edit the configuration file, use the "connector update" command to upd [connector.update.cmd.example] one = ''' -# Update a Connectors instance -rhoas connector update --id=my-connector --file=myconnector.json +# Update name of the current Connectors instance +rhoas connector update --name=my-connector -# Update a Connectors instance from stdin -cat myconnector.json | rhoas connector update +# Update Kafka Instance of a Connectors instance by ID +rhoas connector update --kafka-id ce6pg07k09f3rs6us7sg --id ce6tgb1mk0orirpo5i70 ''' [connector.update.info.success] @@ -424,6 +424,9 @@ one = 'ID of of the Kafka instance that you want the Connectors instance to use' [connector.flag.namespace.description] one = 'ID of the namespace for the Connectors instance (the default is the namespace for the current context)' +[connector.flag.id.description] +one = 'ID of the Connectors instance to be updated (the default is the instance in current context)' + [connector.flag.name.description] one = 'Override the name of the Connectors instance (the default name is the name specified in the connector configuration file)'