/
host.go
412 lines (359 loc) · 11.8 KB
/
host.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package api
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"github.com/pearsonappeng/tensor/api/metadata"
"github.com/pearsonappeng/tensor/db"
"github.com/pearsonappeng/tensor/models/ansible"
"github.com/pearsonappeng/tensor/models/common"
"github.com/Sirupsen/logrus"
"github.com/pearsonappeng/tensor/log/activity"
"github.com/pearsonappeng/tensor/rbac"
"github.com/pearsonappeng/tensor/util"
"github.com/pearsonappeng/tensor/validate"
"gopkg.in/gin-gonic/gin.v1"
"gopkg.in/gin-gonic/gin.v1/binding"
"gopkg.in/mgo.v2/bson"
)
// Keys for credential related items stored in the Gin Context
const (
cHost = "host"
cHostID = "host_id"
)
type HostController struct{}
// Middleware generates a middleware handler function that works inside of a Gin request.
// Middleware takes CTXHostID parameter from the Gin Context and fetches host data from the database
// it set host data under key CTXHost in the Gin Context
func (ctrl HostController) Middleware(c *gin.Context) {
objectID := c.Params.ByName(cHostID)
user := c.MustGet(cUser).(common.User)
if !bson.IsObjectIdHex(objectID) {
AbortWithError(LogFields{Context: c, Status: http.StatusNotFound, Message: "Host does not exist"})
return
}
var host ansible.Host
if err := db.Hosts().FindId(bson.ObjectIdHex(objectID)).One(&host); err != nil {
AbortWithError(LogFields{Context: c, Status: http.StatusNotFound, Message: "Host does not exist",
Log: logrus.Fields{
"Host ID": objectID,
"Error": err.Error(),
},
})
return
}
roles := new(rbac.Inventory)
switch c.Request.Method {
case "GET":
{
if !roles.ReadByID(user, host.InventoryID) {
AbortWithError(LogFields{Context: c, Status: http.StatusUnauthorized,
Message: "You don't have sufficient permissions to perform this action.",
})
return
}
}
case "PUT", "DELETE":
{
// Reject the request if the user doesn't have inventory write permissions
if !roles.WriteByID(user, host.InventoryID) {
AbortWithError(LogFields{Context: c, Status: http.StatusUnauthorized,
Message: "You don't have sufficient permissions to perform this action.",
})
return
}
}
}
c.Set(cHost, host)
c.Next()
}
// GetHost is a Gin Handler function, returns the host as a JSON object
func (ctrl HostController) One(c *gin.Context) {
host := c.MustGet(cHost).(ansible.Host)
metadata.HostMetadata(&host)
c.JSON(http.StatusOK, host)
}
// GetHosts is Gin handler function which returns list of hosts
// This takes lookup parameters and order parameters to filter and sort output data
func (ctrl HostController) All(c *gin.Context) {
user := c.MustGet(cUser).(common.User)
parser := util.NewQueryParser(c)
match := bson.M{}
match = parser.Match([]string{"enabled", "has_active_failures"}, match)
match = parser.Lookups([]string{"name", "description"}, match)
query := db.Hosts().Find(match)
if order := parser.OrderBy(); order != "" {
query.Sort(order)
}
roles := new(rbac.Inventory)
var hosts []ansible.Host
iter := query.Iter()
var tmpHost ansible.Host
for iter.Next(&tmpHost) {
if !roles.ReadByID(user, tmpHost.InventoryID) {
continue
}
metadata.HostMetadata(&tmpHost)
hosts = append(hosts, tmpHost)
}
if err := iter.Close(); err != nil {
AbortWithError(LogFields{Context: c, Status: http.StatusGatewayTimeout,
Message: "Error while getting hosts",
Log: logrus.Fields{"Error": err.Error()},
})
return
}
count := len(hosts)
pgi := util.NewPagination(c, count)
if pgi.HasPage() {
AbortWithError(LogFields{Context: c, Status: http.StatusNotFound,
Message: "#" + strconv.Itoa(pgi.Page()) + " page contains no results.",
})
return
}
c.JSON(http.StatusOK, common.Response{
Count: count,
Next: pgi.NextPage(),
Previous: pgi.PreviousPage(),
Data: hosts[pgi.Skip():pgi.End()],
})
}
// AddHost is a Gin handler function which creates a new host using request payload
// This accepts Host model.
func (ctrl HostController) Create(c *gin.Context) {
var req ansible.Host
user := c.MustGet(cUser).(common.User)
if err := binding.JSON.Bind(c.Request, &req); err != nil {
AbortWithErrors(c, http.StatusBadRequest,
"Invalid JSON body",
validate.GetValidationErrors(err)...)
return
}
// check whether the inventory exist or not
if !req.InventoryExist() {
AbortWithError(LogFields{Context: c, Status: http.StatusBadRequest,
Message: "Inventory does not exists.",
})
return
}
// Reject the request if the user doesn't have inventory write permissions
if !new(rbac.Inventory).WriteByID(user, req.InventoryID) {
AbortWithError(LogFields{Context: c, Status: http.StatusUnauthorized,
Message: "You don't have sufficient permissions to perform this action.",
})
return
}
// if the host exist in the collection it is not unique
if !req.IsUnique() {
AbortWithError(LogFields{Context: c, Status: http.StatusBadRequest,
Message: "Host with this name and inventory already exists.",
})
return
}
// check whether the group exist or not
if req.GroupID != nil && !req.GroupExist() {
AbortWithError(LogFields{Context: c, Status: http.StatusBadRequest,
Message: "Group does not exist.",
})
return
}
req.ID = bson.NewObjectId()
req.Created = time.Now()
req.Modified = time.Now()
req.CreatedByID = user.ID
req.ModifiedByID = user.ID
if err := db.Hosts().Insert(req); err != nil {
AbortWithError(LogFields{Context: c, Status: http.StatusGatewayTimeout,
Message: "Error while creating host",
Log: logrus.Fields{"Error": err.Error()},
})
return
}
activity.AddActivity(activity.Create, user.ID, req, nil)
metadata.HostMetadata(&req)
c.JSON(http.StatusCreated, req)
}
// UpdateHost is a handler function which updates a credential using request payload.
// This replaces all the fields in the database, empty "" fields and
// unspecified fields will be removed from the database object
func (ctrl HostController) Update(c *gin.Context) {
host := c.MustGet(cHost).(ansible.Host)
tmpHost := host
user := c.MustGet(cUser).(common.User)
var req ansible.Host
if err := binding.JSON.Bind(c.Request, &req); err != nil {
AbortWithErrors(c, http.StatusBadRequest,
"Invalid JSON body",
validate.GetValidationErrors(err)...)
return
}
// check whether the inventory exist or not
if !req.InventoryExist() {
AbortWithError(LogFields{Context: c, Status: http.StatusBadRequest,
Message: "Inventory does not exists.",
})
return
}
if req.Name != host.Name && !req.IsUnique() {
AbortWithError(LogFields{Context: c, Status: http.StatusBadRequest,
Message: "Host with this name and inventory already exists.",
})
return
}
// check whether the group exist or not
if req.GroupID != nil && !req.GroupExist() {
AbortWithError(LogFields{Context: c, Status: http.StatusBadRequest,
Message: "Group does not exists.",
})
return
}
host.Name = strings.Trim(req.Name, " ")
host.InventoryID = req.InventoryID
host.Description = strings.Trim(req.Description, " ")
host.GroupID = req.GroupID
host.InstanceID = req.InstanceID
host.Variables = req.Variables
host.Enabled = req.Enabled
host.Modified = req.Modified
host.ModifiedByID = user.ID
if err := db.Hosts().UpdateId(host.ID, host); err != nil {
AbortWithError(LogFields{Context: c, Status: http.StatusGatewayTimeout,
Message: "Error while updating host.",
Log: logrus.Fields{"Host ID": req.ID.Hex(), "Error": err.Error()},
})
return
}
activity.AddActivity(activity.Update, user.ID, tmpHost, host)
metadata.HostMetadata(&host)
c.JSON(http.StatusOK, host)
}
// RemoveHost is a Gin handler function which removes a host object from the database
func (ctrl HostController) Delete(c *gin.Context) {
host := c.MustGet(cHost).(ansible.Host)
user := c.MustGet(cUser).(common.User)
if err := db.Hosts().RemoveId(host.ID); err != nil {
AbortWithError(LogFields{Context: c, Status: http.StatusGatewayTimeout,
Message: "Error while removing hosts",
Log: logrus.Fields{"Host ID": host.ID.Hex(), "Error": err.Error()},
})
return
}
activity.AddActivity(activity.Delete, user.ID, host, nil)
c.AbortWithStatus(204)
}
// VariableData is a Gin Handler function which returns variables for
// the host as JSON formatted object.
func (ctrl HostController) VariableData(c *gin.Context) {
host := c.MustGet(cHost).(ansible.Host)
variables := gin.H{}
if err := json.Unmarshal([]byte(host.Variables), &variables); err != nil {
AbortWithError(LogFields{Context: c, Status: http.StatusInternalServerError,
Message: "Error while getting host variables",
Log: logrus.Fields{"Host ID": host.ID.Hex(), "Error": err.Error()},
})
return
}
c.JSON(http.StatusOK, variables)
}
// Groups is a Gin handler function which returns parent group of the host
// TODO: not implemented
func (ctrl HostController) Groups(c *gin.Context) {
host := c.MustGet(cHost).(ansible.Host)
var group ansible.Group
if host.GroupID != nil {
if err := db.Groups().FindId(host.GroupID).One(&group); err != nil {
AbortWithError(LogFields{Context: c, Status: http.StatusGatewayTimeout,
Message: "Error while getting Groups",
Log: logrus.Fields{"Host ID": host.ID.Hex(), "Error": err.Error()},
})
return
}
metadata.GroupMetadata(&group)
c.JSON(http.StatusOK, group)
return
}
c.JSON(http.StatusOK, nil)
}
// AllGroups is a Gin handler function which returns parent groups of a host
// TODO: not implemented
func (ctrl HostController) AllGroups(c *gin.Context) {
host := c.MustGet(cHost).(ansible.Host)
var outobjects []ansible.Group
var group ansible.Group
if host.GroupID != nil {
if err := db.Groups().FindId(host.GroupID).One(&group); err != nil {
logrus.Errorln("Error while getting groups")
c.JSON(http.StatusInternalServerError, common.Error{
Code: http.StatusInternalServerError,
Errors: []string{"Error while getting groups"},
})
return
}
metadata.GroupMetadata(&group)
outobjects = append(outobjects, group)
group = ansible.Group{}
for group.ParentGroupID != nil {
if err := db.Groups().FindId(host.GroupID).One(&group); err != nil {
logrus.Errorln("Error while getting groups")
c.JSON(http.StatusInternalServerError, common.Error{
Code: http.StatusInternalServerError,
Errors: []string{"Error while getting groups"},
})
return
}
metadata.GroupMetadata(&group)
outobjects = append(outobjects, group)
group = ansible.Group{}
}
nobj := len(outobjects)
pgi := util.NewPagination(c, nobj)
if pgi.HasPage() {
c.JSON(http.StatusNotFound, gin.H{"detail": "Invalid page " + strconv.Itoa(pgi.Page()) + ": That page contains no results."})
return
}
c.JSON(http.StatusOK, common.Response{
Count: nobj,
Next: pgi.NextPage(),
Previous: pgi.PreviousPage(),
Data: outobjects[pgi.Limit():pgi.Offset()],
})
return
}
c.JSON(http.StatusOK, common.Response{
Count: 0,
})
}
// ActivityStream returns the activities of the user on Hosts
func (ctrl HostController) ActivityStream(c *gin.Context) {
host := c.MustGet(cHost).(ansible.Host)
var activities []common.Activity
var act common.Activity
iter := db.ActivityStream().Find(bson.M{"object1_id": host.ID}).Iter()
for iter.Next(&act) {
metadata.ActivityHostMetadata(&act)
activities = append(activities, act)
}
if err := iter.Close(); err != nil {
AbortWithError(LogFields{Context: c, Status: http.StatusGatewayTimeout,
Message: "Error while getting activities",
Log: logrus.Fields{"Host ID": host.ID.Hex(), "Error": err.Error()},
})
return
}
count := len(activities)
pgi := util.NewPagination(c, count)
if pgi.HasPage() {
AbortWithError(LogFields{Context: c, Status: http.StatusNotFound,
Message: "#" + strconv.Itoa(pgi.Page()) + " page contains no results.",
})
return
}
c.JSON(http.StatusOK, common.Response{
Count: count,
Next: pgi.NextPage(),
Previous: pgi.PreviousPage(),
Data: activities[pgi.Skip():pgi.End()],
})
}