github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/client/machinemanager/state.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machinemanager
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/apiserver/common/storagecommon"
    11  	"github.com/juju/juju/core/instance"
    12  	"github.com/juju/juju/core/status"
    13  	"github.com/juju/juju/environs/config"
    14  	"github.com/juju/juju/state"
    15  )
    16  
    17  type Backend interface {
    18  	state.CloudAccessor
    19  
    20  	Machine(string) (Machine, error)
    21  	Model() (Model, error)
    22  	GetBlockForType(t state.BlockType) (state.Block, bool, error)
    23  	AddOneMachine(template state.MachineTemplate) (*state.Machine, error)
    24  	AddMachineInsideNewMachine(template, parentTemplate state.MachineTemplate, containerType instance.ContainerType) (*state.Machine, error)
    25  	AddMachineInsideMachine(template state.MachineTemplate, parentId string, containerType instance.ContainerType) (*state.Machine, error)
    26  }
    27  
    28  type Pool interface {
    29  	GetModel(string) (Model, func(), error)
    30  }
    31  
    32  type Model interface {
    33  	Name() string
    34  	UUID() string
    35  	Cloud() string
    36  	CloudCredential() (names.CloudCredentialTag, bool)
    37  	CloudRegion() string
    38  	Config() (*config.Config, error)
    39  }
    40  
    41  type Machine interface {
    42  	Destroy() error
    43  	ForceDestroy() error
    44  	Series() string
    45  	Units() ([]Unit, error)
    46  	SetKeepInstance(keepInstance bool) error
    47  	CreateUpgradeSeriesLock([]string, string) error
    48  	RemoveUpgradeSeriesLock() error
    49  	CompleteUpgradeSeries() error
    50  	VerifyUnitsSeries(unitNames []string, series string, force bool) ([]Unit, error)
    51  	Principals() []string
    52  	WatchUpgradeSeriesNotifications() (state.NotifyWatcher, error)
    53  	GetUpgradeSeriesMessages() ([]string, bool, error)
    54  	IsManager() bool
    55  }
    56  
    57  type stateShim struct {
    58  	*state.State
    59  }
    60  
    61  func (s stateShim) Machine(name string) (Machine, error) {
    62  	m, err := s.State.Machine(name)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return machineShim{m}, nil
    67  }
    68  
    69  func (s stateShim) Model() (Model, error) {
    70  	return s.State.Model()
    71  }
    72  
    73  type poolShim struct {
    74  	pool *state.StatePool
    75  }
    76  
    77  func (p *poolShim) GetModel(uuid string) (Model, func(), error) {
    78  	m, ph, err := p.pool.GetModel(uuid)
    79  	if err != nil {
    80  		return nil, nil, errors.Trace(err)
    81  	}
    82  	return m, func() { ph.Release() }, nil
    83  }
    84  
    85  type machineShim struct {
    86  	*state.Machine
    87  }
    88  
    89  func (m machineShim) Units() ([]Unit, error) {
    90  	units, err := m.Machine.Units()
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	out := make([]Unit, len(units))
    95  	for i, u := range units {
    96  		out[i] = u
    97  	}
    98  	return out, nil
    99  }
   100  
   101  type Unit interface {
   102  	UnitTag() names.UnitTag
   103  	Name() string
   104  	AgentStatus() (status.StatusInfo, error)
   105  	Status() (status.StatusInfo, error)
   106  }
   107  
   108  func (m machineShim) VerifyUnitsSeries(unitNames []string, series string, force bool) ([]Unit, error) {
   109  	units, err := m.Machine.VerifyUnitsSeries(unitNames, series, force)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	out := make([]Unit, len(units))
   114  	for i, u := range units {
   115  		out[i] = u
   116  	}
   117  	return out, nil
   118  }
   119  
   120  type storageInterface interface {
   121  	storagecommon.StorageAccess
   122  	VolumeAccess() storagecommon.VolumeAccess
   123  	FilesystemAccess() storagecommon.FilesystemAccess
   124  }
   125  
   126  var getStorageState = func(st *state.State) (storageInterface, error) {
   127  	m, err := st.Model()
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	sb, err := state.NewStorageBackend(st)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	storageAccess := &storageShim{
   136  		StorageAccess: sb,
   137  		va:            sb,
   138  		fa:            sb,
   139  	}
   140  	// CAAS models don't support volume storage yet.
   141  	if m.Type() == state.ModelTypeCAAS {
   142  		storageAccess.va = nil
   143  	}
   144  	return storageAccess, nil
   145  }
   146  
   147  type storageShim struct {
   148  	storagecommon.StorageAccess
   149  	fa storagecommon.FilesystemAccess
   150  	va storagecommon.VolumeAccess
   151  }
   152  
   153  func (s *storageShim) VolumeAccess() storagecommon.VolumeAccess {
   154  	return s.va
   155  }
   156  
   157  func (s *storageShim) FilesystemAccess() storagecommon.FilesystemAccess {
   158  	return s.fa
   159  }