Skip to content

Commit

Permalink
ability to filter compose app according by extra labels
Browse files Browse the repository at this point in the history
  • Loading branch information
bbklab committed Aug 23, 2017
1 parent 48f6907 commit 9d35f9d
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions api/compose-ng.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -272,6 +273,9 @@ func (r *Server) listComposesNG(w http.ResponseWriter, req *http.Request) {
return
}

req.ParseForm()
cs = r.filterComposeNG(cs, req.Form)

sort.Sort(types.ComposeAppSorter(cs))
writeJSON(w, http.StatusOK, cs)
}
Expand Down Expand Up @@ -458,3 +462,33 @@ func (r *Server) wrapComposeNG(cmpApp *types.ComposeApp) (*types.ComposeAppWrapp
}
return wrapper, nil
}

// filter compose apps according by labels
func (r *Server) filterComposeNG(cmpApps []*types.ComposeApp, filter url.Values) []*types.ComposeApp {
var idx int

for _, cmpApp := range cmpApps {
labels := cmpApp.Labels
if len(labels) == 0 {
continue
}

var n int
for k, v := range filter {
for key, val := range labels {
if key == k && val == v[0] {
n++
break
}
}
}

// if matched all filters
if n == len(filter) {
cmpApps[idx] = cmpApp
idx++
}
}

return cmpApps[0:idx]
}

0 comments on commit 9d35f9d

Please sign in to comment.