Skip to content

Commit

Permalink
feat(dbm-services): 追加写标签 TencentBlueKing#8444
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Dec 6, 2024
1 parent 77499c2 commit 89c9ba9
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 371 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package manage

import (
rf "github.com/gin-gonic/gin"

"dbm-services/common/db-resource/internal/model"
"dbm-services/common/go-pubpkg/logger"
)

// AddLabelsParam add labels param
type AddLabelsParam struct {
BkHostIds []int `json:"bk_host_ids" binding:"required,gt=0,dive"`
Labels []string `json:"labels,omitempty"`
}

// AddLabels add labels
func (c *MachineResourceHandler) AddLabels(r *rf.Context) {
var input AddLabelsParam
if err := c.Prepare(r, &input); err != nil {
logger.Error("Preare Error %s", err.Error())
return
}
db := model.DB.Self.Table("tb_rp_detail").Exec("update tb_rp_detail set labels=? where bk_host_id in (?)",
model.JsonMerge("labels", input.Labels), input.BkHostIds)
err := db.Error
if err != nil {
logger.Error("failed to add labels:%s", err.Error())
c.SendResponse(r, err, nil)
return
}
c.SendResponse(r, nil, map[string]interface{}{"affected_count": db.RowsAffected})
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (c *MachineResourceHandler) RegisterRouter(engine *rf.Engine) {
r.POST("/list/all", c.ListAll)
r.POST("/update", c.Update)
r.POST("/batch/update", c.BatchUpdate)
r.POST("/append/labels", c.AddLabels)
r.POST("/delete", c.Delete)
r.POST("/import", c.Import)
r.POST("/mountpoints", c.GetMountPoints)
Expand Down
48 changes: 48 additions & 0 deletions dbm-services/common/db-resource/internal/model/json_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package model

import (
"gorm.io/gorm/clause"
)

// JSONMergeBuilder json query expression, implements clause.Expression interface to use as querier
type JSONMergeBuilder struct {
column string
keys []string
}

// Build json merge expression
// nolint
func (m *JSONMergeBuilder) Build(builder clause.Builder) {
builder.WriteString("JSON_MERGE(")
builder.WriteString(m.column)
builder.WriteString(",")
builder.WriteByte('\'')
builder.WriteByte('[')
for i, key := range m.keys {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString(key)
}
builder.WriteByte(']')
builder.WriteByte('\'')
builder.WriteByte(')')
}

// JsonMerge IntArry json merge int array
func JsonMerge(column string, keys []string) *JSONMergeBuilder {
return &JSONMergeBuilder{
column: column,
keys: keys,
}
}
Loading

0 comments on commit 89c9ba9

Please sign in to comment.