-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
143 lines (119 loc) · 3.18 KB
/
http.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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 client
import (
"net/http"
"net/url"
"path"
"google.golang.org/api/googleapi"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/schema"
)
func NewHTTPClient(c *http.Client, ep url.URL) (API, error) {
svc, err := schema.New(c)
if err != nil {
return nil, err
}
// append a slash so the schema.Service knows this is the root path
ep.Path = path.Join(ep.Path, "fleet", "v1") + "/"
svc.BasePath = ep.String()
return &HTTPClient{svc: svc}, nil
}
type HTTPClient struct {
svc *schema.Service
//NOTE(bcwaldon): This is only necessary until the API interface
// is fully implemented by HTTPClient
API
}
func (c *HTTPClient) Machines() ([]machine.MachineState, error) {
machines := make([]machine.MachineState, 0)
call := c.svc.Machines.List()
for call != nil {
page, err := call.Do()
if err != nil {
return nil, err
}
machines = append(machines, schema.MapSchemaToMachineStates(page.Machines)...)
if len(page.NextPageToken) > 0 {
call = c.svc.Machines.List()
call.NextPageToken(page.NextPageToken)
} else {
call = nil
}
}
return machines, nil
}
func (c *HTTPClient) Units() ([]*schema.Unit, error) {
var units []*schema.Unit
call := c.svc.Units.List()
for call != nil {
page, err := call.Do()
if err != nil {
return nil, err
}
units = append(units, page.Units...)
if len(page.NextPageToken) > 0 {
call = c.svc.Units.List()
call.NextPageToken(page.NextPageToken)
} else {
call = nil
}
}
return units, nil
}
func (c *HTTPClient) Unit(name string) (*schema.Unit, error) {
u, err := c.svc.Units.Get(name).Do()
if err != nil && !is404(err) {
return nil, err
}
return u, nil
}
func (c *HTTPClient) UnitStates() ([]*schema.UnitState, error) {
var states []*schema.UnitState
call := c.svc.UnitState.List()
for call != nil {
page, err := call.Do()
if err != nil {
return nil, err
}
states = append(states, page.States...)
if len(page.NextPageToken) > 0 {
call = c.svc.UnitState.List()
call.NextPageToken(page.NextPageToken)
} else {
call = nil
}
}
return states, nil
}
func (c *HTTPClient) DestroyUnit(name string) error {
return c.svc.Units.Delete(name).Do()
}
func (c *HTTPClient) CreateUnit(u *schema.Unit) error {
return c.svc.Units.Set(u.Name, u).Do()
}
func (c *HTTPClient) SetUnitTargetState(name, target string) error {
u := schema.Unit{
Name: name,
DesiredState: target,
}
return c.svc.Units.Set(name, &u).Do()
}
func is404(err error) bool {
googerr, ok := err.(*googleapi.Error)
return ok && googerr.Code == http.StatusNotFound
}
func IsErrorUnitNotFound(err error) bool {
return is404(err)
}