Skip to content

Commit

Permalink
Merge pull request #758 from Chamindu36/chamindu_2.x
Browse files Browse the repository at this point in the history
[2.0.X] Add limit param to api list command and app list command
  • Loading branch information
Chamindu36 authored Jun 10, 2021
2 parents e0013d6 + 4bad030 commit d1e402d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 13 deletions.
24 changes: 21 additions & 3 deletions import-export-cli/cmd/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
var listApisCmdEnvironment string
var listApisCmdFormat string
var listApisCmdQuery string
var listApisCmdLimit string
var queryParamAdded bool = false

// apisCmd related info
const apisCmdLiteral = "apis"
Expand All @@ -58,6 +60,7 @@ const apisCmdLongDesc = `Display a list of APIs in the environment specified by
var apisCmdExamples = utils.ProjectName + ` ` + apisCmdLiteral + ` ` + listCmdLiteral + ` -e dev
` + utils.ProjectName + ` ` + apisCmdLiteral + ` ` + listCmdLiteral + ` -e dev -q version:1.0.0
` + utils.ProjectName + ` ` + apisCmdLiteral + ` ` + listCmdLiteral + ` -e prod -q provider:admin
` + utils.ProjectName + ` ` + listCmdLiteral + ` ` + apisCmdLiteral + ` -e prod -l 100
` + utils.ProjectName + ` ` + apisCmdLiteral + ` ` + listCmdLiteral + ` -e staging`

// apisCmd represents the apis command
Expand Down Expand Up @@ -134,7 +137,7 @@ func executeApisCmd(credential credentials.Credential) {
}

apiListEndpoint := utils.GetApiListEndpointOfEnv(listApisCmdEnvironment, utils.MainConfigFilePath)
_, apis, err := GetAPIList(listApisCmdQuery, accessToken, apiListEndpoint)
_, apis, err := GetAPIList(listApisCmdQuery, listApisCmdLimit, accessToken, apiListEndpoint)
if err == nil {
printAPIs(apis, listApisCmdFormat)
} else {
Expand All @@ -144,17 +147,21 @@ func executeApisCmd(credential credentials.Credential) {

// GetAPIList
// @param query : string to be matched against the API names
// @param list : The maximum number of APIs to be listed
// @param accessToken : Access Token for the environment
// @param apiManagerEndpoint : API Manager Endpoint for the environment
// @return count (no. of APIs)
// @return array of API objects
// @return error
func GetAPIList(query, accessToken, apiListEndpoint string) (count int32, apis []utils.API, err error) {
func GetAPIList(query, limit, accessToken, apiListEndpoint string) (count int32, apis []utils.API, err error) {
headers := make(map[string]string)
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix + " " + accessToken

if query != "" {
apiListEndpoint += "?query=" + query
apiListEndpoint += getQueryParamConnector() + "query=" + query
}
if limit != "" {
apiListEndpoint += getQueryParamConnector() + "limit=" + limit
}
utils.Logln(utils.LogPrefixInfo+"URL:", apiListEndpoint)
resp, err := utils.InvokeGETRequest(apiListEndpoint, headers)
Expand All @@ -180,6 +187,15 @@ func GetAPIList(query, accessToken, apiListEndpoint string) (count int32, apis [

}

func getQueryParamConnector() (connector string) {
if queryParamAdded {
return "&"
} else {
queryParamAdded = true
return "?"
}
}

// printAPIs
func printAPIs(apis []utils.API, format string) {
if format == "" {
Expand Down Expand Up @@ -222,6 +238,8 @@ func init() {
"", "Environment to be searched")
apisCmd.Flags().StringVarP(&listApisCmdQuery, "query", "q",
"", "Query pattern")
apisCmd.Flags().StringVarP(&listApisCmdLimit, "limit", "l",
"", "Maximum number of APIs to return")
apisCmd.Flags().StringVarP(&listApisCmdFormat, "format", "", "", "Pretty-print apis "+
"using Go Templates. Use {{ jsonPretty . }} to list all fields")
_ = apisCmd.MarkFlagRequired("environment")
Expand Down
15 changes: 12 additions & 3 deletions import-export-cli/cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
var listAppsCmdEnvironment string
var listAppsCmdAppOwner string
var listAppsCmdFormat string
var listAppsCmdLimit string

// appsCmd related info
const appsCmdLiteral = "apps"
Expand Down Expand Up @@ -97,7 +98,8 @@ const appsCmdLongDesc = "Display a list of Applications of the user in the envir
const appsCmdExamples = utils.ProjectName + ` ` + listCmdLiteral + ` ` + appsCmdLiteral + ` -e dev
` + utils.ProjectName + ` ` + listCmdLiteral + ` ` + appsCmdLiteral + ` -e dev -o sampleUser
` + utils.ProjectName + ` ` + listCmdLiteral + ` ` + appsCmdLiteral + ` -e prod -o sampleUser
` + utils.ProjectName + ` ` + listCmdLiteral + ` ` + appsCmdLiteral + ` -e staging -o sampleUser`
` + utils.ProjectName + ` ` + listCmdLiteral + ` ` + appsCmdLiteral + ` -e staging -o sampleUser
` + utils.ProjectName + ` ` + listCmdLiteral + ` ` + appsCmdLiteral + ` -e dev -l 40`

