forked from cloudfoundry/bosh-bootloader
/
aws.go
135 lines (107 loc) · 3.35 KB
/
aws.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
package actors
import (
"fmt"
"os"
"strings"
"github.com/cloudfoundry/bosh-bootloader/application"
"github.com/cloudfoundry/bosh-bootloader/aws"
"github.com/cloudfoundry/bosh-bootloader/storage"
. "github.com/onsi/gomega"
awslib "github.com/aws/aws-sdk-go/aws"
acceptance "github.com/cloudfoundry/bosh-bootloader/acceptance-tests"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/elbv2"
)
type AWS struct {
client aws.Client
elbClient *elb.ELB
elbV2Client *elbv2.ELBV2
}
func NewAWS(c acceptance.Config) AWS {
creds := storage.AWS{
AccessKeyID: c.AWSAccessKeyID,
SecretAccessKey: c.AWSSecretAccessKey,
Region: c.AWSRegion,
}
client := aws.NewClient(creds, application.NewLogger(os.Stdout))
elbConfig := &awslib.Config{
Credentials: credentials.NewStaticCredentials(creds.AccessKeyID, creds.SecretAccessKey, ""),
Region: awslib.String(creds.Region),
}
return AWS{
client: client,
elbClient: elb.New(session.New(elbConfig)),
elbV2Client: elbv2.New(session.New(elbConfig)),
}
}
func (a AWS) Instances(envID string) []string {
instances, err := a.client.Instances(envID)
Expect(err).NotTo(HaveOccurred())
return instances
}
func (a AWS) LoadBalancers(vpcName string) []string {
var loadBalancerNames []string
vpcID := a.GetVPC(vpcName)
loadBalancerOutput, err := a.elbClient.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{})
Expect(err).NotTo(HaveOccurred())
for _, lbDescription := range loadBalancerOutput.LoadBalancerDescriptions {
if *lbDescription.VPCId == *vpcID {
loadBalancerNames = append(loadBalancerNames, *lbDescription.LoadBalancerName)
}
}
return loadBalancerNames
}
func (a AWS) NetworkLoadBalancers(vpcName string) []string {
output, err := a.elbV2Client.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{})
Expect(err).NotTo(HaveOccurred())
vpcId := a.GetVPC(vpcName)
lbNames := []string{}
for _, lb := range output.LoadBalancers {
if *lb.VpcId == *vpcId {
lbNames = append(lbNames, *lb.LoadBalancerName)
}
}
return lbNames
}
func (a AWS) GetSSLCertificateNameFromLBs(envID string) string {
var retryCount int
retry:
loadBalancerOutput, err := a.elbClient.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{})
Expect(err).NotTo(HaveOccurred())
vpcID := a.GetVPC(fmt.Sprintf("%s-vpc", envID))
var certificateName string
for _, lbDescription := range loadBalancerOutput.LoadBalancerDescriptions {
if *lbDescription.VPCId == *vpcID {
for _, ld := range lbDescription.ListenerDescriptions {
if int(*ld.Listener.LoadBalancerPort) == 443 {
certificateArn := ld.Listener.SSLCertificateId
certificateArnParts := strings.Split(awslib.StringValue(certificateArn), "/")
if len(certificateArnParts) != 2 && retryCount <= 5 {
retryCount++
goto retry
}
certificateName = certificateArnParts[1]
Expect(certificateName).NotTo(BeEmpty())
return certificateName
}
}
}
}
return ""
}
func (a AWS) GetVPC(vpcName string) *string {
vpc, err := a.client.GetVPC(vpcName)
Expect(err).NotTo(HaveOccurred())
return vpc
}
func (a AWS) NetworkHasBOSHDirector(envID string) bool {
instances := a.Instances(envID)
for _, instance := range instances {
if instance == "bosh/0" {
return true
}
}
return false
}