From 9d50333e9aaa162d24cd6204b7916e62e5bf54cf Mon Sep 17 00:00:00 2001 From: sbolosan Date: Wed, 28 Sep 2022 09:16:54 -0700 Subject: [PATCH] APIGOV-SB001 - more revert for sonar fixes --- pkg/agent/provisioning.go | 67 ++++++++++++----------------- pkg/migrate/marketplacemigration.go | 55 +++++++++++------------ 2 files changed, 52 insertions(+), 70 deletions(-) diff --git a/pkg/agent/provisioning.go b/pkg/agent/provisioning.go index a42023241..c471b58a7 100644 --- a/pkg/agent/provisioning.go +++ b/pkg/agent/provisioning.go @@ -7,7 +7,6 @@ import ( "github.com/Axway/agent-sdk/pkg/apic/definitions" "github.com/Axway/agent-sdk/pkg/apic/provisioning" "github.com/Axway/agent-sdk/pkg/authz/oauth" - "github.com/Axway/agent-sdk/pkg/cache" "github.com/Axway/agent-sdk/pkg/config" "github.com/Axway/agent-sdk/pkg/migrate" "github.com/Axway/agent-sdk/pkg/util" @@ -52,10 +51,24 @@ func migrateMarketPlace(marketplaceMigration migrate.Migrator, ri *v1.ResourceIn agent.cacheManager.AddCredentialRequestDefinition(ri) } + apiSvcResources := make([]*v1.ResourceInstance, 0) + cache := agent.cacheManager.GetAPIServiceCache() - apiSvcResources := getAPISvcResources(cache) + for _, key := range cache.GetKeys() { + item, _ := cache.Get(key) + if item == nil { + continue + } + + svc, ok := item.(*v1.ResourceInstance) + if ok { + apiSvcResources = append(apiSvcResources, svc) + } + } + for _, svc := range apiSvcResources { + var err error mig := marketplaceMigration.(*migrate.MarketplaceMigration) alreadyMigrated := mig.InstanceAlreadyMigrated(svc) @@ -65,51 +78,27 @@ func migrateMarketPlace(marketplaceMigration migrate.Migrator, ri *v1.ResourceIn logger.Tracef("update apiserviceinstances with request definition %s: %s", ri.Kind, ri.Name) mig.UpdateService(svc) - err := setAgentDetails(svc, ri) + + // Mark marketplace migration completed here in provisioning + util.SetAgentDetailsKey(svc, definitions.MarketplaceMigration, definitions.MigrationCompleted) + ri, err = GetCentralClient().UpdateResourceInstance(svc) if err != nil { return nil, err } + //update sub resources + inst, err := svc.AsInstance() + if xagentdetails, found := inst.SubResources[definitions.XAgentDetails]; found && err == nil { + err = GetCentralClient().CreateSubResource(ri.ResourceMeta, map[string]interface{}{definitions.XAgentDetails: xagentdetails}) + if err != nil { + return nil, err + } + log.Debugf("updated x-agent-details with marketplace-migration: completed") + } } } return ri, nil } -func getAPISvcResources(cache cache.Cache) []*v1.ResourceInstance { - apiSvcResources := make([]*v1.ResourceInstance, 0) - for _, key := range cache.GetKeys() { - item, _ := cache.Get(key) - if item == nil { - continue - } - - svc, ok := item.(*v1.ResourceInstance) - if ok { - apiSvcResources = append(apiSvcResources, svc) - } - } - return apiSvcResources -} - -func setAgentDetails(svc *v1.ResourceInstance, ri *v1.ResourceInstance) error { - var err error - // Mark marketplace migration completed here in provisioning - util.SetAgentDetailsKey(svc, definitions.MarketplaceMigration, definitions.MigrationCompleted) - ri, err = GetCentralClient().UpdateResourceInstance(svc) - if err != nil { - return err - } - //update sub resources - inst, err := svc.AsInstance() - if xagentdetails, found := inst.SubResources[definitions.XAgentDetails]; found && err == nil { - err = GetCentralClient().CreateSubResource(ri.ResourceMeta, map[string]interface{}{definitions.XAgentDetails: xagentdetails}) - if err != nil { - return err - } - log.Debugf("updated x-agent-details with marketplace-migration: completed") - } - return nil -} - // createOrUpdateCredentialRequestDefinition - func createOrUpdateCredentialRequestDefinition(data *management.CredentialRequestDefinition) (*management.CredentialRequestDefinition, error) { ri, err := createOrUpdateDefinition(data, agent.marketplaceMigration) diff --git a/pkg/migrate/marketplacemigration.go b/pkg/migrate/marketplacemigration.go index fe349841e..b39353bc4 100644 --- a/pkg/migrate/marketplacemigration.go +++ b/pkg/migrate/marketplacemigration.go @@ -278,6 +278,8 @@ func (m *MarketplaceMigration) handleSvcInstance( // get oauth scopes oauthScopes := processor.GetOAuthScopes() + var updateRequestDefinition = false + // Check if ARD exists if apiSvcInst.Spec.AccessRequestDefinition == "" && len(oauthScopes) > 0 { // Only migrate resource with oauth scopes. Spec with type apiKey will be handled on startup @@ -296,44 +298,35 @@ func (m *MarketplaceMigration) handleSvcInstance( // Find only the known CRDs credentialRequestDefinitions := m.checkCredentialRequestDefinitions(credentialRequestPolicies) - m.updateRequestDefinition(credentialRequestDefinitions, ardRIName, revision, apiSvcInst, svcInstance, logger) - - } + if len(credentialRequestDefinitions) > 0 && !sortCompare(apiSvcInst.Spec.CredentialRequestDefinitions, credentialRequestDefinitions) { + logger.Debugf("adding the following credential request definitions %s to apiserviceinstance %s", credentialRequestDefinitions, apiSvcInst.Name) + updateRequestDefinition = true + } - return nil -} + existingARD, _ := m.cache.GetAccessRequestDefinitionByName(ardRIName) + if existingARD == nil { + ardRIName = "" + } else { + if apiSvcInst.Spec.AccessRequestDefinition == "" { + logger.Debugf("adding the following access request definition %s to apiserviceinstance %s", ardRIName, apiSvcInst.Name) + updateRequestDefinition = true + } + } -func (m *MarketplaceMigration) updateRequestDefinition(credentialRequestDefinitions []string, ardRIName string, revision *apiv1.ResourceInstance, - apiSvcInst *management.APIServiceInstance, svcInstance *apiv1.ResourceInstance, logger log.FieldLogger) error { - var updateRequestDefinition = false + // update apiserivceinstane spec with necessary request definitions + if updateRequestDefinition { + inInterface := m.newInstanceSpec(apiSvcInst.Spec.Endpoint, revision.Name, ardRIName, credentialRequestDefinitions) + svcInstance.Spec = inInterface - if len(credentialRequestDefinitions) > 0 && !sortCompare(apiSvcInst.Spec.CredentialRequestDefinitions, credentialRequestDefinitions) { - logger.Debugf("adding the following credential request definitions %s to apiserviceinstance %s", credentialRequestDefinitions, apiSvcInst.Name) - updateRequestDefinition = true - } + err = m.updateRI(svcInstance) + if err != nil { + return err + } - existingARD, _ := m.cache.GetAccessRequestDefinitionByName(ardRIName) - if existingARD == nil { - ardRIName = "" - } else { - if apiSvcInst.Spec.AccessRequestDefinition == "" { - logger.Debugf("adding the following access request definition %s to apiserviceinstance %s", ardRIName, apiSvcInst.Name) - updateRequestDefinition = true + logger.Debugf("migrated instance %s with the necessary request definitions", apiSvcInst.Name) } } - // update apiserivceinstane spec with necessary request definitions - if updateRequestDefinition { - inInterface := m.newInstanceSpec(apiSvcInst.Spec.Endpoint, revision.Name, ardRIName, credentialRequestDefinitions) - svcInstance.Spec = inInterface - - err := m.updateRI(svcInstance) - if err != nil { - return err - } - - logger.Debugf("migrated instance %s with the necessary request definitions", apiSvcInst.Name) - } return nil }