github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/apiv3/model/host.go (about) 1 package model 2 3 import ( 4 "fmt" 5 6 "github.com/evergreen-ci/evergreen/model/host" 7 "github.com/evergreen-ci/evergreen/model/task" 8 ) 9 10 // APIHost is the model to be returned by the API whenever hosts are fetched. 11 type APIHost struct { 12 Id APIString `json:"host_id"` 13 Distro distroInfo `json:"distro"` 14 Provisioned bool `json:"provisioned"` 15 StartedBy APIString `json:"started_by"` 16 Type APIString `json:"host_type"` 17 User APIString `json:"user"` 18 Status APIString `json:"status"` 19 RunningTask taskInfo `json:"running_task"` 20 } 21 22 type distroInfo struct { 23 Id APIString `json:"distro_id"` 24 Provider APIString `json:"provider"` 25 } 26 27 type taskInfo struct { 28 Id APIString `json:"task_id"` 29 Name APIString `json:"name"` 30 DispatchTime APITime `json:"dispatch_time"` 31 VersionId APIString `json:"version_id"` 32 BuildId APIString `json:"build_id"` 33 } 34 35 // BuildFromService converts from service level structs to an APIHost. It can 36 // be called multiple times with different data types, a service layer host and 37 // a service layer task, which are each loaded into the data structure. 38 func (apiHost *APIHost) BuildFromService(h interface{}) error { 39 switch v := h.(type) { 40 case host.Host: 41 apiHost.Id = APIString(v.Id) 42 apiHost.Provisioned = v.Provisioned 43 apiHost.StartedBy = APIString(v.StartedBy) 44 apiHost.Type = APIString(v.InstanceType) 45 apiHost.User = APIString(v.UserData) 46 apiHost.Status = APIString(v.Status) 47 48 di := distroInfo{ 49 Id: APIString(v.Distro.Id), 50 Provider: APIString(v.Distro.Provider), 51 } 52 apiHost.Distro = di 53 case task.Task: 54 rt := taskInfo{ 55 Id: APIString(v.Id), 56 Name: APIString(v.DisplayName), 57 DispatchTime: APITime(v.DispatchTime), 58 VersionId: APIString(v.Version), 59 BuildId: APIString(v.BuildId), 60 } 61 apiHost.RunningTask = rt 62 default: 63 return fmt.Errorf("incorrect type when fetching converting host type") 64 } 65 return nil 66 } 67 68 // ToService returns a service layer host using the data from the APIHost. 69 func (apiHost *APIHost) ToService() (interface{}, error) { 70 h := host.Host{ 71 Id: string(apiHost.Id), 72 Provisioned: apiHost.Provisioned, 73 StartedBy: string(apiHost.StartedBy), 74 InstanceType: string(apiHost.Type), 75 UserData: string(apiHost.User), 76 Status: string(apiHost.Status), 77 } 78 return interface{}(h), nil 79 }