Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIGOV-SB001 - more revert for sonar fixes #555

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 28 additions & 39 deletions pkg/agent/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
55 changes: 24 additions & 31 deletions pkg/migrate/marketplacemigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down