Skip to content

Commit

Permalink
feat: adds support to delete category by name #356 (#359)
Browse files Browse the repository at this point in the history
* feat: adds support to delete category by name #356

* update description #356
  • Loading branch information
srinandan authored Dec 18, 2023
1 parent f0eefe4 commit bcd428f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cmd/apicategories/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ import (
// DelCmd to get a catalog items
var DelCmd = &cobra.Command{
Use: "delete",
Short: "Deletes an API Category by ID",
Long: "Deletes an API Category by ID",
Short: "Deletes an API Category by ID or name",
Long: "Deletes an API Category by ID or name",
Args: func(cmd *cobra.Command, args []string) (err error) {
if siteid == "" {
return fmt.Errorf("siteid is a mandatory parameter")
}
if name == "" && id == "" {
return fmt.Errorf("name or id must be set as a parameter")
}
if name != "" && id != "" {
return fmt.Errorf("name and id cannot be set as a parameter")
}
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if siteid == "" {
return fmt.Errorf("siteid is a mandatory parameter")
if name != "" {
if id, err = apicategories.GetIDByName(siteid, name); err != nil {
return err
}
}
_, err = apicategories.Delete(siteid, id)
return
Expand All @@ -43,5 +54,6 @@ var DelCmd = &cobra.Command{
func init() {
DelCmd.Flags().StringVarP(&id, "id", "i",
"", "API Category ID")
_ = DelCmd.MarkFlagRequired("id")
DelCmd.Flags().StringVarP(&name, "name", "n",
"", "API Catalog Name")
}
18 changes: 18 additions & 0 deletions internal/client/apicategories/apicategories.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ func Update(siteid string, name string) (respBody []byte, err error) {

// GetByName
func GetByName(siteid string, name string) (respBody []byte, err error) {
return getByName(siteid, name)
}

// GetIDByName
func GetIDByName(siteid string, name string) (id string, err error) {
outBytes, err := getByName(siteid, name)
if err != nil {
return "", err
}
var apicatResp = map[string]string{}
err = json.Unmarshal(outBytes, &apicatResp)
if err != nil {
return "", err
}
return apicatResp["id"], nil
}

func getByName(siteid string, name string) (respBytes []byte, err error) {
apiclient.ClientPrintHttpResponse.Set(false)
defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
listRespBytes, err := List(siteid)
Expand Down

0 comments on commit bcd428f

Please sign in to comment.