Skip to content

Commit c55c270

Browse files
fix: Validate config cm cs (#5750)
* validateConfigRequest before CMGlobalAddUpdate and CSGlobalAddUpdate * checkIfConfigDataAlreadyExist
1 parent 0c56885 commit c55c270

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pkg/pipeline/ConfigMapService.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ func NewConfigMapServiceImpl(chartRepository chartRepoRepository.ChartRepository
129129
}
130130
}
131131

132+
func (impl ConfigMapServiceImpl) checkIfConfigDataAlreadyExist(appId int) (int, error) {
133+
config, err := impl.configMapRepository.GetByAppIdAppLevel(appId)
134+
if err != nil && err != pg.ErrNoRows {
135+
impl.logger.Errorw("error while checking if config data exist from db by appId", "appId", appId, "error", err)
136+
return 0, err
137+
} else if util.IsErrNoRows(err) {
138+
return 0, nil
139+
}
140+
return config.Id, nil
141+
}
142+
132143
func (impl ConfigMapServiceImpl) CMGlobalAddUpdate(configMapRequest *bean.ConfigDataRequest) (*bean.ConfigDataRequest, error) {
133144
if len(configMapRequest.ConfigData) != 1 {
134145
return nil, fmt.Errorf("invalid request multiple config found for add or update")
@@ -140,6 +151,14 @@ func (impl ConfigMapServiceImpl) CMGlobalAddUpdate(configMapRequest *bean.Config
140151
return configMapRequest, err
141152
}
142153
var model *chartConfig.ConfigMapAppModel
154+
requestId, err := impl.checkIfConfigDataAlreadyExist(configMapRequest.AppId)
155+
if err != nil {
156+
impl.logger.Errorw("error in checking if config map data already exists or not for appId", "appId", configMapRequest.AppId, "error", err)
157+
return configMapRequest, err
158+
}
159+
if requestId > 0 {
160+
configMapRequest.Id = requestId
161+
}
143162
if configMapRequest.Id > 0 {
144163
model, err = impl.configMapRepository.GetByIdAppLevel(configMapRequest.Id)
145164
if err != nil {
@@ -525,6 +544,14 @@ func (impl ConfigMapServiceImpl) CSGlobalAddUpdate(configMapRequest *bean.Config
525544
impl.logger.Errorw("error in validating", "error", err)
526545
return configMapRequest, err
527546
}
547+
requestId, err := impl.checkIfConfigDataAlreadyExist(configMapRequest.AppId)
548+
if err != nil {
549+
impl.logger.Errorw("error in checking if config secret data already exists or not for appId", "appId", configMapRequest.AppId, "error", err)
550+
return configMapRequest, err
551+
}
552+
if requestId > 0 {
553+
configMapRequest.Id = requestId
554+
}
528555
var model *chartConfig.ConfigMapAppModel
529556
if configMapRequest.Id > 0 {
530557
model, err = impl.configMapRepository.GetByIdAppLevel(configMapRequest.Id)

0 commit comments

Comments
 (0)