@@ -34,13 +34,14 @@ type QualifierMappingService interface {
3434 CreateQualifierMappings (qualifierMappings []* QualifierMapping , tx * pg.Tx ) ([]* QualifierMapping , error )
3535 GetQualifierMappings (resourceType ResourceType , scope * Scope , resourceIds []int ) ([]* QualifierMapping , error )
3636 DeleteAllQualifierMappings (resourceType ResourceType , auditLog sql.AuditLog , tx * pg.Tx ) error
37- DeleteByIdentifierKeyValue (resourceType ResourceType , identifierKey int , identifierValue int , auditLog sql.AuditLog , tx * pg.Tx ) error
37+ DeleteByIdentifierKeyAndValue (resourceType ResourceType , identifierKey int , identifierValue int , qualifierId int , auditLog sql.AuditLog , tx * pg.Tx ) error
3838 DeleteAllByIds (qualifierMappingIds []int , userId int32 , tx * pg.Tx ) error
3939 DeleteResourceMappingsForScopes (tx * pg.Tx , userId int32 , resourceType ResourceType , qualifierSelector QualifierSelector , scopes []* SelectionIdentifier ) error
4040 CreateMappingsForSelections (tx * pg.Tx , userId int32 , resourceMappingSelections []* ResourceMappingSelection ) ([]* ResourceMappingSelection , error )
4141 CreateMappings (tx * pg.Tx , userId int32 , resourceType ResourceType , resourceIds []int , qualifierSelector QualifierSelector , selectionIdentifiers []* SelectionIdentifier ) error
4242 GetResourceMappingsForSelections (resourceType ResourceType , qualifierSelector QualifierSelector , selectionIdentifiers []* SelectionIdentifier ) ([]ResourceQualifierMappings , error )
4343 GetResourceMappingsForResources (resourceType ResourceType , resourceIds []int , qualifierSelector QualifierSelector ) ([]ResourceQualifierMappings , error )
44+ QualifierMappingServiceEnt
4445}
4546
4647type QualifierMappingServiceImpl struct {
@@ -57,24 +58,24 @@ func NewQualifierMappingServiceImpl(logger *zap.SugaredLogger, qualifierMappingR
5758 }, nil
5859}
5960
60- func (impl QualifierMappingServiceImpl ) CreateQualifierMappings (qualifierMappings []* QualifierMapping , tx * pg.Tx ) ([]* QualifierMapping , error ) {
61+ func (impl * QualifierMappingServiceImpl ) CreateQualifierMappings (qualifierMappings []* QualifierMapping , tx * pg.Tx ) ([]* QualifierMapping , error ) {
6162 return impl .qualifierMappingRepository .CreateQualifierMappings (qualifierMappings , tx )
6263}
6364
64- func (impl QualifierMappingServiceImpl ) GetQualifierMappings (resourceType ResourceType , scope * Scope , resourceIds []int ) ([]* QualifierMapping , error ) {
65+ func (impl * QualifierMappingServiceImpl ) GetQualifierMappings (resourceType ResourceType , scope * Scope , resourceIds []int ) ([]* QualifierMapping , error ) {
6566 searchableKeyNameIdMap := impl .devtronResourceSearchableKeyService .GetAllSearchableKeyNameIdMap ()
6667 return impl .qualifierMappingRepository .GetQualifierMappings (resourceType , scope , searchableKeyNameIdMap , resourceIds )
6768}
6869
69- func (impl QualifierMappingServiceImpl ) DeleteAllQualifierMappings (resourceType ResourceType , auditLog sql.AuditLog , tx * pg.Tx ) error {
70+ func (impl * QualifierMappingServiceImpl ) DeleteAllQualifierMappings (resourceType ResourceType , auditLog sql.AuditLog , tx * pg.Tx ) error {
7071 return impl .qualifierMappingRepository .DeleteAllQualifierMappings (resourceType , auditLog , tx )
7172}
7273
73- func (impl QualifierMappingServiceImpl ) DeleteByIdentifierKeyValue (resourceType ResourceType , identifierKey int , identifierValue int , auditLog sql.AuditLog , tx * pg.Tx ) error {
74+ func (impl * QualifierMappingServiceImpl ) DeleteByIdentifierKeyAndValue (resourceType ResourceType , identifierKey int , identifierValue int , qualifierId int , auditLog sql.AuditLog , tx * pg.Tx ) error {
7475 return impl .qualifierMappingRepository .DeleteByResourceTypeIdentifierKeyAndValue (resourceType , identifierKey , identifierValue , auditLog , tx )
7576}
7677
77- func (impl QualifierMappingServiceImpl ) DeleteAllByIds (qualifierMappingIds []int , userId int32 , tx * pg.Tx ) error {
78+ func (impl * QualifierMappingServiceImpl ) DeleteAllByIds (qualifierMappingIds []int , userId int32 , tx * pg.Tx ) error {
7879 auditLog := sql.AuditLog {
7980 CreatedOn : time .Now (),
8081 CreatedBy : userId ,
@@ -84,30 +85,10 @@ func (impl QualifierMappingServiceImpl) DeleteAllByIds(qualifierMappingIds []int
8485 return impl .qualifierMappingRepository .DeleteAllByIds (qualifierMappingIds , auditLog , tx )
8586}
8687
87- func (impl QualifierMappingServiceImpl ) CreateMappingsForSelections (tx * pg.Tx , userId int32 , resourceMappingSelections []* ResourceMappingSelection ) ([]* ResourceMappingSelection , error ) {
88+ func (impl * QualifierMappingServiceImpl ) CreateMappingsForSelections (tx * pg.Tx , userId int32 , resourceMappingSelections []* ResourceMappingSelection ) ([]* ResourceMappingSelection , error ) {
8889
89- resourceKeyMap := impl .devtronResourceSearchableKeyService .GetAllSearchableKeyNameIdMap ()
90-
91- parentMappings := make ([]* QualifierMapping , 0 )
92- childrenMappings := make ([]* QualifierMapping , 0 )
93- parentMappingsMap := make (map [string ]* QualifierMapping )
94-
95- mappingsToSelection := make (map [* QualifierMapping ]* ResourceMappingSelection )
96- for _ , selection := range resourceMappingSelections {
97-
98- var parent * QualifierMapping
99- children := make ([]* QualifierMapping , 0 )
100- if selection .QualifierSelector .isCompound () {
101- parent , children = GetQualifierMappingsForCompoundQualifier (selection , resourceKeyMap , userId )
102- parentMappingsMap [parent .CompositeKey ] = parent
103- } else {
104- intValue , stringValue := GetValuesFromSelectionIdentifier (selection .QualifierSelector , selection .SelectionIdentifier )
105- parent = selection .toResourceMapping (selection .QualifierSelector , resourceKeyMap , intValue , stringValue , "" , userId )
106- }
107- mappingsToSelection [parent ] = selection
108- parentMappings = append (parentMappings , parent )
109- childrenMappings = append (childrenMappings , children ... )
110- }
90+ parentMappings , childrenMappings , mappingsToSelection , parentMappingsMap := getParentAndChildrenMappingForCreation (resourceMappingSelections ,
91+ impl .devtronResourceSearchableKeyService .GetAllSearchableKeyNameIdMap (), userId )
11192
11293 if len (parentMappings ) > 0 {
11394 _ , err := impl .qualifierMappingRepository .CreateQualifierMappings (parentMappings , tx )
@@ -140,7 +121,33 @@ func (impl QualifierMappingServiceImpl) CreateMappingsForSelections(tx *pg.Tx, u
140121 return maps .Values (mappingsToSelection ), nil
141122}
142123
143- func (impl QualifierMappingServiceImpl ) CreateMappings (tx * pg.Tx , userId int32 , resourceType ResourceType , resourceIds []int , qualifierSelector QualifierSelector , selectionIdentifiers []* SelectionIdentifier ) error {
124+ func getParentAndChildrenMappingForCreation (resourceMappingSelections []* ResourceMappingSelection , resourceKeyMap map [bean.DevtronResourceSearchableKeyName ]int ,
125+ userId int32 ) ([]* QualifierMapping , []* QualifierMapping , map [* QualifierMapping ]* ResourceMappingSelection , map [string ]* QualifierMapping ) {
126+
127+ parentMappings := make ([]* QualifierMapping , 0 )
128+ childrenMappings := make ([]* QualifierMapping , 0 )
129+ parentMappingsMap := make (map [string ]* QualifierMapping )
130+
131+ mappingsToSelection := make (map [* QualifierMapping ]* ResourceMappingSelection )
132+ for _ , selection := range resourceMappingSelections {
133+
134+ var parent * QualifierMapping
135+ children := make ([]* QualifierMapping , 0 )
136+ if selection .QualifierSelector .isCompound () {
137+ parent , children = GetQualifierMappingsForCompoundQualifier (selection , resourceKeyMap , userId )
138+ parentMappingsMap [parent .CompositeKey ] = parent
139+ } else {
140+ intValue , stringValue := GetValuesFromSelectionIdentifier (selection .QualifierSelector , selection .SelectionIdentifier )
141+ parent = selection .toResourceMapping (selection .QualifierSelector , resourceKeyMap , intValue , stringValue , "" , userId )
142+ }
143+ mappingsToSelection [parent ] = selection
144+ parentMappings = append (parentMappings , parent )
145+ childrenMappings = append (childrenMappings , children ... )
146+ }
147+ return parentMappings , childrenMappings , mappingsToSelection , parentMappingsMap
148+ }
149+
150+ func (impl * QualifierMappingServiceImpl ) CreateMappings (tx * pg.Tx , userId int32 , resourceType ResourceType , resourceIds []int , qualifierSelector QualifierSelector , selectionIdentifiers []* SelectionIdentifier ) error {
144151 mappings := make ([]* ResourceMappingSelection , 0 )
145152 for _ , id := range resourceIds {
146153 for _ , selectionIdentifier := range selectionIdentifiers {
@@ -157,7 +164,7 @@ func (impl QualifierMappingServiceImpl) CreateMappings(tx *pg.Tx, userId int32,
157164 return err
158165}
159166
160- func ( impl * QualifierMappingServiceImpl ) filterAndGroupMappings (mappings []* QualifierMapping , selector QualifierSelector , composites mapset.Set ) [][]* QualifierMapping {
167+ func filterAndGroupMappings (mappings []* QualifierMapping , selector QualifierSelector , composites mapset.Set ) [][]* QualifierMapping {
161168
162169 numQualifiers := GetNumOfChildQualifiers (selector .toQualifier ())
163170 parentIdToChildMappings := make (map [int ][]* QualifierMapping )
@@ -197,7 +204,7 @@ func (impl *QualifierMappingServiceImpl) filterAndGroupMappings(mappings []*Qual
197204 return groupedMappings
198205}
199206
200- func (impl QualifierMappingServiceImpl ) getAppEnvIdentifierFromGroup (group []* QualifierMapping ) * SelectionIdentifier {
207+ func (impl * QualifierMappingServiceImpl ) getAppEnvIdentifierFromGroup (group []* QualifierMapping ) * SelectionIdentifier {
201208 resourceKeyToName := impl .devtronResourceSearchableKeyService .GetAllSearchableKeyIdNameMap ()
202209 var appId , envId int
203210 var appName , envName string
@@ -213,7 +220,7 @@ func (impl QualifierMappingServiceImpl) getAppEnvIdentifierFromGroup(group []*Qu
213220 return getSelectionIdentifierForAppEnv (appId , envId , getIdentifierNamesForAppEnv (envName , appName ))
214221}
215222
216- func (impl QualifierMappingServiceImpl ) getSelectionIdentifierForAppEnvSelector (mappingGroups [][]* QualifierMapping ) map [int ][]* SelectionIdentifier {
223+ func (impl * QualifierMappingServiceImpl ) getSelectionIdentifierForAppEnvSelector (mappingGroups [][]* QualifierMapping ) map [int ][]* SelectionIdentifier {
217224
218225 resourceIdToIdentifier := make (map [int ][]* SelectionIdentifier )
219226 for _ , group := range mappingGroups {
@@ -229,7 +236,7 @@ func (impl QualifierMappingServiceImpl) getSelectionIdentifierForAppEnvSelector(
229236 return resourceIdToIdentifier
230237}
231238
232- func (impl QualifierMappingServiceImpl ) DeleteResourceMappingsForScopes (tx * pg.Tx , userId int32 , resourceType ResourceType , qualifierSelector QualifierSelector , scopes []* SelectionIdentifier ) error {
239+ func (impl * QualifierMappingServiceImpl ) DeleteResourceMappingsForScopes (tx * pg.Tx , userId int32 , resourceType ResourceType , qualifierSelector QualifierSelector , scopes []* SelectionIdentifier ) error {
233240 if qualifierSelector != ApplicationEnvironmentSelector {
234241 return fmt .Errorf ("selector currently not implemented" )
235242 }
@@ -256,7 +263,7 @@ func (impl QualifierMappingServiceImpl) DeleteResourceMappingsForScopes(tx *pg.T
256263
257264}
258265
259- func (impl QualifierMappingServiceImpl ) GetResourceMappingsForSelections (resourceType ResourceType , qualifierSelector QualifierSelector , selectionIdentifiers []* SelectionIdentifier ) ([]ResourceQualifierMappings , error ) {
266+ func (impl * QualifierMappingServiceImpl ) GetResourceMappingsForSelections (resourceType ResourceType , qualifierSelector QualifierSelector , selectionIdentifiers []* SelectionIdentifier ) ([]ResourceQualifierMappings , error ) {
260267 if qualifierSelector != ApplicationEnvironmentSelector {
261268 return nil , fmt .Errorf ("selector currently not implemented" )
262269 }
@@ -278,7 +285,7 @@ func (impl QualifierMappingServiceImpl) GetResourceMappingsForSelections(resourc
278285
279286 return impl .processMappings (resourceType , mappings , qualifierSelector , getCompositeStringsAppEnvSelection (selectionIdentifiers ))
280287}
281- func (impl QualifierMappingServiceImpl ) GetResourceMappingsForResources (resourceType ResourceType , resourceIds []int , qualifierSelector QualifierSelector ) ([]ResourceQualifierMappings , error ) {
288+ func (impl * QualifierMappingServiceImpl ) GetResourceMappingsForResources (resourceType ResourceType , resourceIds []int , qualifierSelector QualifierSelector ) ([]ResourceQualifierMappings , error ) {
282289 mappings , err := impl .qualifierMappingRepository .GetMappingsByResourceTypeAndIdsAndQualifierId (resourceType , resourceIds , int (qualifierSelector .toQualifier ()))
283290 if err != nil {
284291 return nil , err
@@ -287,8 +294,8 @@ func (impl QualifierMappingServiceImpl) GetResourceMappingsForResources(resource
287294 return impl .processMappings (resourceType , mappings , qualifierSelector , mapset .NewSet ())
288295}
289296
290- func (impl QualifierMappingServiceImpl ) processMappings (resourceType ResourceType , mappings []* QualifierMapping , qualifierSelector QualifierSelector , composites mapset.Set ) ([]ResourceQualifierMappings , error ) {
291- groups := impl . filterAndGroupMappings (mappings , qualifierSelector , composites )
297+ func (impl * QualifierMappingServiceImpl ) processMappings (resourceType ResourceType , mappings []* QualifierMapping , qualifierSelector QualifierSelector , composites mapset.Set ) ([]ResourceQualifierMappings , error ) {
298+ groups := filterAndGroupMappings (mappings , qualifierSelector , composites )
292299 if qualifierSelector != ApplicationEnvironmentSelector {
293300 return nil , fmt .Errorf ("selector currently not implemented" )
294301 }
0 commit comments