/
client.go
73 lines (62 loc) · 1.74 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
package cmd
import (
"context"
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
type ebsAction struct {
ec2Client *ec2.Client
}
//func newClient(cfg aws.Config) *ec2Client {
// return &ec2Client{
// c: ec2.NewFromConfig(cfg),
// }
//}
func (ebs *ebsAction) createSnapshot(vId string) (*string, error) {
params := &ec2.CreateSnapshotInput{
VolumeId: aws.String(vId),
}
snapshotStatus, err := ebs.ec2Client.CreateSnapshot(context.TODO(), params)
if err != nil {
return nil, fmt.Errorf("error occred: %w", err)
}
return snapshotStatus.SnapshotId, nil
}
func (ebs *ebsAction) restoreSnapshot(ssId, zone string) (*string, error) {
params := &ec2.CreateVolumeInput{
AvailabilityZone: aws.String(zone),
SnapshotId: aws.String(ssId),
VolumeType: "gp3",
}
volumeStatus, err := ebs.ec2Client.CreateVolume(context.TODO(), params)
if err != nil {
return nil, fmt.Errorf("error occred: %w", err)
}
return volumeStatus.VolumeId, nil
}
func (ebs *ebsAction) deleteVolumeSnap(id string) (string, error) {
if strings.HasPrefix(id, "snap-") {
params := &ec2.DeleteSnapshotInput{
SnapshotId: aws.String(id),
}
_, err := ebs.ec2Client.DeleteSnapshot(context.TODO(), params)
if err != nil {
return "", fmt.Errorf("error occred: %w", err)
}
return fmt.Sprintf("Snapshot [%s] was successfully deleted.", id), nil
}
if strings.HasPrefix(id, "vol-") {
params := &ec2.DeleteVolumeInput{
VolumeId: aws.String(id),
}
_, err := ebs.ec2Client.DeleteVolume(context.TODO(), params)
if err != nil {
return "", fmt.Errorf("error occred: %w", err)
}
return fmt.Sprintf("Volume [%s] was successfully deleted.", id), nil
}
return "", errors.New("invalid input id")
}