Skip to content

Commit f86e4af

Browse files
author
吴迎松
committed
api: 产品线内主机的主机组信息只显示本产品线的
1 parent b68c01e commit f86e4af

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

api/host_groups.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func listProductHostGroupHosts(c *gin.Context) {
5858
c.GetInt("limit"),
5959
)
6060
response["total"] = total
61-
response["hosts"] = warpHosts(hosts)
61+
response["hosts"] = warpProductHosts(c.GetInt("product_id"), hosts)
6262
}
6363

6464
func listNotInProductHostGroupHosts(c *gin.Context) {

api/hosts.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,37 @@ func warpHosts(hosts []Host) []WarpHost {
132132
return warpHosts
133133
}
134134

135+
func warpProductHosts(productID int, hosts []Host) []WarpHost {
136+
warpHosts := []WarpHost{}
137+
for _, host := range hosts {
138+
warpHosts = append(warpHosts, warpProductHost(productID, host))
139+
}
140+
return warpHosts
141+
}
142+
135143
func warpHost(host Host) WarpHost {
136144
pluginCnt, _ := mydb.getHostPlugins(host.ID, false, "", "", 0, 0)
137145
return WarpHost{
138146
host,
139147
mydb.getHostAppNames(host.ID),
140148
pluginCnt,
141-
mydb.getHostHostGroups(host.ID),
149+
mydb.getHostHostGroups(0, host.ID),
142150
mydb.getHostProducts(host.ID),
143151
}
144152
}
145153

154+
func warpProductHost(productID int, host Host) WarpHost {
155+
pluginCnt, _ := mydb.getHostPlugins(host.ID, false, "", "", 0, 0)
156+
return WarpHost{
157+
host,
158+
mydb.getHostAppNames(host.ID),
159+
pluginCnt,
160+
mydb.getHostHostGroups(productID, host.ID),
161+
nil,
162+
// mydb.getHostProducts(host.ID),
163+
}
164+
}
165+
146166
func listHostPlugins(c *gin.Context) {
147167
response := gin.H{"code": http.StatusOK}
148168
defer c.JSON(http.StatusOK, response)

api/mysql.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,14 +1256,20 @@ func (d *db) getHostAppNames(hostID string) []string {
12561256
return appNames
12571257
}
12581258

1259-
func (d *db) getHostHostGroups(hostID string) []HostGroup {
1259+
func (d *db) getHostHostGroups(productID int, hostID string) []HostGroup {
12601260
var (
12611261
hostGroups = make([]HostGroup, 0)
12621262
err error
1263+
rawSQL string
12631264
)
1264-
rawSQL := fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+
1265+
rawSQL = fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+
12651266
"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')",
12661267
dbDateFormat, dbDateFormat, hostID)
1268+
if productID != 0 {
1269+
rawSQL = fmt.Sprintf("select id, name, description, creator, DATE_FORMAT(create_at,'%s') as create_at,"+
1270+
"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')",
1271+
dbDateFormat, dbDateFormat, productID, hostID)
1272+
}
12671273
log.Println(rawSQL)
12681274
if err = d.Select(&hostGroups, rawSQL); err != nil {
12691275
log.Println(err)

api/product.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,17 @@ func listProductHosts(c *gin.Context) {
226226
order = "status asc"
227227
}
228228
var (
229-
noGroup bool
230-
err error
229+
noGroup bool
230+
err error
231+
productID = c.GetInt("product_id")
231232
)
232233
noGroup, err = strconv.ParseBool(c.DefaultQuery("no_group", "false"))
233234
if err != nil {
234235
noGroup = false
235236
}
236237

237238
total, hosts := mydb.getProductHosts(
238-
c.GetInt("product_id"),
239+
productID,
239240
noGroup,
240241
c.GetBool("paging"),
241242
c.DefaultQuery("query", ""),
@@ -244,7 +245,7 @@ func listProductHosts(c *gin.Context) {
244245
c.GetInt("limit"),
245246
)
246247
response["total"] = total
247-
response["hosts"] = warpHosts(hosts)
248+
response["hosts"] = warpProductHosts(productID, hosts)
248249
}
249250

250251
func listNotInProductHosts(c *gin.Context) {

0 commit comments

Comments
 (0)