forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2-volume.go
73 lines (57 loc) · 1.48 KB
/
ec2-volume.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 resources
import (
"context"
"time"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)
const EC2VolumeResource = "EC2Volume"
func init() {
registry.Register(®istry.Registration{
Name: EC2VolumeResource,
Scope: nuke.Account,
Lister: &EC2VolumeLister{},
})
}
type EC2Volume struct {
svc *ec2.EC2
volume *ec2.Volume
}
type EC2VolumeLister struct{}
func (l *EC2VolumeLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := ec2.New(opts.Session)
resp, err := svc.DescribeVolumes(nil)
if err != nil {
return nil, err
}
resources := make([]resource.Resource, 0)
for _, out := range resp.Volumes {
resources = append(resources, &EC2Volume{
svc: svc,
volume: out,
})
}
return resources, nil
}
func (e *EC2Volume) Remove(_ context.Context) error {
_, err := e.svc.DeleteVolume(&ec2.DeleteVolumeInput{
VolumeId: e.volume.VolumeId,
})
return err
}
func (e *EC2Volume) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("State", e.volume.State)
properties.Set("CreateTime", e.volume.CreateTime.Format(time.RFC3339))
for _, tagValue := range e.volume.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
return properties
}
func (e *EC2Volume) String() string {
return *e.volume.VolumeId
}