-
Notifications
You must be signed in to change notification settings - Fork 39
/
service.go
228 lines (208 loc) · 6.19 KB
/
service.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Swift double testing service - internal direct API implementation
package swiftservice
import (
"fmt"
"net/url"
"sort"
"strings"
"sync"
"time"
"gopkg.in/goose.v2/swift"
"gopkg.in/goose.v2/testservices"
"gopkg.in/goose.v2/testservices/identityservice"
)
type object map[string][]byte
var _ testservices.HttpService = (*Swift)(nil)
var _ identityservice.ServiceProvider = (*Swift)(nil)
type Swift struct {
testservices.ServiceInstance
mu sync.Mutex // protects the remaining fields
containers map[string]object
}
// New creates an instance of the Swift object, given the parameters.
func New(hostURL, versionPath, tenantId, region string, identityService, fallbackIdentity identityservice.IdentityService) *Swift {
URL, err := url.Parse(hostURL)
if err != nil {
panic(err)
}
hostname := URL.Host
if !strings.HasSuffix(hostname, "/") {
hostname += "/"
}
swift := &Swift{
containers: make(map[string]object),
ServiceInstance: testservices.ServiceInstance{
IdentityService: identityService,
FallbackIdentityService: fallbackIdentity,
Scheme: URL.Scheme,
Hostname: hostname,
VersionPath: versionPath,
TenantId: tenantId,
Region: region,
},
}
if identityService != nil {
identityService.RegisterServiceProvider("swift", "object-store", swift)
}
return swift
}
func Stop() {
// noop
}
func (s *Swift) endpointURL(path string) string {
// Note: We're using the Openstack Style endpoints for this testservice
// Openstack object-storage endpoint url:
// http://<ip addr>:<port>/swift/v1
// Rackspace object-storage endpoint url:
// https://<rackspace addr>/v1/MossoCloudFS_<tenant id>
ep := s.Scheme + "://" + s.Hostname + "swift/" + s.VersionPath
if path != "" {
ep += "/" + strings.TrimLeft(path, "/")
}
return ep
}
func (s *Swift) Endpoints() []identityservice.Endpoint {
ep := identityservice.Endpoint{
AdminURL: s.endpointURL(""),
InternalURL: s.endpointURL(""),
PublicURL: s.endpointURL(""),
Region: s.Region,
}
return []identityservice.Endpoint{ep}
}
func (s *Swift) V3Endpoints() []identityservice.V3Endpoint {
url := s.endpointURL("")
return identityservice.NewV3Endpoints(url, url, url, s.RegionID)
}
// HasContainer verifies the given container exists or not.
func (s *Swift) HasContainer(name string) bool {
s.mu.Lock()
_, ok := s.containers[name]
s.mu.Unlock()
return ok
}
// GetObject retrieves a given object from its container, returning
// the object data or an error.
func (s *Swift) GetObject(container, name string) ([]byte, error) {
if err := s.ProcessFunctionHook(s, container, name); err != nil {
return nil, err
}
s.mu.Lock()
data, ok := s.containers[container][name]
s.mu.Unlock()
if !ok {
return nil, fmt.Errorf("no such object %q in container %q", name, container)
}
return data, nil
}
// AddContainer creates a new container with the given name, if it
// does not exist. Otherwise an error is returned.
func (s *Swift) AddContainer(name string) error {
if err := s.ProcessFunctionHook(s, name); err != nil {
return err
}
if s.HasContainer(name) {
return fmt.Errorf("container already exists %q", name)
}
s.mu.Lock()
s.containers[name] = make(object)
s.mu.Unlock()
return nil
}
// ListContainer lists the objects in the given container.
// params contains filtering attributes: prefix, delimiter, marker.
// Only prefix is currently supported.
func (s *Swift) ListContainer(name string, params map[string]string) ([]swift.ContainerContents, error) {
if err := s.ProcessFunctionHook(s, name); err != nil {
return nil, err
}
if ok := s.HasContainer(name); !ok {
return nil, fmt.Errorf("no such container %q", name)
}
s.mu.Lock()
items := s.containers[name]
s.mu.Unlock()
sorted := make([]string, 0, len(items))
prefix := params["prefix"]
for filename := range items {
if prefix != "" && !strings.HasPrefix(filename, prefix) {
continue
}
sorted = append(sorted, filename)
}
sort.Strings(sorted)
contents := make([]swift.ContainerContents, len(sorted))
var i = 0
for _, filename := range sorted {
contents[i] = swift.ContainerContents{
Name: filename,
Hash: "", // not implemented
LengthBytes: len(items[filename]),
ContentType: "application/octet-stream",
LastModified: time.Now().Format("2006-01-02 15:04:05"), //not implemented
}
i++
}
return contents, nil
}
// AddObject creates a new object with the given name in the specified
// container, setting the object's data. It's an error if the object
// already exists. If the container does not exist, it will be
// created.
func (s *Swift) AddObject(container, name string, data []byte) error {
if err := s.ProcessFunctionHook(s, container, name); err != nil {
return err
}
if _, err := s.GetObject(container, name); err == nil {
return fmt.Errorf(
"object %q in container %q already exists",
name,
container)
}
if ok := s.HasContainer(container); !ok {
if err := s.AddContainer(container); err != nil {
return err
}
}
s.mu.Lock()
s.containers[container][name] = data
s.mu.Unlock()
return nil
}
// RemoveContainer deletes an existing container with the given name.
func (s *Swift) RemoveContainer(name string) error {
if err := s.ProcessFunctionHook(s, name); err != nil {
return err
}
if ok := s.HasContainer(name); !ok {
return fmt.Errorf("no such container %q", name)
}
s.mu.Lock()
delete(s.containers, name)
s.mu.Unlock()
return nil
}
// RemoveObject deletes an existing object in a given container.
func (s *Swift) RemoveObject(container, name string) error {
if err := s.ProcessFunctionHook(s, container, name); err != nil {
return err
}
if _, err := s.GetObject(container, name); err != nil {
return err
}
s.mu.Lock()
delete(s.containers[container], name)
s.mu.Unlock()
return nil
}
// GetURL returns the full URL, which can be used to GET the
// object. An error occurs if the object does not exist.
func (s *Swift) GetURL(container, object string) (string, error) {
if err := s.ProcessFunctionHook(s, container, object); err != nil {
return "", err
}
if _, err := s.GetObject(container, object); err != nil {
return "", err
}
return s.endpointURL(fmt.Sprintf("%s/%s", container, object)), nil
}