Skip to content

Commit

Permalink
Merge pull request #227 from jojohappy/master
Browse files Browse the repository at this point in the history
[api] add new api to update partial hosts in grp_host
merged. thanks for contribute this  👍
  • Loading branch information
masato25 authored Aug 22, 2017
2 parents 9071ef2 + c053ea3 commit 4b0257a
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/_posts/HostGroup/2017-08-22-hostgroup_update_partial_hosts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
category: HostGroup
apiurl: '/api/v1/hostgroup/#{hostgroup_id}/host'
title: "Update partial hosts in HostGroup"
type: 'PATCH'
sample_doc: 'hostgroup.html'
layout: default
---

* [Session](#/authentication) Required
* 如果使用者不是 Admin 只能对创建的 HostGroup 做操作
* ex. /api/v1/hostgroup/1/host

### Request
```{
"hosts": [
"host01",
"host02"
],
"action": "add"
}```

### Response

```Status: 200```
```{
"message": "[host01, host02] bind to hostgroup: test, [] have been exist"
}```

### Request
```{
"hosts": [
"host01",
"host02"
],
"action": "remove"
}```

### Response

```Status: 200```
```{
"message": "[host01, host02] unbind to hostgroup: test"
}```

1 change: 1 addition & 0 deletions modules/api/app/controller/host/host_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Routes(r *gin.Engine) {
hostr.GET("/hostgroup/:host_group", GetHostGroup)
hostr.PUT("/hostgroup", PutHostGroup)
hostr.DELETE("/hostgroup/:host_group", DeleteHostGroup)
hostr.PATCH("/hostgroup/:host_group/host", PatchHostGroupHost)

//plugins
hostr.GET("/hostgroup/:host_group/plugins", GetPluginOfGrp)
Expand Down
110 changes: 110 additions & 0 deletions modules/api/app/controller/host/hostgroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,113 @@ func GetTemplateOfHostGroup(c *gin.Context) {
})
return
}

type APIPatchHostGroupHost struct {
Action string `json:"action" binding:"required"`
Hosts []string `json:"hosts" binding:"required"`
}

func PatchHostGroupHost(c *gin.Context) {
var inputs APIPatchHostGroupHost
if err := c.Bind(&inputs); err != nil {
h.JSONR(c, badstatus, err)
return
}

grpIDParams := c.Params.ByName("host_group")
if grpIDParams == "" {
h.JSONR(c, badstatus, "grp id is missing")
return
}
grpID, err := strconv.Atoi(grpIDParams)
if err != nil {
log.Debugf("grpIDParams: %v", grpIDParams)
h.JSONR(c, badstatus, err)
return
}

action := inputs.Action
if action != "add" && action != "remove" {
h.JSONR(c, badstatus, "action must be add or remove")
return
}

user, _ := h.GetUser(c)

hostgroup := f.HostGroup{ID: int64(grpID)}
if dt := db.Falcon.Find(&hostgroup); dt.Error != nil {
h.JSONR(c, expecstatus, dt.Error)
return
}
if !user.IsAdmin() && hostgroup.CreateUser != user.Name {
h.JSONR(c, expecstatus, "You don't have permission.")
return
}

switch action {
case "add":
bindHostToHostGroup(c, hostgroup, inputs.Hosts)
return
case "remove":
unbindHostToHostGroup(c, hostgroup, inputs.Hosts)
return
}
}

func bindHostToHostGroup(c *gin.Context, hostgroup f.HostGroup, hosts []string) {
tx := db.Falcon.Begin()
var bindHosts []string
var existHosts []string
for _, host := range hosts {
ahost := f.Host{Hostname: host}
var id int64
var ok bool
if id, ok = ahost.Existing(); !ok {
if dt := tx.Save(&ahost); dt.Error != nil {
h.JSONR(c, expecstatus, dt.Error)
tx.Rollback()
return
}
id = ahost.ID
}

tGrpHost := f.GrpHost{GrpID: hostgroup.ID, HostID: id}
if ok = tGrpHost.Existing(); ok {
existHosts = append(existHosts, host)
} else {
bindHosts = append(bindHosts, host)
if dt := tx.Debug().Create(&tGrpHost); dt.Error != nil {
h.JSONR(c, expecstatus, fmt.Sprintf("create grphost got error: %s , grp_id: %v, host_id: %v", dt.Error, hostgroup.ID, id))
tx.Rollback()
return
}
}
}
tx.Commit()
h.JSONR(c, fmt.Sprintf("%v bind to hostgroup: %s, %v have been exist", bindHosts, hostgroup.Name, existHosts))
return
}

func unbindHostToHostGroup(c *gin.Context, hostgroup f.HostGroup, hosts []string) {
tx := db.Falcon.Begin()
var unbindHosts []string
for _, host := range hosts {
dhost := f.Host{Hostname: host}
var id int64
var ok bool
if id, ok = dhost.Existing(); ok {
unbindHosts = append(unbindHosts, host)
} else {
log.Debugf("Host %s does not exists!", host)
continue
}
if dt := db.Falcon.Where("grp_id = ? AND host_id = ?", hostgroup.ID, id).Delete(&f.GrpHost{}); dt.Error != nil {
h.JSONR(c, expecstatus, dt.Error)
tx.Rollback()
return
}
}
tx.Commit()
h.JSONR(c, fmt.Sprintf("%v unbind to hostgroup: %s", unbindHosts, hostgroup.Name))
return
}
15 changes: 15 additions & 0 deletions modules/api/app/model/falcon_portal/grp_host.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package falcon_portal

import (
con "github.com/open-falcon/falcon-plus/modules/api/config"
)

// +---------+------------------+------+-----+---------+-------+
// | Field | Type | Null | Key | Default | Extra |
// +---------+------------------+------+-----+---------+-------+
Expand All @@ -15,3 +19,14 @@ type GrpHost struct {
func (this GrpHost) TableName() string {
return "grp_host"
}

func (this GrpHost) Existing() bool {
var tGrpHost GrpHost
db := con.Con()
db.Falcon.Table(this.TableName()).Where("grp_id = ? AND host_id = ?", this.GrpID, this.HostID).Scan(&tGrpHost)
if tGrpHost.GrpID != 0 {
return true
} else {
return false
}
}

0 comments on commit 4b0257a

Please sign in to comment.