/
helper.go
146 lines (125 loc) · 3.44 KB
/
helper.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2022 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package migrate
import (
"fmt"
"github.com/vmware-tanzu/service-instance-migrator-for-cloud-foundry/pkg/cf"
"github.com/vmware-tanzu/service-instance-migrator-for-cloud-foundry/pkg/config"
"sync"
)
type (
Migrator int64
Service string
ServiceType string
)
const (
ECS Migrator = iota
MySQL
SQLServer
CredHub
)
const (
ECSBucketService Service = "ecs-bucket"
MySQLService Service = "p.mysql"
SQLServerService Service = "SQLServer"
CustomSQLServerService Service = "MSSQL-Broker"
CredHubService Service = "credhub"
)
const (
ManagedService ServiceType = "managed_service_instance"
UserProvidedService ServiceType = "user_provided_service_instance"
)
func (m Migrator) String() string {
switch m {
case ECS:
return "ecs"
case MySQL:
return "mysql"
case SQLServer:
return "sqlserver"
case CredHub:
return "credhub"
}
return "unknown"
}
func (m Migrator) HasCCDBConfig() bool {
switch m {
case ECS:
return true
case MySQL:
return false
case SQLServer:
return true
case CredHub:
return false
}
return false
}
func (s Service) String() string {
return string(s)
}
func (t ServiceType) String() string {
return string(t)
}
type MigratorHelper struct {
migrationReader config.MigrationReader
supportedMigrators map[Service]Migrator
mu sync.Mutex
}
func NewMigratorHelper(mr config.MigrationReader) *MigratorHelper {
return &MigratorHelper{
migrationReader: mr,
}
}
func (h *MigratorHelper) GetMigratorConfig(data interface{}, si *cf.ServiceInstance) (interface{}, error) {
cfg, err := h.migrationReader.GetMigration()
if err != nil {
return nil, err
}
var migratorType Migrator
var ok bool
if migratorType, ok = h.GetMigratorType(si.Service); !ok {
return nil, fmt.Errorf("service %q of type %q not supported", si.Service, si.Type)
}
return config.NewMapDecoder(data).Decode(*cfg, migratorType.String()), nil
}
func (h *MigratorHelper) GetMigratorType(service string) (Migrator, bool) {
h.mu.Lock()
defer h.mu.Unlock()
if len(h.supportedMigrators) == 0 {
h.loadSupportedMigrators()
}
// return the value used as a key to look up the supported migrator config
if m, ok := h.supportedMigrators[Service(service)]; ok {
return m, true
}
return 0, false
}
func (h *MigratorHelper) GetReader() config.MigrationReader {
return h.migrationReader
}
func (h *MigratorHelper) IsCCDBMigrator(service string) bool {
if migratorType, ok := h.GetMigratorType(service); ok {
return migratorType.HasCCDBConfig()
}
return false
}
func (h *MigratorHelper) loadSupportedMigrators() {
h.supportedMigrators = make(map[Service]Migrator)
h.supportedMigrators[CustomSQLServerService] = SQLServer
h.supportedMigrators[SQLServerService] = SQLServer
h.supportedMigrators[ECSBucketService] = ECS
h.supportedMigrators[MySQLService] = MySQL
h.supportedMigrators[CredHubService] = CredHub
}