/
maas_storage.go
117 lines (100 loc) · 3.16 KB
/
maas_storage.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package maas
import (
"bytes"
"fmt"
"io"
"sort"
"strings"
"github.com/juju/errors"
"github.com/juju/gomaasapi/v2"
"github.com/juju/utils/v3"
"github.com/juju/juju/environs/storage"
)
type maasStorage struct {
// The Environ that this Storage is for.
environ *maasEnviron
// The Controller for the MAAS 2 cluster.
maasController gomaasapi.Controller
}
var _ storage.Storage = (*maasStorage)(nil)
func (stor *maasStorage) prefixWithPrivateNamespace(name string) string {
return prefixWithPrivateNamespace(stor.environ, name)
}
// All filenames need to be namespaced so they are private to this environment.
// This prevents different environments from interfering with each other.
// We're using the agent name UUID here.
func prefixWithPrivateNamespace(env *maasEnviron, name string) string {
return env.uuid + "-" + name
}
// Get implements storage.StorageReader
func (stor *maasStorage) Get(name string) (io.ReadCloser, error) {
name = stor.prefixWithPrivateNamespace(name)
file, err := stor.maasController.GetFile(name)
if err != nil {
if gomaasapi.IsNoMatchError(err) {
return nil, fmt.Errorf("%s %w", name, errors.NotFound)
}
return nil, errors.Trace(err)
}
contents, err := file.ReadAll()
if err != nil {
return nil, errors.Trace(err)
}
return io.NopCloser(bytes.NewReader(contents)), nil
}
// List implements storage.StorageReader
func (stor *maasStorage) List(prefix string) ([]string, error) {
prefix = stor.prefixWithPrivateNamespace(prefix)
files, err := stor.maasController.Files(prefix)
if err != nil {
return nil, errors.Trace(err)
}
privatePrefix := stor.prefixWithPrivateNamespace("")
names := make([]string, len(files))
for i, file := range files {
// Remove the private namespacing.
names[i] = strings.TrimPrefix(file.Filename(), privatePrefix)
}
sort.Strings(names)
return names, nil
}
// URL implements storage.StorageReader
func (stor *maasStorage) URL(name string) (string, error) {
name = stor.prefixWithPrivateNamespace(name)
file, err := stor.maasController.GetFile(name)
if err != nil {
return "", errors.Trace(err)
}
return file.AnonymousURL(), nil
}
// DefaultConsistencyStrategy implements storage.StorageReader
//
// TODO(katco): 2016-08-09: lp:1611427
func (stor *maasStorage) DefaultConsistencyStrategy() utils.AttemptStrategy {
return utils.AttemptStrategy{}
}
// ShouldRetry implements storage.StorageReader
func (stor *maasStorage) ShouldRetry(err error) bool {
return false
}
// Put implements storage.StorageWriter
func (stor *maasStorage) Put(name string, r io.Reader, length int64) error {
name = stor.prefixWithPrivateNamespace(name)
args := gomaasapi.AddFileArgs{Filename: name, Reader: r, Length: length}
return stor.maasController.AddFile(args)
}
// Remove implements storage.StorageWriter
func (stor *maasStorage) Remove(name string) error {
name = stor.prefixWithPrivateNamespace(name)
file, err := stor.maasController.GetFile(name)
if err != nil {
return errors.Trace(err)
}
return file.Delete()
}
// RemoveAll implements storage.StorageWriter
func (stor *maasStorage) RemoveAll() error {
return removeAll(stor)
}