forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machines.go
113 lines (89 loc) · 2.58 KB
/
machines.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright 2014 CoreOS, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"fmt"
"net/http"
"path"
"github.com/coreos/fleet/client"
"github.com/coreos/fleet/log"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/schema"
)
func wireUpMachinesResource(mux *http.ServeMux, prefix string, cAPI client.API) {
res := path.Join(prefix, "machines")
mr := machinesResource{cAPI}
mux.Handle(res, &mr)
}
type machinesResource struct {
cAPI client.API
}
func (mr *machinesResource) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
sendError(rw, http.StatusBadRequest, fmt.Errorf("only HTTP GET supported against this resource"))
return
}
token, err := findNextPageToken(req.URL)
if err != nil {
sendError(rw, http.StatusBadRequest, err)
return
}
if token == nil {
def := DefaultPageToken()
token = &def
}
page, err := getMachinePage(mr.cAPI, *token)
if err != nil {
log.Errorf("Failed fetching page of Machines: %v", err)
sendError(rw, http.StatusInternalServerError, nil)
return
}
sendResponse(rw, http.StatusOK, page)
}
func getMachinePage(cAPI client.API, tok PageToken) (*schema.MachinePage, error) {
all, err := cAPI.Machines()
if err != nil {
return nil, err
}
page := extractMachinePage(all, tok)
return page, nil
}
func extractMachinePage(all []machine.MachineState, tok PageToken) *schema.MachinePage {
total := len(all)
startIndex := int((tok.Page - 1) * tok.Limit)
stopIndex := int(tok.Page * tok.Limit)
var items []machine.MachineState
var next *PageToken
if startIndex < total {
if stopIndex > total {
stopIndex = total
} else {
n := tok.Next()
next = &n
}
items = all[startIndex:stopIndex]
}
return newMachinePage(items, next)
}
func newMachinePage(items []machine.MachineState, tok *PageToken) *schema.MachinePage {
smp := schema.MachinePage{
Machines: make([]*schema.Machine, 0, len(items)),
}
if tok != nil {
smp.NextPageToken = tok.Encode()
}
for _, sm := range items {
smp.Machines = append(smp.Machines, schema.MapMachineStateToSchema(&sm))
}
return &smp
}