use ips when recycle and del hosts

This commit is contained in:
Ulric Qin 2020-10-25 18:16:20 +08:00
parent 5b9a03a261
commit 28a2196143
2 changed files with 26 additions and 2 deletions

View File

@ -117,6 +117,11 @@ func HostByIds(ids []int64) (hosts []Host, err error) {
return
}
func HostIdsByIps(ips []string) (ids []int64, err error) {
err = DB["ams"].Table(new(Host)).In("ip", ips).Select("id").Find(&ids)
return
}
func HostSearch(batch, field string) ([]Host, error) {
arr := str.ParseLines(strings.Replace(batch, ",", "\n", -1))
if len(arr) == 0 {

View File

@ -66,10 +66,28 @@ func hostPost(c *gin.Context) {
renderMessage(c, nil)
}
type idsOrIpsForm struct {
Ids []int64 `json:"ids"`
Ips []string `json:"ips"`
}
func (f *idsOrIpsForm) Validate() {
if len(f.Ids) == 0 {
if len(f.Ips) == 0 {
bomb("args invalid")
}
ids, err := models.HostIdsByIps(f.Ips)
dangerous(err)
f.Ids = ids
}
}
// 从某个租户手上回收资源
func hostBackPut(c *gin.Context) {
var f idsForm
var f idsOrIpsForm
bind(c, &f)
f.Validate()
loginUser(c).CheckPermGlobal("ams_host_modify")
@ -166,8 +184,9 @@ func hostCatePut(c *gin.Context) {
// 先检查tenant字段是否为空如果不为空说明机器仍然在业务线使用拒绝删除
// 管理员可以先点【回收】从业务线回收机器unregister之后tenant字段为空即可删除
func hostDel(c *gin.Context) {
var f idsForm
var f idsOrIpsForm
bind(c, &f)
f.Validate()
loginUser(c).CheckPermGlobal("ams_host_delete")