github.com/polarismesh/polaris@v1.17.8/config/config_file_release_history.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package config
    19  
    20  import (
    21  	"context"
    22  
    23  	apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
    24  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    25  	"go.uber.org/zap"
    26  
    27  	api "github.com/polarismesh/polaris/common/api/v1"
    28  	"github.com/polarismesh/polaris/common/model"
    29  	commonstore "github.com/polarismesh/polaris/common/store"
    30  	"github.com/polarismesh/polaris/common/utils"
    31  )
    32  
    33  // recordReleaseHistory 新增配置文件发布历史记录
    34  func (s *Server) recordReleaseHistory(ctx context.Context, fileRelease *model.ConfigFileRelease,
    35  	releaseType, status, reason string) {
    36  
    37  	releaseHistory := &model.ConfigFileReleaseHistory{
    38  		Name:               fileRelease.Name,
    39  		Namespace:          fileRelease.Namespace,
    40  		Group:              fileRelease.Group,
    41  		FileName:           fileRelease.FileName,
    42  		Content:            fileRelease.Content,
    43  		Format:             fileRelease.Format,
    44  		Metadata:           fileRelease.Metadata,
    45  		Comment:            fileRelease.Comment,
    46  		Md5:                fileRelease.Md5,
    47  		Version:            fileRelease.Version,
    48  		Type:               releaseType,
    49  		Status:             status,
    50  		Reason:             reason,
    51  		CreateBy:           utils.ParseUserName(ctx),
    52  		ModifyBy:           utils.ParseUserName(ctx),
    53  		ReleaseDescription: fileRelease.ReleaseDescription,
    54  	}
    55  
    56  	if err := s.storage.CreateConfigFileReleaseHistory(releaseHistory); err != nil {
    57  		log.Error("[Config][History] create config file release history error.", utils.RequestID(ctx),
    58  			utils.ZapNamespace(fileRelease.Namespace), utils.ZapGroup(fileRelease.Group),
    59  			utils.ZapFileName(fileRelease.FileName), zap.Error(err))
    60  	}
    61  }
    62  
    63  // GetConfigFileReleaseHistories 获取配置文件发布历史记录
    64  func (s *Server) GetConfigFileReleaseHistories(ctx context.Context,
    65  	filter map[string]string) *apiconfig.ConfigBatchQueryResponse {
    66  
    67  	offset, limit, err := utils.ParseOffsetAndLimit(filter)
    68  	if err != nil {
    69  		return api.NewConfigBatchQueryResponseWithInfo(apimodel.Code_BadRequest, err.Error())
    70  	}
    71  
    72  	searchFilter := map[string]string{}
    73  	for k, v := range filter {
    74  		if nk, ok := availableSearch["config_file_release_history"][k]; ok {
    75  			searchFilter[nk] = v
    76  		}
    77  	}
    78  
    79  	count, saveDatas, err := s.storage.QueryConfigFileReleaseHistories(searchFilter, offset, limit)
    80  	if err != nil {
    81  		log.Error("[Config][History] get config file release history error.", utils.RequestID(ctx),
    82  			zap.Any("filter", filter), zap.Error(err))
    83  		return api.NewConfigBatchQueryResponseWithInfo(commonstore.StoreCode2APICode(err), err.Error())
    84  	}
    85  
    86  	if len(saveDatas) == 0 {
    87  		out := api.NewConfigBatchQueryResponse(apimodel.Code_ExecuteSuccess)
    88  		out.Total = utils.NewUInt32Value(0)
    89  		return out
    90  	}
    91  
    92  	var histories []*apiconfig.ConfigFileReleaseHistory
    93  	for _, data := range saveDatas {
    94  		data, err := s.chains.AfterGetFileHistory(ctx, data)
    95  		if err != nil {
    96  			return api.NewConfigBatchQueryResponseWithInfo(apimodel.Code_ExecuteException, err.Error())
    97  		}
    98  		history := model.ToReleaseHistoryAPI(data)
    99  		histories = append(histories, history)
   100  	}
   101  	out := api.NewConfigBatchQueryResponse(apimodel.Code_ExecuteSuccess)
   102  	out.Total = utils.NewUInt32Value(count)
   103  	out.ConfigFileReleaseHistories = histories
   104  	return out
   105  }