-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.go
94 lines (83 loc) · 3.42 KB
/
import.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
/*
* 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 cc
import (
"context"
"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"
"github.com/vmware-tanzu/service-instance-migrator-for-cloud-foundry/pkg/exec"
"github.com/vmware-tanzu/service-instance-migrator-for-cloud-foundry/pkg/flow"
"github.com/vmware-tanzu/service-instance-migrator-for-cloud-foundry/pkg/log"
)
func NewImportSequence(org, space string, service Service, instance *cf.ServiceInstance, encryptionKey string, executor exec.Executor, manager config.OpsManager, controller *DatabaseConfig) flow.Flow {
return flow.ProgressBarSequence(
fmt.Sprintf("Importing %s", instance.Name),
flow.StepWithProgressBar(SetCloudControllerDatabaseCredentials(executor, controller, manager), flow.WithDisplay("Setting cc credentials")),
flow.StepWithProgressBar(Import(org, space, service, instance, encryptionKey), flow.WithDisplay("Creating service instance")),
)
}
func Import(org, space string, service Service, instance *cf.ServiceInstance, encryptionKey string) flow.StepFunc {
return func(ctx context.Context, c interface{}, dryRun bool) (flow.Result, error) {
exists, err := service.ServiceInstanceExists(org, space, instance.Name)
if err != nil {
return nil, err
}
if exists {
return nil, fmt.Errorf("service instance name %q already exists", instance.Name)
}
if encryptionKey == "" {
return nil, fmt.Errorf("ccdb encryption key is not set")
}
log.Debugf("Creating service instance %q in ccdb...", instance.GUID)
err = service.Create(org, space, instance, encryptionKey)
if err != nil {
return nil, err
}
if len(instance.ServiceBindings) > 0 {
for _, binding := range instance.ServiceBindings {
if appName, ok := instance.Apps[binding.Guid]; ok {
log.Debugf("Creating placeholder app %q in ccdb...", appName)
appGuid, err := service.CreateApp(org, space, appName)
if err != nil {
// just log the error and keep going, so we can finish the migration without bindings
log.Errorf("Could not create service binding: %q for app: %q: %s", binding.Name, appName, err.Error())
continue
}
err = service.CreateServiceBinding(&binding, appGuid, encryptionKey)
if err != nil {
return nil, err
}
}
}
}
if cfg, ok := config.FromContext(ctx); ok {
if cfg.IgnoreServiceKeys {
return instance, nil
}
}
var skErr error
for _, key := range instance.ServiceKeys {
err := service.CreateServiceKey(*instance, key)
if err != nil {
if skErr != nil {
skErr = fmt.Errorf("failed to create service key %q, for service instance %q: %w", key, instance.Name, skErr)
} else {
skErr = fmt.Errorf("failed to create service key %q, for service instance %q: %w", key, instance.Name, err)
}
}
}
return instance, skErr
}
}