/
service_list.go
71 lines (57 loc) · 1.46 KB
/
service_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
60
61
62
63
64
65
66
67
68
69
70
71
package command
import (
"sync"
"github.com/dokkur/swanager/core/entities"
"github.com/dokkur/swanager/core/swarm"
swarm_service "github.com/dokkur/swanager/core/swarm/service"
"github.com/gin-gonic/gin"
)
// ServiceList returns services list
type ServiceList struct {
CommonCommand
User *entities.User
ApplicationID string
WithStatuses bool
WithVolumeSizes bool
responseChan chan<- []entities.Service
}
// NewServiceListCommand create command
func NewServiceListCommand(command ServiceList) (ServiceList, chan []entities.Service, chan error) {
response := make(chan []entities.Service, 1)
err := make(chan error, 1)
command.errorChan = err
command.responseChan = response
return command, response, err
}
// Process executes command
func (c ServiceList) Process() {
searchOptions := make(gin.H)
if c.User != nil {
searchOptions["user_id"] = c.User.ID
}
if c.ApplicationID != "" {
searchOptions["application_id"] = c.ApplicationID
}
services, err := entities.GetServices(searchOptions)
if err != nil {
c.errorChan <- err
return
}
if c.WithStatuses || c.WithVolumeSizes {
var wg sync.WaitGroup
for serviceIndex := range services {
wg.Add(1)
go func(service *entities.Service) {
defer wg.Done()
if c.WithStatuses {
swarm.GetServiceStatuses(service)
}
if c.WithVolumeSizes {
swarm_service.LoadVolumeSizes(service)
}
}(&services[serviceIndex])
}
wg.Wait()
}
c.responseChan <- services
}