github.com/Axway/agent-sdk@v1.1.101/pkg/migrate/migration.go (about) 1 package migrate 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/Axway/agent-sdk/pkg/api" 8 v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 9 defs "github.com/Axway/agent-sdk/pkg/apic/definitions" 10 "github.com/Axway/agent-sdk/pkg/config" 11 "github.com/Axway/agent-sdk/pkg/util" 12 ) 13 14 // migration - used for migrating resources 15 type migration struct { 16 client client 17 cfg config.CentralConfig 18 } 19 20 // updateRI updates the resource, and the sub resource 21 func (m *migration) updateRI(apiInterface v1.Interface) error { 22 ri, _ := apiInterface.AsInstance() 23 _, err := m.client.UpdateResourceInstance(ri) 24 if err != nil { 25 return err 26 } 27 28 return m.createSubResource(ri) 29 } 30 31 func (m *migration) createSubResource(ri *v1.ResourceInstance) error { 32 subResources := map[string]interface{}{ 33 defs.XAgentDetails: ri.SubResources[defs.XAgentDetails], 34 } 35 return m.client.CreateSubResource(ri.ResourceMeta, subResources) 36 } 37 38 func (m *migration) getRI(url string) (*v1.ResourceInstance, error) { 39 response, err := m.client.ExecuteAPI(api.GET, url, nil, nil) 40 if err != nil { 41 return nil, fmt.Errorf("error while retrieving ResourceInstance: %s", err) 42 } 43 44 resourceInstance := &v1.ResourceInstance{} 45 err = json.Unmarshal(response, &resourceInstance) 46 return resourceInstance, err 47 } 48 49 func (m *migration) getAllRI(url string, q map[string]string) ([]*v1.ResourceInstance, error) { 50 resources, err := m.client.GetAPIV1ResourceInstances(q, url) 51 if err != nil { 52 return nil, fmt.Errorf("error while retrieving all ResourceInstances: %s", err) 53 } 54 return resources, nil 55 } 56 57 func queryFuncByMetadataID(id string) string { 58 return fmt.Sprintf("metadata.references.id==%s", id) 59 } 60 61 func isMigrationCompleted(h v1.Interface, migrationKey string) bool { 62 details := util.GetAgentDetails(h) 63 if len(details) > 0 { 64 completed := details[migrationKey] 65 if completed == defs.MigrationCompleted { 66 return true 67 } 68 } 69 return false 70 }