-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
74 lines (63 loc) · 1.76 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
package autoscaling
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
)
// Autoscaling represents the structure of a series
// of AWS autoscaling group parameters to trigger
// a dimension change
type Autoscaling struct {
AutoScalingGroupName string
PolicyName string
Region string
}
// Client type is an Autoscaling group client
type Client struct {
*autoscaling.AutoScaling
}
// New is the Package constructor that initialises
// the Autoscaling config
func New(asgName, policyName, region string) (*Autoscaling, error) {
config, err := checkConfig(asgName, policyName, region)
if err != nil {
return nil, fmt.Errorf("Initialisation error: %v", err)
}
return config, nil
}
// NewClient creates an Autoscaling client
func (asg *Autoscaling) NewClient() (*Client, error) {
var svc Client
sess, err := newSession(asg.Region)
if err != nil {
return nil, fmt.Errorf("Cannot open a new AWS session: %v", err)
}
svc.AutoScaling = autoscaling.New(sess)
return &svc, nil
}
func checkConfig(asgName, policyName, region string) (*Autoscaling, error) {
switch {
case asgName == "":
return nil, errors.New("The autoscaling group name cannot be empty")
case policyName == "":
return nil, errors.New("The autoscaling policy cannot be empty")
case region == "":
return nil, errors.New("Region cannot be empty")
}
return &Autoscaling{
AutoScalingGroupName: asgName,
PolicyName: policyName,
Region: region,
}, nil
}
func newSession(region string) (*session.Session, error) {
session, err := session.NewSession(&aws.Config{
Region: aws.String(region),
})
if err != nil {
return nil, err
}
return session, nil
}