1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
"gopkg.in/juju/names.v2"
10
"github.com/juju/juju/apiserver/common"
11
"github.com/juju/juju/apiserver/facade"
12
"github.com/juju/juju/environs"
13
"github.com/juju/juju/state"
14
"github.com/juju/juju/state/stateenvirons"
15
"github.com/juju/juju/storage/poolmanager"
18
// This file contains untested shims to let us wrap state in a sensible
19
// interface and avoid writing tests that depend on mongodb. If you were
20
// to change any part of it so that it were no longer *obviously* and
21
// *trivially* correct, you would be Doing It Wrong.
24
common.RegisterStandardFacade("Storage", 3, newAPI)
29
resources facade.Resources,
30
authorizer facade.Authorizer,
32
env, err := stateenvirons.GetNewEnvironFunc(environs.New)(st)
34
return nil, errors.Annotate(err, "getting environ")
36
registry := stateenvirons.NewStorageProviderRegistry(env)
37
pm := poolmanager.New(state.NewStateSettings(st), registry)
38
return NewAPI(getState(st), registry, pm, resources, authorizer)
41
type storageAccess interface {
42
// StorageInstance is required for storage functionality.
43
StorageInstance(names.StorageTag) (state.StorageInstance, error)
45
// AllStorageInstances is required for storage functionality.
46
AllStorageInstances() ([]state.StorageInstance, error)
48
// StorageAttachments is required for storage functionality.
49
StorageAttachments(names.StorageTag) ([]state.StorageAttachment, error)
51
// UnitAssignedMachine is required for storage functionality.
52
UnitAssignedMachine(names.UnitTag) (names.MachineTag, error)
54
// FilesystemAttachment is required for storage functionality.
55
FilesystemAttachment(names.MachineTag, names.FilesystemTag) (state.FilesystemAttachment, error)
57
// StorageInstanceFilesystem is required for storage functionality.
58
StorageInstanceFilesystem(names.StorageTag) (state.Filesystem, error)
60
// StorageInstanceVolume is required for storage functionality.
61
StorageInstanceVolume(names.StorageTag) (state.Volume, error)
63
// VolumeAttachment is required for storage functionality.
64
VolumeAttachment(names.MachineTag, names.VolumeTag) (state.VolumeAttachment, error)
66
// WatchStorageAttachment is required for storage functionality.
67
WatchStorageAttachment(names.StorageTag, names.UnitTag) state.NotifyWatcher
69
// WatchFilesystemAttachment is required for storage functionality.
70
WatchFilesystemAttachment(names.MachineTag, names.FilesystemTag) state.NotifyWatcher
72
// WatchVolumeAttachment is required for storage functionality.
73
WatchVolumeAttachment(names.MachineTag, names.VolumeTag) state.NotifyWatcher
75
// WatchBlockDevices is required for storage functionality.
76
WatchBlockDevices(names.MachineTag) state.NotifyWatcher
78
// BlockDevices is required for storage functionality.
79
BlockDevices(names.MachineTag) ([]state.BlockDeviceInfo, error)
81
// ModelName is required for pool functionality.
82
ModelName() (string, error)
84
// AllVolumes is required for volume functionality.
85
AllVolumes() ([]state.Volume, error)
87
// VolumeAttachments is required for volume functionality.
88
VolumeAttachments(volume names.VolumeTag) ([]state.VolumeAttachment, error)
90
// MachineVolumeAttachments is required for volume functionality.
91
MachineVolumeAttachments(machine names.MachineTag) ([]state.VolumeAttachment, error)
93
// Volume is required for volume functionality.
94
Volume(tag names.VolumeTag) (state.Volume, error)
96
// AllFilesystems is required for filesystem functionality.
97
AllFilesystems() ([]state.Filesystem, error)
99
// FilesystemAttachments is required for filesystem functionality.
100
FilesystemAttachments(filesystem names.FilesystemTag) ([]state.FilesystemAttachment, error)
102
// MachineFilesystemAttachments is required for filesystem functionality.
103
MachineFilesystemAttachments(machine names.MachineTag) ([]state.FilesystemAttachment, error)
105
// Filesystem is required for filesystem functionality.
106
Filesystem(tag names.FilesystemTag) (state.Filesystem, error)
108
// AddStorageForUnit is required for storage add functionality.
109
AddStorageForUnit(tag names.UnitTag, name string, cons state.StorageConstraints) error
111
// GetBlockForType is required to block operations.
112
GetBlockForType(t state.BlockType) (state.Block, bool, error)
115
var getState = func(st *state.State) storageAccess {
119
type stateShim struct {
123
// UnitAssignedMachine returns the tag of the machine that the unit
124
// is assigned to, or an error if the unit cannot be obtained or is
125
// not assigned to a machine.
126
func (s stateShim) UnitAssignedMachine(tag names.UnitTag) (names.MachineTag, error) {
127
unit, err := s.Unit(tag.Id())
129
return names.MachineTag{}, errors.Trace(err)
131
mid, err := unit.AssignedMachineId()
133
return names.MachineTag{}, errors.Trace(err)
135
return names.NewMachineTag(mid), nil
138
// ModelName returns the name of Juju environment,
139
// or an error if environment configuration is not retrievable.
140
func (s stateShim) ModelName() (string, error) {
141
cfg, err := s.State.ModelConfig()
143
return "", errors.Trace(err)
145
return cfg.Name(), nil