1
+ /*
2
+ * Copyright (c) 2020-2024. Devtron Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
1
17
package common
2
18
3
19
import (
@@ -18,6 +34,7 @@ import (
18
34
19
35
type DeploymentConfigService interface {
20
36
CreateOrUpdateConfig (tx * pg.Tx , config * bean.DeploymentConfig , userId int32 ) (* bean.DeploymentConfig , error )
37
+ CreateOrUpdateConfigInBulk (tx * pg.Tx , configToBeCreated , configToBeUpdated []* bean.DeploymentConfig , userId int32 ) error
21
38
IsDeploymentConfigUsed () bool
22
39
GetConfigForDevtronApps (appId , envId int ) (* bean.DeploymentConfig , error )
23
40
GetAndMigrateConfigIfAbsentForDevtronApps (appId , envId int ) (* bean.DeploymentConfig , error )
@@ -27,6 +44,7 @@ type DeploymentConfigService interface {
27
44
GetAppLevelConfigForDevtronApp (appId int ) (* bean.DeploymentConfig , error )
28
45
UpdateRepoUrlForAppAndEnvId (repoURL string , appId , envId int ) error
29
46
GetDeploymentAppTypeForCDInBulk (pipelines []* pipelineConfig.Pipeline ) (map [int ]string , error )
47
+ GetConfigsByAppIds (appIds []int ) ([]* bean.DeploymentConfig , error )
30
48
}
31
49
32
50
type DeploymentConfigServiceImpl struct {
@@ -90,6 +108,41 @@ func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfig(tx *pg.Tx, config
90
108
return ConvertDeploymentConfigDbObjToDTO (newDBObj ), nil
91
109
}
92
110
111
+ func (impl * DeploymentConfigServiceImpl ) CreateOrUpdateConfigInBulk (tx * pg.Tx , configToBeCreated , configToBeUpdated []* bean.DeploymentConfig , userId int32 ) error {
112
+
113
+ dbObjCreate := make ([]* deploymentConfig.DeploymentConfig , 0 , len (configToBeCreated ))
114
+ for i := range configToBeCreated {
115
+ dbObj := ConvertDeploymentConfigDTOToDbObj (configToBeCreated [i ])
116
+ dbObj .AuditLog .CreateAuditLog (userId )
117
+ dbObjCreate = append (dbObjCreate , dbObj )
118
+ }
119
+
120
+ dbObjUpdate := make ([]* deploymentConfig.DeploymentConfig , 0 , len (configToBeUpdated ))
121
+ for i := range configToBeUpdated {
122
+ dbObj := ConvertDeploymentConfigDTOToDbObj (configToBeUpdated [i ])
123
+ dbObj .AuditLog .UpdateAuditLog (userId )
124
+ dbObjUpdate = append (dbObjUpdate , dbObj )
125
+ }
126
+
127
+ if len (dbObjCreate ) > 0 {
128
+ _ , err := impl .deploymentConfigRepository .SaveAll (tx , dbObjCreate )
129
+ if err != nil {
130
+ impl .logger .Errorw ("error in saving deploymentConfig" , "dbObjCreate" , dbObjCreate , "err" , err )
131
+ return err
132
+ }
133
+ }
134
+
135
+ if len (dbObjUpdate ) > 0 {
136
+ _ , err := impl .deploymentConfigRepository .UpdateAll (tx , dbObjUpdate )
137
+ if err != nil {
138
+ impl .logger .Errorw ("error in updating deploymentConfig" , "dbObjUpdate" , dbObjUpdate , "err" , err )
139
+ return err
140
+ }
141
+ }
142
+
143
+ return nil
144
+ }
145
+
93
146
func (impl * DeploymentConfigServiceImpl ) IsDeploymentConfigUsed () bool {
94
147
return impl .deploymentServiceTypeConfig .UseDeploymentConfigData
95
148
}
@@ -478,3 +531,19 @@ func (impl *DeploymentConfigServiceImpl) GetDeploymentAppTypeForCDInBulk(pipelin
478
531
}
479
532
return resp , nil
480
533
}
534
+
535
+ func (impl * DeploymentConfigServiceImpl ) GetConfigsByAppIds (appIds []int ) ([]* bean.DeploymentConfig , error ) {
536
+ if len (appIds ) == 0 {
537
+ return nil , nil
538
+ }
539
+ configs , err := impl .deploymentConfigRepository .GetConfigByAppIds (appIds )
540
+ if err != nil {
541
+ impl .logger .Errorw ("error in getting deployment config db object by appIds" , "appIds" , appIds , "err" , err )
542
+ return nil , err
543
+ }
544
+ resp := make ([]* bean.DeploymentConfig , 0 , len (configs ))
545
+ for _ , config := range configs {
546
+ resp = append (resp , ConvertDeploymentConfigDbObjToDTO (config ))
547
+ }
548
+ return resp , nil
549
+ }
0 commit comments