Skip to content

Commit

Permalink
Various improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl committed Jan 30, 2025
1 parent 70f6fcb commit 99c16b3
Show file tree
Hide file tree
Showing 24 changed files with 3,885 additions and 346 deletions.
9 changes: 6 additions & 3 deletions client/console_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,12 @@ func (client *Client) Get(kind *schema.Kind, parentPathValue []string, parentQue
return result, err
}

func (client *Client) Execute(execute schema.Execute, pathValue []string, queryParams map[string]string, body interface{}) ([]byte, error) {
func (client *Client) Run(run schema.Run, pathValue []string, queryParams map[string]string, body interface{}) ([]byte, error) {
if run.BackendType != schema.CONSOLE {
return nil, fmt.Errorf("Only console backend type is supported by console client")
}
client.setAuthMethodFromEnvIfNeeded()
path := execute.BuildPath(pathValue)
path := run.BuildPath(pathValue)
url := client.baseUrl + path
requestBuilder := client.client.R()
for k, v := range queryParams {
Expand All @@ -315,7 +318,7 @@ func (client *Client) Execute(execute schema.Execute, pathValue []string, queryP
if body != nil {
requestBuilder = requestBuilder.SetBody(body)
}
resp, err := requestBuilder.Execute(execute.GetMethod(), url)
resp, err := requestBuilder.Execute(run.Method, url)
if err != nil {
return nil, err
} else if resp.IsError() {
Expand Down
41 changes: 30 additions & 11 deletions client/gateway_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type GatewayClient struct {
cdkGatewayPassword string
baseUrl string
client *resty.Client
kinds schema.KindCatalog
schemaCatalog *schema.Catalog
}

type GatewayApiParameter struct {
Expand All @@ -43,19 +43,19 @@ func MakeGateway(apiParameter GatewayApiParameter) (*GatewayClient, error) {
cdkGatewayPassword: apiParameter.CdkGatewayPassword,
baseUrl: apiParameter.BaseUrl,
client: restyClient,
kinds: nil,
schemaCatalog: nil,
}

result.client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
result.client.SetDisableWarn(true)
result.client.SetBasicAuth(apiParameter.CdkGatewayUser, apiParameter.CdkGatewayPassword)

err := result.initKindFromApi()
err := result.initCatalogFromApi()
if err != nil {
if apiParameter.Debug {
fmt.Fprintf(os.Stderr, "Cannot access the Gateway Conduktor API: %s\nUsing offline defaults.\n", err)
}
result.kinds = schema.GatewayDefaultCatalog().Kind
result.schemaCatalog = schema.GatewayDefaultCatalog()
}

return result, nil
Expand Down Expand Up @@ -283,6 +283,28 @@ func (client *GatewayClient) ActivateDebug() {
client.client.SetDebug(true)
}

func (client *GatewayClient) Run(run schema.Run, pathValue []string, queryParams map[string]string, body interface{}) ([]byte, error) {
if run.BackendType != schema.GATEWAY {
return nil, fmt.Errorf("Only console backend type is supported by console client")
}
path := run.BuildPath(pathValue)
url := client.baseUrl + path
requestBuilder := client.client.R()
for k, v := range queryParams {
requestBuilder.SetQueryParam(k, v)
}
if body != nil {
requestBuilder = requestBuilder.SetBody(body)
}
resp, err := requestBuilder.Execute(run.Method, url)
if err != nil {
return nil, err
} else if resp.IsError() {
return nil, fmt.Errorf(extractApiError(resp))
}
return resp.Body(), nil
}

func (client *GatewayClient) Apply(resource *resource.Resource, dryMode bool) (string, error) {
kinds := client.GetKinds()
kind, ok := kinds[resource.Kind]
Expand Down Expand Up @@ -328,7 +350,7 @@ func (client *GatewayClient) GetOpenApi() ([]byte, error) {
return resp.Body(), nil
}

func (client *GatewayClient) initKindFromApi() error {
func (client *GatewayClient) initCatalogFromApi() error {
data, err := client.GetOpenApi()
if err != nil {
return fmt.Errorf("Cannot get openapi: %s", err)
Expand All @@ -338,20 +360,17 @@ func (client *GatewayClient) initKindFromApi() error {
return fmt.Errorf("Cannot parse openapi: %s", err)
}
strict := false
client.kinds, err = schema.GetGatewayKinds(strict)
client.schemaCatalog, err = schema.GetGatewayCatalog(strict)
if err != nil {
fmt.Errorf("Cannot extract schemaCatalog from openapi: %s", err)
}
return nil
}

func (client *GatewayClient) GetKinds() schema.KindCatalog {
return client.kinds
return client.schemaCatalog.Kind
}

func (client *GatewayClient) GetCatalog() *schema.Catalog {
return &schema.Catalog{
Kind: client.GetKinds(),
Execute: map[string]schema.Execute{},
}
return client.schemaCatalog
}
10 changes: 5 additions & 5 deletions cmd/consoleMkKind.go → cmd/consoleMakeCatalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (
"github.com/spf13/cobra"
)

func initConsoleMkKind() {
func intConsoleMakeCatalog() {
var prettyPrint *bool
var nonStrict *bool

var makeKind = &cobra.Command{
Use: "makeKind [file]",
Short: "Make kind json from openapi file if file not given it will read from api",
Use: "makeConsoleCatalog [file]",
Short: "Make catalog json from openapi file if file not given it will read from api",
Long: ``,
Aliases: []string{"mkKind", "makeConsoleKind"},
Aliases: []string{"mkKind", "makeConsoleKind", "makeKind"}, // for backward compatibility
Args: cobra.RangeArgs(0, 1),
Hidden: !utils.CdkDevMode(),
Run: func(cmd *cobra.Command, args []string) {
runMkKind(cmd, args, *prettyPrint, *nonStrict, func() ([]byte, error) { return consoleApiClient().GetOpenApi() }, func(schema *schema.OpenApiParser, strict bool) (*schema.Catalog, error) {
runMakeCatalog(cmd, args, *prettyPrint, *nonStrict, func() ([]byte, error) { return consoleApiClient().GetOpenApi() }, func(schema *schema.OpenApiParser, strict bool) (*schema.Catalog, error) {
return schema.GetConsoleCatalog(strict)
})
},
Expand Down
116 changes: 0 additions & 116 deletions cmd/execute.go

This file was deleted.

18 changes: 9 additions & 9 deletions cmd/gatewayMkKind.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import (
"github.com/spf13/cobra"
)

func initGatewayMkKind() {
func initGatewayMakeCatalog() {
var prettyPrint *bool
var nonStrict *bool

var makeKind = &cobra.Command{
Use: "gatewayMakeKind [file]",
Short: "Make kind json from openapi file if file not given it will read from api",
var makeCatalog = &cobra.Command{
Use: "gatewayMakeCatalog [file]",
Short: "Make catalog json from openapi file if file not given it will read from api",
Long: ``,
Aliases: []string{"gatewayMkKind", "gwMkKind"},
Aliases: []string{"gatewayMkKind", "gwMkKind", "gatewayMakeKind"}, // for backward compatibility
Args: cobra.RangeArgs(0, 1),
Hidden: !utils.CdkDevMode(),
Run: func(cmd *cobra.Command, args []string) {
runMkKind(cmd, args, *prettyPrint, *nonStrict, func() ([]byte, error) { return gatewayApiClient().GetOpenApi() }, func(schema *schema.OpenApiParser, strict bool) (*schema.Catalog, error) {
runMakeCatalog(cmd, args, *prettyPrint, *nonStrict, func() ([]byte, error) { return gatewayApiClient().GetOpenApi() }, func(schema *schema.OpenApiParser, strict bool) (*schema.Catalog, error) {
return schema.GetGatewayCatalog(strict)
})
},
}
rootCmd.AddCommand(makeKind)
rootCmd.AddCommand(makeCatalog)

prettyPrint = makeKind.Flags().BoolP("pretty", "p", false, "Pretty print the output")
nonStrict = makeKind.Flags().BoolP("non-strict", "n", false, "Don't be strict on the parsing of the schema")
prettyPrint = makeCatalog.Flags().BoolP("pretty", "p", false, "Pretty print the output")
nonStrict = makeCatalog.Flags().BoolP("non-strict", "n", false, "Don't be strict on the parsing of the schema")
}
6 changes: 3 additions & 3 deletions cmd/genericMkKind.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

func runMkKind(cmd *cobra.Command, args []string, prettyPrint bool, nonStrict bool, getOpenApi func() ([]byte, error), getKinds func(*schema.OpenApiParser, bool) (*schema.Catalog, error)) {
func runMakeCatalog(cmd *cobra.Command, args []string, prettyPrint bool, nonStrict bool, getOpenApi func() ([]byte, error), getCatalog func(*schema.OpenApiParser, bool) (*schema.Catalog, error)) {
var kinds *schema.Catalog
if len(args) == 1 {
data, err := os.ReadFile(args[0])
Expand All @@ -20,7 +20,7 @@ func runMkKind(cmd *cobra.Command, args []string, prettyPrint bool, nonStrict bo
if err != nil {
panic(err)
}
kinds, err = getKinds(schema, !nonStrict)
kinds, err = getCatalog(schema, !nonStrict)
if err != nil {
panic(err)
}
Expand All @@ -33,7 +33,7 @@ func runMkKind(cmd *cobra.Command, args []string, prettyPrint bool, nonStrict bo
if err != nil {
panic(err)
}
kinds, err = getKinds(schema, !nonStrict)
kinds, err = getCatalog(schema, !nonStrict)
if err != nil {
panic(err)
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ func buildQueryParams(params map[string]interface{}) map[string]string {
if value != nil {
str, strOk := value.(*string)
boolValue, boolOk := value.(*bool)
intValue, intOk := value.(*int)

if strOk {
if *str != "" {
queryParams[key] = *str
}
} else if boolOk {
queryParams[key] = strconv.FormatBool(*boolValue)
} else if intOk {
queryParams[key] = strconv.Itoa(*intValue)
} else {
panic("Unknown query flag type")
}
Expand Down Expand Up @@ -229,6 +232,7 @@ func initGet(kinds schema.KindCatalog) {
for i, flag := range parentQueryFlags {
parentQueryFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag)
}
//TODO: factorize with run.go
for key, flag := range listFlags {
var isFlagSet bool
if flag.Type == "string" {
Expand All @@ -237,6 +241,9 @@ func initGet(kinds schema.KindCatalog) {
} else if flag.Type == "boolean" {
isFlagSet = true
listFlagValue[key] = kindCmd.Flags().Bool(flag.FlagName, false, "")
} else if flag.Type == "integer" {
isFlagSet = true
listFlagValue[key] = kindCmd.Flags().Int(flag.FlagName, 0, "")
}
if isFlagSet && flag.Required {
kindCmd.MarkFlagRequired(flag.FlagName)
Expand Down
13 changes: 7 additions & 6 deletions cmd/printKind.go → cmd/printCatalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"github.com/spf13/cobra"
)

func initPrintKind(kinds schema.KindCatalog) {
func initPrintCatalog(kinds schema.Catalog) {

var prettyPrint *bool

var printKind = &cobra.Command{
Use: "printKind",
Short: "Print kind catalog used",
Long: ``,
Args: cobra.NoArgs,
Hidden: !utils.CdkDevMode(),
Use: "printCatalog",
Aliases: []string{"printKind"}, // for backward compatibility
Short: "Print catalog",
Long: ``,
Args: cobra.NoArgs,
Hidden: !utils.CdkDevMode(),
Run: func(cmd *cobra.Command, args []string) {
var payload []byte
var err error
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func init() {
initTemplate(catalog.Kind)
initDelete(catalog.Kind, !*permissive)
initApply(catalog.Kind, !*permissive)
initConsoleMkKind()
initGatewayMkKind()
initPrintKind(catalog.Kind)
intConsoleMakeCatalog()
initGatewayMakeCatalog()
initPrintCatalog(catalog)
initSql(catalog.Kind)
initExecute(catalog.Execute)
initRun(catalog.Run)
}
Loading

0 comments on commit 99c16b3

Please sign in to comment.