github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/interface.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/names/v5"
    10  	"github.com/juju/version/v2"
    11  
    12  	"github.com/juju/juju/cloud"
    13  	"github.com/juju/juju/core/instance"
    14  	"github.com/juju/juju/core/status"
    15  	"github.com/juju/juju/environs/config"
    16  	"github.com/juju/juju/tools"
    17  )
    18  
    19  // EntityFinder is implemented by *State. See State.FindEntity
    20  // for documentation on the method.
    21  type EntityFinder interface {
    22  	FindEntity(tag names.Tag) (Entity, error)
    23  }
    24  
    25  // Entity represents any entity that can be returned
    26  // by State.FindEntity. All entities have a tag.
    27  type Entity interface {
    28  	Tag() names.Tag
    29  }
    30  
    31  // EntityWithApplication is implemented by Units it is intended
    32  // for anything that can return its Application.
    33  type EntityWithApplication interface {
    34  	Application() (*Application, error)
    35  }
    36  
    37  // Lifer represents an entity with a life.
    38  type Lifer interface {
    39  	Life() Life
    40  }
    41  
    42  // AgentTooler is implemented by entities
    43  // that have associated agent tools.
    44  type AgentTooler interface {
    45  	AgentTools() (*tools.Tools, error)
    46  	SetAgentVersion(version.Binary) error
    47  }
    48  
    49  // EnsureDeader with an EnsureDead method.
    50  type EnsureDeader interface {
    51  	EnsureDead() error
    52  }
    53  
    54  // Remover represents entities with a Remove method.
    55  type Remover interface {
    56  	Remove() error
    57  }
    58  
    59  // Authenticator represents entites capable of handling password
    60  // authentication.
    61  type Authenticator interface {
    62  	Refresh() error
    63  	SetPassword(pass string) error
    64  	PasswordValid(pass string) bool
    65  }
    66  
    67  // NotifyWatcherFactory represents an entity that
    68  // can be watched.
    69  type NotifyWatcherFactory interface {
    70  	Watch() NotifyWatcher
    71  }
    72  
    73  // AgentEntity represents an entity that can
    74  // have an agent responsible for it.
    75  type AgentEntity interface {
    76  	Entity
    77  	Lifer
    78  	Authenticator
    79  	AgentTooler
    80  	status.StatusSetter
    81  	EnsureDeader
    82  	Remover
    83  	NotifyWatcherFactory
    84  }
    85  
    86  // CloudAccessor defines the methods needed to obtain information
    87  // about clouds and credentials.
    88  type CloudAccessor interface {
    89  	Cloud(cloud string) (cloud.Cloud, error)
    90  	Clouds() (map[names.CloudTag]cloud.Cloud, error)
    91  	CloudCredential(tag names.CloudCredentialTag) (Credential, error)
    92  }
    93  
    94  // ModelAccessor defines the methods needed to watch for model
    95  // config changes, and read the model config.
    96  type ModelAccessor interface {
    97  	WatchForModelConfigChanges() NotifyWatcher
    98  	ModelConfig() (*config.Config, error)
    99  }
   100  
   101  // UnitsWatcher defines the methods needed to retrieve an entity (a
   102  // machine or an application) and watch its units.
   103  type UnitsWatcher interface {
   104  	Entity
   105  	WatchUnits() StringsWatcher
   106  }
   107  
   108  // ModelMachinesWatcher defines the methods required for listening to
   109  // machine lifecycle events or a combination of lifecycle events and changes
   110  // to the agent start time field.
   111  // WatchModelMachines.
   112  type ModelMachinesWatcher interface {
   113  	WatchModelMachines() StringsWatcher
   114  	WatchModelMachineStartTimes(quiesceInterval time.Duration) StringsWatcher
   115  }
   116  
   117  // InstanceIdGetter defines a single method - InstanceId.
   118  type InstanceIdGetter interface {
   119  	InstanceId() (instance.Id, error)
   120  }
   121  
   122  // ActionsWatcher defines the methods an entity exposes to watch Actions
   123  // queued up for itself
   124  type ActionsWatcher interface {
   125  	Entity
   126  	WatchActionNotifications() StringsWatcher
   127  	WatchPendingActionNotifications() StringsWatcher
   128  }
   129  
   130  // ActionReceiver describes Entities that can have Actions queued for
   131  // them, and that can get ActionRelated information about those actions.
   132  // TODO(jcw4) consider implementing separate Actor classes for this
   133  // interface; for example UnitActor that implements this interface, and
   134  // takes a Unit and performs all these actions.
   135  type ActionReceiver interface {
   136  	Entity
   137  
   138  	// PrepareActionPayload returns the payload to use in creating an action for this receiver.
   139  	PrepareActionPayload(name string, payload map[string]interface{}, parallel *bool, executionGroup *string) (map[string]interface{}, bool, string, error)
   140  
   141  	// CancelAction removes a pending Action from the queue for this
   142  	// ActionReceiver and marks it as cancelled.
   143  	CancelAction(action Action) (Action, error)
   144  
   145  	// WatchActionNotifications returns a StringsWatcher that will notify
   146  	// on changes to the queued actions for this ActionReceiver.
   147  	WatchActionNotifications() StringsWatcher
   148  
   149  	// WatchPendingActionNotifications returns a StringsWatcher that will notify
   150  	// on pending queued actions for this ActionReceiver.
   151  	WatchPendingActionNotifications() StringsWatcher
   152  
   153  	// Actions returns the list of Actions queued and completed for this
   154  	// ActionReceiver.
   155  	Actions() ([]Action, error)
   156  
   157  	// CompletedActions returns the list of Actions completed for this
   158  	// ActionReceiver.
   159  	CompletedActions() ([]Action, error)
   160  
   161  	// PendingActions returns the list of Actions queued for this
   162  	// ActionReceiver.
   163  	PendingActions() ([]Action, error)
   164  
   165  	// RunningActions returns the list of Actions currently running for
   166  	// this ActionReceiver.
   167  	RunningActions() ([]Action, error)
   168  }
   169  
   170  // GlobalEntity specifies entity.
   171  type GlobalEntity interface {
   172  	globalKey() string
   173  	Tag() names.Tag
   174  }
   175  
   176  // Action represents  an instance of an action designated for a unit or machine
   177  // in the model.
   178  type Action interface {
   179  	Entity
   180  
   181  	// Id returns the local id of the Action.
   182  	Id() string
   183  
   184  	// Receiver returns the Name of the ActionReceiver for which this action
   185  	// is enqueued.  Usually this is a Unit Name().
   186  	Receiver() string
   187  
   188  	// Name returns the name of the action, as defined in the charm.
   189  	Name() string
   190  
   191  	// Parameters will contain a structure representing arguments or parameters to
   192  	// an action, and is expected to be validated by the Unit using the Charm
   193  	// definition of the Action.
   194  	Parameters() map[string]interface{}
   195  
   196  	// Parallel returns true if the action can run without
   197  	// needed to acquire the machine lock.
   198  	Parallel() bool
   199  
   200  	// ExecutionGroup is the group of actions which cannot
   201  	// execute in parallel with each other.
   202  	ExecutionGroup() string
   203  
   204  	// Enqueued returns the time the action was added to state as a pending
   205  	// Action.
   206  	Enqueued() time.Time
   207  
   208  	// Started returns the time that the Action execution began.
   209  	Started() time.Time
   210  
   211  	// Completed returns the completion time of the Action.
   212  	Completed() time.Time
   213  
   214  	// Status returns the final state of the action.
   215  	Status() ActionStatus
   216  
   217  	// Results returns the structured output of the action and any error.
   218  	Results() (map[string]interface{}, string)
   219  
   220  	// ActionTag returns an ActionTag constructed from this action's
   221  	// Prefix and Sequence.
   222  	ActionTag() names.ActionTag
   223  
   224  	// Begin marks an action as running, and logs the time it was started.
   225  	// It asserts that the action is currently pending.
   226  	Begin() (Action, error)
   227  
   228  	// Finish removes action from the pending queue and captures the output
   229  	// and end state of the action.
   230  	Finish(results ActionResults) (Action, error)
   231  
   232  	// Log adds message to the action's progress message array.
   233  	Log(message string) error
   234  
   235  	// Messages returns the action's progress messages.
   236  	Messages() []ActionMessage
   237  
   238  	// Cancel or Abort the action.
   239  	Cancel() (Action, error)
   240  
   241  	// Refresh the contents of the action.
   242  	Refresh() error
   243  }
   244  
   245  // ApplicationEntity represents a local or remote application.
   246  type ApplicationEntity interface {
   247  	status.StatusGetter
   248  
   249  	// Life returns the life status of the application.
   250  	Life() Life
   251  
   252  	// IsRemote returns true if the application is remote (hosted in a different model).
   253  	IsRemote() bool
   254  
   255  	// Endpoints returns the application's currently available relation endpoints.
   256  	Endpoints() ([]Endpoint, error)
   257  
   258  	// Endpoint returns the relation endpoint with the supplied name, if it exists.
   259  	Endpoint(relationName string) (Endpoint, error)
   260  
   261  	// Relations returns a Relation for every relation the application is in.
   262  	Relations() (relations []*Relation, err error)
   263  }