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

feat: group create, get, list command #1849

Open
wants to merge 1 commit into
base: main
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
132 changes: 132 additions & 0 deletions pkg/cmd/registry/group/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package create

import (
"github.com/AlecAivazis/survey/v2"
"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/ioutil/icon"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
registryinstanceclient "github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/registryinstance/apiv1internal/client"
"github.com/spf13/cobra"
"k8s.io/utils/strings/slices"

"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
)

type options struct {
registryID string

groupId string
description string
properties map[string]string

f *factory.Factory
}

// NewCreateCommand creates a new command to create a new artifact group
func NewCreateCommand(f *factory.Factory) *cobra.Command {

opts := &options{
f: f,
}

cmd := &cobra.Command{
Use: "create",
Short: f.Localizer.MustLocalize("group.create.cmd.description.short"),
Long: f.Localizer.MustLocalize("group.create.cmd.description.long"),
Example: f.Localizer.MustLocalize("group.create.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
var missingFlags []string

if opts.groupId == "" {
missingFlags = append(missingFlags, "group-id")
}

if !opts.f.IOStreams.CanPrompt() && len(missingFlags) > 0 {
return flagutil.RequiredWhenNonInteractiveError(missingFlags...)
}

if len(missingFlags) > 0 {
err = runInteractivePrompt(opts, missingFlags)
if err != nil {
return err
}
}

if opts.registryID != "" {
return runCreate(opts)
}

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

opts.registryID = registryInstance.GetId()

return runCreate(opts)
},
}

flags := rulecmdutil.NewFlagSet(cmd, f)

flags.StringVarP(&opts.groupId, "group-id", "g", "", opts.f.Localizer.MustLocalize("group.cmd.create.flag.group-id"))
flags.StringVarP(&opts.description, "description", "d", "", opts.f.Localizer.MustLocalize("group.cmd.create.flag.description"))
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't currently have shorthand for description and properties currently.

flags.StringToStringVarP(&opts.properties, "properties", "p", map[string]string{}, opts.f.Localizer.MustLocalize("group.cmd.create.flag.properties"))
Copy link
Contributor

Choose a reason for hiding this comment

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

--properties doesn't work as expected.

> ./rhoas service-registry group create --group-id rama-test-104 --properties "balance=100,max:90990" --description "[]" --instance-id 79fb0b01-cc23-4e06-81f4-47004b74211e
{
  "createdBy": "rpattnai",
  "createdOn": "2023-04-13T05:50:32Z",
  "description": "[]",
  "id": "rama-test-104",
  "modifiedBy": "",
  "modifiedOn": "1970-01-01T00:00:00Z",
  "properties": {
    "balance": "100,max:90990"
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

WIll suggest to check the implementation in connector cluster update command.


flags.AddRegistryInstance(&opts.registryID)

return cmd

}

func runCreate(opts *options) error {
conn, err := opts.f.Connection()
if err != nil {
return err
}

api := conn.API()

a, _, err := api.ServiceRegistryInstance(opts.registryID)
if err != nil {
return err
}
request := a.GroupsApi.CreateGroup(opts.f.Context)

createGroupMetaData := registryinstanceclient.CreateGroupMetaData{
Id: opts.groupId,
Description: &opts.description,
Properties: &opts.properties,
}

request = request.CreateGroupMetaData(createGroupMetaData)

_, _, err = request.Execute()
if err != nil {
return registrycmdutil.TransformInstanceError(err)
}

opts.f.Logger.Info(icon.SuccessPrefix(), opts.f.Localizer.MustLocalize("group.cmd.create.log.info.created", localize.NewEntry("GroupId", opts.groupId)))

return nil
}

func runInteractivePrompt(opts *options, missingFlags []string) (err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The interactive mode allows --group-id to be "". Is this expected? Should we add some validations?


if slices.Contains(missingFlags, "group-id") {
settingNamePrompt := &survey.Input{
Message: opts.f.Localizer.MustLocalize("group.cmd.create.input.group-id.message"),
}

err = survey.AskOne(settingNamePrompt, &opts.groupId)
if err != nil {
return err
}
}

return nil
}
122 changes: 122 additions & 0 deletions pkg/cmd/registry/group/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package get

import (
"github.com/AlecAivazis/survey/v2"
"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/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
"k8s.io/utils/strings/slices"

"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
)

type options struct {
registryID string

groupId string
outputFormat string

f *factory.Factory
}

// NewGetCommand creates a new command to get an artifacts group metadata
func NewGetCommand(f *factory.Factory) *cobra.Command {

opts := &options{
f: f,
}

cmd := &cobra.Command{
Use: "get",
Short: f.Localizer.MustLocalize("group.get.cmd.description.short"),
Long: f.Localizer.MustLocalize("group.get.cmd.description.long"),
Example: f.Localizer.MustLocalize("group.get.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
var missingFlags []string

if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, flagutil.ValidOutputFormats...) {
return flagutil.InvalidValueError("output", opts.outputFormat, flagutil.ValidOutputFormats...)
}

if opts.groupId == "" {
missingFlags = append(missingFlags, "group-id")
}

if !opts.f.IOStreams.CanPrompt() && len(missingFlags) > 0 {
return flagutil.RequiredWhenNonInteractiveError(missingFlags...)
}

if len(missingFlags) > 0 {
err = runInteractivePrompt(opts, missingFlags)
if err != nil {
return err
}
}

if opts.registryID != "" {
return runGet(opts)
}

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

opts.registryID = registryInstance.GetId()

return runGet(opts)
},
}

flags := rulecmdutil.NewFlagSet(cmd, f)

flags.StringVarP(&opts.groupId, "group-id", "g", "", opts.f.Localizer.MustLocalize("group.cmd.get.flag.group-id"))
flags.AddOutput(&opts.outputFormat)

flags.AddRegistryInstance(&opts.registryID)

return cmd

}

func runGet(opts *options) error {
conn, err := opts.f.Connection()
if err != nil {
return err
}

api := conn.API()

a, _, err := api.ServiceRegistryInstance(opts.registryID)
if err != nil {
return err
}
request := a.GroupsApi.GetGroupById(opts.f.Context, opts.groupId)

groupMetaData, _, err := request.Execute()
if err != nil {
return registrycmdutil.TransformInstanceError(err)
}

return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, groupMetaData)
}

func runInteractivePrompt(opts *options, missingFlags []string) (err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have interactive mode for get operations, it will be better to remove it.


if slices.Contains(missingFlags, "group-id") {
settingNamePrompt := &survey.Input{
Message: opts.f.Localizer.MustLocalize("group.cmd.get.input.group-id.message"),
}

err = survey.AskOne(settingNamePrompt, &opts.groupId)
if err != nil {
return err
}
}

return nil
}
29 changes: 29 additions & 0 deletions pkg/cmd/registry/group/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package group

import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/group/create"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/group/get"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/group/list"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)

func NewGroupCommand(f *factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "group",
Short: f.Localizer.MustLocalize("group.cmd.description.short"),
Long: f.Localizer.MustLocalize("group.cmd.description.long"),
Example: f.Localizer.MustLocalize("group.cmd.example"),
Args: cobra.MinimumNArgs(1),
Hidden: true,
}

// add sub-commands
cmd.AddCommand(
list.NewListCommand(f),
create.NewCreateCommand(f),
get.NewGetCommand(f),
)

return cmd
}
Loading