github.com/polarismesh/polaris@v1.17.8/common/model/config_file.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 model
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/polarismesh/specification/source/go/api/v1/config_manage"
    24  
    25  	commontime "github.com/polarismesh/polaris/common/time"
    26  	"github.com/polarismesh/polaris/common/utils"
    27  )
    28  
    29  /** ----------- DataObject ------------- */
    30  
    31  // ConfigFileGroup 配置文件组数据持久化对象
    32  type ConfigFileGroup struct {
    33  	Id         uint64
    34  	Name       string
    35  	Namespace  string
    36  	Comment    string
    37  	Owner      string
    38  	Business   string
    39  	Department string
    40  	Metadata   map[string]string
    41  	CreateTime time.Time
    42  	ModifyTime time.Time
    43  	CreateBy   string
    44  	ModifyBy   string
    45  	Valid      bool
    46  }
    47  
    48  type ConfigFileKey struct {
    49  	Name      string
    50  	Namespace string
    51  	Group     string
    52  }
    53  
    54  // ConfigFile 配置文件数据持久化对象
    55  type ConfigFile struct {
    56  	Id        uint64
    57  	Name      string
    58  	Namespace string
    59  	Group     string
    60  	// OriginContent 最原始的配置文件内容数据
    61  	OriginContent string
    62  	Content       string
    63  	Comment       string
    64  	Format        string
    65  	Flag          int
    66  	Valid         bool
    67  	Metadata      map[string]string
    68  	Encrypt       bool
    69  	EncryptAlgo   string
    70  	Status        string
    71  	CreateBy      string
    72  	ModifyBy      string
    73  	ReleaseBy     string
    74  	CreateTime    time.Time
    75  	ModifyTime    time.Time
    76  	ReleaseTime   time.Time
    77  }
    78  
    79  func (s *ConfigFile) Key() *ConfigFileKey {
    80  	return &ConfigFileKey{
    81  		Name:      s.Name,
    82  		Namespace: s.Namespace,
    83  		Group:     s.Group,
    84  	}
    85  }
    86  
    87  func (s *ConfigFile) KeyString() string {
    88  	return s.Namespace + "@" + s.Group + "@" + s.Name
    89  }
    90  
    91  func (s *ConfigFile) GetEncryptDataKey() string {
    92  	return s.Metadata[utils.ConfigFileTagKeyDataKey]
    93  }
    94  
    95  func (s *ConfigFile) GetEncryptAlgo() string {
    96  	if s.EncryptAlgo != "" {
    97  		return s.EncryptAlgo
    98  	}
    99  	return s.Metadata[utils.ConfigFileTagKeyEncryptAlgo]
   100  }
   101  
   102  func (s *ConfigFile) IsEncrypted() bool {
   103  	return s.Encrypt || s.GetEncryptDataKey() != ""
   104  }
   105  
   106  func NewConfigFileRelease() *ConfigFileRelease {
   107  	return &ConfigFileRelease{
   108  		SimpleConfigFileRelease: &SimpleConfigFileRelease{
   109  			ConfigFileReleaseKey: &ConfigFileReleaseKey{},
   110  		},
   111  	}
   112  }
   113  
   114  // ConfigFileRelease 配置文件发布数据持久化对象
   115  type ConfigFileRelease struct {
   116  	*SimpleConfigFileRelease
   117  	Content string
   118  }
   119  
   120  type ConfigFileReleaseKey struct {
   121  	Id        uint64
   122  	Name      string
   123  	Namespace string
   124  	Group     string
   125  	FileName  string
   126  }
   127  
   128  func (c ConfigFileReleaseKey) ToFileKey() *ConfigFileKey {
   129  	return &ConfigFileKey{
   130  		Name:      c.FileName,
   131  		Group:     c.Group,
   132  		Namespace: c.Namespace,
   133  	}
   134  }
   135  
   136  func (c ConfigFileReleaseKey) OwnerKey() string {
   137  	return c.Namespace + "@" + c.Group
   138  }
   139  
   140  func (c ConfigFileReleaseKey) ActiveKey() string {
   141  	return c.Namespace + "@" + c.Group + "@" + c.FileName
   142  }
   143  
   144  func (c ConfigFileReleaseKey) ReleaseKey() string {
   145  	return c.Namespace + "@" + c.Group + "@" + c.FileName + "@" + c.Name
   146  }
   147  
   148  // SimpleConfigFileRelease 配置文件发布数据持久化对象
   149  type SimpleConfigFileRelease struct {
   150  	*ConfigFileReleaseKey
   151  	Version            uint64
   152  	Comment            string
   153  	Md5                string
   154  	Flag               int
   155  	Active             bool
   156  	Valid              bool
   157  	Format             string
   158  	Metadata           map[string]string
   159  	CreateTime         time.Time
   160  	CreateBy           string
   161  	ModifyTime         time.Time
   162  	ModifyBy           string
   163  	ReleaseDescription string
   164  }
   165  
   166  func (s SimpleConfigFileRelease) GetEncryptDataKey() string {
   167  	return s.Metadata[utils.ConfigFileTagKeyDataKey]
   168  }
   169  
   170  func (s SimpleConfigFileRelease) GetEncryptAlgo() string {
   171  	return s.Metadata[utils.ConfigFileTagKeyEncryptAlgo]
   172  }
   173  
   174  func (s SimpleConfigFileRelease) IsEncrypted() bool {
   175  	return s.GetEncryptDataKey() != ""
   176  }
   177  
   178  // ConfigFileReleaseHistory 配置文件发布历史记录数据持久化对象
   179  type ConfigFileReleaseHistory struct {
   180  	Id                 uint64
   181  	Name               string
   182  	Namespace          string
   183  	Group              string
   184  	FileName           string
   185  	Format             string
   186  	Metadata           map[string]string
   187  	Content            string
   188  	Comment            string
   189  	Version            uint64
   190  	Md5                string
   191  	Type               string
   192  	Status             string
   193  	CreateTime         time.Time
   194  	CreateBy           string
   195  	ModifyTime         time.Time
   196  	ModifyBy           string
   197  	Valid              bool
   198  	Reason             string
   199  	ReleaseDescription string
   200  }
   201  
   202  func (s ConfigFileReleaseHistory) GetEncryptDataKey() string {
   203  	return s.Metadata[utils.ConfigFileTagKeyDataKey]
   204  }
   205  
   206  func (s ConfigFileReleaseHistory) GetEncryptAlgo() string {
   207  	return s.Metadata[utils.ConfigFileTagKeyEncryptAlgo]
   208  }
   209  
   210  func (s ConfigFileReleaseHistory) IsEncrypted() bool {
   211  	return s.GetEncryptDataKey() != ""
   212  }
   213  
   214  // ConfigFileTag 配置文件标签数据持久化对象
   215  type ConfigFileTag struct {
   216  	Id         uint64
   217  	Key        string
   218  	Value      string
   219  	Namespace  string
   220  	Group      string
   221  	FileName   string
   222  	CreateTime time.Time
   223  	CreateBy   string
   224  	ModifyTime time.Time
   225  	ModifyBy   string
   226  	Valid      bool
   227  }
   228  
   229  // ConfigFileTemplate config file template data object
   230  type ConfigFileTemplate struct {
   231  	Id         uint64
   232  	Name       string
   233  	Content    string
   234  	Comment    string
   235  	Format     string
   236  	CreateTime time.Time
   237  	CreateBy   string
   238  	ModifyTime time.Time
   239  	ModifyBy   string
   240  }
   241  
   242  func ToConfigFileStore(file *config_manage.ConfigFile) *ConfigFile {
   243  	var comment string
   244  	if file.Comment != nil {
   245  		comment = file.Comment.Value
   246  	}
   247  	var createBy string
   248  	if file.CreateBy != nil {
   249  		createBy = file.CreateBy.Value
   250  	}
   251  	var content string
   252  	if file.Content != nil {
   253  		content = file.Content.Value
   254  	}
   255  	var format string
   256  	if file.Format != nil {
   257  		format = file.Format.Value
   258  	}
   259  
   260  	metadata := ToTagMap(file.GetTags())
   261  	if file.GetEncryptAlgo().GetValue() != "" {
   262  		metadata[utils.ConfigFileTagKeyEncryptAlgo] = file.GetEncryptAlgo().GetValue()
   263  	}
   264  
   265  	return &ConfigFile{
   266  		Name:        file.Name.GetValue(),
   267  		Namespace:   file.Namespace.GetValue(),
   268  		Group:       file.Group.GetValue(),
   269  		Content:     content,
   270  		Comment:     comment,
   271  		Format:      format,
   272  		CreateBy:    createBy,
   273  		Encrypt:     file.GetEncrypted().GetValue(),
   274  		EncryptAlgo: file.GetEncryptAlgo().GetValue(),
   275  		Metadata:    metadata,
   276  	}
   277  }
   278  
   279  func ToConfigFileAPI(file *ConfigFile) *config_manage.ConfigFile {
   280  	if file == nil {
   281  		return nil
   282  	}
   283  	return &config_manage.ConfigFile{
   284  		Id:          utils.NewUInt64Value(file.Id),
   285  		Name:        utils.NewStringValue(file.Name),
   286  		Namespace:   utils.NewStringValue(file.Namespace),
   287  		Group:       utils.NewStringValue(file.Group),
   288  		Content:     utils.NewStringValue(file.Content),
   289  		Comment:     utils.NewStringValue(file.Comment),
   290  		Format:      utils.NewStringValue(file.Format),
   291  		Status:      utils.NewStringValue(file.Status),
   292  		Tags:        FromTagMap(file.Metadata),
   293  		Encrypted:   utils.NewBoolValue(file.IsEncrypted()),
   294  		EncryptAlgo: utils.NewStringValue(file.GetEncryptAlgo()),
   295  		CreateBy:    utils.NewStringValue(file.CreateBy),
   296  		ModifyBy:    utils.NewStringValue(file.ModifyBy),
   297  		ReleaseBy:   utils.NewStringValue(file.ReleaseBy),
   298  		CreateTime:  utils.NewStringValue(commontime.Time2String(file.CreateTime)),
   299  		ModifyTime:  utils.NewStringValue(commontime.Time2String(file.ModifyTime)),
   300  		ReleaseTime: utils.NewStringValue(commontime.Time2String(file.ReleaseTime)),
   301  	}
   302  }
   303  
   304  // ToConfiogFileReleaseApi
   305  func ToConfiogFileReleaseApi(release *ConfigFileRelease) *config_manage.ConfigFileRelease {
   306  	if release == nil {
   307  		return nil
   308  	}
   309  
   310  	return &config_manage.ConfigFileRelease{
   311  		Id:                 utils.NewUInt64Value(release.Id),
   312  		Name:               utils.NewStringValue(release.Name),
   313  		Namespace:          utils.NewStringValue(release.Namespace),
   314  		Group:              utils.NewStringValue(release.Group),
   315  		FileName:           utils.NewStringValue(release.FileName),
   316  		Format:             utils.NewStringValue(release.Format),
   317  		Content:            utils.NewStringValue(release.Content),
   318  		Comment:            utils.NewStringValue(release.Comment),
   319  		Md5:                utils.NewStringValue(release.Md5),
   320  		Version:            utils.NewUInt64Value(release.Version),
   321  		CreateBy:           utils.NewStringValue(release.CreateBy),
   322  		CreateTime:         utils.NewStringValue(commontime.Time2String(release.CreateTime)),
   323  		ModifyBy:           utils.NewStringValue(release.ModifyBy),
   324  		ModifyTime:         utils.NewStringValue(commontime.Time2String(release.ModifyTime)),
   325  		ReleaseDescription: utils.NewStringValue(release.ReleaseDescription),
   326  		Tags:               FromTagMap(release.Metadata),
   327  	}
   328  }
   329  
   330  // ToConfigFileReleaseStore
   331  func ToConfigFileReleaseStore(release *config_manage.ConfigFileRelease) *ConfigFileRelease {
   332  	if release == nil {
   333  		return nil
   334  	}
   335  	var comment string
   336  	if release.Comment != nil {
   337  		comment = release.Comment.Value
   338  	}
   339  	var content string
   340  	if release.Content != nil {
   341  		content = release.Content.Value
   342  	}
   343  	var md5 string
   344  	if release.Md5 != nil {
   345  		md5 = release.Md5.Value
   346  	}
   347  	var version uint64
   348  	if release.Version != nil {
   349  		version = release.Version.Value
   350  	}
   351  	var createBy string
   352  	if release.CreateBy != nil {
   353  		createBy = release.CreateBy.Value
   354  	}
   355  	var modifyBy string
   356  	if release.ModifyBy != nil {
   357  		createBy = release.ModifyBy.Value
   358  	}
   359  	var id uint64
   360  	if release.Id != nil {
   361  		id = release.Id.Value
   362  	}
   363  
   364  	return &ConfigFileRelease{
   365  		SimpleConfigFileRelease: &SimpleConfigFileRelease{
   366  			ConfigFileReleaseKey: &ConfigFileReleaseKey{
   367  				Id:        id,
   368  				Namespace: release.Namespace.GetValue(),
   369  				Group:     release.Group.GetValue(),
   370  				FileName:  release.FileName.GetValue(),
   371  			},
   372  			Comment:  comment,
   373  			Md5:      md5,
   374  			Version:  version,
   375  			CreateBy: createBy,
   376  			ModifyBy: modifyBy,
   377  		},
   378  		Content: content,
   379  	}
   380  }
   381  
   382  func ToReleaseHistoryAPI(releaseHistory *ConfigFileReleaseHistory) *config_manage.ConfigFileReleaseHistory {
   383  	if releaseHistory == nil {
   384  		return nil
   385  	}
   386  	return &config_manage.ConfigFileReleaseHistory{
   387  		Id:         utils.NewUInt64Value(releaseHistory.Id),
   388  		Name:       utils.NewStringValue(releaseHistory.Name),
   389  		Namespace:  utils.NewStringValue(releaseHistory.Namespace),
   390  		Group:      utils.NewStringValue(releaseHistory.Group),
   391  		FileName:   utils.NewStringValue(releaseHistory.FileName),
   392  		Content:    utils.NewStringValue(releaseHistory.Content),
   393  		Comment:    utils.NewStringValue(releaseHistory.Comment),
   394  		Format:     utils.NewStringValue(releaseHistory.Format),
   395  		Tags:       FromTagMap(releaseHistory.Metadata),
   396  		Md5:        utils.NewStringValue(releaseHistory.Md5),
   397  		Type:       utils.NewStringValue(releaseHistory.Type),
   398  		Status:     utils.NewStringValue(releaseHistory.Status),
   399  		CreateBy:   utils.NewStringValue(releaseHistory.CreateBy),
   400  		CreateTime: utils.NewStringValue(commontime.Time2String(releaseHistory.CreateTime)),
   401  		ModifyBy:   utils.NewStringValue(releaseHistory.ModifyBy),
   402  		ModifyTime: utils.NewStringValue(commontime.Time2String(releaseHistory.ModifyTime)),
   403  	}
   404  }
   405  
   406  type kv struct {
   407  	Key   string
   408  	Value string
   409  }
   410  
   411  // FromTagJson 从 Tags Json 字符串里反序列化出 Tags
   412  func FromTagMap(kvs map[string]string) []*config_manage.ConfigFileTag {
   413  	tags := make([]*config_manage.ConfigFileTag, 0, len(kvs))
   414  	for k, v := range kvs {
   415  		tags = append(tags, &config_manage.ConfigFileTag{
   416  			Key:   utils.NewStringValue(k),
   417  			Value: utils.NewStringValue(v),
   418  		})
   419  	}
   420  
   421  	return tags
   422  }
   423  
   424  func ToTagMap(tags []*config_manage.ConfigFileTag) map[string]string {
   425  	kvs := map[string]string{}
   426  	for i := range tags {
   427  		kvs[tags[i].GetKey().GetValue()] = tags[i].GetValue().GetValue()
   428  	}
   429  
   430  	return kvs
   431  }
   432  
   433  func ToConfigGroupAPI(group *ConfigFileGroup) *config_manage.ConfigFileGroup {
   434  	if group == nil {
   435  		return nil
   436  	}
   437  	return &config_manage.ConfigFileGroup{
   438  		Id:         utils.NewUInt64Value(group.Id),
   439  		Name:       utils.NewStringValue(group.Name),
   440  		Namespace:  utils.NewStringValue(group.Namespace),
   441  		Comment:    utils.NewStringValue(group.Comment),
   442  		Owner:      utils.NewStringValue(group.Owner),
   443  		CreateBy:   utils.NewStringValue(group.CreateBy),
   444  		ModifyBy:   utils.NewStringValue(group.ModifyBy),
   445  		CreateTime: utils.NewStringValue(commontime.Time2String(group.CreateTime)),
   446  		ModifyTime: utils.NewStringValue(commontime.Time2String(group.ModifyTime)),
   447  		Business:   utils.NewStringValue(group.Business),
   448  		Department: utils.NewStringValue(group.Department),
   449  		Metadata:   group.Metadata,
   450  	}
   451  }
   452  
   453  func ToConfigGroupStore(group *config_manage.ConfigFileGroup) *ConfigFileGroup {
   454  	var comment string
   455  	if group.Comment != nil {
   456  		comment = group.Comment.Value
   457  	}
   458  	var createBy string
   459  	if group.CreateBy != nil {
   460  		createBy = group.CreateBy.Value
   461  	}
   462  	var groupOwner string
   463  	if group.Owner != nil && group.Owner.GetValue() != "" {
   464  		groupOwner = group.Owner.GetValue()
   465  	} else {
   466  		groupOwner = createBy
   467  	}
   468  	return &ConfigFileGroup{
   469  		Name:       group.GetName().GetValue(),
   470  		Namespace:  group.GetNamespace().GetValue(),
   471  		Comment:    comment,
   472  		CreateBy:   createBy,
   473  		Valid:      true,
   474  		Owner:      groupOwner,
   475  		Business:   group.GetBusiness().GetValue(),
   476  		Department: group.GetDepartment().GetValue(),
   477  		Metadata:   group.GetMetadata(),
   478  	}
   479  }
   480  
   481  func ToConfigFileTemplateAPI(template *ConfigFileTemplate) *config_manage.ConfigFileTemplate {
   482  	return &config_manage.ConfigFileTemplate{
   483  		Id:         utils.NewUInt64Value(template.Id),
   484  		Name:       utils.NewStringValue(template.Name),
   485  		Content:    utils.NewStringValue(template.Content),
   486  		Comment:    utils.NewStringValue(template.Comment),
   487  		Format:     utils.NewStringValue(template.Format),
   488  		CreateBy:   utils.NewStringValue(template.CreateBy),
   489  		CreateTime: utils.NewStringValue(commontime.Time2String(template.CreateTime)),
   490  		ModifyBy:   utils.NewStringValue(template.ModifyBy),
   491  		ModifyTime: utils.NewStringValue(commontime.Time2String(template.ModifyTime)),
   492  	}
   493  }
   494  
   495  func ToConfigFileTemplateStore(template *config_manage.ConfigFileTemplate) *ConfigFileTemplate {
   496  	return &ConfigFileTemplate{
   497  		Id:       template.Id.GetValue(),
   498  		Name:     template.Name.GetValue(),
   499  		Content:  template.Content.GetValue(),
   500  		Comment:  template.Comment.GetValue(),
   501  		Format:   template.Format.GetValue(),
   502  		CreateBy: template.CreateBy.GetValue(),
   503  		ModifyBy: template.ModifyBy.GetValue(),
   504  	}
   505  }