-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecuritygroup.go
45 lines (40 loc) · 980 Bytes
/
securitygroup.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
package main
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
func getEc2Client() *ec2.EC2 {
region := "ap-northeast-1"
s, err := session.NewSession()
if err != nil {
fmt.Println(err)
return nil
}
return ec2.New(s, &aws.Config{Region: ®ion})
}
func getSecurityGroupList() []*ec2.SecurityGroup {
svc := getEc2Client()
result, err := svc.DescribeSecurityGroups(
&ec2.DescribeSecurityGroupsInput{},
)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case "InvalidGroupId.Malformed":
fallthrough
case "InvalidGroup.NotFound":
exitErrorf("%s.", aerr.Message())
}
}
exitErrorf("Unable to get descriptions for security groups, %v", err)
}
return result.SecurityGroups
}