github.com/Axway/agent-sdk@v1.1.101/pkg/migrate/ardmigration.go (about)

     1  package migrate
     2  
     3  import (
     4  	"context"
     5  
     6  	apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
     7  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
     8  	"github.com/Axway/agent-sdk/pkg/config"
     9  )
    10  
    11  // ArdMigration - used for migrating access request definitions
    12  type ArdMigration struct {
    13  	migration
    14  }
    15  
    16  // NewArdMigration creates a new ArdMigration
    17  func NewArdMigration(client client, cfg config.CentralConfig) *ArdMigration {
    18  	return &ArdMigration{
    19  		migration: migration{
    20  			client: client,
    21  			cfg:    cfg,
    22  		},
    23  	}
    24  }
    25  
    26  // Migrate checks an AccessRequestDefinition for the "scopes" key in the schema, and removes it if it is found.
    27  func (m *ArdMigration) Migrate(_ context.Context, ri *apiv1.ResourceInstance) (*apiv1.ResourceInstance, error) {
    28  	if ri.Kind != management.AccessRequestDefinitionGVK().Kind {
    29  		return ri, nil
    30  	}
    31  
    32  	ard := management.NewAccessRequestDefinition("", m.cfg.GetEnvironmentName())
    33  	err := ard.FromInstance(ri)
    34  	if err != nil {
    35  		return ri, err
    36  	}
    37  
    38  	scopes := m.getScopes(ard.Spec.Schema)
    39  	if scopes != nil {
    40  		res, err := m.client.UpdateResourceInstance(ard)
    41  		if err != nil {
    42  			return ri, err
    43  		}
    44  		ri = res
    45  	}
    46  
    47  	return ri, nil
    48  }
    49  
    50  func (m *ArdMigration) getScopes(schema map[string]interface{}) interface{} {
    51  	if properties, ok := schema["properties"]; ok {
    52  		if props, ok := properties.(map[string]interface{}); ok {
    53  			if scopes, ok := props["scopes"]; ok {
    54  				delete(props, "scopes")
    55  				return scopes
    56  			}
    57  		}
    58  	}
    59  	return nil
    60  }