-
Notifications
You must be signed in to change notification settings - Fork 465
/
AttributesService.go
313 lines (282 loc) · 8.22 KB
/
AttributesService.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 attributes
import (
"encoding/json"
"errors"
"fmt"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/go-pg/pg"
"go.uber.org/zap"
"strconv"
"time"
)
type AttributesService interface {
AddAttributes(request *AttributesDto) (*AttributesDto, error)
UpdateAttributes(request *AttributesDto) (*AttributesDto, error)
GetById(id int) (*AttributesDto, error)
GetActiveList() ([]*AttributesDto, error)
GetByKey(key string) (*AttributesDto, error)
UpdateKeyValueByOne(key string) error
AddDeploymentEnforcementConfig(request *AttributesDto) (*AttributesDto, error)
}
const (
HostUrlKey string = "url"
API_SECRET_KEY string = "apiTokenSecret"
ENFORCE_DEPLOYMENT_TYPE_CONFIG string = "enforceDeploymentTypeConfig"
)
type AttributesDto struct {
Id int `json:"id"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
Active bool `json:"active"`
UserId int32 `json:"-"`
}
type AttributesServiceImpl struct {
logger *zap.SugaredLogger
attributesRepository repository.AttributesRepository
}
func NewAttributesServiceImpl(logger *zap.SugaredLogger,
attributesRepository repository.AttributesRepository) *AttributesServiceImpl {
serviceImpl := &AttributesServiceImpl{
logger: logger,
attributesRepository: attributesRepository,
}
return serviceImpl
}
func (impl AttributesServiceImpl) AddAttributes(request *AttributesDto) (*AttributesDto, error) {
dbConnection := impl.attributesRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
return nil, err
}
// Rollback tx on error.
defer tx.Rollback()
existingModel, err := impl.attributesRepository.FindByKey(request.Key)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in update new attributes", "error", err)
return nil, err
}
if existingModel != nil && existingModel.Id > 0 {
existingModel.Active = false
err = impl.attributesRepository.Update(existingModel, tx)
if err != nil {
impl.logger.Errorw("error in creating new attributes", "error", err)
return nil, err
}
}
model := &repository.Attributes{
Key: request.Key,
Value: request.Value,
}
model.Active = true
model.CreatedBy = request.UserId
model.UpdatedBy = request.UserId
model.CreatedOn = time.Now()
model.UpdatedOn = time.Now()
_, err = impl.attributesRepository.Save(model, tx)
if err != nil {
impl.logger.Errorw("error in creating new attributes", "error", err)
return nil, err
}
request.Id = model.Id
err = tx.Commit()
if err != nil {
return nil, err
}
return request, nil
}
func (impl AttributesServiceImpl) UpdateAttributes(request *AttributesDto) (*AttributesDto, error) {
dbConnection := impl.attributesRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
return nil, err
}
// Rollback tx on error.
defer tx.Rollback()
model, err := impl.attributesRepository.FindById(request.Id)
if err != nil {
impl.logger.Errorw("error in update new host url", "error", err)
return nil, err
}
model.Key = request.Key
model.Value = request.Value
model.Active = true
model.CreatedBy = request.UserId
model.UpdatedBy = request.UserId
model.CreatedOn = time.Now()
model.UpdatedOn = time.Now()
err = impl.attributesRepository.Update(model, tx)
if err != nil {
impl.logger.Errorw("error in update new attributes", "error", err)
return nil, err
}
request.Id = model.Id
err = tx.Commit()
if err != nil {
return nil, err
}
return request, nil
}
func (impl AttributesServiceImpl) GetById(id int) (*AttributesDto, error) {
model, err := impl.attributesRepository.FindById(id)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in fetching attributes", "error", err)
return nil, err
}
if err == pg.ErrNoRows {
return nil, nil
}
ssoLoginDto := &AttributesDto{
Id: model.Id,
Active: model.Active,
Key: model.Key,
Value: model.Value,
}
return ssoLoginDto, nil
}
func (impl AttributesServiceImpl) GetActiveList() ([]*AttributesDto, error) {
results := make([]*AttributesDto, 0)
models, err := impl.attributesRepository.FindActiveList()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in fetching attributes", "error", err)
return nil, err
}
if err == pg.ErrNoRows {
return results, nil
}
for _, model := range models {
dto := &AttributesDto{
Id: model.Id,
Active: model.Active,
Key: model.Key,
Value: model.Value,
}
results = append(results, dto)
}
return results, nil
}
func (impl AttributesServiceImpl) GetByKey(key string) (*AttributesDto, error) {
model, err := impl.attributesRepository.FindByKey(key)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in fetching attributes", "error", err, "key", key)
return nil, err
}
if err == pg.ErrNoRows {
return nil, nil
}
dto := &AttributesDto{
Id: model.Id,
Active: model.Active,
Key: model.Key,
Value: model.Value,
}
return dto, nil
}
func (impl AttributesServiceImpl) UpdateKeyValueByOne(key string) error {
model, err := impl.attributesRepository.FindByKey(key)
dbConnection := impl.attributesRepository.GetConnection()
tx, err := dbConnection.Begin()
defer tx.Rollback()
model.Key = key
if err != nil {
return err
}
if model.Value == "" {
model.Value = "1"
model.Active = true
} else {
newValue, _ := strconv.Atoi(model.Value)
model.Value = strconv.Itoa(newValue + 1)
}
err = impl.attributesRepository.Update(model, tx)
if err == pg.ErrNoRows {
_, err = impl.attributesRepository.Save(model, tx)
}
err = tx.Commit()
return err
}
func (impl AttributesServiceImpl) AddDeploymentEnforcementConfig(request *AttributesDto) (*AttributesDto, error) {
newConfig := make(map[string]map[string]bool)
attributesErr := json.Unmarshal([]byte(request.Value), &newConfig)
if attributesErr != nil {
return request, attributesErr
}
for environmentId, envConfig := range newConfig {
AllowedDeploymentAppTypes := 0
for _, allowed := range envConfig {
if allowed {
AllowedDeploymentAppTypes++
}
}
if AllowedDeploymentAppTypes == 0 && len(envConfig) > 0 {
return request, errors.New(fmt.Sprintf("Received invalid config for environment with id %s, "+
"at least one deployment app type should be allowed", environmentId))
}
}
dbConnection := impl.attributesRepository.GetConnection()
tx, terr := dbConnection.Begin()
if terr != nil {
return request, terr
}
// Rollback tx on error.
defer tx.Rollback()
model, err := impl.attributesRepository.FindByKey(ENFORCE_DEPLOYMENT_TYPE_CONFIG)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in fetching deploymentEnforcementConfig from db", "error", err, "key", request.Key)
return request, err
}
if err == pg.ErrNoRows {
model := &repository.Attributes{
Key: ENFORCE_DEPLOYMENT_TYPE_CONFIG,
Value: request.Value,
}
model.Active = true
model.UpdatedOn = time.Now()
model.UpdatedBy = request.UserId
_, err = impl.attributesRepository.Save(model, tx)
if err != nil {
return request, err
}
} else {
oldConfig := make(map[string]map[string]bool)
oldConfigString := model.Value
//initialConfigString = `{ "1": {"argo_cd": true}}`
err = json.Unmarshal([]byte(oldConfigString), &oldConfig)
if err != nil {
return request, err
}
mergedConfig := oldConfig
for k, v := range newConfig {
mergedConfig[k] = v
}
value, err := json.Marshal(mergedConfig)
if err != nil {
return request, err
}
model.Value = string(value)
model.UpdatedOn = time.Now()
model.UpdatedBy = request.UserId
model.Active = true
err = impl.attributesRepository.Update(model, tx)
if err != nil {
return request, err
}
}
tx.Commit()
return request, nil
}