-
Notifications
You must be signed in to change notification settings - Fork 106
/
initialize.go
155 lines (130 loc) · 3.56 KB
/
initialize.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
147
148
149
150
151
152
153
154
155
package vmmanagers
import (
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/pivotal-cf/om/vmlifecycle/runner"
"reflect"
"time"
)
type vmManager interface {
CreateVM() (status Status, state StateInfo, err error)
DeleteVM() error
}
//go:generate counterfeiter -o ./fakes/delete_vm.go --fake-name DeleteVMService . DeleteVMService
type DeleteVMService interface {
DeleteVM() error
}
//go:generate counterfeiter -o ./fakes/create_vm.go --fake-name CreateVMService . CreateVMService
type CreateVMService interface {
CreateVM() (Status, StateInfo, error)
}
func NewDeleteVMManager(config *OpsmanConfigFilePayload, image string, state StateInfo, outWriter, errWriter io.Writer) (DeleteVMService, error) {
return initializeVMManager(config, image, state, outWriter, errWriter)
}
func NewCreateVMManager(config *OpsmanConfigFilePayload, image string, state StateInfo, outWriter, errWriter io.Writer) (CreateVMService, error) {
return initializeVMManager(config, image, state, outWriter, errWriter)
}
func initializeVMManager(config *OpsmanConfigFilePayload, image string, state StateInfo, outWriter, errWriter io.Writer) (vmManager, error) {
if err := ValidateOpsManConfig(config); err != nil {
return nil, err
}
if config.OpsmanConfig.Vsphere != nil {
_, _ = outWriter.Write([]byte(fmt.Sprintln("Using vSphere...")))
govcCLI, err := runner.NewRunner("govc", outWriter, errWriter)
if err != nil {
return nil, err
}
return NewVsphereVMManager(
config,
image,
state,
govcCLI,
), nil
}
if config.OpsmanConfig.GCP != nil {
_, _ = outWriter.Write([]byte(fmt.Sprintln("Using gcp...")))
gcpCLI, err := runner.NewRunner("gcloud", outWriter, errWriter)
if err != nil {
return nil, err
}
return NewGcpVMManager(
config,
image,
state,
gcpCLI,
), nil
}
if config.OpsmanConfig.AWS != nil {
_, _ = outWriter.Write([]byte(fmt.Sprintln("Using aws...")))
awsCLI, err := runner.NewRunner("aws", outWriter, errWriter)
if err != nil {
return nil, err
}
return NewAWSVMManager(
os.Stdout,
os.Stderr,
config,
image,
state,
awsCLI,
5*time.Second,
), nil
}
if config.OpsmanConfig.Azure != nil {
_, _ = outWriter.Write([]byte(fmt.Sprintln("Using azure...")))
azureCLI, err := runner.NewRunner("az", outWriter, errWriter)
if err != nil {
return nil, err
}
return NewAzureVMManager(
os.Stdout,
os.Stderr,
config,
image,
state,
azureCLI,
5*time.Second,
), nil
}
if config.OpsmanConfig.Openstack != nil {
_, _ = outWriter.Write([]byte(fmt.Sprintln("Using openstack...")))
openstackCLI, err := runner.NewRunner("openstack", outWriter, errWriter)
if err != nil {
return nil, err
}
return NewOpenstackVMManager(
config,
image,
state,
openstackCLI,
), nil
}
return nil, errors.New("unexpected error")
}
func ValidateOpsManConfig(config *OpsmanConfigFilePayload) error {
if len(config.OpsmanConfig.Unknown) > 0 {
var unknownIaas []string
for key := range config.OpsmanConfig.Unknown {
unknownIaas = append(unknownIaas, key)
}
return fmt.Errorf("unknown iaas: %v, please refer to documentation: %s", strings.Join(unknownIaas, ", "), "github.com/pivotal/platform-automation/docstest")
}
fields := reflect.ValueOf(&config.OpsmanConfig).Elem()
count := 0
for i := 0; i < fields.NumField(); i++ {
f := fields.Field(i)
if !f.IsNil() {
count += 1
}
}
if count > 1 {
return errors.New("more than one iaas matched, only one in config allowed")
}
if count == 0 {
return errors.New("no iaas configuration provided, please refer to documentation")
}
return nil
}