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