-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a BackupStore to pkg/persistence that supports prefixes
Signed-off-by: Steve Kriss <steve@heptio.com>
- Loading branch information
Showing
28 changed files
with
1,392 additions
and
1,069 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
Copyright 2018 the Heptio Ark contributors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cloudprovider | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type BucketData map[string][]byte | ||
|
||
// InMemoryObjectStore is a simple implementation of the ObjectStore interface | ||
// that stores its data in-memory/in-proc. This is mainly intended to be used | ||
// as a test fake. | ||
type InMemoryObjectStore struct { | ||
Data map[string]BucketData | ||
} | ||
|
||
func NewInMemoryObjectStore(buckets ...string) *InMemoryObjectStore { | ||
o := &InMemoryObjectStore{ | ||
Data: make(map[string]BucketData), | ||
} | ||
|
||
for _, bucket := range buckets { | ||
o.Data[bucket] = make(map[string][]byte) | ||
} | ||
|
||
return o | ||
} | ||
|
||
// | ||
// Interface Implementation | ||
// | ||
|
||
func (o *InMemoryObjectStore) Init(config map[string]string) error { | ||
return nil | ||
} | ||
|
||
func (o *InMemoryObjectStore) PutObject(bucket, key string, body io.Reader) error { | ||
bucketData, ok := o.Data[bucket] | ||
if !ok { | ||
return errors.New("bucket not found") | ||
} | ||
|
||
obj, err := ioutil.ReadAll(body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bucketData[key] = obj | ||
|
||
return nil | ||
} | ||
|
||
func (o *InMemoryObjectStore) GetObject(bucket, key string) (io.ReadCloser, error) { | ||
bucketData, ok := o.Data[bucket] | ||
if !ok { | ||
return nil, errors.New("bucket not found") | ||
} | ||
|
||
obj, ok := bucketData[key] | ||
if !ok { | ||
return nil, errors.New("key not found") | ||
} | ||
|
||
return ioutil.NopCloser(bytes.NewReader(obj)), nil | ||
} | ||
|
||
func (o *InMemoryObjectStore) ListCommonPrefixes(bucket, prefix, delimiter string) ([]string, error) { | ||
keys, err := o.ListObjects(bucket, prefix) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// For each key, check if it has an instance of the delimiter *after* the prefix. | ||
// If not, skip it; if so, return the prefix of the key up to/including the delimiter. | ||
|
||
var prefixes []string | ||
for _, key := range keys { | ||
// everything after 'prefix' | ||
afterPrefix := key[len(prefix):] | ||
|
||
// index of the *start* of 'delimiter' in 'afterPrefix' | ||
delimiterStart := strings.Index(afterPrefix, delimiter) | ||
if delimiterStart == -1 { | ||
continue | ||
} | ||
|
||
// return the prefix, plus everything after the prefix and before | ||
// the delimiter, plus the delimiter | ||
fullPrefix := prefix + afterPrefix[0:delimiterStart] + delimiter | ||
|
||
prefixes = append(prefixes, fullPrefix) | ||
} | ||
|
||
return prefixes, nil | ||
} | ||
|
||
func (o *InMemoryObjectStore) ListObjects(bucket, prefix string) ([]string, error) { | ||
bucketData, ok := o.Data[bucket] | ||
if !ok { | ||
return nil, errors.New("bucket not found") | ||
} | ||
|
||
var objs []string | ||
for key := range bucketData { | ||
if strings.HasPrefix(key, prefix) { | ||
objs = append(objs, key) | ||
} | ||
} | ||
|
||
return objs, nil | ||
} | ||
|
||
func (o *InMemoryObjectStore) DeleteObject(bucket, key string) error { | ||
bucketData, ok := o.Data[bucket] | ||
if !ok { | ||
return errors.New("bucket not found") | ||
} | ||
|
||
delete(bucketData, key) | ||
|
||
return nil | ||
} | ||
|
||
func (o *InMemoryObjectStore) CreateSignedURL(bucket, key string, ttl time.Duration) (string, error) { | ||
bucketData, ok := o.Data[bucket] | ||
if !ok { | ||
return "", errors.New("bucket not found") | ||
} | ||
|
||
_, ok = bucketData[key] | ||
if !ok { | ||
return "", errors.New("key not found") | ||
} | ||
|
||
return "a-url", nil | ||
} | ||
|
||
// | ||
// Test Helper Methods | ||
// | ||
|
||
func (o *InMemoryObjectStore) ClearBucket(bucket string) { | ||
if _, ok := o.Data[bucket]; !ok { | ||
return | ||
} | ||
|
||
o.Data[bucket] = make(map[string][]byte) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.