-
Notifications
You must be signed in to change notification settings - Fork 149
/
ec2.go
107 lines (95 loc) · 3.13 KB
/
ec2.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
// Copyright 2019 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ec2
import (
"context"
"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/pkg/errors"
)
const (
maxRetries = 10
)
// EC2 is a wrapper around ec2.EC2 structs
type EC2 struct {
*ec2.EC2
DryRun bool
}
// NewEC2Client returns ec2 client struct.
func NewClient(ctx context.Context, awsConfig *aws.Config, region string) (*EC2, error) {
s, err := session.NewSession(awsConfig)
if err != nil {
return nil, errors.Wrap(err, "Failed to create session")
}
return &EC2{EC2: ec2.New(s, awsConfig.WithMaxRetries(maxRetries).WithRegion(region).WithCredentials(awsConfig.Credentials))}, nil
}
func (e EC2) DescribeSecurityGroup(ctx context.Context, groupName string) (*ec2.DescribeSecurityGroupsOutput, error) {
sgi := &ec2.DescribeSecurityGroupsInput{
DryRun: &e.DryRun,
GroupNames: []*string{&groupName},
}
return e.DescribeSecurityGroupsWithContext(ctx, sgi)
}
func (e EC2) CreateSecurityGroup(ctx context.Context, groupName, description, vpcId string) (*ec2.CreateSecurityGroupOutput, error) {
sgi := &ec2.CreateSecurityGroupInput{
DryRun: &e.DryRun,
Description: &description,
GroupName: &groupName,
VpcId: aws.String(vpcId),
}
return e.CreateSecurityGroupWithContext(ctx, sgi)
}
func (e EC2) AuthorizeSecurityGroupIngress(ctx context.Context, groupId, cidr, protocol string, port int64) (*ec2.AuthorizeSecurityGroupIngressOutput, error) {
sgi := &ec2.AuthorizeSecurityGroupIngressInput{
DryRun: &e.DryRun,
GroupId: &groupId,
CidrIp: &cidr,
IpProtocol: &protocol,
ToPort: &port,
FromPort: &port,
}
return e.AuthorizeSecurityGroupIngressWithContext(ctx, sgi)
}
func (e EC2) DeleteSecurityGroup(ctx context.Context, groupId string) (*ec2.DeleteSecurityGroupOutput, error) {
sgi := &ec2.DeleteSecurityGroupInput{
DryRun: &e.DryRun,
GroupId: aws.String(groupId),
}
return e.DeleteSecurityGroupWithContext(ctx, sgi)
}
func (e EC2) DescribeSubnets(ctx context.Context, vpcId string) (*ec2.DescribeSubnetsOutput, error) {
paramsEC2 := &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcId)},
},
},
}
return e.DescribeSubnetsWithContext(ctx, paramsEC2)
}
func (e EC2) DescribeDefaultVpc(ctx context.Context) (*ec2.DescribeVpcsOutput, error) {
vpci := &ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("isDefault"),
Values: []*string{
aws.String("true"),
},
},
},
}
return e.DescribeVpcsWithContext(ctx, vpci)
}