Skip to content

Commit

Permalink
Make gateway get more generic by using openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl committed Aug 5, 2024
1 parent 7327d31 commit ea1da8a
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 243 deletions.
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestGetShouldWork(t *testing.T) {
)

app := client.GetKinds()["Application"]
result, err := client.Get(&app, []string{})
result, err := client.Get(&app, []string{}, nil)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestGetShouldFailIfN2xx(t *testing.T) {
)

app := client.GetKinds()["Application"]
_, err = client.Get(&app, []string{})
_, err = client.Get(&app, []string{}, nil)
if err == nil {
t.Failed()
}
Expand Down
8 changes: 6 additions & 2 deletions client/console_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,15 @@ func (client *Client) Apply(resource *resource.Resource, dryMode bool) (string,
return upsertResponse.UpsertResult, nil
}

func (client *Client) Get(kind *schema.Kind, parentPathValue []string) ([]resource.Resource, error) {
func (client *Client) Get(kind *schema.Kind, parentPathValue []string, queryParams map[string]string) ([]resource.Resource, error) {
var result []resource.Resource
client.setApiKeyFromEnvIfNeeded()
url := client.baseUrl + kind.ListPath(parentPathValue)
resp, err := client.client.R().Get(url)
requestBuilder := client.client.R()
if queryParams != nil {
requestBuilder = requestBuilder.SetQueryParams(queryParams)
}
resp, err := requestBuilder.Get(url)
if err != nil {
return result, err
} else if resp.IsError() {
Expand Down
52 changes: 5 additions & 47 deletions client/gateway_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,56 +74,14 @@ func MakeGatewayClientFromEnv() (*GatewayClient, error) {
return client, nil
}

func (client *GatewayClient) Get(kind *schema.Kind, parentPathValue []string) ([]resource.Resource, error) {
func (client *GatewayClient) Get(kind *schema.Kind, parentPathValue []string, queryParams map[string]string) ([]resource.Resource, error) {
var result []resource.Resource
url := client.baseUrl + kind.ListPath(parentPathValue)
resp, err := client.client.R().Get(url)
if err != nil {
return result, err
} else if resp.IsError() {
return result, fmt.Errorf(extractApiError(resp))
}
err = json.Unmarshal(resp.Body(), &result)
return result, err
}

func (client *GatewayClient) ListKindWithFilters(kind *schema.Kind, param map[string]string) ([]resource.Resource, error) {
var result []resource.Resource
url := client.baseUrl + kind.ListPath(nil)
req := client.client.R()
req.SetQueryParams(param)
resp, err := req.Get(url)
if err != nil {
return result, err
} else if resp.IsError() {
return result, fmt.Errorf(extractApiError(resp))
}
err = json.Unmarshal(resp.Body(), &result)
return result, err
}

func (client *GatewayClient) ListInterceptorsFilters(kind *schema.Kind, name string, global bool, vCluster string, group string, username string) ([]resource.Resource, error) {
var result []resource.Resource
url := client.baseUrl + kind.ListPath(nil)
req := client.client.R()
queryParams := make(map[string]string)
if name != "" {
queryParams["name"] = name
}
if vCluster != "" {
queryParams["vCluster"] = vCluster
}
if group != "" {
queryParams["group"] = group
}
if username != "" {
queryParams["username"] = username
}
if global {
queryParams["global"] = "true"
requestBuilder := client.client.R()
if queryParams != nil {
requestBuilder = requestBuilder.SetQueryParams(queryParams)
}
req.SetQueryParams(queryParams)
resp, err := req.Get(url)
resp, err := requestBuilder.Get(url)
if err != nil {
return result, err
} else if resp.IsError() {
Expand Down
4 changes: 2 additions & 2 deletions client/gateway_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestGwGetShouldWork(t *testing.T) {
)

vClusterKind := gatewayClient.GetKinds()["VClusters"]
result, err := gatewayClient.Get(&vClusterKind, []string{})
result, err := gatewayClient.Get(&vClusterKind, []string{}, nil)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestGwGetShouldFailIfN2xx(t *testing.T) {
)

vClusterKind := gatewayClient.GetKinds()["VClusters"]
_, err = gatewayClient.Get(&vClusterKind, []string{})
_, err = gatewayClient.Get(&vClusterKind, []string{}, nil)
if err == nil {
t.Failed()
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func initDelete(kinds schema.KindCatalog) {
interceptorsDeleteCmd := buildDeleteInterceptorsCmd(kind)
deleteCmd.AddCommand(interceptorsDeleteCmd)
} else {
flags := kind.GetFlag()
flags := kind.GetParentFlag()
parentFlagValue := make([]*string, len(flags))
kindCmd := &cobra.Command{
Use: fmt.Sprintf("%s [name]", name),
Expand All @@ -83,7 +83,7 @@ func initDelete(kinds schema.KindCatalog) {
}
},
}
for i, flag := range kind.GetFlag() {
for i, flag := range kind.GetParentFlag() {
parentFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag)
kindCmd.MarkFlagRequired(flag)
}
Expand Down
145 changes: 98 additions & 47 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package cmd
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/conduktor/ctl/resource"
"github.com/conduktor/ctl/schema"
"github.com/conduktor/ctl/utils"
"github.com/spf13/cobra"
)

Expand All @@ -20,62 +23,110 @@ var getCmd = &cobra.Command{
},
}

func buildQueryParams(params map[string]interface{}) map[string]string {
queryParams := make(map[string]string)
for key, value := range params {
if value != nil {
str, strOk := value.(*string)
boolValue, boolOk := value.(*bool)

if strOk {
if *str != "" {
queryParams[key] = *str
}
} else if boolOk {
queryParams[key] = strconv.FormatBool(*boolValue)
} else {
panic("Unknown query flag type")
}
}
}
return queryParams
}

func removeTrailingSIfAny(name string) string {
return strings.TrimSuffix(name, "s")
}

func buildAlias(name string) []string {
return []string{strings.ToLower(name), removeTrailingSIfAny(strings.ToLower(name)), removeTrailingSIfAny(name)}
}

func initGet(kinds schema.KindCatalog) {
rootCmd.AddCommand(getCmd)

for name, kind := range kinds {
if isKindIdentifiedByNameAndVCluster(kind) {
byVClusterAndNamGetCmd := buildListFilteredByVClusterOrNameCmd(kind)
getCmd.AddCommand(byVClusterAndNamGetCmd)
} else if isKindInterceptor(kind) {
interceptorsGetCmd := buildListFilteredInterceptorCmd(kind)
getCmd.AddCommand(interceptorsGetCmd)
} else {
flags := kind.GetFlag()
parentFlagValue := make([]*string, len(flags))
kindCmd := &cobra.Command{
Use: fmt.Sprintf("%s [name]", name),
Short: "Get resource of kind " + name,
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
Long: `If name not provided it will list all resource`,
Aliases: buildAlias(name),
Run: func(cmd *cobra.Command, args []string) {
parentValue := make([]string, len(parentFlagValue))
for i, v := range parentFlagValue {
parentValue[i] = *v
gatewayKind, isGatewayKind := kind.GetLatestKindVersion().(*schema.GatewayKindVersion)
args := cobra.MaximumNArgs(1)
use := fmt.Sprintf("%s [name]", name)
if isGatewayKind && !gatewayKind.GetAvailable {
args = cobra.NoArgs
use = fmt.Sprintf("%s", name)
}
parentFlags := kind.GetParentFlag()
listFlags := kind.GetListFlag()
parentFlagValue := make([]*string, len(parentFlags))
listFlagValue := make(map[string]interface{}, len(listFlags))
kindCmd := &cobra.Command{
Use: use,
Short: "Get resource of kind " + name,
Args: args,
Long: `If name not provided it will list all resource`,
Aliases: buildAlias(name),
Run: func(cmd *cobra.Command, args []string) {
parentValue := make([]string, len(parentFlagValue))
queryParams := buildQueryParams(listFlagValue)
for i, v := range parentFlagValue {
parentValue[i] = *v
}
var err error
if len(args) == 0 {
var result []resource.Resource
if isGatewayKind {
result, err = gatewayApiClient().Get(&kind, parentValue, queryParams)
} else {
result, err = consoleApiClient().Get(&kind, parentValue, queryParams)
}
var err error
if len(args) == 0 {
var result []resource.Resource
if isGatewayKind(kind) {
result, err = gatewayApiClient().Get(&kind, parentValue)
} else {
result, err = consoleApiClient().Get(&kind, parentValue)
}
for _, r := range result {
r.PrintPreservingOriginalFieldOrder()
fmt.Println("---")
}
} else if len(args) == 1 {
var result resource.Resource
if isGatewayKind(kind) {
result, err = gatewayApiClient().Describe(&kind, parentValue, args[0])
} else {
result, err = consoleApiClient().Describe(&kind, parentValue, args[0])
}
result.PrintPreservingOriginalFieldOrder()
for _, r := range result {
r.PrintPreservingOriginalFieldOrder()
fmt.Println("---")
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
} else if len(args) == 1 {
var result resource.Resource
if isGatewayKind {
result, err = gatewayApiClient().Describe(&kind, parentValue, args[0])
} else {
result, err = consoleApiClient().Describe(&kind, parentValue, args[0])
}
},
result.PrintPreservingOriginalFieldOrder()
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
},
}
for i, flag := range parentFlags {
parentFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag)
kindCmd.MarkFlagRequired(flag)
}
for key, flag := range listFlags {
var flagSetted = false
if flag.Type == "string" {
flagSetted = true
listFlagValue[key] = kindCmd.Flags().String(flag.FlagName, "", "")
} else if flag.Type == "boolean" {
flagSetted = true
listFlagValue[key] = kindCmd.Flags().Bool(flag.FlagName, false, "")
} else {
if *debug || utils.CdkDebug() {
fmt.Fprintf(os.Stderr, "Unknown flag type %s\n", flag.Type)
}
}
for i, flag := range kind.GetFlag() {
parentFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag)
kindCmd.MarkFlagRequired(flag)
if flagSetted && flag.Required {
kindCmd.MarkFlagRequired(flag.FlagName)
}
getCmd.AddCommand(kindCmd)
}
getCmd.AddCommand(kindCmd)
}
}
Loading

0 comments on commit ea1da8a

Please sign in to comment.