From bcd428fad09d767dabdbe3c5d16e0352b7e42b6a Mon Sep 17 00:00:00 2001 From: Srinandan Sridhar <13950006+srinandan@users.noreply.github.com> Date: Mon, 18 Dec 2023 09:12:52 -0800 Subject: [PATCH] feat: adds support to delete category by name #356 (#359) * feat: adds support to delete category by name #356 * update description #356 --- cmd/apicategories/delete.go | 22 ++++++++++++++----- .../client/apicategories/apicategories.go | 18 +++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/cmd/apicategories/delete.go b/cmd/apicategories/delete.go index a1ad0bf4a..8e9be6e6b 100644 --- a/cmd/apicategories/delete.go +++ b/cmd/apicategories/delete.go @@ -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 @@ -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") } diff --git a/internal/client/apicategories/apicategories.go b/internal/client/apicategories/apicategories.go index db9a96eba..8cbb6c5fc 100644 --- a/internal/client/apicategories/apicategories.go +++ b/internal/client/apicategories/apicategories.go @@ -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)