@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "encoding/json"
2222 "fmt"
23+ "strings"
2324
2425 capabilitiesv1beta1 "github.com/3scale/3scale-operator/apis/capabilities/v1beta1"
2526 controllerhelper "github.com/3scale/3scale-operator/pkg/controller/helper"
@@ -168,7 +169,7 @@ func (r *ApplicationAuthReconciler) Reconcile(ctx context.Context, req ctrl.Requ
168169 return reconcileStatus (r .BaseReconciler , applicationAuth , err , reqLogger )
169170 }
170171
171- err = syncApplicationAuth (* developerAccount .Status .ID , * application .Status .ID , * authMode , * authSecret , threescaleAPIClient )
172+ err = syncApplicationAuth (* developerAccount .Status .ID , * application .Status .ID , * authMode , * authSecret , threescaleAPIClient , reqLogger )
172173 if err != nil {
173174 return reconcileStatus (r .BaseReconciler , applicationAuth , err , reqLogger )
174175 }
@@ -190,6 +191,7 @@ func syncApplicationAuth(
190191 authMode string ,
191192 authSecret AuthSecret ,
192193 threescaleClient * threescaleapi.ThreeScaleClient ,
194+ logger logr.Logger ,
193195) error {
194196 switch authMode {
195197 case "1" :
@@ -209,27 +211,42 @@ func syncApplicationAuth(
209211 }
210212 }
211213 case "2" :
214+ desiredKeys := strings .Split (authSecret .ApplicationKey , "," )
215+ if len (desiredKeys ) > 5 {
216+ return fmt .Errorf ("secret contains more than 5 application_key" )
217+ }
218+
212219 // get the existing value from the portal
213- existingKeys , err := threescaleClient .ApplicationKeys (developerAccountID , applicationID )
220+ applicationKeys , err := threescaleClient .ApplicationKeys (developerAccountID , applicationID )
214221 if err != nil {
215222 return err
216223 }
217224
218- // pre-existing keys
219- if len (existingKeys ) > 0 {
220- // Nothing to do, return early
221- if existingKeys [0 ].Value == authSecret .ApplicationKey {
222- return nil
223- }
225+ existingKeys := make ([]string , 0 , len (applicationKeys ))
226+ for _ , key := range applicationKeys {
227+ existingKeys = append (existingKeys , key .Value )
228+ }
224229
225- // if the key is not match, delete it
226- if err := threescaleClient .DeleteApplicationKey (developerAccountID , applicationID , existingKeys [0 ].Value ); err != nil {
227- return err
230+ // delete existing and not desired
231+ notDesiredExistingKeys := helper .ArrayStringDifference (existingKeys , desiredKeys )
232+ logger .V (1 ).Info ("syncApplicationAuth" , "notDesiredExistingKeys" , notDesiredExistingKeys )
233+ for _ , key := range notDesiredExistingKeys {
234+ // key is expected to exist
235+ // notDesiredExistingKeys is a subset of the existingMap key set
236+ if err := threescaleClient .DeleteApplicationKey (developerAccountID , applicationID , key ); err != nil {
237+ return fmt .Errorf ("error sync applicationAuth for developerAccountID: %d, applicationID: %d, error: %w" , developerAccountID , applicationID , err )
228238 }
229239 }
230240
231- if _ , err := threescaleClient .CreateApplicationKey (developerAccountID , applicationID , authSecret .ApplicationKey ); err != nil {
232- return err
241+ // Create not existing and desired
242+ desiredNewKeys := helper .ArrayStringDifference (desiredKeys , existingKeys )
243+ logger .V (1 ).Info ("syncApplicationPlans" , "desiredNewKeys" , desiredNewKeys )
244+ for _ , key := range desiredNewKeys {
245+ // key is expected to exist
246+ // desiredNewKeys is a subset of the Spec.ApplicationPlans map key set
247+ if _ , err := threescaleClient .CreateApplicationKey (developerAccountID , applicationID , key ); err != nil {
248+ return fmt .Errorf ("error sync applicationAuth for developerAccountID: %d, applicationID: %d, error: %w" , developerAccountID , applicationID , err )
249+ }
233250 }
234251 }
235252
0 commit comments