Skip to content

Commit

Permalink
Merge remote-tracking branch 'github-bk-bcs/master'
Browse files Browse the repository at this point in the history
* github-bk-bcs/master:
  fix: 修复导出kv时和导入的内容一致 (#2934)
  feat: 新建密钥后置顶该密钥,变绿底 (#2941)
  • Loading branch information
wenxinlee2015 committed Feb 19, 2024
2 parents 6473f46 + 4375578 commit 12fafc9
Show file tree
Hide file tree
Showing 8 changed files with 5,276 additions and 5,228 deletions.
23 changes: 20 additions & 3 deletions bcs-services/bcs-bscp/cmd/api-server/service/released_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package service

import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/xuri/excelize/v2"
"gopkg.in/yaml.v3"

"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/kit"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/logs"
pbcs "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/config-server"
Expand Down Expand Up @@ -64,7 +66,14 @@ func (ye *YAMLExporter) Export() ([]byte, error) {

// Export method implements the Exporter interface, exporting data as a byte slice in JSON format.
func (je *JSONExporter) Export() ([]byte, error) {
return json.Marshal(je.OutData)
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(je.OutData)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}

// Export method implements the Exporter interface, exporting data as a byte slice in XLSX format.
Expand Down Expand Up @@ -147,7 +156,6 @@ func (m *kvService) Export(w http.ResponseWriter, r *http.Request) {
logs.Errorf("export kv fail, err: %v", err)
_ = render.Render(w, r, rest.BadRequest(err))
}

_, err = w.Write(content)
if err != nil {
logs.Errorf("Error writing response:%s", err)
Expand All @@ -166,9 +174,18 @@ type RkvOutData struct {
func rkvsToOutData(rkvs []*pbrkv.ReleasedKv) map[string]interface{} {
d := map[string]interface{}{}
for _, rkv := range rkvs {
var value interface{}
value = rkv.Spec.Value
switch rkv.Spec.KvType {
case string(table.KvNumber):
i, _ := strconv.Atoi(rkv.Spec.Value)
value = i
case string(table.KvJson):
_ = json.Unmarshal([]byte(rkv.Spec.Value), &value)
}
d[rkv.Spec.Key] = map[string]interface{}{
"kv_type": rkv.Spec.KvType,
"value": rkv.Spec.Value,
"value": value,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (s *Service) ListCredentials(ctx context.Context,
SearchKey: req.SearchKey,
Start: req.Start,
Limit: req.Limit,
TopIds: req.TopIds,
}

rp, err := s.client.DS.ListCredentials(grpcKit.RpcCtx(), r)
Expand Down
5 changes: 4 additions & 1 deletion bcs-services/bcs-bscp/cmd/data-service/service/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
pbbase "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/base"
pbcredential "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/credential"
pbds "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/types"
)

Expand Down Expand Up @@ -66,7 +67,9 @@ func (s *Service) ListCredentials(ctx context.Context, req *pbds.ListCredentialR
return nil, err
}

details, count, err := s.dao.Credential().List(kt, req.BizId, req.SearchKey, opt)
// StrToUint32Slice the comma separated string goes to uint32 slice
topIds, _ := tools.StrToUint32Slice(req.TopIds)
details, count, err := s.dao.Credential().List(kt, req.BizId, req.SearchKey, opt, topIds)

if err != nil {
logs.Errorf("list credential failed, err: %v, rid: %s", err, kt.Rid)
Expand Down
17 changes: 12 additions & 5 deletions bcs-services/bcs-bscp/pkg/dal/dao/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/criteria/errf"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/gen"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/utils"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/kit"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/logs"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
Expand All @@ -41,7 +42,8 @@ type Credential interface {
// Create one credential instance.
Create(kit *kit.Kit, credential *table.Credential) (uint32, error)
// List get credentials
List(kit *kit.Kit, bizID uint32, searchKey string, opt *types.BasePage) ([]*table.Credential, int64, error)
List(kit *kit.Kit, bizID uint32, searchKey string, opt *types.BasePage,
topIds []uint32) ([]*table.Credential, int64, error)
// DeleteWithTx delete credential with transaction
DeleteWithTx(kit *kit.Kit, tx *gen.QueryTx, bizID, id uint32) error
// Update update credential
Expand Down Expand Up @@ -189,21 +191,26 @@ func (dao *credentialDao) Create(kit *kit.Kit, g *table.Credential) (uint32, err
}

// List get credentials
func (dao *credentialDao) List(kit *kit.Kit, bizID uint32, searchKey string, opt *types.BasePage) (
[]*table.Credential, int64, error) {
func (dao *credentialDao) List(kit *kit.Kit, bizID uint32, searchKey string, opt *types.BasePage,
topIds []uint32) ([]*table.Credential, int64, error) {
m := dao.genQ.Credential
q := dao.genQ.Credential.WithContext(kit.Ctx)

var conds []rawgen.Condition
if searchKey != "" {
searchVal := "%" + searchKey + "%"
conds = append(conds, q.Where(m.Memo.Like(searchVal)).Or(m.Reviser.Like(searchVal)).
Or(m.Name.Like(searchKey)))
Or(m.Name.Like(searchVal)))
}

if len(topIds) != 0 {
q = q.Order(utils.NewCustomExpr(`CASE WHEN id IN (?) THEN 0 ELSE 1 END,name ASC`, []interface{}{topIds}))
} else {
q = q.Order(m.Name)
}

result, count, err := q.Where(m.BizID.Eq(bizID)).
Where(conds...).
Order(m.Name).
FindByPage(opt.Offset(), opt.LimitInt())
if err != nil {
return nil, 0, err
Expand Down
Loading

0 comments on commit 12fafc9

Please sign in to comment.