router_host_field.go 1.9 KB
Newer Older
U
Ulric Qin 已提交
1 2 3 4 5 6 7 8
package http

import (
	"github.com/gin-gonic/gin"

	"github.com/didi/nightingale/src/models"
)

U
Ulric Qin 已提交
9 10 11 12 13 14
func hostFieldNew(c *gin.Context) {
	loginUser(c).CheckPermGlobal("ams_host_field_mgr")

	var obj models.HostField
	bind(c, &obj)

U
Ulric Qin 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	if obj.FieldIdent == "" {
		bomb("field_ident is blank")
	}

	if obj.FieldName == "" {
		bomb("field_name is blank")
	}

	if obj.FieldType == "" {
		bomb("field_type is blank")
	}

	if obj.FieldCate == "" {
		obj.FieldCate = "Default"
	}

U
Ulric Qin 已提交
31 32 33
	renderMessage(c, models.HostFieldNew(&obj))
}

U
Ulric Qin 已提交
34 35 36 37 38 39 40 41 42 43
func hostFieldsGets(c *gin.Context) {
	lst, err := models.HostFieldGets()
	renderData(c, lst, err)
}

func hostFieldGet(c *gin.Context) {
	obj, err := models.HostFieldGet("id = ?", urlParamInt64(c, "id"))
	renderData(c, obj, err)
}

U
Ulric Qin 已提交
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
func hostFieldPut(c *gin.Context) {
	loginUser(c).CheckPermGlobal("ams_host_field_mgr")

	var f models.HostField
	bind(c, &f)

	obj, err := models.HostFieldGet("id = ?", urlParamInt64(c, "id"))
	dangerous(err)

	if obj == nil {
		bomb("no such field")
	}

	if obj.FieldType != f.FieldType {
		bomb("field_type cannot modify")
	}

	obj.FieldName = f.FieldName
	obj.FieldExtra = f.FieldExtra
	obj.FieldRequired = f.FieldRequired
	obj.FieldCate = f.FieldCate

	renderMessage(c, obj.Update("field_name", "field_extra", "field_required", "field_cate"))
}

func hostFieldDel(c *gin.Context) {
	loginUser(c).CheckPermGlobal("ams_host_field_mgr")

	obj, err := models.HostFieldGet("id = ?", urlParamInt64(c, "id"))
	dangerous(err)

	if obj == nil {
		renderMessage(c, nil)
		return
	}

	renderMessage(c, obj.Del())
}

U
Ulric Qin 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
func hostFieldGets(c *gin.Context) {
	lst, err := models.HostFieldValueGets(urlParamInt64(c, "id"))
	renderData(c, lst, err)
}

func hostFieldPuts(c *gin.Context) {
	var objs []models.HostFieldValue
	bind(c, &objs)

	loginUser(c).CheckPermGlobal("ams_host_modify")

	renderMessage(c, models.HostFieldValuePuts(urlParamInt64(c, "id"), objs))
}