/
config.go
96 lines (81 loc) · 2.32 KB
/
config.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
package config
import (
"net/url"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
// Config contains the values about the different parsing mechanisms for
// Config names
type Config struct {
automaticENIConfig bool
eniConfigName string
eniConfigTagName string
awsSession *session.Session
}
// New will return and instance of the ENIConfig object
func New(automaticENIConfig bool, eniconfigName string, eniconfigTagName string, awsSession *session.Session) Config {
return Config{
automaticENIConfig: automaticENIConfig,
eniConfigName: eniconfigName,
eniConfigTagName: eniconfigTagName,
awsSession: awsSession,
}
}
// GetName will return the name of the Config using either the name in the
// base object OR will load it via the AWS EC2 DescribeTags API.
func (c Config) GetName(providerID string) (string, error) {
if c.automaticENIConfig == false {
return c.eniConfigName, nil
}
instanceID, err := getInstanceIDFromProviderID(providerID)
if err != nil {
return "", err
}
return c.GetConfigTag(instanceID)
}
// GetConfigTag allows you to get the ENIConfig name from the EC2 instances
// tag, customize this by using the ENIConfig.eniConfigTagName
func (c Config) GetConfigTag(instanceID string) (name string, err error) {
svc := ec2.New(c.awsSession)
filterID := &ec2.Filter{}
filterID.SetName("resource-id")
filterID.SetValues([]*string{aws.String(instanceID)})
input := &ec2.DescribeTagsInput{}
input.SetFilters([]*ec2.Filter{filterID})
output, err := svc.DescribeTags(input)
if err != nil {
return "", err
}
for _, tag := range output.Tags {
if *tag.Key == c.eniConfigTagName {
name = *tag.Value
break
}
}
return name, nil
}
// Below are custom functions to parse the InstanceID and AZ from the ProviderID
// This can probably be refactored
func getInstanceIDFromProviderID(providerID string) (string, error) {
u, err := url.Parse(providerID)
if err != nil {
return "", err
}
_, instanceID := parsePath(u)
return instanceID, nil
}
func parsePath(u *url.URL) (az, instanceID string) {
trimmed := trimLeftChar(u.Path)
parts := strings.Split(trimmed, "/")
return parts[0], parts[1]
}
func trimLeftChar(s string) string {
for i := range s {
if i > 0 {
return s[i:]
}
}
return s[:0]
}