Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
support filters
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Jul 3, 2021
1 parent 2d9c24b commit f919621
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
65 changes: 64 additions & 1 deletion client/operations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,43 @@
package operations

import (
"encoding/json"
"fmt"
"net/url"
"path"

"github.com/srinandan/apigeecli/apiclient"
)

type ops struct {
Operations []op `json:"operations,omitempty"`
}

type op struct {
Name string `json:"name,omitempty"`
Metadata metadata `json:"metadata,omitempty"`
Done bool `json:"done,omitempty"`
Error operationError `json:"error,omitempty"`
}

type operationError struct {
Message string `json:"message,omitempty"`
Code int `json:"code,omitempty"`
}

type metadata struct {
Type string `json:"@type,omitempty"`
OperationType string `json:"operationType,omitempty"`
TargetResourceName string `json:"targetResourceName,omitempty"`
State string `json:"state,omitempty"`
Progress progress `json:"progress,omitempty"`
}

type progress struct {
Description string `json:"description,omitempty"`
PercentDone int `json:"percentDone,omitempty"`
}

//Get
func Get(name string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
Expand All @@ -30,9 +61,41 @@ func Get(name string) (respBody []byte, err error) {
}

//List
func List() (respBody []byte, err error) {
func List(state string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "operations")
if state != "" {
apiclient.SetPrintOutput(false)
if respBody, err = apiclient.HttpClient(false, u.String()); err != nil {
return nil, err
}
return filterOperation(respBody, state)
}

respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String())
return respBody, err
}

func filterOperation(respBody []byte, state string) (operationsRespBody []byte, err error) {
operationsResponse := ops{}
filteredOperationsResponse := ops{}
if err = json.Unmarshal(respBody, &operationsResponse); err != nil {
return nil, err
}

for _, operationResponse := range operationsResponse.Operations {
if operationResponse.Metadata.State == state {
fmt.Println(operationResponse.Metadata.State)
filteredOperationsResponse.Operations = append(filteredOperationsResponse.Operations, operationResponse)
}
}

if operationsRespBody, err = json.Marshal(filteredOperationsResponse); err != nil {
return nil, err
}

if apiclient.GetPrintOutput() {
apiclient.PrettyPrint(operationsRespBody)
}
return operationsRespBody, nil
}
6 changes: 5 additions & 1 deletion cmd/ops/listops.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ var ListCmd = &cobra.Command{
return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
_, err = operations.List()
_, err = operations.List(state)
return
},
}

var state string

func init() {

ListCmd.Flags().StringVarP(&state, "state", "s",
"", "filter by operation state: FINISHED, ERROR, IN_PROGRESS")
}

0 comments on commit f919621

Please sign in to comment.