github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/facades/client/storage/shim.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  	"time"
     8  
     9  	"github.com/juju/names/v5"
    10  
    11  	"github.com/juju/juju/apiserver/common/storagecommon"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  // This file contains untested shims to let us wrap state in a sensible
    16  // interface and avoid writing tests that depend on mongodb. If you were
    17  // to change any part of it so that it were no longer *obviously* and
    18  // *trivially* correct, you would be Doing It Wrong.
    19  
    20  type storageAccess interface {
    21  	storageInterface
    22  	storageVolume
    23  	storageFile
    24  }
    25  
    26  type storageInterface interface {
    27  	// StorageInstance is required for storage functionality.
    28  	StorageInstance(names.StorageTag) (state.StorageInstance, error)
    29  
    30  	// AllStorageInstances is required for storage functionality.
    31  	AllStorageInstances() ([]state.StorageInstance, error)
    32  
    33  	// RemoveStoragePool removes a pool only if its not currently in use
    34  	RemoveStoragePool(poolName string) error
    35  
    36  	// StorageAttachments is required for storage functionality.
    37  	StorageAttachments(names.StorageTag) ([]state.StorageAttachment, error)
    38  
    39  	// UnitStorageAttachments returns the storage attachments for the
    40  	// identified unit.
    41  	UnitStorageAttachments(names.UnitTag) ([]state.StorageAttachment, error)
    42  
    43  	// AddStorageForUnit is required for storage add functionality.
    44  	AddStorageForUnit(tag names.UnitTag, name string, cons state.StorageConstraints) ([]names.StorageTag, error)
    45  
    46  	// AttachStorage attaches the storage instance with the
    47  	// specified tag to the unit with the specified tag.
    48  	AttachStorage(names.StorageTag, names.UnitTag) error
    49  
    50  	// DetachStorage detaches the storage instance with the
    51  	// specified tag from the unit with the specified tag.
    52  	DetachStorage(names.StorageTag, names.UnitTag, bool, time.Duration) error
    53  
    54  	// DestroyStorageInstance destroys the storage instance with the specified tag.
    55  	DestroyStorageInstance(names.StorageTag, bool, bool, time.Duration) error
    56  
    57  	// ReleaseStorageInstance releases the storage instance with the specified tag.
    58  	ReleaseStorageInstance(names.StorageTag, bool, bool, time.Duration) error
    59  }
    60  
    61  type storageVolume interface {
    62  	storagecommon.VolumeAccess
    63  
    64  	// AllVolumes is required for volume functionality.
    65  	AllVolumes() ([]state.Volume, error)
    66  
    67  	// VolumeAttachments is required for volume functionality.
    68  	VolumeAttachments(volume names.VolumeTag) ([]state.VolumeAttachment, error)
    69  
    70  	VolumeAttachmentPlans(volume names.VolumeTag) ([]state.VolumeAttachmentPlan, error)
    71  
    72  	// MachineVolumeAttachments is required for volume functionality.
    73  	MachineVolumeAttachments(machine names.MachineTag) ([]state.VolumeAttachment, error)
    74  
    75  	// Volume is required for volume functionality.
    76  	Volume(tag names.VolumeTag) (state.Volume, error)
    77  
    78  	// AddExistingFilesystem imports an existing filesystem into the model.
    79  	AddExistingFilesystem(f state.FilesystemInfo, v *state.VolumeInfo, storageName string) (names.StorageTag, error)
    80  }
    81  
    82  type storageFile interface {
    83  	storagecommon.FilesystemAccess
    84  
    85  	// AllFilesystems is required for filesystem functionality.
    86  	AllFilesystems() ([]state.Filesystem, error)
    87  
    88  	// FilesystemAttachments is required for filesystem functionality.
    89  	FilesystemAttachments(filesystem names.FilesystemTag) ([]state.FilesystemAttachment, error)
    90  
    91  	// MachineFilesystemAttachments is required for filesystem functionality.
    92  	MachineFilesystemAttachments(machine names.MachineTag) ([]state.FilesystemAttachment, error)
    93  
    94  	// Filesystem is required for filesystem functionality.
    95  	Filesystem(tag names.FilesystemTag) (state.Filesystem, error)
    96  
    97  	// AddExistingFilesystem imports an existing filesystem into the model.
    98  	AddExistingFilesystem(f state.FilesystemInfo, v *state.VolumeInfo, storageName string) (names.StorageTag, error)
    99  }
   100  
   101  var getStorageAccessor = func(st *state.State) (storageAccess, error) {
   102  	sb, err := state.NewStorageBackend(st)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	return sb, nil
   107  }
   108  
   109  type backend interface {
   110  	ControllerTag() names.ControllerTag
   111  	ModelTag() names.ModelTag
   112  	Unit(string) (Unit, error)
   113  	GetBlockForType(state.BlockType) (state.Block, bool, error)
   114  }
   115  
   116  type Unit interface {
   117  	AssignedMachineId() (string, error)
   118  }
   119  
   120  type stateShim struct {
   121  	*state.State
   122  }
   123  
   124  func (s stateShim) ModelTag() names.ModelTag {
   125  	return names.NewModelTag(s.ModelUUID())
   126  }
   127  
   128  func (s stateShim) GetBlockForType(t state.BlockType) (state.Block, bool, error) {
   129  	return s.State.GetBlockForType(t)
   130  }
   131  
   132  func (s stateShim) Unit(name string) (Unit, error) {
   133  	return s.State.Unit(name)
   134  }