add host fields

This commit is contained in:
Ulric Qin 2020-10-25 19:33:34 +08:00
parent 28a2196143
commit 6987b3b4d4
5 changed files with 206 additions and 0 deletions

View File

@ -27,4 +27,29 @@ CREATE TABLE `host`
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE `host_field`
(
`id` int unsigned not null AUTO_INCREMENT,
`field_ident` varchar(255) not null comment 'english identity',
`field_name` varchar(255) not null comment 'chinese name',
`field_type` varchar(64) not null,
`field_required` tinyint(1) not null default 0,
`field_extra` varchar(2048) not null default '',
`field_cate` varchar(255) not null default 'Default',
PRIMARY KEY (`id`),
KEY (`field_cate`, `field_ident`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE `host_field_value`
(
`id` int unsigned not null AUTO_INCREMENT,
`host_id` int unsigned not null,
`field_ident` varchar(255) not null,
`field_value` varchar(1024) not null default '',
PRIMARY KEY (`id`),
KEY (`host_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
/* 网络设备管理、机柜机架、配件耗材等相关的功能是商业版本才有的,表结构不要放到这里 */

88
src/models/host_field.go Normal file
View File

@ -0,0 +1,88 @@
package models
import (
"fmt"
)
type HostField struct {
Id int64 `json:"id"`
FieldIdent string `json:"field_ident"`
FieldName string `json:"field_name"`
FieldType string `json:"field_type"`
FieldRequired int `json:"field_required"`
FieldExtra string `json:"field_extra"`
FieldCate string `json:"field_cate"`
}
func (hf *HostField) Validate() error {
if len(hf.FieldIdent) > 255 {
return fmt.Errorf("field ident too long")
}
if len(hf.FieldName) > 255 {
return fmt.Errorf("field name too long")
}
if len(hf.FieldExtra) > 2048 {
return fmt.Errorf("field extra too long")
}
if len(hf.FieldCate) > 255 {
return fmt.Errorf("field cate too long")
}
return nil
}
func HostFieldNew(objPtr *HostField) error {
if err := objPtr.Validate(); err != nil {
return err
}
cnt, err := DB["ams"].Where("field_ident=?", objPtr.FieldIdent).Count(new(HostField))
if err != nil {
return err
}
if cnt > 0 {
return fmt.Errorf("%s already exists", objPtr.FieldIdent)
}
_, err = DB["ams"].Insert(objPtr)
return err
}
func (hf *HostField) Update(cols ...string) error {
if err := hf.Validate(); err != nil {
return err
}
_, err := DB["ams"].Where("id=?", hf.Id).Cols(cols...).Update(hf)
return err
}
func HostFieldGet(where string, args ...interface{}) (*HostField, error) {
var obj HostField
has, err := DB["ams"].Where(where, args...).Get(&obj)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return &obj, nil
}
func (hf *HostField) Del() error {
_, err := DB["ams"].Where("id=?", hf.Id).Delete(new(HostField))
return err
}
// HostFieldGets 条数非常少,全部返回
func HostFieldGets() ([]HostField, error) {
var objs []HostField
err := DB["ams"].OrderBy("field_cate, field_ident").Find(&objs)
return objs, err
}

View File

@ -0,0 +1,58 @@
package models
import (
"fmt"
)
type HostFieldValue struct {
Id int64 `json:"id"`
HostId int64 `json:"host_id"`
FieldIdent string `json:"field_ident"`
FieldValue string `json:"field_value"`
}
func (hfv *HostFieldValue) Validate() error {
if len(hfv.FieldValue) > 1024 {
return fmt.Errorf("field value too long")
}
return nil
}
// HostFieldValueGets 条数非常少,全部返回
func HostFieldValueGets(hostId int64) ([]HostFieldValue, error) {
var objs []HostFieldValue
err := DB["ams"].Where("host_id = ?", hostId).OrderBy("field_ident").Find(&objs)
return objs, err
}
func HostFieldValuePuts(hostId int64, objs []HostFieldValue) error {
count := len(objs)
session := DB["ams"].NewSession()
defer session.Close()
for i := 0; i < count; i++ {
num, err := session.Where("host_id = ? and field_ident = ?", hostId, objs[i].FieldIdent).Count(new(HostFieldValue))
if err != nil {
return fmt.Errorf("count host_field_value fail: %v", err)
}
if num > 0 {
_, err = session.Exec("UPDATE host_field_value SET field_value = ? WHERE host_id = ? and field_ident = ?", objs[i].FieldValue, hostId, objs[i].FieldIdent)
if err != nil {
return fmt.Errorf("update host_field_value fail: %v", err)
}
} else {
_, err = session.InsertOne(HostFieldValue{
HostId: hostId,
FieldIdent: objs[i].FieldIdent,
FieldValue: objs[i].FieldValue,
})
if err != nil {
return fmt.Errorf("insert host_field_value fail: %v", err)
}
}
}
return nil
}

View File

@ -19,6 +19,10 @@ func Config(r *gin.Engine) {
userLogin.PUT("/hosts/cate", hostCatePut)
userLogin.DELETE("/hosts", hostDel)
userLogin.GET("/hosts/search", hostSearchGets)
userLogin.GET("/hosts/fields", hostFieldsGets)
userLogin.GET("/hosts/field/:id", hostFieldGet)
userLogin.GET("/host/:id/fields", hostFieldGets)
userLogin.PUT("/host/:id/fields", hostFieldPuts)
}
v1 := r.Group("/v1/ams-ce").Use(shouldBeService())

View File

@ -0,0 +1,31 @@
package http
import (
"github.com/gin-gonic/gin"
"github.com/didi/nightingale/src/models"
)
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)
}
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))
}