forked from ehazlett/stellar
/
list.go
59 lines (50 loc) · 1.32 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package application
import (
"context"
"fmt"
"sort"
"github.com/ehazlett/stellar"
api "github.com/ehazlett/stellar/api/services/application/v1"
"github.com/sirupsen/logrus"
)
type AppSorter []*api.App
func (a AppSorter) Len() int { return len(a) }
func (a AppSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a AppSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }
func (s *service) List(ctx context.Context, req *api.ListRequest) (*api.ListResponse, error) {
// filter containers for application label
c, err := s.client()
if err != nil {
return nil, err
}
defer c.Close()
containers, err := c.Cluster().Containers(fmt.Sprintf("labels.\"%s\"", stellar.StellarApplicationLabel))
if err != nil {
return nil, err
}
apps := map[string]*api.App{}
for _, c := range containers {
svc, err := s.containerToService(ctx, c)
if err != nil {
logrus.Error(err)
continue
}
name := c.Container.Labels[stellar.StellarApplicationLabel]
app, ok := apps[name]
if !ok {
app = &api.App{
Name: name,
}
apps[name] = app
}
app.Services = append(app.Services, svc)
}
applications := []*api.App{}
for _, app := range apps {
applications = append(applications, app)
}
sort.Sort(AppSorter(applications))
return &api.ListResponse{
Applications: applications,
}, nil
}