forked from evergreen-ci/evergreen
/
creathost.go
50 lines (43 loc) · 1.59 KB
/
creathost.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
package data
import (
"net/http"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/gimlet"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
// DBCreateHostConnector supports `host.create` commands from the agent.
type DBCreateHostConnector struct{}
// ListHostsForTask lists running hosts scoped to the task or the task's build.
func (*DBCreateHostConnector) ListHostsForTask(taskID string) ([]host.Host, error) {
t, err := task.FindOneId(taskID)
if err != nil {
return nil, gimlet.ErrorResponse{StatusCode: http.StatusInternalServerError, Message: "error finding task"}
}
if t == nil {
return nil, gimlet.ErrorResponse{StatusCode: http.StatusInternalServerError, Message: "no task found"}
}
catcher := grip.NewBasicCatcher()
hostsSpawnedByTask, err := host.FindHostsSpawnedByTask(t.Id)
catcher.Add(err)
hostsSpawnedByBuild, err := host.FindHostsSpawnedByBuild(t.BuildId)
catcher.Add(err)
if catcher.HasErrors() {
return nil, gimlet.ErrorResponse{StatusCode: http.StatusInternalServerError, Message: catcher.String()}
}
hosts := []host.Host{}
for _, h := range hostsSpawnedByBuild {
hosts = append(hosts, h)
}
for _, h := range hostsSpawnedByTask {
hosts = append(hosts, h)
}
return hosts, nil
}
// MockCreateHostConnector mocks `DBCreateHostConnector`.
type MockCreateHostConnector struct{}
// ListHostsForTask lists running hosts scoped to the task or the task's build.
func (*MockCreateHostConnector) ListHostsForTask(taskID string) ([]host.Host, error) {
return nil, errors.New("method not implemented")
}