/
storage.go
189 lines (177 loc) · 6.22 KB
/
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
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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package uniter
import (
"github.com/juju/errors"
"github.com/juju/names/v4"
"github.com/juju/juju/api/base"
apiwatcher "github.com/juju/juju/api/watcher"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/watcher"
)
type StorageAccessor struct {
facade base.FacadeCaller
}
// NewStorageAccessor creates a StorageAccessor on the specified facade,
// and uses this name when calling through the caller.
func NewStorageAccessor(facade base.FacadeCaller) *StorageAccessor {
return &StorageAccessor{facade}
}
// UnitStorageAttachments returns the IDs of a unit's storage attachments.
func (sa *StorageAccessor) UnitStorageAttachments(unitTag names.UnitTag) ([]params.StorageAttachmentId, error) {
if sa.facade.BestAPIVersion() < 2 {
return nil, errors.NotImplementedf("UnitStorageAttachments() (need V2+)")
}
args := params.Entities{
Entities: []params.Entity{{Tag: unitTag.String()}},
}
var results params.StorageAttachmentIdsResults
err := sa.facade.FacadeCall("UnitStorageAttachments", args, &results)
if err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return nil, result.Error
}
return result.Result.Ids, nil
}
// DestroyUnitStorageAttachments ensures that the specified unit's storage
// attachments will be removed at some point in the future.
func (sa *StorageAccessor) DestroyUnitStorageAttachments(unitTag names.UnitTag) error {
if sa.facade.BestAPIVersion() < 2 {
return errors.NotImplementedf("DestroyUnitStorageAttachments() (need V2+)")
}
args := params.Entities{
Entities: []params.Entity{{Tag: unitTag.String()}},
}
var results params.ErrorResults
err := sa.facade.FacadeCall("DestroyUnitStorageAttachments", args, &results)
if err != nil {
return errors.Trace(err)
}
if len(results.Results) != 1 {
return errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return result.Error
}
return nil
}
// WatchUnitStorageAttachments starts a watcher for changes to storage
// attachments related to the unit. The watcher will return the
// IDs of the corresponding storage instances.
func (sa *StorageAccessor) WatchUnitStorageAttachments(unitTag names.UnitTag) (watcher.StringsWatcher, error) {
var results params.StringsWatchResults
args := params.Entities{
Entities: []params.Entity{{Tag: unitTag.String()}},
}
err := sa.facade.FacadeCall("WatchUnitStorageAttachments", args, &results)
if err != nil {
return nil, err
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return nil, result.Error
}
w := apiwatcher.NewStringsWatcher(sa.facade.RawAPICaller(), result)
return w, nil
}
// StorageAttachment returns the storage attachment with the specified
// unit and storage tags.
func (sa *StorageAccessor) StorageAttachment(storageTag names.StorageTag, unitTag names.UnitTag) (params.StorageAttachment, error) {
if sa.facade.BestAPIVersion() < 2 {
return params.StorageAttachment{}, errors.NotImplementedf("StorageAttachment() (need V2+)")
}
args := params.StorageAttachmentIds{
Ids: []params.StorageAttachmentId{{
StorageTag: storageTag.String(),
UnitTag: unitTag.String(),
}},
}
var results params.StorageAttachmentResults
err := sa.facade.FacadeCall("StorageAttachments", args, &results)
if err != nil {
return params.StorageAttachment{}, errors.Trace(err)
}
if len(results.Results) != 1 {
return params.StorageAttachment{}, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return params.StorageAttachment{}, result.Error
}
return result.Result, nil
}
// StorageAttachmentLife returns the lifecycle state of the storage attachments
// with the specified IDs.
func (sa *StorageAccessor) StorageAttachmentLife(ids []params.StorageAttachmentId) ([]params.LifeResult, error) {
if sa.facade.BestAPIVersion() < 2 {
return nil, errors.NotImplementedf("StorageAttachmentLife() (need V2+)")
}
args := params.StorageAttachmentIds{ids}
var results params.LifeResults
err := sa.facade.FacadeCall("StorageAttachmentLife", args, &results)
if err != nil {
return nil, errors.Trace(err)
}
if len(results.Results) != len(ids) {
return nil, errors.Errorf("expected %d results, got %d", len(ids), len(results.Results))
}
return results.Results, nil
}
// WatchStorageAttachments starts a watcher for changes to the info
// of the storage attachment with the specified unit and storage tags.
func (sa *StorageAccessor) WatchStorageAttachment(storageTag names.StorageTag, unitTag names.UnitTag) (watcher.NotifyWatcher, error) {
var results params.NotifyWatchResults
args := params.StorageAttachmentIds{
Ids: []params.StorageAttachmentId{{
StorageTag: storageTag.String(),
UnitTag: unitTag.String(),
}},
}
err := sa.facade.FacadeCall("WatchStorageAttachments", args, &results)
if err != nil {
return nil, err
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return nil, result.Error
}
w := apiwatcher.NewNotifyWatcher(sa.facade.RawAPICaller(), result)
return w, nil
}
// RemoveStorageAttachment removes the storage attachment with the
// specified unit and storage tags from state. This method is only
// expected to succeed if the storage attachment is Dead.
func (sa *StorageAccessor) RemoveStorageAttachment(storageTag names.StorageTag, unitTag names.UnitTag) error {
var results params.ErrorResults
args := params.StorageAttachmentIds{
Ids: []params.StorageAttachmentId{{
StorageTag: storageTag.String(),
UnitTag: unitTag.String(),
}},
}
err := sa.facade.FacadeCall("RemoveStorageAttachments", args, &results)
if err != nil {
return err
}
if len(results.Results) != 1 {
return errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return result.Error
}
return nil
}