Skip to content

Commit

Permalink
add list mesos agents filter
Browse files Browse the repository at this point in the history
  • Loading branch information
pwzgorilla committed Sep 12, 2017
1 parent 46b5b37 commit 8e69dbb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
22 changes: 20 additions & 2 deletions api/mesos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ func (s *Server) listMesosAgents(w http.ResponseWriter, r *http.Request) {
return
}

writeJSON(w, http.StatusOK, agents)
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

filtered := s.filter(agents, r.Form)

writeJSON(w, http.StatusOK, filtered)
}

func (s *Server) getAgentLabels(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -197,7 +204,6 @@ func (s *Server) canOperated(label *types.MesosLabel) bool {
}

constraints := ver.Constraints
log.Println("===", constraints)
if len(constraints) == 0 {
continue
}
Expand All @@ -211,3 +217,15 @@ func (s *Server) canOperated(label *types.MesosLabel) bool {

return true
}

func (s *Server) filter(agents []*types.MesosAgent, filter map[string][]string) []*types.MesosAgent {
filtered := make([]*types.MesosAgent, 0)

for _, agent := range agents {
if agent.Match(filter) {
filtered = append(filtered, agent)
}
}

return filtered
}
1 change: 1 addition & 0 deletions types/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (c *Compose) Valid() error {
if err := utils.LegalDomain(c.Name); err != nil {
return err
}

if c.Name == "default" {
return errors.New("compose name `default` is reserved")
}
Expand Down
23 changes: 23 additions & 0 deletions types/mesos.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,26 @@ type MesosAgent struct {
Ports int64 `json:"ports"`
Attrs map[string]string `json:"attrs"`
}

func (m *MesosAgent) Match(filter map[string][]string) bool {
var n int
for k, vals := range filter {
if v, ok := m.Attrs[k]; ok {
if in(v, vals) {
n++
}
}
}

return n == len(filter)
}

func in(v string, vals []string) bool {
for i := 0; i < len(vals); i++ {
if v == vals[i] {
return true
}
}

return false
}

0 comments on commit 8e69dbb

Please sign in to comment.