github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	// WatchBlockDevices is required for storage functionality.
    48  	WatchBlockDevices(names.MachineTag) state.NotifyWatcher
    49  
    50  	// BlockDevices is required for storage functionality.
    51  	BlockDevices(names.MachineTag) ([]state.BlockDeviceInfo, error)
    52  
    53  	// ModelName is required for pool functionality.
    54  	ModelName() (string, error)
    55  
    56  	// AllVolumes is required for volume functionality.
    57  	AllVolumes() ([]state.Volume, error)
    58  
    59  	// VolumeAttachments is required for volume functionality.
    60  	VolumeAttachments(volume names.VolumeTag) ([]state.VolumeAttachment, error)
    61  
    62  	// MachineVolumeAttachments is required for volume functionality.
    63  	MachineVolumeAttachments(machine names.MachineTag) ([]state.VolumeAttachment, error)
    64  
    65  	// Volume is required for volume functionality.
    66  	Volume(tag names.VolumeTag) (state.Volume, error)
    67  
    68  	// AllFilesystems is required for filesystem functionality.
    69  	AllFilesystems() ([]state.Filesystem, error)
    70  
    71  	// FilesystemAttachments is required for filesystem functionality.
    72  	FilesystemAttachments(filesystem names.FilesystemTag) ([]state.FilesystemAttachment, error)
    73  
    74  	// MachineFilesystemAttachments is required for filesystem functionality.
    75  	MachineFilesystemAttachments(machine names.MachineTag) ([]state.FilesystemAttachment, error)
    76  
    77  	// Filesystem is required for filesystem functionality.
    78  	Filesystem(tag names.FilesystemTag) (state.Filesystem, error)
    79  
    80  	// AddStorageForUnit is required for storage add functionality.
    81  	AddStorageForUnit(tag names.UnitTag, name string, cons state.StorageConstraints) error
    82  
    83  	// GetBlockForType is required to block operations.
    84  	GetBlockForType(t state.BlockType) (state.Block, bool, error)
    85  }
    86  
    87  var getState = func(st *state.State) storageAccess {
    88  	return stateShim{st}
    89  }
    90  
    91  type stateShim struct {
    92  	*state.State
    93  }
    94  
    95  // UnitAssignedMachine returns the tag of the machine that the unit
    96  // is assigned to, or an error if the unit cannot be obtained or is
    97  // not assigned to a machine.
    98  func (s stateShim) UnitAssignedMachine(tag names.UnitTag) (names.MachineTag, error) {
    99  	unit, err := s.Unit(tag.Id())
   100  	if err != nil {
   101  		return names.MachineTag{}, errors.Trace(err)
   102  	}
   103  	mid, err := unit.AssignedMachineId()
   104  	if err != nil {
   105  		return names.MachineTag{}, errors.Trace(err)
   106  	}
   107  	return names.NewMachineTag(mid), nil
   108  }
   109  
   110  // ModelName returns the name of Juju environment,
   111  // or an error if environment configuration is not retrievable.
   112  func (s stateShim) ModelName() (string, error) {
   113  	cfg, err := s.State.ModelConfig()
   114  	if err != nil {
   115  		return "", errors.Trace(err)
   116  	}
   117  	return cfg.Name(), nil
   118  }