Skip to content

Commit

Permalink
alias topic specifics
Browse files Browse the repository at this point in the history
  • Loading branch information
achever1 committed Jul 17, 2024
1 parent bce86af commit 4232d30
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 77 deletions.
56 changes: 48 additions & 8 deletions client/gatway_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down
100 changes: 72 additions & 28 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
128 changes: 87 additions & 41 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4232d30

Please sign in to comment.