github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/storage/state.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/state"
    11  )
    12  
    13  type storageAccess interface {
    14  	// StorageInstance is required for storage functionality.
    15  	StorageInstance(names.StorageTag) (state.StorageInstance, error)
    16  
    17  	// AllStorageInstances is required for storage functionality.
    18  	AllStorageInstances() ([]state.StorageInstance, error)
    19  
    20  	// StorageAttachments is required for storage functionality.
    21  	StorageAttachments(names.StorageTag) ([]state.StorageAttachment, error)
    22  
    23  	// UnitAssignedMachine is required for storage functionality.
    24  	UnitAssignedMachine(names.UnitTag) (names.MachineTag, error)
    25  
    26  	// FilesystemAttachment is required for storage functionality.
    27  	FilesystemAttachment(names.MachineTag, names.FilesystemTag) (state.FilesystemAttachment, error)
    28  
    29  	// StorageInstanceFilesystem is required for storage functionality.
    30  	StorageInstanceFilesystem(names.StorageTag) (state.Filesystem, error)
    31  
    32  	// StorageInstanceVolume is required for storage functionality.
    33  	StorageInstanceVolume(names.StorageTag) (state.Volume, error)
    34  
    35  	// VolumeAttachment is required for storage functionality.
    36  	VolumeAttachment(names.MachineTag, names.VolumeTag) (state.VolumeAttachment, error)
    37  
    38  	// WatchStorageAttachment is required for storage functionality.
    39  	WatchStorageAttachment(names.StorageTag, names.UnitTag) state.NotifyWatcher
    40  
    41  	// WatchFilesystemAttachment is required for storage functionality.
    42  	WatchFilesystemAttachment(names.MachineTag, names.FilesystemTag) state.NotifyWatcher
    43  
    44  	// WatchVolumeAttachment is required for storage functionality.
    45  	WatchVolumeAttachment(names.MachineTag, names.VolumeTag) state.NotifyWatcher
    46  
    47  	// EnvName is required for pool functionality.
    48  	EnvName() (string, error)
    49  
    50  	// AllVolumes is required for volume functionality.
    51  	AllVolumes() ([]state.Volume, error)
    52  
    53  	// VolumeAttachments is required for volume functionality.
    54  	VolumeAttachments(volume names.VolumeTag) ([]state.VolumeAttachment, error)
    55  
    56  	// MachineVolumeAttachments is required for volume functionality.
    57  	MachineVolumeAttachments(machine names.MachineTag) ([]state.VolumeAttachment, error)
    58  
    59  	// Volume is required for volume functionality.
    60  	Volume(tag names.VolumeTag) (state.Volume, error)
    61  
    62  	// AddStorageForUnit is required for storage add functionality.
    63  	AddStorageForUnit(tag names.UnitTag, name string, cons state.StorageConstraints) error
    64  
    65  	// GetBlockForType is required to block operations.
    66  	GetBlockForType(t state.BlockType) (state.Block, bool, error)
    67  }
    68  
    69  var getState = func(st *state.State) storageAccess {
    70  	return stateShim{st}
    71  }
    72  
    73  type stateShim struct {
    74  	*state.State
    75  }
    76  
    77  // UnitAssignedMachine returns the tag of the machine that the unit
    78  // is assigned to, or an error if the unit cannot be obtained or is
    79  // not assigned to a machine.
    80  func (s stateShim) UnitAssignedMachine(tag names.UnitTag) (names.MachineTag, error) {
    81  	unit, err := s.Unit(tag.Id())
    82  	if err != nil {
    83  		return names.MachineTag{}, errors.Trace(err)
    84  	}
    85  	mid, err := unit.AssignedMachineId()
    86  	if err != nil {
    87  		return names.MachineTag{}, errors.Trace(err)
    88  	}
    89  	return names.NewMachineTag(mid), nil
    90  }
    91  
    92  // EnvName returns the name of Juju environment,
    93  // or an error if environment configuration is not retrievable.
    94  func (s stateShim) EnvName() (string, error) {
    95  	cfg, err := s.State.EnvironConfig()
    96  	if err != nil {
    97  		return "", errors.Trace(err)
    98  	}
    99  	return cfg.Name(), nil
   100  }