Skip to content

Commit

Permalink
Merge pull request #627 from ipfs/issue_445
Browse files Browse the repository at this point in the history
 Added filter option to `ipfs-cluster-ctl status`
  • Loading branch information
hsanjuan committed Jan 8, 2019
2 parents 595db7a + 964ced6 commit d80f3ee
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 75 deletions.
2 changes: 1 addition & 1 deletion api/rest/client/client.go
Expand Up @@ -71,7 +71,7 @@ type Client interface {
// is fetched from all cluster peers.
Status(ci cid.Cid, local bool) (api.GlobalPinInfo, error)
// StatusAll gathers Status() for all tracked items.
StatusAll(local bool) ([]api.GlobalPinInfo, error)
StatusAll(filter api.TrackerStatus, local bool) ([]api.GlobalPinInfo, error)

// Sync makes sure the state of a Cid corresponds to the state reported
// by the ipfs daemon, and returns it. If local is true, this operation
Expand Down
21 changes: 17 additions & 4 deletions api/rest/client/methods.go
Expand Up @@ -135,10 +135,23 @@ func (c *defaultClient) Status(ci cid.Cid, local bool) (api.GlobalPinInfo, error
return gpi.ToGlobalPinInfo(), err
}

// StatusAll gathers Status() for all tracked items.
func (c *defaultClient) StatusAll(local bool) ([]api.GlobalPinInfo, error) {
// StatusAll gathers Status() for all tracked items. If a filter is
// provided, only entries matching the given filter statuses
// will be returned. A filter can be built by merging TrackerStatuses with
// a bitwise OR operation (st1 | st2 | ...). A "0" filter value (or
// api.TrackerStatusUndefined), means all.
func (c *defaultClient) StatusAll(filter api.TrackerStatus, local bool) ([]api.GlobalPinInfo, error) {
var gpis []api.GlobalPinInfoSerial
err := c.do("GET", fmt.Sprintf("/pins?local=%t", local), nil, nil, &gpis)

filterStr := ""
if filter != api.TrackerStatusUndefined { // undefined filter means "all"
filterStr = filter.String()
if filterStr == "" {
return nil, errors.New("invalid filter value")
}
}

err := c.do("GET", fmt.Sprintf("/pins?local=%t&filter=%s", local, url.QueryEscape(filterStr)), nil, nil, &gpis)
result := make([]api.GlobalPinInfo, len(gpis))
for i, p := range gpis {
result[i] = p.ToGlobalPinInfo()
Expand Down Expand Up @@ -332,7 +345,7 @@ func statusReached(target api.TrackerStatus, gblPinInfo api.GlobalPinInfo) (bool
switch pinInfo.Status {
case target:
continue
case api.TrackerStatusBug, api.TrackerStatusClusterError, api.TrackerStatusPinError, api.TrackerStatusUnpinError:
case api.TrackerStatusUndefined, api.TrackerStatusClusterError, api.TrackerStatusPinError, api.TrackerStatusUnpinError:
return false, fmt.Errorf("error has occurred while attempting to reach status: %s", target.String())
case api.TrackerStatusRemote:
if target == api.TrackerStatusPinned {
Expand Down
33 changes: 32 additions & 1 deletion api/rest/client/methods_test.go
Expand Up @@ -219,14 +219,45 @@ func TestStatusAll(t *testing.T) {
defer shutdown(api)

testF := func(t *testing.T, c Client) {
pins, err := c.StatusAll(false)
pins, err := c.StatusAll(0, false)
if err != nil {
t.Fatal(err)
}

if len(pins) == 0 {
t.Error("there should be some pins")
}

// With local true
pins, err = c.StatusAll(0, true)
if err != nil {
t.Fatal(err)
}
if len(pins) != 2 {
t.Error("there should be two pins")
}

// With filter option
pins, err = c.StatusAll(types.TrackerStatusPinning, false)
if err != nil {
t.Fatal(err)
}
if len(pins) != 1 {
t.Error("there should be one pin")
}

pins, err = c.StatusAll(types.TrackerStatusPinned|types.TrackerStatusError, false)
if err != nil {
t.Fatal(err)
}
if len(pins) != 2 {
t.Error("there should be two pins")
}

pins, err = c.StatusAll(1<<25, false)
if err == nil {
t.Error("expected an error")
}
}

testClients(t, api, testF)
Expand Down

0 comments on commit d80f3ee

Please sign in to comment.