-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathEnvironmentService.go
More file actions
619 lines (577 loc) · 22.1 KB
/
EnvironmentService.go
File metadata and controls
619 lines (577 loc) · 22.1 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* 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 cluster
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/devtron-labs/devtron/client/k8s/informer"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/cluster/repository"
"github.com/devtron-labs/devtron/pkg/user"
repository2 "github.com/devtron-labs/devtron/pkg/user/repository"
"github.com/go-pg/pg"
"github.com/pkg/errors"
"go.uber.org/zap"
)
type EnvironmentBean struct {
Id int `json:"id,omitempty" validate:"number"`
Environment string `json:"environment_name,omitempty" validate:"required,max=50"`
ClusterId int `json:"cluster_id,omitempty" validate:"number,required"`
ClusterName string `json:"cluster_name,omitempty"`
Active bool `json:"active"`
Default bool `json:"default"`
PrometheusEndpoint string `json:"prometheus_endpoint,omitempty"`
Namespace string `json:"namespace,omitempty" validate:"name-space-component,max=50"`
CdArgoSetup bool `json:"isClusterCdActive"`
EnvironmentIdentifier string `json:"environmentIdentifier"`
}
type EnvDto struct {
EnvironmentId int `json:"environmentId" validate:"number"`
EnvironmentName string `json:"environmentName,omitempty" validate:"max=50"`
Namespace string `json:"namespace,omitempty" validate:"name-space-component,max=50"`
EnvironmentIdentifier string `json:"environmentIdentifier,omitempty"`
}
type ClusterEnvDto struct {
ClusterId int `json:"clusterId"`
ClusterName string `json:"clusterName,omitempty"`
Environments []*EnvDto `json:"environments,omitempty"`
}
type EnvironmentService interface {
FindOne(environment string) (*EnvironmentBean, error)
Create(mappings *EnvironmentBean, userId int32) (*EnvironmentBean, error)
GetAll() ([]EnvironmentBean, error)
GetAllActive() ([]EnvironmentBean, error)
Delete(deleteReq *EnvironmentBean, userId int32) error
FindById(id int) (*EnvironmentBean, error)
Update(mappings *EnvironmentBean, userId int32) (*EnvironmentBean, error)
FindClusterByEnvId(id int) (*ClusterBean, error)
GetEnvironmentListForAutocomplete() ([]EnvironmentBean, error)
FindByIds(ids []*int) ([]*EnvironmentBean, error)
FindByNamespaceAndClusterName(namespaces string, clusterName string) (*repository.Environment, error)
GetByClusterId(id int) ([]*EnvironmentBean, error)
GetCombinedEnvironmentListForDropDown(token string, isActionUserSuperAdmin bool, auth func(token string, object string) bool) ([]*ClusterEnvDto, error)
GetCombinedEnvironmentListForDropDownByClusterIds(token string, clusterIds []int, auth func(token string, object string) bool) ([]*ClusterEnvDto, error)
}
type EnvironmentServiceImpl struct {
environmentRepository repository.EnvironmentRepository
logger *zap.SugaredLogger
clusterService ClusterService
K8sUtil *util.K8sUtil
k8sInformerFactory informer.K8sInformerFactory
//propertiesConfigService pipeline.PropertiesConfigService
userAuthService user.UserAuthService
}
func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepository,
clusterService ClusterService, logger *zap.SugaredLogger,
K8sUtil *util.K8sUtil, k8sInformerFactory informer.K8sInformerFactory,
// propertiesConfigService pipeline.PropertiesConfigService,
userAuthService user.UserAuthService) *EnvironmentServiceImpl {
return &EnvironmentServiceImpl{
environmentRepository: environmentRepository,
logger: logger,
clusterService: clusterService,
K8sUtil: K8sUtil,
k8sInformerFactory: k8sInformerFactory,
//propertiesConfigService: propertiesConfigService,
userAuthService: userAuthService,
}
}
func (impl EnvironmentServiceImpl) Create(mappings *EnvironmentBean, userId int32) (*EnvironmentBean, error) {
existingEnvs, err := impl.environmentRepository.FindByClusterId(mappings.ClusterId)
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("error while fetch", "err", err)
return nil, err
}
err = impl.validateNamespaces(mappings.Namespace, existingEnvs)
if err != nil {
return nil, err
}
clusterBean, err := impl.clusterService.FindById(mappings.ClusterId)
if err != nil {
return nil, err
}
identifier := clusterBean.ClusterName + "__" + mappings.Namespace
model, err := impl.environmentRepository.FindByNameOrIdentifier(mappings.Environment, identifier)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in finding environment for update", "err", err)
return mappings, err
}
if model.Id > 0 {
impl.logger.Warnw("environment already exists for this cluster and namespace", "model", model)
return mappings, fmt.Errorf("environment already exists")
}
model = &repository.Environment{
Name: mappings.Environment,
ClusterId: mappings.ClusterId,
Active: mappings.Active,
Namespace: mappings.Namespace,
Default: mappings.Default,
EnvironmentIdentifier: identifier,
}
model.CreatedBy = userId
model.UpdatedBy = userId
model.CreatedOn = time.Now()
model.UpdatedOn = time.Now()
err = impl.environmentRepository.Create(model)
if err != nil {
impl.logger.Errorw("error in saving environment", "err", err)
return mappings, err
}
if len(model.Namespace) > 0 {
cfg, err := impl.clusterService.GetClusterConfig(clusterBean)
if err != nil {
return nil, err
}
if err := impl.K8sUtil.CreateNsIfNotExists(model.Namespace, cfg); err != nil {
impl.logger.Errorw("error in creating ns", "ns", model.Namespace, "err", err)
}
}
//ignore grafana if no prometheus url found
if len(clusterBean.PrometheusUrl) > 0 {
_, err = impl.clusterService.CreateGrafanaDataSource(clusterBean, model)
if err != nil {
impl.logger.Errorw("unable to create grafana data source", "env", model)
}
}
mappings.Id = model.Id
return mappings, nil
}
func (impl EnvironmentServiceImpl) FindOne(environment string) (*EnvironmentBean, error) {
model, err := impl.environmentRepository.FindOne(environment)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
return nil, err
}
bean := &EnvironmentBean{
Id: model.Id,
Environment: model.Name,
ClusterId: model.Cluster.Id,
Active: model.Active,
PrometheusEndpoint: model.Cluster.PrometheusEndpoint,
Namespace: model.Namespace,
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
}
return bean, nil
}
func (impl EnvironmentServiceImpl) GetAll() ([]EnvironmentBean, error) {
models, err := impl.environmentRepository.FindAll()
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []EnvironmentBean
for _, model := range models {
beans = append(beans, EnvironmentBean{
Id: model.Id,
Environment: model.Name,
ClusterId: model.Cluster.Id,
ClusterName: model.Cluster.ClusterName,
Active: model.Active,
PrometheusEndpoint: model.Cluster.PrometheusEndpoint,
Namespace: model.Namespace,
Default: model.Default,
CdArgoSetup: model.Cluster.CdArgoSetup,
EnvironmentIdentifier: model.EnvironmentIdentifier,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) GetAllActive() ([]EnvironmentBean, error) {
models, err := impl.environmentRepository.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []EnvironmentBean
for _, model := range models {
beans = append(beans, EnvironmentBean{
Id: model.Id,
Environment: model.Name,
ClusterId: model.Cluster.Id,
ClusterName: model.Cluster.ClusterName,
Active: model.Active,
PrometheusEndpoint: model.Cluster.PrometheusEndpoint,
Namespace: model.Namespace,
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) FindById(id int) (*EnvironmentBean, error) {
model, err := impl.environmentRepository.FindById(id)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
return nil, err
}
bean := &EnvironmentBean{
Id: model.Id,
Environment: model.Name,
ClusterId: model.Cluster.Id,
Active: model.Active,
PrometheusEndpoint: model.Cluster.PrometheusEndpoint,
Namespace: model.Namespace,
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
}
/*clusterBean := &ClusterBean{
id:model.Cluster.id,
ClusterName: model.Cluster.ClusterName,
Active:model.Cluster.Active,
}*/
return bean, nil
}
func (impl EnvironmentServiceImpl) Update(mappings *EnvironmentBean, userId int32) (*EnvironmentBean, error) {
model, err := impl.environmentRepository.FindById(mappings.Id)
if err != nil {
impl.logger.Errorw("error in finding environment for update", "err", err)
return mappings, err
}
/*isNamespaceChange := false
if model.Namespace != mappings.Namespace {
isNamespaceChange = true
}*/
clusterBean, err := impl.clusterService.FindById(mappings.ClusterId)
if err != nil {
return nil, err
}
model.Name = mappings.Environment
model.Active = mappings.Active
model.Namespace = mappings.Namespace
model.Default = mappings.Default
model.UpdatedBy = userId
model.UpdatedOn = time.Now()
//namespace create if not exist
if len(model.Namespace) > 0 {
cfg, err := impl.clusterService.GetClusterConfig(clusterBean)
if err != nil {
return nil, err
}
if err := impl.K8sUtil.CreateNsIfNotExists(model.Namespace, cfg); err != nil {
impl.logger.Errorw("error in creating ns", "ns", model.Namespace, "err", err)
}
}
//namespace changed, update it on chart env override config as well
/*if isNamespaceChange == true {
impl.logger.Debug("namespace has modified in request, it will update related config")
envPropertiesList, err := impl.propertiesConfigService.GetEnvironmentPropertiesById(mappings.Id)
if err != nil {
impl.logger.Error("failed to fetch chart environment override config", "err", err)
//TODO - atomic operation breaks, throw internal error codes
} else {
for _, envProperties := range envPropertiesList {
_, err := impl.propertiesConfigService.UpdateEnvironmentProperties(0, &pipeline.EnvironmentProperties{Id: envProperties.Id, Namespace: mappings.Namespace}, userId)
if err != nil {
impl.logger.Error("failed to update chart environment override config", "err", err)
//TODO - atomic operation breaks, throw internal error codes
}
}
}
}*/
grafanaDatasourceId := model.GrafanaDatasourceId
//grafana datasource create if not exist
if len(clusterBean.PrometheusUrl) > 0 && grafanaDatasourceId == 0 {
grafanaDatasourceId, err = impl.clusterService.CreateGrafanaDataSource(clusterBean, model)
if err != nil {
impl.logger.Errorw("unable to create grafana data source for missing env", "env", model)
}
}
model.GrafanaDatasourceId = grafanaDatasourceId
err = impl.environmentRepository.Update(model)
if err != nil {
impl.logger.Errorw("error in updating environment", "err", err)
return mappings, err
}
mappings.Id = model.Id
return mappings, nil
}
func (impl EnvironmentServiceImpl) FindClusterByEnvId(id int) (*ClusterBean, error) {
model, err := impl.environmentRepository.FindById(id)
if err != nil {
impl.logger.Errorw("fetch cluster by environment id", "err", err)
return nil, err
}
clusterBean := &ClusterBean{
Id: model.Cluster.Id,
ClusterName: model.Cluster.ClusterName,
Active: model.Cluster.Active,
ServerUrl: model.Cluster.ServerUrl,
Config: model.Cluster.Config,
}
return clusterBean, nil
}
func (impl EnvironmentServiceImpl) GetEnvironmentListForAutocomplete() ([]EnvironmentBean, error) {
models, err := impl.environmentRepository.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []EnvironmentBean
for _, model := range models {
beans = append(beans, EnvironmentBean{
Id: model.Id,
Environment: model.Name,
Namespace: model.Namespace,
CdArgoSetup: model.Cluster.CdArgoSetup,
EnvironmentIdentifier: model.EnvironmentIdentifier,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) validateNamespaces(namespace string, envs []*repository.Environment) error {
if len(envs) >= 1 {
if namespace == "" {
impl.logger.Errorw("namespace cannot be empty")
return errors.New("namespace cannot be empty")
}
for _, n := range envs {
if n.Namespace == "" {
impl.logger.Errorw("cannot create env as existing old envs have no namespace")
return errors.New("cannot create env as existing old envs have no namespace")
}
}
}
return nil
}
func (impl EnvironmentServiceImpl) FindByIds(ids []*int) ([]*EnvironmentBean, error) {
models, err := impl.environmentRepository.FindByIds(ids)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []*EnvironmentBean
for _, model := range models {
beans = append(beans, &EnvironmentBean{
Id: model.Id,
Environment: model.Name,
Active: model.Active,
Namespace: model.Namespace,
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
ClusterId: model.ClusterId,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) FindByNamespaceAndClusterName(namespaces string, clusterName string) (*repository.Environment, error) {
env, err := impl.environmentRepository.FindByNamespaceAndClusterName(namespaces, clusterName)
return env, err
}
func (impl EnvironmentServiceImpl) GetByClusterId(id int) ([]*EnvironmentBean, error) {
models, err := impl.environmentRepository.FindByClusterId(id)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []*EnvironmentBean
for _, model := range models {
beans = append(beans, &EnvironmentBean{
Id: model.Id,
Environment: model.Name,
Namespace: model.Namespace,
EnvironmentIdentifier: model.EnvironmentIdentifier,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) GetCombinedEnvironmentListForDropDown(token string, isActionUserSuperAdmin bool, auth func(token string, object string) bool) ([]*ClusterEnvDto, error) {
var namespaceGroupByClusterResponse []*ClusterEnvDto
clusterModels, err := impl.clusterService.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching clusters", "err", err)
return namespaceGroupByClusterResponse, err
}
clusterMap := make(map[string]int)
for _, item := range clusterModels {
clusterMap[item.ClusterName] = item.Id
}
models, err := impl.environmentRepository.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching environments", "err", err)
return namespaceGroupByClusterResponse, err
}
uniqueComboMap := make(map[string]bool)
grantedEnvironmentMap := make(map[string][]*EnvDto)
for _, model := range models {
// isActionUserSuperAdmin tell that user is super admin or not. auth check skip for admin
if !isActionUserSuperAdmin {
// auth enforcer applied here
isValidAuth := auth(token, model.EnvironmentIdentifier)
if !isValidAuth {
impl.logger.Debugw("authentication for env failed", "object", model.EnvironmentIdentifier)
continue
}
}
key := fmt.Sprintf("%s__%s", model.Cluster.ClusterName, model.Namespace)
groupKey := fmt.Sprintf("%s__%d", model.Cluster.ClusterName, model.ClusterId)
uniqueComboMap[key] = true
grantedEnvironmentMap[groupKey] = append(grantedEnvironmentMap[groupKey], &EnvDto{
EnvironmentId: model.Id,
EnvironmentName: model.Name,
Namespace: model.Namespace,
EnvironmentIdentifier: model.EnvironmentIdentifier,
})
}
namespaceListGroupByClusters := impl.k8sInformerFactory.GetLatestNamespaceListGroupByCLuster()
for clusterName, namespaces := range namespaceListGroupByClusters {
clusterId := clusterMap[clusterName]
for namespace := range namespaces {
//deduplication for cluster and namespace combination
key := fmt.Sprintf("%s__%s", clusterName, namespace)
groupKey := fmt.Sprintf("%s__%d", clusterName, clusterId)
if _, ok := uniqueComboMap[key]; !ok {
environmentIdentifier := fmt.Sprintf("%s__%s", clusterName, namespace)
// isActionUserSuperAdmin tell that user is super admin or not. auth check skip for admin
if !isActionUserSuperAdmin {
// auth enforcer applied here
isValidAuth := auth(token, environmentIdentifier)
if !isValidAuth {
impl.logger.Debugw("authentication for env failed", "object", environmentIdentifier)
continue
}
}
grantedEnvironmentMap[groupKey] = append(grantedEnvironmentMap[groupKey], &EnvDto{
EnvironmentName: environmentIdentifier,
Namespace: namespace,
EnvironmentIdentifier: environmentIdentifier,
})
}
}
}
//final result builds here, namespace group by clusters
for k, v := range grantedEnvironmentMap {
clusterInfo := strings.Split(k, "__")
clusterId, err := strconv.Atoi(clusterInfo[1])
if err != nil {
clusterId = 0
}
namespaceGroupByClusterResponse = append(namespaceGroupByClusterResponse, &ClusterEnvDto{
ClusterName: clusterInfo[0],
ClusterId: clusterId,
Environments: v,
})
}
return namespaceGroupByClusterResponse, nil
}
func (impl EnvironmentServiceImpl) GetCombinedEnvironmentListForDropDownByClusterIds(token string, clusterIds []int, auth func(token string, object string) bool) ([]*ClusterEnvDto, error) {
var namespaceGroupByClusterResponse []*ClusterEnvDto
clusterModels, err := impl.clusterService.FindByIds(clusterIds)
if err != nil {
impl.logger.Errorw("error in fetching clusters", "err", err)
return namespaceGroupByClusterResponse, err
}
clusterMap := make(map[string]int)
for _, item := range clusterModels {
clusterMap[item.ClusterName] = item.Id
}
models, err := impl.environmentRepository.FindByClusterIds(clusterIds)
if err != nil {
impl.logger.Errorw("error in fetching environments", "err", err)
return namespaceGroupByClusterResponse, err
}
uniqueComboMap := make(map[string]bool)
grantedEnvironmentMap := make(map[string][]*EnvDto)
for _, model := range models {
// auth enforcer applied here
isValidAuth := auth(token, model.EnvironmentIdentifier)
if !isValidAuth {
impl.logger.Debugw("authentication for env failed", "object", model.EnvironmentIdentifier)
continue
}
key := fmt.Sprintf("%s__%s", model.Cluster.ClusterName, model.Namespace)
groupKey := fmt.Sprintf("%s__%d", model.Cluster.ClusterName, model.ClusterId)
uniqueComboMap[key] = true
grantedEnvironmentMap[groupKey] = append(grantedEnvironmentMap[groupKey], &EnvDto{
EnvironmentId: model.Id,
EnvironmentName: model.Name,
Namespace: model.Namespace,
EnvironmentIdentifier: model.EnvironmentIdentifier,
})
}
namespaceListGroupByClusters := impl.k8sInformerFactory.GetLatestNamespaceListGroupByCLuster()
for clusterName, namespaces := range namespaceListGroupByClusters {
clusterId := clusterMap[clusterName]
if clusterId == 0 {
continue
}
for namespace := range namespaces {
//deduplication for cluster and namespace combination
key := fmt.Sprintf("%s__%s", clusterName, namespace)
groupKey := fmt.Sprintf("%s__%d", clusterName, clusterId)
if _, ok := uniqueComboMap[key]; !ok {
environmentIdentifier := fmt.Sprintf("%s__%s", clusterName, namespace)
// auth enforcer applied here
isValidAuth := auth(token, environmentIdentifier)
if !isValidAuth {
impl.logger.Debugw("authentication for env failed", "object", environmentIdentifier)
continue
}
grantedEnvironmentMap[groupKey] = append(grantedEnvironmentMap[groupKey], &EnvDto{
EnvironmentName: environmentIdentifier,
Namespace: namespace,
EnvironmentIdentifier: environmentIdentifier,
})
}
}
}
//final result builds here, namespace group by clusters
for k, v := range grantedEnvironmentMap {
clusterInfo := strings.Split(k, "__")
clusterId, err := strconv.Atoi(clusterInfo[1])
if err != nil {
clusterId = 0
}
namespaceGroupByClusterResponse = append(namespaceGroupByClusterResponse, &ClusterEnvDto{
ClusterName: clusterInfo[0],
ClusterId: clusterId,
Environments: v,
})
}
return namespaceGroupByClusterResponse, nil
}
func (impl EnvironmentServiceImpl) Delete(deleteReq *EnvironmentBean, userId int32) error {
existingEnv, err := impl.environmentRepository.FindById(deleteReq.Id)
if err != nil {
impl.logger.Errorw("No matching entry found for delete.", "id", deleteReq.Id)
return err
}
dbConnection := impl.environmentRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
impl.logger.Errorw("error in establishing connection", "err", err)
return err
}
// Rollback tx on error.
defer tx.Rollback()
deleteRequest := existingEnv
deleteRequest.UpdatedOn = time.Now()
deleteRequest.UpdatedBy = userId
err = impl.environmentRepository.MarkEnvironmentDeleted(deleteRequest, tx)
if err != nil {
impl.logger.Errorw("error in deleting environment", "envId", deleteReq.Id, "envName", deleteReq.Environment)
return err
}
//deleting auth roles entries for this environment
err = impl.userAuthService.DeleteRoles(repository2.ENV_TYPE, deleteRequest.Name, tx, existingEnv.EnvironmentIdentifier)
if err != nil {
impl.logger.Errorw("error in deleting auth roles", "err", err)
return err
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}