Skip to content

Commit

Permalink
0809
Browse files Browse the repository at this point in the history
  • Loading branch information
qishu321 committed Aug 9, 2023
1 parent 6ead99a commit 83455d1
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 14 deletions.
90 changes: 90 additions & 0 deletions api/cmdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package api

import (
"cmdb-ops-flow/models"
"cmdb-ops-flow/service"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"github.com/gin-gonic/gin"
"net/http"
)

func AddCmdb(c *gin.Context) {
var data models.Cmdb
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := models.Checkcmdb(data.Cmdbname)
if code != msg.SUCCSE {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, "Cmdbname不能重复", msg.GetErrMsg(msg.ERROR)))
return
}

// 调用 service.AddCmdb 执行业务逻辑
list, err := service.AddCmdb(data)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))

}
func GetSearchCmdb(c *gin.Context) {
var data struct {
Keyword string `form:"keyword" binding:"required"`
}
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, code := models.SearchCmdb(data.Keyword)
if code != msg.SUCCSE {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, "Cmdbname不能重复", msg.GetErrMsg(msg.ERROR)))
return
}
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}

func GetCmdb(c *gin.Context) {
var data models.Cmdb
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service.GetCmdbList(data)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))

}

func EditCmdb(c *gin.Context) {
var data models.Cmdb
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service.EditCmdb(data)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}
func DelCmdb(c *gin.Context) {
var data models.Cmdb
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := models.Delcmdb(data.Cmdbid)

c.JSON(http.StatusOK, (&result.Result{}).Ok(code, msg.SUCCSE, msg.GetErrMsg(code)))

}
5 changes: 4 additions & 1 deletion conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var (
ReadTimeout time.Duration
WriteTimeout time.Duration

Md5Key string
Md5Key string
Encryptkey string

DbHost string
DbPort string
Expand All @@ -38,6 +39,8 @@ func LoadServer(file *ini.File) {
}
func LoadApp(file *ini.File) {
Md5Key = file.Section("app").Key("Md5Key").String()
Encryptkey = file.Section("app").Key("Encryptkey").String()

}

func LoadData(file *ini.File) {
Expand Down
2 changes: 1 addition & 1 deletion conf/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ReadTimeout: 60
WriteTimeout: 60
[app]
Md5Key = 123456789

Encryptkey = 987123admin65412
[database]
Db = mysql
DbHost = 192.168.2.81
Expand Down
6 changes: 3 additions & 3 deletions middleware/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
func Token() gin.HandlerFunc {
return func(context *gin.Context) {
token := context.Request.Header.Get("X-Token")
fmt.Println("Token value:", token) // 在这里打印 Token 的值
fmt.Println(token)
//fmt.Println("Token value:", token) // 在这里打印 Token 的值
//fmt.Println(token)
token_exsits, err := models.TokenInfo(token)
if err != nil {
resospnseWithError(401, "非法请求", context)
Expand All @@ -24,7 +24,7 @@ func Token() gin.HandlerFunc {
//先做时间判断
target_time, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Time(token_exsits[0].ExpirationAt).Format("2006-01-02 15:04:05"), time.Local) //需要加上time.Local不然会自动加八小时
if target_time.Unix() <= time.Now().Unix() {
fmt.Println("过期报错")
//fmt.Println("过期报错")
//过期报错
resospnseWithError(401, "timeout", context)
return
Expand Down
81 changes: 79 additions & 2 deletions models/cmdb.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package models

import (
"cmdb-ops-flow/utils/msg"
"errors"
"github.com/jinzhu/gorm"
)

type Cmdb struct {
ID int `json:"id" db:"id" form:"id"`
Cmdbid int64 `gorm:"type:bigint;not null" json:"cmdbid" validate:"required"`
Expand All @@ -10,5 +16,76 @@ type Cmdb struct {
Password string `json:"password" db:"password" form:"password"`
PrivateKey string `json:"private_key" db:"private_key" form:"private_key"`
SSHPort int `json:"ssh_port" db:"ssh_port" form:"ssh_port"`
Label string `json:"label" db:"label" form:"label"`
}
Label string `json:"label" db:"label" form:"label"`
}

func Addcmdb(cmdb Cmdb) (interface{}, error) {
err := db.Create(&cmdb).Error
return cmdb, err
}
func Editcmdb(cmdb Cmdb) (interface{}, error) {
err := db.Select("id").Where("cmdbid = ?", cmdb.Cmdbid).First(&cmdb).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return msg.ERROR_CMDB_GET_WRONG, err
}
return msg.InvalidParams, err
}
updateData := map[string]interface{}{
"Cmdbname": cmdb.Cmdbname,
"PublicIP": cmdb.PublicIP,
"PrivateIP": cmdb.PrivateIP,
"Username": cmdb.Username,
"Password": cmdb.Password,
"PrivateKey": cmdb.PrivateKey,
"SSHPort": cmdb.SSHPort,
"Label": cmdb.Label,
}

err = db.Model(&cmdb).Where("cmdbid = ?", cmdb.Cmdbid).Updates(updateData).Error
if err != nil {
return cmdb, err
}

return cmdb, err
}
func Delcmdb(name int64) (code int) {
var cmdb Cmdb
db.Select("id").Where("cmdbid = ?", name).First(&cmdb)
if cmdb.ID > 0 {
err = db.Where("cmdbid = ?", name).Delete(&cmdb).Error
if err != nil {
return msg.InvalidParams
}
return msg.SUCCSE
} else {
return msg.ERROR_CMDB_EDIT_WRONG
}

}
func GetcmdbList(id int) ([]Cmdb, error) {
var list []Cmdb
if id != 0 {
res := db.Debug().Where("id = ?", id).Find(&list)
return list, res.Error
} else {
res := db.Debug().Find(&list)
return list, res.Error
}
}
func Checkcmdb(name string) (code int) {
var cmdb Cmdb
db.Select("id").Where("cmdbname = ?", name).First(&cmdb)
if cmdb.ID > 0 {
return msg.ERROR_CMDB_GET_INFO
}
return msg.SUCCSE
}
func SearchCmdb(keyword string) ([]Cmdb, int) {
var cmdb []Cmdb
db.Where("cmdbname LIKE ? OR public_ip LIKE ? OR private_ip LIKE ? OR label LIKE ?", "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%").Find(&cmdb)
if len(cmdb) > 0 {
return cmdb, msg.SUCCSE
}
return nil, msg.ERROR_CMDB_GET_INFO
}
8 changes: 6 additions & 2 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ func InitRouter() {
apiv1.POST("/user/logout", api.Logout)
apiv1.POST("/user/info", api.Info)

apiv1.POST("/cmdb/addCmdb", api.AddCmdb)
apiv1.POST("/cmdb/getCmdb", api.GetCmdb)
apiv1.POST("/cmdb/editCmdb", api.EditCmdb)
apiv1.POST("/cmdb/delCmdb", api.DelCmdb)
apiv1.POST("/cmdb/GetSearchCmdb", api.GetSearchCmdb)

}
adminuser := r.Group("/api/admin/user")
{
adminuser.POST("/login", api.Login)
adminuser.GET("/info", api.Info)
adminuser.POST("/getUser", api.GetUser)

}

Expand Down
65 changes: 65 additions & 0 deletions service/cmdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package service

import (
"cmdb-ops-flow/conf"
"cmdb-ops-flow/models"
"cmdb-ops-flow/utils/common"
"errors"
"fmt"
)

func AddCmdb(cmdb models.Cmdb) (data interface{}, err error) {
if cmdb.Cmdbname == "" || cmdb.PublicIP == "" || cmdb.PrivateIP == "" ||
cmdb.Username == "" || cmdb.SSHPort == 0 {
return nil, errors.New("所有字段都是必填的")
}
// 如果 Password 和 PrivateKey 都为空,则返回错误
if cmdb.Password == "" && cmdb.PrivateKey == "" {
return nil, errors.New("Password 和 PrivateKey 至少要填写一个")
}

key := []byte(conf.Encryptkey)
fmt.Println(key)
//fmt.Println(cmdb.Password)
passsword, err := common.Encrypt(key, cmdb.Password)
if err != nil {
fmt.Println(err)
}
fmt.Println(passsword)
daocmdb := models.Cmdb{
Cmdbid: common.GenerateRandomNumber(),
Cmdbname: cmdb.Cmdbname,
PublicIP: cmdb.PublicIP,
PrivateIP: cmdb.PrivateIP,
Username: cmdb.Username,
Password: passsword,
PrivateKey: cmdb.PrivateKey,
SSHPort: cmdb.SSHPort,
Label: cmdb.Label,
}

data, err = models.Addcmdb(daocmdb)
return data, err
}
func EditCmdb(cmdb models.Cmdb) (data interface{}, err error) {
key := []byte(conf.Encryptkey)
passsword, _ := common.Encrypt(key, cmdb.Password)
daocmdb := models.Cmdb{
Cmdbid: cmdb.Cmdbid,
Cmdbname: cmdb.Cmdbname,
PublicIP: cmdb.PublicIP,
PrivateIP: cmdb.PrivateIP,
Username: cmdb.Username,
Password: passsword,
PrivateKey: cmdb.PrivateKey,
SSHPort: cmdb.SSHPort,
Label: cmdb.Label,
}

data, err = models.Editcmdb(daocmdb)
return data, err
}
func GetCmdbList(json models.Cmdb) (data interface{}, err error) {
list, err := models.GetcmdbList(json.ID)
return list, err
}
2 changes: 1 addition & 1 deletion service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func AddUser(user models.User) (data interface{}, err error) {
passsword := common.FixMd5(user.Password + conf.Md5Key)
daoUser := models.User{
Userid: common.GenerateRandomUserID(),
Userid: common.GenerateRandomNumber(),
Username: user.Username,
Password: passsword,
}
Expand Down
24 changes: 20 additions & 4 deletions utils/msg/msg.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package msg

const (
SUCCSE = 200
ERROR = 500
SUCCSE = 200
ERROR = 500
InvalidParams = 400

// ERROR_USERNAME_USED code= 1000... 用户模块的错误
ERROR_USERNAME_USED = 1001
Expand All @@ -16,11 +17,20 @@ const (
ERROR_USER_NO_PASSWD = 1009
ERROR_USER_NO_LOGIN = 1010
ERROR_USER_NO_LOGOUT = 1011

// ERROR_CMDB_INFO code= 2000... CMDB模块的错误
ERROR_CMDB_GET_INFO = 2001
ERROR_CMDB_GET_WRONG = 2002
ERROR_CMDB_DELETE_WRONG = 2003
ERROR_CMDB_ADD_WRONG = 2004
ERROR_CMDB_EDIT_WRONG = 2005
)

var codeMsg = map[int]string{
SUCCSE: "OK",
ERROR: "FAIL",
SUCCSE: "OK",
ERROR: "FAIL",
InvalidParams: "请求参数错误",

ERROR_USERNAME_USED: "用户名已存在!",
ERROR_PASSWORD_WRONG: "密码错误",
ERROR_USER_NOT_EXIST: "用户不存在",
Expand All @@ -31,6 +41,12 @@ var codeMsg = map[int]string{
ERROR_USER_NO_RIGHT: "该用户无权限",
ERROR_USER_NO_PASSWD: "未填写用户名或者密码",
ERROR_USER_NO_LOGOUT: "登录失败",

ERROR_CMDB_GET_INFO: "CMDB已存在",
ERROR_CMDB_GET_WRONG: "CMDB不存在",
ERROR_CMDB_DELETE_WRONG: "CMDB删除失败",
ERROR_CMDB_ADD_WRONG: "CMDB新增失败",
ERROR_CMDB_EDIT_WRONG: "CMDB修改失败",
}

func GetErrMsg(code int) string {
Expand Down

0 comments on commit 83455d1

Please sign in to comment.