// appsCmd represents the apps command
var appsCmd = &cobra.Command{
Expand All @@ -123,7 +125,7 @@ func executeAppsCmd(credential credentials.Credential, appOwner string) {
}

applicationListEndpoint := utils.GetApplicationListEndpointOfEnv(listAppsCmdEnvironment, utils.MainConfigFilePath)
_, apps, err := GetApplicationList(appOwner, accessToken, applicationListEndpoint)
_, apps, err := GetApplicationList(appOwner, accessToken, applicationListEndpoint, listAppsCmdLimit)

if err == nil {
// Printing the list of available Applications
Expand All @@ -136,16 +138,21 @@ func executeAppsCmd(credential credentials.Credential, appOwner string) {
//Get Application List
// @param accessToken : Access Token for the environment
// @param apiManagerEndpoint : API Manager Endpoint for the environment
// @param list : The maximum number of Applications to be listed
// @return count (no. of Applications)
// @return array of Application objects
// @return error

func GetApplicationList(appOwner, accessToken, applicationListEndpoint string) (count int32, apps []utils.Application,
func GetApplicationList(appOwner, accessToken, applicationListEndpoint, limit string) (count int32, apps []utils.Application,
err error) {

headers := make(map[string]string)
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix + " " + accessToken

if limit != "" {
applicationListEndpoint += "?limit=" + limit
}

var resp *resty.Response
if appOwner == "" {
resp, err = utils.InvokeGETRequest(applicationListEndpoint, headers)
Expand Down Expand Up @@ -214,6 +221,8 @@ func init() {
"", "Environment to be searched")
appsCmd.Flags().StringVarP(&listAppsCmdAppOwner, "owner", "o", "",
"Owner of the Application")
appsCmd.Flags().StringVarP(&listAppsCmdLimit, "limit", "l",
"", "Maximum number of applications to return")
appsCmd.Flags().StringVarP(&listAppsCmdFormat, "format", "", "", "Pretty-print output"+
"using Go templates. Use {{jsonPretty .}} to list all fields")
_ = appsCmd.MarkFlagRequired("environment")
Expand Down
2 changes: 1 addition & 1 deletion import-export-cli/cmd/exportAPIs.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func getAPIList() (count int32, apis []utils.API) {
if cmdResourceTenantDomain != "" {
apiListEndpoint += "&tenantDomain=" + cmdResourceTenantDomain
}
count, apis, err := GetAPIList("", accessToken, apiListEndpoint)
count, apis, err := GetAPIList("", "", accessToken, apiListEndpoint)
if err == nil {
return count, apis
} else {
Expand Down
2 changes: 1 addition & 1 deletion import-export-cli/cmd/importAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func getApiID(name, version, provider, environment, accessOAuthToken string) (st
if provider != "" {
apiQuery += " provider:" + provider
}
count, apis, err := GetAPIList(url.QueryEscape(apiQuery), accessOAuthToken,
count, apis, err := GetAPIList(url.QueryEscape(apiQuery), "", accessOAuthToken,
utils.GetApiListEndpointOfEnv(environment, utils.MainConfigFilePath))
if err != nil {
return "", err
Expand Down
8 changes: 4 additions & 4 deletions import-export-cli/cmd/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestGetAPIListOK(t *testing.T) {
}))
defer server.Close()

count, apiList, err := GetAPIList("", "access_token", server.URL)
count, apiList, err := GetAPIList("", "", "access_token", server.URL)
fmt.Println("Count:", count)
fmt.Println("List:", apiList)

Expand All @@ -99,7 +99,7 @@ func TestGetAPIListUnreachable(t *testing.T) {
}))
defer server.Close()

count, list, err := GetAPIList("", "access_token", server.URL)
count, list, err := GetAPIList("", "", "access_token", server.URL)
if count != 0 {
t.Errorf("Incorrect Count. Expected %d, got %d\n", 0, count)
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestGetApplicationListOK(t *testing.T) {
}))
defer server.Close()

count, appList, err := GetApplicationList("admin", "access_token", server.URL)
count, appList, err := GetApplicationList("admin", "access_token", server.URL, "")
fmt.Println("Count:", count)
fmt.Println("List:", appList)

Expand All @@ -172,7 +172,7 @@ func TestGetApplicationListUnreachable(t *testing.T) {
}))
defer server.Close()

count, list, err := GetAPIList("", "access_token", server.URL)
count, list, err := GetAPIList("", "", "access_token", server.URL)
if count != 0 {
t.Errorf("Incorrect Count. Expected %d, got %d\n", 0, count)
}
Expand Down
2 changes: 2 additions & 0 deletions import-export-cli/docs/apimcli_list_apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ apimcli list apis [flags]
apimcli apis list -e dev
apimcli apis list -e dev -q version:1.0.0
apimcli apis list -e prod -q provider:admin
apimcli list apis -e prod -l 100
apimcli apis list -e staging
```

Expand All @@ -26,6 +27,7 @@ apimcli apis list -e staging
-e, --environment string Environment to be searched
--format string Pretty-print apis using Go Templates. Use {{ jsonPretty . }} to list all fields
-h, --help help for apis
-l, --limit string Maximum number of APIs to return
-q, --query string Query pattern
```

Expand Down
2 changes: 2 additions & 0 deletions import-export-cli/docs/apimcli_list_apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ apimcli list apps -e dev
apimcli list apps -e dev -o sampleUser
apimcli list apps -e prod -o sampleUser
apimcli list apps -e staging -o sampleUser
apimcli list apps -e dev -l 40
```

### Options
Expand All @@ -26,6 +27,7 @@ apimcli list apps -e staging -o sampleUser
-e, --environment string Environment to be searched
--format string Pretty-print outputusing Go templates. Use {{jsonPretty .}} to list all fields
-h, --help help for apps
-l, --limit string Maximum number of applications to return
-o, --owner string Owner of the Application
```

Expand Down
3 changes: 2 additions & 1 deletion import-export-cli/docs/apimcli_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Set configuration

Set configuration parameters. Use at least one of the following flags
* --http-request-timeout <time-in-milli-seconds>
* --tls_renegotiation_mode <never|once|freely>
* --export-directory <path-to-directory-where-apis-should-be-saved>
* --tls_renegotiation_mode <never|once|freely>

```
apimcli set [flags]
Expand All @@ -29,6 +29,7 @@ apimcli set --tls_renegotiation_mode freely
--export-directory string Path to directory where APIs should be saved (default on Unix, including macOS "$HOME/.wso2apimcli/exported", default on Windows "%HOME%\.wso2apimcli\exported")
-h, --help help for set
--http-request-timeout int Timeout for HTTP Client (default 10000)
--tls_renegotiation_mode string Supported TLS renegotiation mode (default "never")
```

### Options inherited from parent commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ _apimcli_list_apis()
flags+=("--help")
flags+=("-h")
local_nonpersistent_flags+=("--help")
flags+=("--limit=")
two_word_flags+=("-l")
local_nonpersistent_flags+=("--limit=")
flags+=("--query=")
two_word_flags+=("-q")
local_nonpersistent_flags+=("--query=")
Expand Down Expand Up @@ -544,6 +547,9 @@ _apimcli_list_apps()
flags+=("--help")
flags+=("-h")
local_nonpersistent_flags+=("--help")
flags+=("--limit=")
two_word_flags+=("-l")
local_nonpersistent_flags+=("--limit=")
flags+=("--owner=")
two_word_flags+=("-o")
local_nonpersistent_flags+=("--owner=")
Expand Down

0 comments on commit d1e402d

Please sign in to comment.