Documentation ¶
Overview ¶
Adding Models
Each model is kept in the model package of the REST v2 API in its own file. To create a new model, define a struct containing all of the fields that it will return and implement its two main interface methods BuildFromService and ToService. Be sure to include struct tags to the define the names the fields will have when serialized to JSON.
Guidlines for Creating Models ¶
Include as much data as a user is likely to want when inspecting this resource. This is likely to be more information than seems directly needed, but there is little penalty to its inclusion.
Use APIString instead of Golang's string type. APIString serializes empty strings as JSON null instead of Go's zero type of '""'.
Use APITime instead of go's time type. APITime is a type that wraps Go's time.Time and automatically and correctly serializes it to ISO-8601 UTC time.
Return an error when type casting fails.
Model Methods ¶
The Model type is an interface with two methods.
BuildFromService(in interface{}) error
BuildFromService fetches all needed data from the passed in object and sets them on the model. BuildFromService may sometimes be called multiple times with different types that all contain data to build up the model object. In this case, a type switch is likely necessary to determine what has been passed in.
ToService()(interface{}, error)
ToService creates an as-complete-as-possible version of the service layer's version of this model. For example, if this is is a REST v2 Task model, the ToService method creates a service layer Task and sets all of the fields it is able to and returns it.
Index ¶
Constants ¶
const ( // This string defines ISO-8601 UTC with 3 fractional seconds behind a dot // specified by the API spec document. APITimeFormat = "\"2006-01-02T15:04:05.000Z\"" )
const (
LogLinkFormat = "%s/task_log_raw/%s/%d?type=%s"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIHost ¶
type APIHost struct { Id APIString `json:"host_id"` Distro distroInfo `json:"distro"` Provisioned bool `json:"provisioned"` StartedBy APIString `json:"started_by"` Type APIString `json:"host_type"` User APIString `json:"user"` Status APIString `json:"status"` RunningTask taskInfo `json:"running_task"` }
APIHost is the model to be returned by the API whenever hosts are fetched.
func (*APIHost) BuildFromService ¶
BuildFromService converts from service level structs to an APIHost. It can be called multiple times with different data types, a service layer host and a service layer task, which are each loaded into the data structure.
type APIString ¶
type APIString string
func (*APIString) MarshalJSON ¶
func (*APIString) UnmarshalJSON ¶
type APITask ¶
type APITask struct { Id APIString `json:"task_id"` CreateTime APITime `json:"create_time"` DispatchTime APITime `json:"dispatch_time"` PushTime APITime `json:"push_time"` ScheduledTime APITime `json:"scheduled_time"` StartTime APITime `json:"start_time"` FinishTime APITime `json:"finish_time"` Version APIString `json:"version_id"` Branch APIString `json:"branch"` Revision APIString `json:"revision"` Priority int64 `json:"priority"` Activated bool `json:"activated"` ActivatedBy APIString `json:"activated_by"` BuildId APIString `json:"build_id"` DistroId APIString `json:"distro_id"` BuildVariant APIString `json:"build_variant"` DependsOn []string `json:"depends_on"` DisplayName APIString `json:"display_name"` HostId APIString `json:"host_id"` Restarts int `json:"restarts"` Execution int `json:"execution"` Order int `json:"order"` Status APIString `json:"status"` Details apiTaskEndDetail `json:"status_details"` Logs logLinks `json:"logs"` TimeTaken time.Duration `json:"time_taken_ms"` ExpectedDuration time.Duration `json:"expected_duration_ms"` }
APITask is the model to be returned by the API whenever tasks are fetched.
func (*APITask) BuildFromService ¶
BuildFromService converts from a service level task by loading the data into the appropriate fields of the APITask.
type APITest ¶
type APITest struct { TaskId APIString `json:"task_id"` Status APIString `json:"status"` TestFile APIString `json:"test_file"` Logs TestLogs `json:"logs"` ExitCode int `json:"exit_code"` StartTime APITime `json:"start_time"` EndTime APITime `json:"end_time"` }
APITest contains the data to be returned whenever a test is used in the API.
func (*APITest) BuildFromService ¶
type APITime ¶
func NewTime ¶
NewTime creates a new APITime from an existing time.Time. It handles changing converting from the times time zone to UTC.
func (APITime) MarshalJSON ¶
MarshalJSON implements the custom marshalling of this type so that it can be correctly written out in an API response.
func (*APITime) UnmarshalJSON ¶
UnmarshalJSON implements the custom unmarshalling of this type so that it can be correctly parsed from an API request.
type MockModel ¶
type MockModel struct { FieldId string FieldInt1 int FieldInt2 int FieldMap map[string]string FieldStruct *MockSubStruct }
func (*MockModel) BuildFromService ¶
type MockSubStruct ¶
type MockSubStruct struct {
SubInt int
}