-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
269 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters