From c9e13e4f70048819addbae5211d080b1b4210303 Mon Sep 17 00:00:00 2001 From: Ramakrishna Pattnaik Date: Fri, 4 Mar 2022 18:26:58 +0530 Subject: [PATCH] feat(context): add command for context creation --- docs/commands/rhoas_context.md | 1 + docs/commands/rhoas_context_create.md | 39 ++++++++ docs/commands/rhoas_context_status.md | 3 +- pkg/cmd/context/context.go | 2 + pkg/cmd/context/create/create.go | 98 +++++++++++++++++++ pkg/cmd/context/status/status.go | 6 +- pkg/cmd/context/use/use.go | 11 +++ pkg/cmd/registry/rule/describe/describe.go | 27 +++-- pkg/cmd/registry/rule/disable/disable.go | 29 ++++-- pkg/cmd/registry/rule/enable/enable.go | 36 ++++--- pkg/cmd/registry/rule/list/list.go | 3 - pkg/cmd/registry/rule/update/update.go | 35 ++++--- .../localize/locales/en/cmd/context.en.toml | 14 +++ 13 files changed, 257 insertions(+), 47 deletions(-) create mode 100644 docs/commands/rhoas_context_create.md create mode 100644 pkg/cmd/context/create/create.go diff --git a/docs/commands/rhoas_context.md b/docs/commands/rhoas_context.md index c11cee5c6..f5170d127 100644 --- a/docs/commands/rhoas_context.md +++ b/docs/commands/rhoas_context.md @@ -32,6 +32,7 @@ $ rhoas context use --name my-context ### SEE ALSO * [rhoas](rhoas.md) - RHOAS CLI +* [rhoas context create](rhoas_context_create.md) - Create context * [rhoas context list](rhoas_context_list.md) - List contexts * [rhoas context status](rhoas_context_status.md) - Show status of the context * [rhoas context use](rhoas_context_use.md) - Set the current context diff --git a/docs/commands/rhoas_context_create.md b/docs/commands/rhoas_context_create.md new file mode 100644 index 000000000..5bc999c70 --- /dev/null +++ b/docs/commands/rhoas_context_create.md @@ -0,0 +1,39 @@ +## rhoas context create + +Create context + +### Synopsis + +Create a context and assign service identifiers + +``` +rhoas context create [flags] +``` + +### Examples + +``` +## Create context with Kafka ID and Service Registry ID +$ rhoas context create --name my-context --kafka-id c8696ncpoj7gdjmaiqog --registry-id 0282d488-52b3-405b-9e30-9f6f9407de57 + +``` + +### Options + +``` + --kafka-id string Name of the context + --name string Name of the context + --registry-id string Name of the context +``` + +### Options inherited from parent commands + +``` + -h, --help Show help for a command + -v, --verbose Enable verbose mode +``` + +### SEE ALSO + +* [rhoas context](rhoas_context.md) - Group, share and manage your rhoas services + diff --git a/docs/commands/rhoas_context_status.md b/docs/commands/rhoas_context_status.md index f5f3590cd..07b658235 100644 --- a/docs/commands/rhoas_context_status.md +++ b/docs/commands/rhoas_context_status.md @@ -24,7 +24,8 @@ $ rhoas context status --name my-context ### Options ``` - --name string Name of the context + --name string Name of the context + -o, --output string Specify the output format. Choose from: "json", "yaml", "yml" ``` ### Options inherited from parent commands diff --git a/pkg/cmd/context/context.go b/pkg/cmd/context/context.go index bc2c738a0..41d61061c 100644 --- a/pkg/cmd/context/context.go +++ b/pkg/cmd/context/context.go @@ -1,6 +1,7 @@ package context import ( + "github.com/redhat-developer/app-services-cli/pkg/cmd/context/create" "github.com/redhat-developer/app-services-cli/pkg/cmd/context/list" "github.com/redhat-developer/app-services-cli/pkg/cmd/context/status" "github.com/redhat-developer/app-services-cli/pkg/cmd/context/use" @@ -21,6 +22,7 @@ func NewContextCmd(f *factory.Factory) *cobra.Command { use.NewUseCommand(f), status.NewStatusCommand(f), list.NewListCommand(f), + create.NewCreateCommand(f), ) return cmd } diff --git a/pkg/cmd/context/create/create.go b/pkg/cmd/context/create/create.go new file mode 100644 index 000000000..504184e6e --- /dev/null +++ b/pkg/cmd/context/create/create.go @@ -0,0 +1,98 @@ +package create + +import ( + "context" + "errors" + + "github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil" + "github.com/redhat-developer/app-services-cli/pkg/core/config" + "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" + "github.com/redhat-developer/app-services-cli/pkg/core/localize" + "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/profile" + "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" + "github.com/spf13/cobra" +) + +type options struct { + IO *iostreams.IOStreams + Config config.IConfig + Logger logging.Logger + Connection factory.ConnectionFunc + localizer localize.Localizer + Context context.Context + Profiles profile.IContext + + name string + kafkaID string + registryID string +} + +func NewCreateCommand(f *factory.Factory) *cobra.Command { + + opts := &options{ + Config: f.Config, + Connection: f.Connection, + IO: f.IOStreams, + Logger: f.Logger, + localizer: f.Localizer, + Profiles: f.Profile, + } + + cmd := &cobra.Command{ + Use: "create", + Short: f.Localizer.MustLocalize("context.create.cmd.shortDescription"), + Long: f.Localizer.MustLocalize("context.create.cmd.longDescription"), + Example: f.Localizer.MustLocalize("context.create.cmd.example"), + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return runCreate(opts) + }, + } + + flags := flagutil.NewFlagSet(cmd, opts.localizer) + + flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name")) + flags.StringVar(&opts.kafkaID, "kafka-id", "", opts.localizer.MustLocalize("context.common.flag.name")) + flags.StringVar(&opts.registryID, "registry-id", "", opts.localizer.MustLocalize("context.common.flag.name")) + + return cmd + +} + +func runCreate(opts *options) error { + + context, err := opts.Profiles.Load() + if err != nil { + return err + } + + profileHandler := &profileutil.ContextHandler{ + Context: context, + Localizer: opts.localizer, + } + + profiles := context.Contexts + + currentCtx, _ := profileHandler.GetContext(context.CurrentContext) + if currentCtx != nil { + return errors.New("context already exists") + } + + services := profile.ServiceConfig{ + KafkaID: opts.kafkaID, + ServiceRegistryID: opts.registryID, + } + + profiles[opts.name] = services + + context.Contexts = profiles + + err = opts.Profiles.Save(context) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/cmd/context/status/status.go b/pkg/cmd/context/status/status.go index 9653bb519..54e0f4972 100644 --- a/pkg/cmd/context/status/status.go +++ b/pkg/cmd/context/status/status.go @@ -25,7 +25,8 @@ type options struct { Context context.Context Profiles profile.IContext - name string + name string + outputFormat string } func NewStatusCommand(f *factory.Factory) *cobra.Command { @@ -53,6 +54,7 @@ func NewStatusCommand(f *factory.Factory) *cobra.Command { flags := flagutil.NewFlagSet(cmd, opts.localizer) flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("context.common.flag.name")) + flags.AddOutput(&opts.outputFormat) return cmd } @@ -85,7 +87,7 @@ func runStatus(opts *options) error { } } - err = dump.Formatted(stdout, "yml", currentCtx) + err = dump.Formatted(stdout, opts.outputFormat, currentCtx) if err != nil { return err } diff --git a/pkg/cmd/context/use/use.go b/pkg/cmd/context/use/use.go index f1421d544..81d40ade3 100644 --- a/pkg/cmd/context/use/use.go +++ b/pkg/cmd/context/use/use.go @@ -9,6 +9,7 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" "github.com/spf13/cobra" "github.com/redhat-developer/app-services-cli/pkg/core/profile" @@ -62,6 +63,16 @@ func runUse(opts *options) error { return err } + profileHandler := &profileutil.ContextHandler{ + Context: context, + Localizer: opts.localizer, + } + + _, err = profileHandler.GetContext(context.CurrentContext) + if err != nil { + return err + } + context.CurrentContext = opts.name err = opts.Profiles.Save(context) diff --git a/pkg/cmd/registry/rule/describe/describe.go b/pkg/cmd/registry/rule/describe/describe.go index 77546b840..35680d592 100644 --- a/pkg/cmd/registry/rule/describe/describe.go +++ b/pkg/cmd/registry/rule/describe/describe.go @@ -6,13 +6,14 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil" "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil" - "github.com/redhat-developer/app-services-cli/pkg/core/config" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/profile" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" "github.com/spf13/cobra" registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client" @@ -20,11 +21,11 @@ import ( type options struct { IO *iostreams.IOStreams - Config config.IConfig Connection factory.ConnectionFunc Logger logging.Logger localizer localize.Localizer Context context.Context + Profiles profile.IContext ruleType string @@ -39,11 +40,11 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command { opts := &options{ IO: f.IOStreams, - Config: f.Config, Connection: f.Connection, Logger: f.Logger, localizer: f.Localizer, Context: f.Context, + Profiles: f.Profile, } cmd := &cobra.Command{ @@ -63,17 +64,27 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command { return err } - cfg, err := opts.Config.Load() + context, err := opts.Profiles.Load() if err != nil { return err } - instanceID, ok := cfg.GetServiceRegistryIdOk() - if !ok { - return opts.localizer.MustLocalizeError("artifact.cmd.common.error.noServiceRegistrySelected") + profileHandler := &profileutil.ContextHandler{ + Context: context, + Localizer: opts.localizer, + } + + conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) + if err != nil { + return err + } + + registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + if err != nil { + return err } - opts.registryID = instanceID + opts.registryID = registryInstance.GetId() return runDescribe(opts) }, diff --git a/pkg/cmd/registry/rule/disable/disable.go b/pkg/cmd/registry/rule/disable/disable.go index 36fc38db3..489300875 100644 --- a/pkg/cmd/registry/rule/disable/disable.go +++ b/pkg/cmd/registry/rule/disable/disable.go @@ -8,24 +8,25 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil" "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil" "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" - "github.com/redhat-developer/app-services-cli/pkg/core/config" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/profile" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client" "github.com/spf13/cobra" ) type options struct { IO *iostreams.IOStreams - Config config.IConfig Connection factory.ConnectionFunc Logger logging.Logger localizer localize.Localizer Context context.Context + Profiles profile.IContext ruleType string @@ -41,11 +42,11 @@ func NewDisableCommand(f *factory.Factory) *cobra.Command { opts := &options{ IO: f.IOStreams, - Config: f.Config, Connection: f.Connection, Logger: f.Logger, localizer: f.Localizer, Context: f.Context, + Profiles: f.Profile, } cmd := &cobra.Command{ @@ -64,21 +65,31 @@ func NewDisableCommand(f *factory.Factory) *cobra.Command { Localizer: opts.localizer, } - cfg, err := opts.Config.Load() + if err = validator.ValidateRuleType(opts.ruleType); err != nil && opts.ruleType != "" { + return err + } + + context, err := opts.Profiles.Load() if err != nil { return err } - if err = validator.ValidateRuleType(opts.ruleType); err != nil && opts.ruleType != "" { + profileHandler := &profileutil.ContextHandler{ + Context: context, + Localizer: opts.localizer, + } + + conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) + if err != nil { return err } - instanceID, ok := cfg.GetServiceRegistryIdOk() - if !ok { - return opts.localizer.MustLocalizeError("artifact.cmd.common.error.noServiceRegistrySelected") + registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + if err != nil { + return err } - opts.registryID = instanceID + opts.registryID = registryInstance.GetId() return runDisable(opts) }, diff --git a/pkg/cmd/registry/rule/enable/enable.go b/pkg/cmd/registry/rule/enable/enable.go index 8bc222933..5540bf1d9 100644 --- a/pkg/cmd/registry/rule/enable/enable.go +++ b/pkg/cmd/registry/rule/enable/enable.go @@ -9,13 +9,14 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil" "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil" "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" - "github.com/redhat-developer/app-services-cli/pkg/core/config" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/profile" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" "github.com/spf13/cobra" registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client" @@ -23,11 +24,11 @@ import ( type options struct { IO *iostreams.IOStreams - Config config.IConfig Connection factory.ConnectionFunc Logger logging.Logger localizer localize.Localizer Context context.Context + Profiles profile.IContext ruleType string config string @@ -38,15 +39,16 @@ type options struct { } // NewEnableCommand creates a new command for enabling rule +// nolint:funlen func NewEnableCommand(f *factory.Factory) *cobra.Command { opts := &options{ IO: f.IOStreams, - Config: f.Config, Connection: f.Connection, Logger: f.Logger, localizer: f.Localizer, Context: f.Context, + Profiles: f.Profile, } cmd := &cobra.Command{ @@ -83,11 +85,6 @@ func NewEnableCommand(f *factory.Factory) *cobra.Command { return flagutil.RequiredWhenNonInteractiveError(missingFlags...) } - cfg, err := opts.Config.Load() - if err != nil { - return err - } - err = validator.ValidateRuleType(opts.ruleType) if err != nil { return err @@ -102,12 +99,27 @@ func NewEnableCommand(f *factory.Factory) *cobra.Command { ) } - instanceID, ok := cfg.GetServiceRegistryIdOk() - if !ok { - return opts.localizer.MustLocalizeError("artifact.cmd.common.error.noServiceRegistrySelected") + context, err := opts.Profiles.Load() + if err != nil { + return err + } + + profileHandler := &profileutil.ContextHandler{ + Context: context, + Localizer: opts.localizer, + } + + conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) + if err != nil { + return err + } + + registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + if err != nil { + return err } - opts.registryID = instanceID + opts.registryID = registryInstance.GetId() return runEnable(opts) }, diff --git a/pkg/cmd/registry/rule/list/list.go b/pkg/cmd/registry/rule/list/list.go index 0945152b3..d2d33d73a 100644 --- a/pkg/cmd/registry/rule/list/list.go +++ b/pkg/cmd/registry/rule/list/list.go @@ -7,7 +7,6 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil" "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil" - "github.com/redhat-developer/app-services-cli/pkg/core/config" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" "github.com/redhat-developer/app-services-cli/pkg/core/localize" @@ -42,7 +41,6 @@ type ruleRow struct { type options struct { IO *iostreams.IOStreams - Config config.IConfig Connection factory.ConnectionFunc Logger logging.Logger localizer localize.Localizer @@ -59,7 +57,6 @@ func NewListCommand(f *factory.Factory) *cobra.Command { opts := &options{ IO: f.IOStreams, - Config: f.Config, Connection: f.Connection, Logger: f.Logger, localizer: f.Localizer, diff --git a/pkg/cmd/registry/rule/update/update.go b/pkg/cmd/registry/rule/update/update.go index ffde79a67..41476ad48 100644 --- a/pkg/cmd/registry/rule/update/update.go +++ b/pkg/cmd/registry/rule/update/update.go @@ -7,13 +7,14 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil" "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil" "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil" - "github.com/redhat-developer/app-services-cli/pkg/core/config" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/logging" + "github.com/redhat-developer/app-services-cli/pkg/core/profile" "github.com/redhat-developer/app-services-cli/pkg/shared/connection" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/redhat-developer/app-services-cli/pkg/shared/profileutil" "github.com/spf13/cobra" registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client" @@ -21,11 +22,11 @@ import ( type options struct { IO *iostreams.IOStreams - Config config.IConfig Connection factory.ConnectionFunc Logger logging.Logger localizer localize.Localizer Context context.Context + Profiles profile.IContext ruleType string config string @@ -40,11 +41,11 @@ func NewUpdateCommand(f *factory.Factory) *cobra.Command { opts := &options{ IO: f.IOStreams, - Config: f.Config, Connection: f.Connection, Logger: f.Logger, localizer: f.Localizer, Context: f.Context, + Profiles: f.Profile, } cmd := &cobra.Command{ @@ -59,11 +60,6 @@ func NewUpdateCommand(f *factory.Factory) *cobra.Command { Localizer: opts.localizer, } - cfg, err := opts.Config.Load() - if err != nil { - return err - } - err = validator.ValidateRuleType(opts.ruleType) if err != nil { return err @@ -78,12 +74,27 @@ func NewUpdateCommand(f *factory.Factory) *cobra.Command { ) } - instanceID, ok := cfg.GetServiceRegistryIdOk() - if !ok { - return opts.localizer.MustLocalizeError("artifact.cmd.common.error.noServiceRegistrySelected") + context, err := opts.Profiles.Load() + if err != nil { + return err + } + + profileHandler := &profileutil.ContextHandler{ + Context: context, + Localizer: opts.localizer, + } + + conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) + if err != nil { + return err + } + + registryInstance, err := profileHandler.GetCurrentRegistryInstance(conn.API().ServiceRegistryMgmt()) + if err != nil { + return err } - opts.registryID = instanceID + opts.registryID = registryInstance.GetId() return runUpdate(opts) }, diff --git a/pkg/core/localize/locales/en/cmd/context.en.toml b/pkg/core/localize/locales/en/cmd/context.en.toml index bdf5fd36c..7dc7f378f 100644 --- a/pkg/core/localize/locales/en/cmd/context.en.toml +++ b/pkg/core/localize/locales/en/cmd/context.en.toml @@ -71,6 +71,20 @@ one=''' $ rhoas context list ''' +[context.create.cmd] + +[context.create.cmd.shortDescription] +one='Create context' + +[context.create.cmd.longDescription] +one='Create a context and assign service identifiers' + +[context.create.cmd.example] +one=''' +## Create context with Kafka ID and Service Registry ID +$ rhoas context create --name my-context --kafka-id c8696ncpoj7gdjmaiqog --registry-id 0282d488-52b3-405b-9e30-9f6f9407de57 +''' + [context.common.flag.name] one='Name of the context'