generated from kyma-project/template-repository
/
client.go
207 lines (187 loc) · 6.42 KB
/
client.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package client
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/efs"
efsTypes "github.com/aws/aws-sdk-go-v2/service/efs/types"
"github.com/elliotchance/pie/v2"
awsclient "github.com/kyma-project/cloud-manager/pkg/kcp/provider/aws/client"
"k8s.io/utils/pointer"
)
const (
TcpProtocol = "tcp"
NfsPort = int32(2049)
)
func NewClientProvider() awsclient.SkrClientProvider[Client] {
return func(ctx context.Context, region, key, secret, role string) (Client, error) {
cfg, err := awsclient.NewSkrConfig(ctx, region, key, secret, role)
if err != nil {
return nil, err
}
return newClient(
ec2.NewFromConfig(cfg),
efs.NewFromConfig(cfg),
), nil
}
}
type Client interface {
DescribeSubnet(ctx context.Context, subnetId string) (*ec2Types.Subnet, error)
DescribeSecurityGroups(ctx context.Context, filters []ec2Types.Filter, groupIds []string) ([]ec2Types.SecurityGroup, error)
CreateSecurityGroup(ctx context.Context, vpcId, name string, tags []ec2Types.Tag) (string, error)
DeleteSecurityGroup(ctx context.Context, id string) error
AuthorizeSecurityGroupIngress(ctx context.Context, groupId string, ipPermissions []ec2Types.IpPermission) error
DescribeFileSystems(ctx context.Context) ([]efsTypes.FileSystemDescription, error)
CreateFileSystem(
ctx context.Context,
performanceMode efsTypes.PerformanceMode,
throughputMode efsTypes.ThroughputMode,
tags []efsTypes.Tag,
) (*efs.CreateFileSystemOutput, error)
DeleteFileSystem(ctx context.Context, fsId string) error
DescribeMountTargets(ctx context.Context, fsId string) ([]efsTypes.MountTargetDescription, error)
CreateMountTarget(ctx context.Context, fsId, subnetId string, securityGroups []string) (string, error)
DeleteMountTarget(ctx context.Context, mountTargetId string) error
DescribeMountTargetSecurityGroups(ctx context.Context, mountTargetId string) ([]string, error)
}
func newClient(ec2Svc *ec2.Client, efsSvc *efs.Client) Client {
return &client{
ec2Svc: ec2Svc,
efsSvc: efsSvc,
}
}
type client struct {
ec2Svc *ec2.Client
efsSvc *efs.Client
}
func (c *client) DescribeSubnet(ctx context.Context, subnetId string) (*ec2Types.Subnet, error) {
out, err := c.ec2Svc.DescribeSubnets(ctx, &ec2.DescribeSubnetsInput{
Filters: []ec2Types.Filter{
{
Name: pointer.String("subnet-id"),
Values: []string{subnetId},
},
},
})
if err != nil {
return nil, err
}
if len(out.Subnets) > 1 {
return nil, fmt.Errorf("expected at most one subnet by id, but got: %v", pie.Map(out.Subnets, func(s ec2Types.Subnet) string {
return pointer.StringDeref(s.SubnetId, "")
}))
}
var result *ec2Types.Subnet
if len(out.Subnets) > 0 {
result = &out.Subnets[0]
}
return result, nil
}
func (c *client) DescribeSecurityGroups(ctx context.Context, filters []ec2Types.Filter, groupIds []string) ([]ec2Types.SecurityGroup, error) {
out, err := c.ec2Svc.DescribeSecurityGroups(ctx, &ec2.DescribeSecurityGroupsInput{
Filters: filters,
GroupIds: groupIds,
})
if err != nil {
return nil, err
}
return out.SecurityGroups, nil
}
func (c *client) CreateSecurityGroup(ctx context.Context, vpcId, name string, tags []ec2Types.Tag) (string, error) {
out, err := c.ec2Svc.CreateSecurityGroup(ctx, &ec2.CreateSecurityGroupInput{
Description: pointer.String(name),
GroupName: pointer.String(name),
TagSpecifications: []ec2Types.TagSpecification{
{
ResourceType: ec2Types.ResourceTypeSecurityGroup,
Tags: tags,
},
},
VpcId: pointer.String(vpcId),
})
if err != nil {
return "", err
}
return pointer.StringDeref(out.GroupId, ""), nil
}
func (c *client) DeleteSecurityGroup(ctx context.Context, id string) error {
in := &ec2.DeleteSecurityGroupInput{
GroupId: pointer.String(id),
}
_, err := c.ec2Svc.DeleteSecurityGroup(ctx, in)
return err
}
func (c *client) AuthorizeSecurityGroupIngress(ctx context.Context, groupId string, ipPermissions []ec2Types.IpPermission) error {
_, err := c.ec2Svc.AuthorizeSecurityGroupIngress(ctx, &ec2.AuthorizeSecurityGroupIngressInput{
GroupId: pointer.String(groupId),
IpPermissions: ipPermissions,
})
if err != nil {
return err
}
return nil
}
func (c *client) DescribeFileSystems(ctx context.Context) ([]efsTypes.FileSystemDescription, error) {
in := &efs.DescribeFileSystemsInput{}
out, err := c.efsSvc.DescribeFileSystems(ctx, in)
if err != nil {
return nil, err
}
return out.FileSystems, nil
}
func (c *client) CreateFileSystem(ctx context.Context, performanceMode efsTypes.PerformanceMode, throughputMode efsTypes.ThroughputMode, tags []efsTypes.Tag) (*efs.CreateFileSystemOutput, error) {
in := &efs.CreateFileSystemInput{
Encrypted: aws.Bool(true),
PerformanceMode: performanceMode,
Tags: tags,
ThroughputMode: throughputMode,
}
out, err := c.efsSvc.CreateFileSystem(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
func (c *client) DeleteFileSystem(ctx context.Context, fsId string) error {
in := &efs.DeleteFileSystemInput{FileSystemId: pointer.String(fsId)}
_, err := c.efsSvc.DeleteFileSystem(ctx, in)
return err
}
func (c *client) DescribeMountTargets(ctx context.Context, fsId string) ([]efsTypes.MountTargetDescription, error) {
out, err := c.efsSvc.DescribeMountTargets(ctx, &efs.DescribeMountTargetsInput{
FileSystemId: pointer.String(fsId),
})
if err != nil {
return nil, err
}
return out.MountTargets, nil
}
func (c *client) CreateMountTarget(ctx context.Context, fsId, subnetId string, securityGroups []string) (string, error) {
out, err := c.efsSvc.CreateMountTarget(ctx, &efs.CreateMountTargetInput{
FileSystemId: pointer.String(fsId),
SubnetId: pointer.String(subnetId),
SecurityGroups: securityGroups,
})
if err != nil {
return "", err
}
return pointer.StringDeref(out.MountTargetId, ""), nil
}
func (c *client) DeleteMountTarget(ctx context.Context, mountTargetId string) error {
in := &efs.DeleteMountTargetInput{
MountTargetId: pointer.String(mountTargetId),
}
_, err := c.efsSvc.DeleteMountTarget(ctx, in)
return err
}
func (c *client) DescribeMountTargetSecurityGroups(ctx context.Context, mountTargetId string) ([]string, error) {
out, err := c.efsSvc.DescribeMountTargetSecurityGroups(ctx, &efs.DescribeMountTargetSecurityGroupsInput{
MountTargetId: pointer.String(mountTargetId),
})
if err != nil {
return nil, err
}
return out.SecurityGroups, nil
}