/
cms.service.go
152 lines (128 loc) · 4.35 KB
/
cms.service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cms
import (
"context"
"sort"
"github.com/erda-project/erda-infra/providers/mysqlxorm"
"github.com/erda-project/erda-proto-go/core/pipeline/cms/pb"
"github.com/erda-project/erda/modules/pipeline/services/apierrors"
"github.com/erda-project/erda/pkg/common/apis"
)
type cmsService struct {
p *provider
cm ConfigManager
db mysqlxorm.Interface
}
func getCtxWithSource(ctx context.Context, source string) context.Context {
return context.WithValue(ctx, CtxKeyPipelineSource, source)
}
func (s *cmsService) CreateNs(ctx context.Context, req *pb.CmsCreateNsRequest) (*pb.CmsCreateNsResponse, error) {
// authentication
if !apis.IsInternalClient(ctx) {
return nil, apierrors.ErrCreatePipelineCmsNs.AccessDenied()
}
ctx = getCtxWithSource(ctx, req.PipelineSource)
if err := s.cm.IdempotentCreateNs(ctx, req.Ns); err != nil {
return nil, err
}
return &pb.CmsCreateNsResponse{}, nil
}
func (s *cmsService) ListCmsNs(ctx context.Context, req *pb.CmsListNsRequest) (*pb.CmsListNsResponse, error) {
// authentication
if !apis.IsInternalClient(ctx) {
return nil, apierrors.ErrListPipelineCmsNs.AccessDenied()
}
ctx = getCtxWithSource(ctx, req.PipelineSource)
namespaces, err := s.cm.PrefixListNs(ctx, req.NsPrefix)
if err != nil {
return nil, err
}
var data []*pb.PipelineCmsNs
for _, ns := range namespaces {
data = append(data, &pb.PipelineCmsNs{
PipelineSource: ns.PipelineSource,
Ns: ns.Ns,
TimeCreated: ns.TimeCreated,
TimeUpdated: ns.TimeUpdated,
})
}
return &pb.CmsListNsResponse{Data: data}, nil
}
func (s *cmsService) UpdateCmsNsConfigs(ctx context.Context, req *pb.CmsNsConfigsUpdateRequest) (*pb.CmsNsConfigsUpdateResponse, error) {
// authentication
if !apis.IsInternalClient(ctx) {
return nil, apierrors.ErrUpdatePipelineCmsConfigs.AccessDenied()
}
ctx = getCtxWithSource(ctx, req.PipelineSource)
err := s.cm.UpdateConfigs(ctx, req.Ns, req.KVs)
if err != nil {
return nil, err
}
return &pb.CmsNsConfigsUpdateResponse{}, nil
}
func (s *cmsService) DeleteCmsNsConfigs(ctx context.Context, req *pb.CmsNsConfigsDeleteRequest) (*pb.CmsNsConfigsDeleteResponse, error) {
// authentication
if !apis.IsInternalClient(ctx) {
return nil, apierrors.ErrDeletePipelineCmsConfigs.AccessDenied()
}
ctx = getCtxWithSource(ctx, req.PipelineSource)
var opErr error
// 删除 ns
if req.DeleteNs {
opErr = s.cm.IdempotentDeleteNs(ctx, req.Ns)
} else {
ctx = context.WithValue(ctx, CtxKeyForceDelete, req.DeleteForce)
opErr = s.cm.DeleteConfigs(ctx, req.Ns, req.DeleteKeys...)
}
if opErr != nil {
return nil, opErr
}
return &pb.CmsNsConfigsDeleteResponse{}, nil
}
func (s *cmsService) GetCmsNsConfigs(ctx context.Context, req *pb.CmsNsConfigsGetRequest) (*pb.CmsNsConfigsGetResponse, error) {
// authentication
if !apis.IsInternalClient(ctx) {
return nil, apierrors.ErrGetPipelineCmsConfigs.AccessDenied()
}
ctx = getCtxWithSource(ctx, req.PipelineSource)
configs, err := s.cm.GetConfigs(ctx, req.Ns, req.GlobalDecrypt, req.Keys...)
if err != nil {
return nil, err
}
results := make([]*pb.PipelineCmsConfig, 0, len(configs))
for key, value := range configs {
results = append(results, &pb.PipelineCmsConfig{
Key: key,
Value: value.Value,
EncryptInDB: value.EncryptInDB,
Type: value.Type,
Operations: value.Operations,
Comment: value.Comment,
From: value.From,
TimeCreated: value.TimeCreated,
TimeUpdated: value.TimeUpdated,
})
}
// order by timeCreated
sort.SliceStable(results, func(i, j int) bool {
if results[i].TimeCreated == nil {
return true
}
if results[j].TimeCreated == nil {
return false
}
return results[i].TimeCreated.AsTime().Before(results[j].TimeCreated.AsTime())
})
return &pb.CmsNsConfigsGetResponse{Data: results}, nil
}