Skip to content

Commit 16cdc5c

Browse files
authored
Hagrid upgrade to v2 (#5)
* Refactor backend with beego framework * Add tcpService module * Refactor syncing alert * Temp save * Add modal and button class * Temp save frontend codes * Temp save async actions * Finish Update/Add graphite modal * Make the table more stable and beautiful * Add a select for alerts * Add and finish TCPService module * Almost finish template module * Use select2 as tagInput * Finish templates and adjust some styles * Finish user profile page * Finish add alert page * Finish update alert page * Finsh admin management * Finish notifier * Update icinga image and fix some bugs * Remove strict phone number validaton * Refactor error message output * Fix serval bugs - Fix logout failed causing by cookie. - Automatically choose first alert after login if one exists. - Remove href attribute from <a> tags and add a pointer cursor style. - Add HelpCard but with empty content. * Finish help page * Remove alpha verison * Add a health check API * Fix frontend bug - Fix: The selected alert is back to the default after saving * Add check_http module - Remove redudant and compress js files - Format files - Update icinga2 base image * Fix issue of rendering template * Add the link for check_http manual * Fix text error * Fix bug of rendering post data field of check_http
1 parent 789eff1 commit 16cdc5c

File tree

379 files changed

+51441
-26008
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

379 files changed

+51441
-26008
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ hagrid
66
hagrid.conf.json
77
hagrid.iml
88
node_modules/
9+
hagrid.exe
10+
npm-debug.log
11+
static/css/demo.css

Godeps/Godeps.json

Lines changed: 0 additions & 52 deletions
This file was deleted.

Godeps/Readme

Lines changed: 0 additions & 5 deletions
This file was deleted.

controllers/admin.go

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,100 @@
11
package controllers
22

33
import (
4-
"encoding/json"
4+
"fmt"
55
"net/http"
66
"strconv"
77

8-
"github.com/gorilla/mux"
8+
"github.com/astaxie/beego/validation"
9+
"github.com/jinzhu/gorm"
910
"github.com/laincloud/hagrid/models"
1011
)
1112

12-
func AddAdminHandler(w http.ResponseWriter, r *http.Request) {
13-
alertID, _ := strconv.Atoi(r.URL.Query().Get("alert_id"))
14-
var adminID int
15-
16-
_, err := authorize(w, r, alertID)
17-
if err != nil {
18-
return
19-
}
20-
21-
if adminID, err = strconv.Atoi(r.FormValue("adminID")); err != nil {
22-
writeResponse(w, "Field user_id must be an integer", http.StatusBadRequest)
23-
return
24-
}
25-
26-
if models.IsAdminDuplicated(alertID, adminID) {
27-
writeResponse(w, "The admin is duplicated in this alert", http.StatusConflict)
28-
return
29-
}
30-
31-
if err = models.AddAdmin(alertID, adminID); err != nil {
32-
writeResponse(w, err.Error(), http.StatusInternalServerError)
33-
return
34-
}
35-
writeResponse(w, "Add administrator successfully", http.StatusCreated)
13+
type AdminController struct {
14+
AuthController
3615
}
3716

38-
func DeleteAdminHandler(w http.ResponseWriter, r *http.Request) {
39-
40-
alertID, _ := strconv.Atoi(r.URL.Query().Get("alert_id"))
41-
42-
user, err := authorize(w, r, alertID)
43-
if err != nil {
44-
return
17+
func (this *AdminController) NestPrepare() {
18+
var adminID int
19+
if this.Ctx.Input.IsPost() {
20+
validator := validation.Validation{}
21+
adminIDStr := this.GetString("admin_id")
22+
validator.Required(adminIDStr, "admin_id")
23+
validator.Numeric(adminIDStr, "admin_id")
24+
if validator.HasErrors() {
25+
for _, err := range validator.Errors {
26+
this.outputError(http.StatusBadRequest, fmt.Sprintf("[%s]%s", err.Key, err.Message))
27+
this.ServeJSON()
28+
this.StopRun()
29+
}
30+
}
31+
adminID, _ = strconv.Atoi(adminIDStr)
32+
} else {
33+
adminID, _ = strconv.Atoi(this.Ctx.Input.Param(":admin_id"))
34+
if this.Ctx.Input.IsDelete() {
35+
authUser := this.Ctx.Input.GetData("auth_user").(*models.User)
36+
if authUser.ID == adminID {
37+
this.outputError(http.StatusForbidden, errorMsg403)
38+
this.ServeJSON()
39+
this.StopRun()
40+
}
41+
}
4542
}
46-
47-
vars := mux.Vars(r)
48-
id, _ := strconv.Atoi(vars["id"])
49-
50-
if id == user.ID {
51-
writeResponse(w, "You can't delete yourself!", http.StatusForbidden)
52-
return
43+
if isSuper, err := models.IsSuperUser(adminID); isSuper {
44+
this.outputError(http.StatusForbidden, errorMsg403)
45+
this.ServeJSON()
46+
this.StopRun()
47+
} else if err != nil {
48+
this.outputError(http.StatusInternalServerError, errorMsg500)
49+
this.ServeJSON()
50+
this.StopRun()
51+
} else {
52+
this.Ctx.Input.SetData("adminID", adminID)
5353
}
54+
}
5455

55-
if err = models.DeleteAdmin(alertID, id); err != nil {
56-
writeResponse(w, err.Error(), http.StatusForbidden)
57-
return
56+
func (this *AdminController) Post() {
57+
adminID := this.Ctx.Input.GetData("adminID").(int)
58+
alertID, _ := strconv.Atoi(this.Ctx.Input.Param(":alert_id"))
59+
if err := models.AddAdmin(alertID, adminID); err == models.ErrorDuplicatedName {
60+
this.outputError(http.StatusConflict, errorMsg409)
61+
} else if err == gorm.ErrRecordNotFound {
62+
this.outputError(http.StatusNotFound, errorMsg404)
63+
} else if err != nil {
64+
controllerLogger.Printf("PostAdmin failed: %s", err.Error())
65+
this.outputError(http.StatusInternalServerError, errorMsg500)
66+
} else {
67+
this.outputSuccess(http.StatusCreated, map[string]string{
68+
"message": "Add admin successfully",
69+
})
5870
}
59-
writeResponse(w, "Delete administrator successfully", http.StatusNoContent)
71+
this.ServeJSON()
6072
}
6173

62-
func GetAdminsHandler(w http.ResponseWriter, r *http.Request) {
63-
alertID, _ := strconv.Atoi(r.URL.Query().Get("alert_id"))
64-
var (
65-
admins []models.User
66-
err error
67-
data []byte
68-
)
69-
70-
if err = models.GetAdminsByAlertID(alertID, &admins); err != nil {
71-
writeResponse(w, err.Error(), http.StatusInternalServerError)
72-
return
74+
func (this *AdminController) Delete() {
75+
adminID := this.Ctx.Input.GetData("adminID").(int)
76+
alertID, _ := strconv.Atoi(this.Ctx.Input.Param(":alert_id"))
77+
if err := models.DeleteAdmin(alertID, adminID); err == gorm.ErrRecordNotFound {
78+
this.outputError(http.StatusNotFound, errorMsg404)
79+
} else if err != nil {
80+
controllerLogger.Printf("DeleteAdmin failed: %s", err.Error())
81+
this.outputError(http.StatusInternalServerError, errorMsg500)
82+
} else {
83+
this.outputSuccess(http.StatusNoContent, succMsg204)
7384
}
85+
this.ServeJSON()
86+
}
7487

75-
briefUsers := make([]UserMV, 0, len(admins))
76-
for _, user := range admins {
77-
briefUsers = append(
78-
briefUsers,
79-
UserMV{
80-
ID: user.ID,
81-
Name: user.Name,
82-
},
83-
)
84-
}
85-
if data, err = json.Marshal(briefUsers); err != nil {
86-
writeResponse(w, err.Error(), http.StatusInternalServerError)
87-
return
88+
func (this *AdminController) Get() {
89+
alertID, _ := strconv.Atoi(this.Ctx.Input.Param(":alert_id"))
90+
var admins []models.User
91+
if err := models.GetAdminsByAlertID(alertID, &admins); err == gorm.ErrRecordNotFound {
92+
this.outputError(http.StatusNotFound, errorMsg404)
93+
} else if err != nil {
94+
controllerLogger.Printf("GetAdmins failed: %s", err.Error())
95+
this.outputError(http.StatusInternalServerError, errorMsg500)
96+
} else {
97+
this.outputSuccess(http.StatusOK, &admins)
8898
}
89-
w.WriteHeader(http.StatusOK)
90-
w.Write(data)
99+
this.ServeJSON()
91100
}

0 commit comments

Comments
 (0)