forked from juju/juju
/
volumes.go
183 lines (166 loc) · 5.06 KB
/
volumes.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
// Copyright 2021 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package testing
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/juju/collections/set"
)
// CreateVolume implements ec2.Client.
func (srv *Server) CreateVolume(ctx context.Context, in *ec2.CreateVolumeInput, opts ...func(*ec2.Options)) (*ec2.CreateVolumeOutput, error) {
if err, ok := srv.apiCallErrors["CreateVolume"]; ok {
return nil, err
}
srv.mu.Lock()
defer srv.mu.Unlock()
volume := srv.newVolume("magnetic", 1)
volume.AvailabilityZone = in.AvailabilityZone
if in.VolumeType != "" {
volume.VolumeType = in.VolumeType
}
if in.Size != nil {
volume.Size = in.Size
}
volume.Encrypted = in.Encrypted
volume.Iops = in.Iops
volume.KmsKeyId = in.KmsKeyId
volume.Throughput = in.Throughput
return &ec2.CreateVolumeOutput{
Attachments: nil,
AvailabilityZone: volume.AvailabilityZone,
Encrypted: volume.Encrypted,
Iops: volume.Iops,
Size: volume.Size,
State: volume.State,
Tags: volume.Tags,
VolumeId: volume.VolumeId,
VolumeType: volume.VolumeType,
KmsKeyId: volume.KmsKeyId,
Throughput: volume.Throughput,
}, nil
}
// DeleteVolume implements ec2.Client.
func (srv *Server) DeleteVolume(ctx context.Context, in *ec2.DeleteVolumeInput, opts ...func(*ec2.Options)) (*ec2.DeleteVolumeOutput, error) {
if err, ok := srv.apiCallErrors["DeleteVolume"]; ok {
return nil, err
}
v, err := srv.volume(aws.ToString(in.VolumeId))
if err != nil {
return nil, err
}
srv.mu.Lock()
defer srv.mu.Unlock()
volId := aws.ToString(v.VolumeId)
if _, ok := srv.volumeAttachments[volId]; ok {
return nil, apiError("VolumeInUse", "Volume %s is attached", volId)
}
delete(srv.volumes, volId)
return &ec2.DeleteVolumeOutput{}, nil
}
// DescribeVolumes implements ec2.Client.
func (srv *Server) DescribeVolumes(ctx context.Context, in *ec2.DescribeVolumesInput, opts ...func(*ec2.Options)) (*ec2.DescribeVolumesOutput, error) {
if err, ok := srv.apiCallErrors["DescribeVolumes"]; ok {
return nil, err
}
srv.mu.Lock()
defer srv.mu.Unlock()
var f ec2filter
idSet := set.NewStrings()
if in != nil {
f = in.Filters
idSet = set.NewStrings(in.VolumeIds...)
}
result := &ec2.DescribeVolumesOutput{}
for _, v := range srv.volumes {
volId := aws.ToString(v.VolumeId)
ok, err := f.ok(v)
if ok && (len(idSet) == 0 || idSet.Contains(volId)) {
vol := v.Volume
if va, ok := srv.volumeAttachments[volId]; ok {
vol.Attachments = []types.VolumeAttachment{va.VolumeAttachment}
}
result.Volumes = append(result.Volumes, vol)
} else if err != nil {
return nil, apiError("InvalidParameterValue", "describe Volumes: %v", err)
}
}
if f, ok := srv.apiCallModifiers["DescribeVolumes"]; ok {
if len(f) > 0 {
f[0](result)
srv.apiCallModifiers["DescribeVolumes"] = f[1:]
}
}
return result, nil
}
// SetCreateRootDisks records whether or not the server should create
// root disks for each instance created. It defaults to false.
func (srv *Server) SetCreateRootDisks(create bool) {
srv.mu.Lock()
srv.createRootDisks = create
srv.mu.Unlock()
}
func (srv *Server) newVolume(volumeType types.VolumeType, size int32) *volume {
// Create a volume and volume attachment too.
volume := &volume{}
volume.VolumeId = aws.String(fmt.Sprintf("vol-%d", srv.volumeId.next()))
volume.State = "available"
volume.CreateTime = aws.Time(time.Now())
volume.VolumeType = volumeType
volume.Size = aws.Int32(size)
srv.volumes[aws.ToString(volume.VolumeId)] = volume
return volume
}
type volume struct {
types.Volume
}
func (v *volume) matchAttr(attr, value string) (ok bool, err error) {
if strings.HasPrefix(attr, "attachment.") {
return false, fmt.Errorf("%q filter is not implemented", attr)
}
if strings.HasPrefix(attr, "tag:") {
key := attr[len("tag:"):]
return matchTag(v.Tags, key, value), nil
}
switch attr {
case "volume-type":
return string(v.VolumeType) == value, nil
case "status":
return string(v.State) == value, nil
case "volume-id":
return aws.ToString(v.VolumeId) == value, nil
case "size":
size, err := strconv.Atoi(value)
if err != nil {
return false, err
}
return aws.ToInt32(v.Size) == int32(size), nil
case "availability-zone":
return aws.ToString(v.AvailabilityZone) == value, nil
case "snapshot-id":
return aws.ToString(v.SnapshotId) == value, nil
case "encrypted":
encrypted, err := strconv.ParseBool(value)
if err != nil {
return false, err
}
return aws.ToBool(v.Encrypted) == encrypted, nil
case "tag", "tag-key", "tag-value", "create-time":
return false, fmt.Errorf("%q filter is not implemented", attr)
}
return false, fmt.Errorf("unknown attribute %q", attr)
}
func (srv *Server) volume(id string) (*volume, error) {
srv.mu.Lock()
defer srv.mu.Unlock()
v, found := srv.volumes[id]
if !found {
return nil, apiError("InvalidVolume.NotFound", "Volume %s not found", id)
}
return v, nil
}