github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/undertaker/state.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package undertaker
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/environs/config"
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  // State defines the needed methods of state.State
    15  // for the work of the undertaker API.
    16  type State interface {
    17  	state.EntityFinder
    18  
    19  	// Model returns the model entity.
    20  	Model() (Model, error)
    21  
    22  	// IsController returns true if this state instance has the bootstrap
    23  	// model UUID.
    24  	IsController() bool
    25  
    26  	// ProcessDyingModel checks if there are any machines or services left in
    27  	// state. If there are none, the model's life is changed from dying to dead.
    28  	ProcessDyingModel() (err error)
    29  
    30  	// RemoveAllModelDocs removes all documents from multi-environment
    31  	// collections.
    32  	RemoveAllModelDocs() error
    33  
    34  	// AllMachines returns all machines in the model ordered by id.
    35  	AllMachines() ([]Machine, error)
    36  
    37  	// AllApplications returns all deployed services in the model.
    38  	AllApplications() ([]Service, error)
    39  
    40  	// ModelConfig retrieves the model configuration.
    41  	ModelConfig() (*config.Config, error)
    42  }
    43  
    44  type stateShim struct {
    45  	*state.State
    46  }
    47  
    48  func (s *stateShim) AllMachines() ([]Machine, error) {
    49  	stateMachines, err := s.State.AllMachines()
    50  	if err != nil {
    51  		return nil, errors.Trace(err)
    52  	}
    53  
    54  	machines := make([]Machine, len(stateMachines))
    55  	for i := range stateMachines {
    56  		machines[i] = stateMachines[i]
    57  	}
    58  
    59  	return machines, nil
    60  }
    61  
    62  // Machine defines the needed methods of state.Machine for
    63  // the work of the undertaker API.
    64  type Machine interface {
    65  	// Watch returns a watcher for observing changes to a machine.
    66  	Watch() state.NotifyWatcher
    67  }
    68  
    69  func (s *stateShim) AllApplications() ([]Service, error) {
    70  	stateServices, err := s.State.AllApplications()
    71  	if err != nil {
    72  		return nil, errors.Trace(err)
    73  	}
    74  
    75  	services := make([]Service, len(stateServices))
    76  	for i := range stateServices {
    77  		services[i] = stateServices[i]
    78  	}
    79  
    80  	return services, nil
    81  }
    82  
    83  // Service defines the needed methods of state.Service for
    84  // the work of the undertaker API.
    85  type Service interface {
    86  	// Watch returns a watcher for observing changes to a service.
    87  	Watch() state.NotifyWatcher
    88  }
    89  
    90  func (s *stateShim) Model() (Model, error) {
    91  	return s.State.Model()
    92  }
    93  
    94  // Model defines the needed methods of state.Model for
    95  // the work of the undertaker API.
    96  type Model interface {
    97  
    98  	// Owner returns tag representing the owner of the model.
    99  	// The owner is the user that created the model.
   100  	Owner() names.UserTag
   101  
   102  	// Life returns whether the model is Alive, Dying or Dead.
   103  	Life() state.Life
   104  
   105  	// Name returns the human friendly name of the model.
   106  	Name() string
   107  
   108  	// UUID returns the universally unique identifier of the model.
   109  	UUID() string
   110  
   111  	// Destroy sets the model's lifecycle to Dying, preventing
   112  	// addition of services or machines to state.
   113  	Destroy() error
   114  }