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

Connector type list #1641

Merged
merged 4 commits into from
Jul 14, 2022
Merged
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
1 change: 1 addition & 0 deletions docs/commands/rhoas_connector.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions docs/commands/rhoas_connector_type.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions docs/commands/rhoas_connector_type_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pkg/errors v0.9.1
github.com/redhat-developer/app-services-sdk-go/accountmgmt v0.2.0
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.7.0
github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.8.0
github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.6.0
github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.12.0
github.com/redhat-developer/app-services-sdk-go/registryinstance v0.3.1
Expand All @@ -38,11 +38,9 @@ require (
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c
gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2
golang.org/x/oauth2 v0.0.0-20220630143837-2104d58473e0
golang.org/x/text v0.3.7
golang.org/x/tools v0.1.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/segmentio/analytics-go.v3 v3.1.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.22.4
Expand Down
165 changes: 155 additions & 10 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pkg/cmd/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connector
import (
"github.com/redhat-developer/app-services-cli/internal/doc"
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/cluster"
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connector_type"
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/create"
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/delete"
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/describe"
Expand Down Expand Up @@ -36,6 +37,8 @@ func NewConnectorsCommand(f *factory.Factory) *cobra.Command {
use.NewUseCommand(f),
start.NewStartCommand(f),
stop.NewStopCommand(f),
connector_type.NewTypeCommand(f),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wrong naming format :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is due to type being a reserved word in go. I just went with connector_type instead.


// Hidden for the users and docs at the moment
create.NewCreateCommand(f),
delete.NewDeleteCommand(f),
Expand Down
128 changes: 128 additions & 0 deletions pkg/cmd/connector/connector_type/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package list

import (
"fmt"
"net/http"
"strconv"

"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil"
"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/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"
"github.com/spf13/cobra"
)

const (
DefaultSearch = ""
)

type options struct {
search string
limit int
page int
outputFormat string

f *factory.Factory
}

type connectorOutput struct {
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
Description string `json:"description,omitempty"`
}

// NewListCommand creates a new command to list connector types
func NewListCommand(f *factory.Factory) *cobra.Command {

opts := &options{
f: f,
}

cmd := &cobra.Command{
Use: "list",
Short: f.Localizer.MustLocalize("connector.type.list.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.type.list.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.type.list.cmd.example"),
RunE: func(cmd *cobra.Command, args []string) error {

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

return runUpdateCommand(opts)
},
}

flags := connectorcmdutil.NewFlagSet(cmd, f)
flags.AddOutput(&opts.outputFormat)
flags.IntVar(&opts.limit, "limit", 150, f.Localizer.MustLocalize("connector.type.list.flag.page.description"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should use build package limits here as in other implementations

flags.StringVar(&opts.search, "search", DefaultSearch, f.Localizer.MustLocalize("connector.type.list.flag.search.description"))
flags.IntVar(&opts.page, "page", 1, f.Localizer.MustLocalize("connector.type.list.flag.page.description"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is pagination starting from 0? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

afaik 1 is the start and when passing 0 it just gives the same output as 1 anyway

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes. That is common pattern

return cmd

}

func runUpdateCommand(opts *options) error {
f := opts.f

var conn connection.Connection
conn, err := f.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}

api := conn.API()

request := api.ConnectorsMgmt().ConnectorTypesApi.GetConnectorTypes(f.Context)
request = request.Page(strconv.Itoa(opts.page))
request = request.Size(strconv.Itoa(opts.limit))

if opts.search != DefaultSearch {
query := fmt.Sprintf("name like %s", opts.search)
jackdelahunt marked this conversation as resolved.
Show resolved Hide resolved
request = request.Search(query)
}

types, httpRes, err := request.Execute()

if httpRes != nil {
defer httpRes.Body.Close()
}

if err != nil {
switch httpRes.StatusCode {
case http.StatusUnauthorized:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is antipattern. We should not use status codes for validation. We should use error codes.
See kafka create for example

return opts.f.Localizer.MustLocalizeError("connector.common.error.unauthorized")
case http.StatusInternalServerError:
return opts.f.Localizer.MustLocalizeError("connector.common.error.internalServerError")
default:
return err
}
}

rows := mapResponseToConnectorTypes(&types)
for i := 0; i < len(rows); i++ {
if err = dump.Formatted(f.IOStreams.Out, opts.outputFormat, rows[i]); err != nil {
return err
}
}

return nil
}

func mapResponseToConnectorTypes(list *connectormgmtclient.ConnectorTypeList) []connectorOutput {
types := make([]connectorOutput, len(list.Items))

for i := 0; i < len(list.Items); i++ {
item := &list.Items[i]
types[i] = connectorOutput{
Name: item.GetName(),
Id: item.GetId(),
Description: item.GetDescription(),
}
}

return types
}
28 changes: 28 additions & 0 deletions pkg/cmd/connector/connector_type/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package connector_type

import (
"github.com/redhat-developer/app-services-cli/internal/doc"

"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connector_type/list"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)

// NewTypeCommand creates a new command to use different connector types
func NewTypeCommand(f *factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "type",
Annotations: map[string]string{doc.AnnotationName: "Connectors commands"},
Short: f.Localizer.MustLocalize("connector.type.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.type.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.type.cmd.example"),
Args: cobra.MinimumNArgs(1),
}

// add sub-commands
cmd.AddCommand(
list.NewListCommand(f),
)

return cmd
}
68 changes: 67 additions & 1 deletion pkg/core/localize/locales/en/cmd/connectors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,70 @@ one = "Connector instance with ID {{.ID}} not found"

[connector.common.error.nameNotFound]
description = "Error message for when and connector with name is not found"
one = "Connector instance with name {{.Name}} not found"
one = "Connector instance with name {{.Name}} not found"

[connector.type.cmd.shortDescription]
description = "Short description of connector type command"
one = 'List and get details of different connector types'

[connector.type.cmd.longDescription]
description = "Long description of connector type command"
one = '''List, search and get details of connector types that are available to use in the connector catalog.

Use list to list available connector types.

Use describe to get more details about a specific connector type.
'''

[connector.type.cmd.example]
description = "Example commands for connector type"
one = '''
# List all connector types
rhoas connector type list

# List all connector types that start with 'Amazon'
rhoas connector type list --search=Amazon%

# Get more details of connector type with a type id of IEJF87hg2342hsdHFG
rhoas connector type describe --id=IEJF87hg2342hsdHFG
'''

[connector.type.list.cmd.shortDescription]
description = "Short description of connector type list command"
one = 'List connector types'

[connector.type.list.cmd.longDescription]
description = "Long description of connector type list command"
one = 'List connector types available from the catalog, use filter options such as --limit, --page and --search'

[connector.type.list.cmd.example]
description = "Example commands for connector type list"
one = '''
# List all connector types
rhoas connector type list

# List connector types with a limit of 10 from the second page
rhoas connector type list --limit=10 --page=2

# List all connector types that start with 'Amazon'
rhoas connector type list --search=Amazon%

# List all connector types that contain the word 'Amazon'
rhoas connector type list --search=%Amazon%

'''

[connector.type.list.flag.page.description]
one = 'Page of connector limit'

[connector.type.list.flag.search.description]
one = 'Search query for name of connector type'

[connector.type.list.flag.limit.description]
one = 'The max number of connectors to return in the page'

[connector.common.error.unauthorized]
one = 'Auth token provided is invalid'

[connector.common.error.internalServerError]
one = 'Unexpected internal server error occured'