github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/apiv3/servicecontext/host.go (about) 1 package servicecontext 2 3 import ( 4 "net/http" 5 6 "github.com/evergreen-ci/evergreen/apiv3" 7 "github.com/evergreen-ci/evergreen/model/host" 8 ) 9 10 // DBHostConnector is a struct that implements the Host related methods 11 // from the ServiceContext through interactions with the backing database. 12 type DBHostConnector struct{} 13 14 // FindHosts uses the service layer's host type to query the backing database for 15 // the hosts. 16 func (hc *DBHostConnector) FindHostsById(id, status string, limit int, sortDir int) ([]host.Host, error) { 17 hostRes, err := host.GetHostsByFromIdWithStatus(id, status, limit, sortDir) 18 if err != nil { 19 return nil, err 20 } 21 if len(hostRes) == 0 { 22 return nil, apiv3.APIError{ 23 StatusCode: http.StatusNotFound, 24 Message: "no hosts found", 25 } 26 } 27 return hostRes, nil 28 } 29 30 // MockHostConnector is a struct that implements the Host related methods 31 // from the ServiceContext through interactions with he backing database. 32 type MockHostConnector struct { 33 CachedHosts []host.Host 34 } 35 36 // FindHosts uses the service layer's host type to query the backing database for 37 // the hosts. 38 func (hc *MockHostConnector) FindHostsById(id, status string, limit int, sort int) ([]host.Host, error) { 39 // loop until the key is found 40 41 for ix, h := range hc.CachedHosts { 42 if h.Id == id { 43 // We've found the host 44 var hostsToReturn []host.Host 45 if sort < 0 { 46 if ix-limit > 0 { 47 hostsToReturn = hc.CachedHosts[ix-(limit) : ix] 48 } else { 49 hostsToReturn = hc.CachedHosts[:ix] 50 } 51 } else { 52 if ix+limit > len(hc.CachedHosts) { 53 hostsToReturn = hc.CachedHosts[ix:] 54 } else { 55 hostsToReturn = hc.CachedHosts[ix : ix+limit] 56 } 57 } 58 return hostsToReturn, nil 59 } 60 } 61 return nil, nil 62 }