From 861632649c03e9512edec18dd0864b726b5a7cf3 Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Thu, 30 Nov 2023 20:42:28 -0800 Subject: [PATCH] feat: allow delete by app name only #338 --- cmd/apps/delapp.go | 28 +++++++++++++++++++++++----- cmd/apps/listapp.go | 24 ++++++++++++++++++++---- internal/client/apps/apps.go | 35 +++++++++++++++++++++++++++++------ 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/cmd/apps/delapp.go b/cmd/apps/delapp.go index 5c5598fc0..62af1edb2 100644 --- a/cmd/apps/delapp.go +++ b/cmd/apps/delapp.go @@ -15,6 +15,8 @@ package apps import ( + "encoding/json" + "fmt" "internal/apiclient" "internal/client/apps" @@ -31,19 +33,35 @@ var DelCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { + var respBody []byte + var id string + + apiclient.DisableCmdPrintHttpResponse() + if respBody, err = apps.SearchApp(name); err != nil { + return err + } + + if id, err = getDeveloperId(respBody); err != nil { + return err + } + apiclient.EnableCmdPrintHttpResponse() _, err = apps.Delete(name, id) return }, } -var id string - func init() { DelCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the developer app") - DelCmd.Flags().StringVarP(&id, "id", "i", - "", "Developer Id") _ = DelCmd.MarkFlagRequired("name") - _ = DelCmd.MarkFlagRequired("id") +} + +func getDeveloperId(respBody []byte) (id string, err error) { + var respMap map[string]interface{} + if err = json.Unmarshal(respBody, &respMap); err != nil { + return "", nil + } + return fmt.Sprintf("%v", respMap["developerId"]), nil + } diff --git a/cmd/apps/listapp.go b/cmd/apps/listapp.go index 74e77c1ef..656351f54 100644 --- a/cmd/apps/listapp.go +++ b/cmd/apps/listapp.go @@ -31,15 +31,17 @@ var ListCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { - _, err = apps.List(includeCred, expand, count) + _, err = apps.List(includeCred, expand, count, status, + startKey, ids, keyStatus, apiProduct, pageSize, pageToken, filter) return }, } var ( - expand = false - includeCred = false - count int + status, startKey, ids, keyStatus, apiProduct, pageToken, filter string + expand = false + includeCred = false + count, pageSize int ) func init() { @@ -49,4 +51,18 @@ func init() { false, "Expand Details") ListCmd.Flags().BoolVarP(&includeCred, "inclCred", "i", false, "Include Credentials") + ListCmd.Flags().StringVarP(&status, "status", "s", + "", "Filter by the status of the app. Valid values are approved or revoked") + ListCmd.Flags().StringVarP(&ids, "ids", "", + "", "Comma-separated list of app IDs") + ListCmd.Flags().StringVarP(&keyStatus, "key-status", "k", + "", "Key status of the app. Valid values include approved or revoked") + ListCmd.Flags().StringVarP(&apiProduct, "api-product", "p", + "", "Name of the API Product to filter by") + ListCmd.Flags().IntVarP(&pageSize, "page-size", "", + -1, "Count of apps a single page can have in the response") + ListCmd.Flags().StringVarP(&pageToken, "page-token", "", + "", "The starting index record for listing the apps") + ListCmd.Flags().StringVarP(&filter, "filter", "f", + "", "The filter expression to be used to get the list of apps") } diff --git a/internal/client/apps/apps.go b/internal/client/apps/apps.go index 2130957a9..72207ce0e 100644 --- a/internal/client/apps/apps.go +++ b/internal/client/apps/apps.go @@ -179,11 +179,9 @@ func SearchApp(name string) (respBody []byte, err error) { defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) u, _ := url.Parse(apiclient.BaseURL) - // search by name is not implemented; use list and return the appropriate app u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "apps") q := u.Query() - q.Set("expand", "true") - q.Set("includeCred", "false") + q.Set("filter", "appName="+name) u.RawQuery = q.Encode() respBody, err = apiclient.HttpClient(u.String()) @@ -191,7 +189,7 @@ func SearchApp(name string) (respBody []byte, err error) { return respBody, err } jq := gojsonq.New().JSONString(string(respBody)).From("app").Where("name", "eq", name) - out := jq.Get() + out := jq.First() outBytes, err := json.Marshal(out) if err != nil { return outBytes, err @@ -200,7 +198,10 @@ func SearchApp(name string) (respBody []byte, err error) { } // List -func List(includeCred bool, expand bool, count int) (respBody []byte, err error) { +func List(includeCred bool, expand bool, count int, status string, startKey string, + ids string, keyStatus string, apiProduct string, pageSize int, + pageToken string, filter string) (respBody []byte, err error) { + u, _ := url.Parse(apiclient.BaseURL) u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "apps") q := u.Query() @@ -215,8 +216,30 @@ func List(includeCred bool, expand bool, count int) (respBody []byte, err error) q.Set("includeCred", "false") } if count != -1 { - q.Set("row", strconv.Itoa(count)) + q.Set("rows", strconv.Itoa(count)) + } + if startKey != "" { + q.Set("startKey", startKey) + } + if ids != "" { + q.Set("ids", ids) + } + if keyStatus != "" { + q.Set("keyStatus", keyStatus) + } + if apiProduct != "" { + q.Set("apiProduct", apiProduct) } + if pageSize != -1 { + q.Set("pageSize", strconv.Itoa(pageSize)) + } + if pageToken != "" { + q.Set("pageToken", pageToken) + } + if filter != "" { + q.Set("filter", filter) + } + u.RawQuery = q.Encode() respBody, err = apiclient.HttpClient(u.String()) return respBody, err