-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
97 lines (61 loc) · 1.48 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
package resources
import (
"fmt"
"log"
"io/ioutil"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/aws/credentials"
"gopkg.in/yaml.v2"
)
type AWSClient struct {
ec2conn *ec2.EC2
region string
}
type Config struct {
Awsconf string
Profile string
Region string
Cfgfile string
Call map[string] func()
Conf yamlConfig
}
type yamlConfig struct {
Resources map[string] struct {
Delete_older_than int16
Whitelist []string
}
}
func (c *Config) Connect() interface{} {
c.readConf()
var client AWSClient
awsConfig := new(aws.Config)
if len(c.Profile)>0 {
awsConfig = &aws.Config{
Credentials: credentials.NewSharedCredentials(c.Awsconf, fmt.Sprintf("profile %s", c.Profile)),
Region: aws.String(c.Region),
MaxRetries: aws.Int(3),
}
} else {
// use instance role
awsConfig = &aws.Config{
Region: aws.String(c.Region),
}
}
sess := session.New(awsConfig)
client.ec2conn = ec2.New(sess)
return &client
}
func (c *Config) readConf() {
configFile, _ := filepath.Abs(c.Cfgfile)
yamlConf, file_err := ioutil.ReadFile(configFile)
if (file_err != nil) {
log.Println("[ERROR] File does not exist: ", file_err)
}
yaml_err := yaml.Unmarshal(yamlConf, &c.Conf)
if (yaml_err != nil) {
panic(yaml_err)
}
}