forked from TencentBlueKing/blueking-dbm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dbm-services): 追加写标签 TencentBlueKing#8444
- Loading branch information
Showing
6 changed files
with
426 additions
and
371 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
dbm-services/common/db-resource/internal/controller/manage/rs_labels.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
52 changes: 0 additions & 52 deletions
52
dbm-services/common/db-resource/internal/controller/manage/rs_lable.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
dbm-services/common/db-resource/internal/model/json_merge.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
Oops, something went wrong.