Skip to content

Commit

Permalink
api: 产品线内主机的主机组信息只显示本产品线的
Browse files Browse the repository at this point in the history
  • Loading branch information
吴迎松 committed Jul 2, 2018
1 parent b68c01e commit f86e4af
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/host_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func listProductHostGroupHosts(c *gin.Context) {
c.GetInt("limit"),
)
response["total"] = total
response["hosts"] = warpHosts(hosts)
response["hosts"] = warpProductHosts(c.GetInt("product_id"), hosts)
}

func listNotInProductHostGroupHosts(c *gin.Context) {
Expand Down
22 changes: 21 additions & 1 deletion api/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,37 @@ func warpHosts(hosts []Host) []WarpHost {
return warpHosts
}

func warpProductHosts(productID int, hosts []Host) []WarpHost {
warpHosts := []WarpHost{}
for _, host := range hosts {
warpHosts = append(warpHosts, warpProductHost(productID, host))
}
return warpHosts
}

func warpHost(host Host) WarpHost {
pluginCnt, _ := mydb.getHostPlugins(host.ID, false, "", "", 0, 0)
return WarpHost{
host,
mydb.getHostAppNames(host.ID),
pluginCnt,
mydb.getHostHostGroups(host.ID),
mydb.getHostHostGroups(0, host.ID),
mydb.getHostProducts(host.ID),
}
}

func warpProductHost(productID int, host Host) WarpHost {
pluginCnt, _ := mydb.getHostPlugins(host.ID, false, "", "", 0, 0)
return WarpHost{
host,
mydb.getHostAppNames(host.ID),
pluginCnt,
mydb.getHostHostGroups(productID, host.ID),
nil,
// mydb.getHostProducts(host.ID),
}
}

func listHostPlugins(c *gin.Context) {
response := gin.H{"code": http.StatusOK}
defer c.JSON(http.StatusOK, response)
Expand Down
10 changes: 8 additions & 2 deletions api/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1256,14 +1256,20 @@ func (d *db) getHostAppNames(hostID string) []string {
return appNames
}

func (d *db) getHostHostGroups(hostID string) []HostGroup {
func (d *db) getHostHostGroups(productID int, hostID string) []HostGroup {
var (
hostGroups = make([]HostGroup, 0)
err error
rawSQL string
)
rawSQL := fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+
rawSQL = fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+
"DATE_FORMAT(update_at,'%s') as update_at from host_group where id in (select host_group_id from host_group_host where host_id='%s')",
dbDateFormat, dbDateFormat, hostID)
if productID != 0 {
rawSQL = fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+
"DATE_FORMAT(update_at,'%s') as update_at from host_group where product_id = %d and id in (select host_group_id from host_group_host where host_id='%s')",
dbDateFormat, dbDateFormat, productID, hostID)
}
log.Println(rawSQL)
if err = d.Select(&hostGroups, rawSQL); err != nil {
log.Println(err)
Expand Down
9 changes: 5 additions & 4 deletions api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,17 @@ func listProductHosts(c *gin.Context) {
order = "status asc"
}
var (
noGroup bool
err error
noGroup bool
err error
productID = c.GetInt("product_id")
)
noGroup, err = strconv.ParseBool(c.DefaultQuery("no_group", "false"))
if err != nil {
noGroup = false
}

total, hosts := mydb.getProductHosts(
c.GetInt("product_id"),
productID,
noGroup,
c.GetBool("paging"),
c.DefaultQuery("query", ""),
Expand All @@ -244,7 +245,7 @@ func listProductHosts(c *gin.Context) {
c.GetInt("limit"),
)
response["total"] = total
response["hosts"] = warpHosts(hosts)
response["hosts"] = warpProductHosts(productID, hosts)
}

func listNotInProductHosts(c *gin.Context) {
Expand Down

0 comments on commit f86e4af

Please sign in to comment.