Skip to content

Commit

Permalink
feat: allow delete by app name only #338
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Dec 1, 2023
1 parent f7fa956 commit 8616326
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
28 changes: 23 additions & 5 deletions cmd/apps/delapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package apps

import (
"encoding/json"
"fmt"

Check failure on line 19 in cmd/apps/delapp.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 19 in cmd/apps/delapp.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
"internal/apiclient"

"internal/client/apps"
Expand All @@ -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) {

Check warning on line 60 in cmd/apps/delapp.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func getDeveloperId should be getDeveloperID (revive)

Check warning on line 60 in cmd/apps/delapp.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func getDeveloperId should be getDeveloperID (revive)
var respMap map[string]interface{}
if err = json.Unmarshal(respBody, &respMap); err != nil {
return "", nil
}
return fmt.Sprintf("%v", respMap["developerId"]), nil

Check failure on line 66 in cmd/apps/delapp.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 66 in cmd/apps/delapp.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}
24 changes: 20 additions & 4 deletions cmd/apps/listapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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")
}
35 changes: 29 additions & 6 deletions internal/client/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,17 @@ 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())
if err != nil {
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
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 8616326

Please sign in to comment.