/
aws_client.go
87 lines (78 loc) · 2.53 KB
/
aws_client.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
package bosh
import (
"fmt"
"io"
"net"
"github.com/lib/pq"
"golang.org/x/crypto/ssh"
"github.com/EngineerBetter/control-tower/bosh/internal/boshcli"
"github.com/EngineerBetter/control-tower/bosh/internal/workingdir"
"github.com/EngineerBetter/control-tower/config"
"github.com/EngineerBetter/control-tower/iaas"
"github.com/EngineerBetter/control-tower/terraform"
)
//AWSClient is an AWS specific implementation of IClient
type AWSClient struct {
config config.ConfigView
outputs terraform.Outputs
workingdir workingdir.IClient
db Opener
stdout io.Writer
stderr io.Writer
provider iaas.Provider
boshCLI boshcli.ICLI
versionFile []byte
}
//NewAWSClient returns a AWS specific implementation of IClient
func NewAWSClient(config config.ConfigView, outputs terraform.Outputs, workingdir workingdir.IClient, stdout, stderr io.Writer, provider iaas.Provider, boshCLI boshcli.ICLI, versionFile []byte) (IClient, error) {
directorPublicIP, err := outputs.Get("DirectorPublicIP")
if err != nil {
return nil, fmt.Errorf("failed to get DirectorPublicIP from terraform outputs: [%v]", err)
}
addr := net.JoinHostPort(directorPublicIP, "22")
key, err := ssh.ParsePrivateKey([]byte(config.GetPrivateKey()))
if err != nil {
return nil, fmt.Errorf("failed to parse private key for bosh: [%v]", err)
}
conf := &ssh.ClientConfig{
User: "vcap",
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
}
var boshDBAddress, boshDBPort string
boshDBAddress, err = outputs.Get("BoshDBAddress")
if err != nil {
return nil, fmt.Errorf("failed to get BoshDBAddress from terraform outputs: [%v]", err)
}
boshDBPort, err = outputs.Get("BoshDBPort")
if err != nil {
return nil, fmt.Errorf("failed to get BoshDBPort from terraform outputs: [%v]", err)
}
db, err := newProxyOpener(addr, conf, &pq.Driver{},
fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=require",
config.GetRDSUsername(),
config.GetRDSPassword(),
boshDBAddress,
boshDBPort,
config.GetRDSDefaultDatabaseName(),
),
)
if err != nil {
return nil, fmt.Errorf("failed to create db proxyOpener: [%v]", err)
}
return &AWSClient{
config: config,
outputs: outputs,
workingdir: workingdir,
db: db,
stdout: stdout,
stderr: stderr,
provider: provider,
boshCLI: boshCLI,
versionFile: versionFile,
}, nil
}
//Cleanup is AWS specific implementation of Cleanup
func (client *AWSClient) Cleanup() error {
return client.workingdir.Cleanup()
}