From 4232d3021cfad1232e5ad9a16351241f7fc0bc65 Mon Sep 17 00:00:00 2001 From: achever1 Date: Tue, 16 Jul 2024 17:46:08 +0200 Subject: [PATCH] alias topic specifics --- client/gatway_client.go | 56 +++++++++++++++--- cmd/delete.go | 100 ++++++++++++++++++++++--------- cmd/get.go | 128 +++++++++++++++++++++++++++------------- 3 files changed, 207 insertions(+), 77 deletions(-) diff --git a/client/gatway_client.go b/client/gatway_client.go index fd45e2f..079b449 100644 --- a/client/gatway_client.go +++ b/client/gatway_client.go @@ -75,26 +75,45 @@ func MakeGatewayClientFromEnv() (*GatewayClient, error) { return client, nil } -func (client *GatewayClient) Get(kind *schema.Kind, parentPathValue []string) error { +func (client *GatewayClient) Get(kind *schema.Kind, parentPathValue []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 err + return result, err } else if resp.IsError() { - return fmt.Errorf(extractApiError(resp)) + return result, fmt.Errorf(extractApiError(resp)) } - return printResponseAsYaml(resp.Body()) + err = json.Unmarshal(resp.Body(), &result) + return result, err } -func (client *GatewayClient) Describe(kind *schema.Kind, parentPathValue []string, name string) error { +func (client *GatewayClient) GetAliasTopics(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) Describe(kind *schema.Kind, parentPathValue []string, name string) (resource.Resource, error) { + var result resource.Resource url := client.baseUrl + kind.DescribePath(parentPathValue, name) resp, err := client.client.R().Get(url) if err != nil { - return err + return result, err } else if resp.IsError() { - return fmt.Errorf("error describing resources %s/%s, got status code: %d:\n %s", kind.GetName(), name, resp.StatusCode(), string(resp.Body())) + return result, fmt.Errorf("error describing resources %s/%s, got status code: %d:\n %s", kind.GetName(), name, resp.StatusCode(), string(resp.Body())) } - return printResponseAsYaml(resp.Body()) + err = json.Unmarshal(resp.Body(), &result) + return result, err } func (client *GatewayClient) Delete(kind *schema.Kind, parentPathValue []string, name string) error { @@ -111,6 +130,27 @@ func (client *GatewayClient) Delete(kind *schema.Kind, parentPathValue []string, return err } +func (client *GatewayClient) DeleteAliasTopics(kind *schema.Kind, param map[string]string) error { + url := client.baseUrl + kind.ListPath(nil) + req := client.client.R() + req.SetBody( + map[string]interface{}{ + "name": param["name"], + "vCluster": param["vcluster"], + }, + ) + resp, err := req.Delete(url) + if err != nil { + return err + } else if resp.IsError() { + return fmt.Errorf(extractApiError(resp)) + } else { + fmt.Printf("%s/%s deleted\n", kind.GetName(), param) + } + + return err +} + func (client *GatewayClient) Apply(resource *resource.Resource, dryMode bool) (string, error) { kinds := client.GetKinds() kind, ok := kinds[resource.Kind] diff --git a/cmd/delete.go b/cmd/delete.go index d3973aa..2797b00 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -41,34 +41,78 @@ func initDelete(kinds schema.KindCatalog) { deleteCmd.MarkFlagRequired("file") for name, kind := range kinds { - flags := kind.GetFlag() - parentFlagValue := make([]*string, len(flags)) - kindCmd := &cobra.Command{ - Use: fmt.Sprintf("%s [name]", name), - Short: "Delete resource of kind " + name, - Args: cobra.MatchAll(cobra.ExactArgs(1)), - Aliases: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"}, - Run: func(cmd *cobra.Command, args []string) { - parentValue := make([]string, len(parentFlagValue)) - for i, v := range parentFlagValue { - parentValue[i] = *v - } - var err error - if strings.Contains(kind.GetLatestKindVersion().ListPath, "gateway") { - err = gatewayApiClient().Delete(&kind, parentValue, args[0]) - } else { - err = apiClient().Delete(&kind, parentValue, args[0]) - } - if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) - os.Exit(1) - } - }, - } - for i, flag := range kind.GetFlag() { - parentFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag) - kindCmd.MarkFlagRequired(flag) + if name == "AliasTopics" { + aliasTopicDeleteCmd := buildDeleteAliasTopicCmd(name, kind) + deleteCmd.AddCommand(aliasTopicDeleteCmd) + } else { + flags := kind.GetFlag() + parentFlagValue := make([]*string, len(flags)) + kindCmd := &cobra.Command{ + Use: fmt.Sprintf("%s [name]", name), + Short: "Delete resource of kind " + name, + Args: cobra.MatchAll(cobra.ExactArgs(1)), + Aliases: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"}, + Run: func(cmd *cobra.Command, args []string) { + parentValue := make([]string, len(parentFlagValue)) + for i, v := range parentFlagValue { + parentValue[i] = *v + } + var err error + if strings.Contains(kind.GetLatestKindVersion().ListPath, "gateway") { + err = gatewayApiClient().Delete(&kind, parentValue, args[0]) + } else { + err = apiClient().Delete(&kind, parentValue, args[0]) + } + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } + }, + } + for i, flag := range kind.GetFlag() { + parentFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag) + kindCmd.MarkFlagRequired(flag) + } + deleteCmd.AddCommand(kindCmd) } - deleteCmd.AddCommand(kindCmd) } } + +func buildDeleteAliasTopicCmd(name string, kind schema.Kind) *cobra.Command { + const nameFlag = "name" + const vClusterFlag = "vcluster" + var nameValue string + var vClusterValue string + var aliasTopicDeleteCmd = &cobra.Command{ + Use: fmt.Sprintf("%s [name]", name), + Short: "Delete an Alias Topic", + Args: cobra.ExactArgs(0), + Aliases: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"}, + Run: func(cmd *cobra.Command, args []string) { + var err error + queryParams := make(map[string]string) + if nameValue != "" { + queryParams[nameFlag] = nameValue + } + if vClusterValue != "" { + queryParams[vClusterFlag] = vClusterValue + } else { + queryParams[vClusterFlag] = "passthrough" + } + + err = gatewayApiClient().DeleteAliasTopics(&kind, queryParams) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } + }, + } + + aliasTopicDeleteCmd.Flags().StringVar(&nameValue, nameFlag, "", "name of the alias topic") + aliasTopicDeleteCmd.Flags().StringVar(&vClusterValue, "vCluster", "", "vCluster of the alias topic") + + aliasTopicDeleteCmd.MarkFlagRequired(nameFlag) + aliasTopicDeleteCmd.MarkFlagRequired(vClusterFlag) + + return aliasTopicDeleteCmd +} diff --git a/cmd/get.go b/cmd/get.go index 87c5a59..b66bf19 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -25,50 +25,96 @@ func initGet(kinds schema.KindCatalog) { rootCmd.AddCommand(getCmd) for name, kind := range kinds { - 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: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"}, - Run: func(cmd *cobra.Command, args []string) { - parentValue := make([]string, len(parentFlagValue)) - for i, v := range parentFlagValue { - parentValue[i] = *v - } - var err error - if len(args) == 0 { - var result []resource.Resource - if strings.Contains(kind.GetLatestKindVersion().ListPath, "gateway") { - result, err = gatewayApiClient().Get(&kind, parentValue) - } else { - result, err = apiClient().Get(&kind, parentValue) + if name == "AliasTopics" { + aliasTopicGetCmd := buildGetAliasTopicCmd(name, kind) + getCmd.AddCommand(aliasTopicGetCmd) + } 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: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"}, + Run: func(cmd *cobra.Command, args []string) { + parentValue := make([]string, len(parentFlagValue)) + for i, v := range parentFlagValue { + parentValue[i] = *v } - for _, r := range result { - r.PrintPreservingOriginalFieldOrder() - fmt.Println("---") + var err error + if len(args) == 0 { + var result []resource.Resource + if strings.Contains(kind.GetLatestKindVersion().ListPath, "gateway") { + result, err = gatewayApiClient().Get(&kind, parentValue) + } else { + result, err = apiClient().Get(&kind, parentValue) + } + for _, r := range result { + r.PrintPreservingOriginalFieldOrder() + fmt.Println("---") + } + } else if len(args) == 1 { + var result resource.Resource + if strings.Contains(kind.GetLatestKindVersion().ListPath, "gateway") { + result, err = gatewayApiClient().Describe(&kind, parentValue, args[0]) + } else { + result, err = apiClient().Describe(&kind, parentValue, args[0]) + } + result.PrintPreservingOriginalFieldOrder() } - } else if len(args) == 1 { - var result resource.Resource - if strings.Contains(kind.GetLatestKindVersion().ListPath, "gateway") { - result, err = gatewayApiClient().Describe(&kind, parentValue, args[0]) - } else { - result, err = apiClient().Describe(&kind, parentValue, args[0]) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) } - result.PrintPreservingOriginalFieldOrder() - } - if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) - os.Exit(1) - } - }, + }, + } + getCmd.AddCommand(kindCmd) } - for i, flag := range kind.GetFlag() { - parentFlagValue[i] = kindCmd.Flags().String(flag, "", "Parent "+flag) - kindCmd.MarkFlagRequired(flag) - } - getCmd.AddCommand(kindCmd) } } + +func buildGetAliasTopicCmd(name string, kind schema.Kind) *cobra.Command { + const nameFlag = "name" + const vClusterFlag = "vcluster" + const showDefaultsFlag = "showDefaults" + var nameValue string + var vClusterValue string + var showDefaultsValue string + var aliasTopicGetCmd = &cobra.Command{ + Use: fmt.Sprintf("%s [name]", name), + Short: "List Alias Topics", + Args: cobra.ExactArgs(0), + Aliases: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"}, + Run: func(cmd *cobra.Command, args []string) { + var result []resource.Resource + var err error + queryParams := make(map[string]string) + if nameValue != "" { + queryParams[nameFlag] = nameValue + } + if vClusterValue != "" { + queryParams[vClusterFlag] = vClusterValue + } + if showDefaultsValue != "" { + queryParams[showDefaultsFlag] = showDefaultsValue + } + + result, err = gatewayApiClient().GetAliasTopics(&kind, queryParams) + for _, r := range result { + r.PrintPreservingOriginalFieldOrder() + fmt.Println("---") + } + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } + }, + } + + aliasTopicGetCmd.Flags().StringVar(&nameValue, nameFlag, "", "filter the alias topic result list by name") + aliasTopicGetCmd.Flags().StringVar(&vClusterValue, "vCluster", "", "filter the alias topic result list by vcluster") + aliasTopicGetCmd.Flags().StringVar(&showDefaultsValue, "showDefaults", "", "Toggle show defaults values (true|false, default false)") + + return aliasTopicGetCmd +}