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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package remotestate
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/names/v5"
    10  
    11  	"github.com/juju/juju/api/agent/uniter"
    12  	"github.com/juju/juju/core/life"
    13  	"github.com/juju/juju/core/model"
    14  	"github.com/juju/juju/core/watcher"
    15  	"github.com/juju/juju/rpc/params"
    16  )
    17  
    18  type Waiter interface {
    19  	After() <-chan time.Time
    20  }
    21  
    22  type UpdateStatusTimerFunc func(time.Duration) Waiter
    23  
    24  type State interface {
    25  	Charm(string) (Charm, error)
    26  	Relation(names.RelationTag) (Relation, error)
    27  	StorageAttachment(names.StorageTag, names.UnitTag) (params.StorageAttachment, error)
    28  	StorageAttachmentLife([]params.StorageAttachmentId) ([]params.LifeResult, error)
    29  	Unit(names.UnitTag) (Unit, error)
    30  	WatchRelationUnits(names.RelationTag, names.UnitTag) (watcher.RelationUnitsWatcher, error)
    31  	WatchStorageAttachment(names.StorageTag, names.UnitTag) (watcher.NotifyWatcher, error)
    32  	WatchUpdateStatusHookInterval() (watcher.NotifyWatcher, error)
    33  	UpdateStatusHookInterval() (time.Duration, error)
    34  }
    35  
    36  type Unit interface {
    37  	Life() life.Value
    38  	LXDProfileName() (string, error)
    39  	Refresh() error
    40  	ProviderID() string
    41  	Resolved() params.ResolvedMode
    42  	Application() (Application, error)
    43  	Tag() names.UnitTag
    44  	Watch() (watcher.NotifyWatcher, error)
    45  	WatchAddressesHash() (watcher.StringsWatcher, error)
    46  	WatchConfigSettingsHash() (watcher.StringsWatcher, error)
    47  	WatchTrustConfigSettingsHash() (watcher.StringsWatcher, error)
    48  	WatchUpgradeSeriesNotifications() (watcher.NotifyWatcher, error)
    49  	WatchInstanceData() (watcher.NotifyWatcher, error)
    50  	WatchStorage() (watcher.StringsWatcher, error)
    51  	WatchActionNotifications() (watcher.StringsWatcher, error)
    52  	// WatchRelations returns a watcher that fires when relations
    53  	// relevant for this unit change.
    54  	WatchRelations() (watcher.StringsWatcher, error)
    55  	UpgradeSeriesStatus() (model.UpgradeSeriesStatus, string, error)
    56  }
    57  
    58  type Application interface {
    59  	// CharmModifiedVersion returns a revision number for the charm that
    60  	// increments whenever the charm or a resource for the charm changes.
    61  	CharmModifiedVersion() (int, error)
    62  	// CharmURL returns the url for the charm for this application.
    63  	CharmURL() (string, bool, error)
    64  	// Life returns whether the application is alive.
    65  	Life() life.Value
    66  	// Refresh syncs this value with the api server.
    67  	Refresh() error
    68  	// Tag returns the tag for this application.
    69  	Tag() names.ApplicationTag
    70  	// Watch returns a watcher that fires when this application changes.
    71  	Watch() (watcher.NotifyWatcher, error)
    72  	// WatchLeadershipSettings returns a watcher that fires when the leadership
    73  	// settings for this application change.
    74  	WatchLeadershipSettings() (watcher.NotifyWatcher, error)
    75  }
    76  
    77  type Relation interface {
    78  	Id() int
    79  	Tag() names.RelationTag
    80  	Life() life.Value
    81  	Suspended() bool
    82  	UpdateSuspended(bool)
    83  }
    84  
    85  type Charm interface {
    86  	// LXDProfileRequired returns true if this charm has an lxdprofile.yaml
    87  	LXDProfileRequired() (bool, error)
    88  }
    89  
    90  func NewAPIState(st *uniter.State) State {
    91  	return apiState{st}
    92  }
    93  
    94  type apiState struct {
    95  	*uniter.State
    96  }
    97  
    98  type apiUnit struct {
    99  	*uniter.Unit
   100  }
   101  
   102  type apiApplication struct {
   103  	*uniter.Application
   104  }
   105  
   106  type apiRelation struct {
   107  	*uniter.Relation
   108  }
   109  
   110  func (st apiState) Relation(tag names.RelationTag) (Relation, error) {
   111  	r, err := st.State.Relation(tag)
   112  	return apiRelation{r}, err
   113  }
   114  
   115  func (st apiState) Unit(tag names.UnitTag) (Unit, error) {
   116  	u, err := st.State.Unit(tag)
   117  	return apiUnit{u}, err
   118  }
   119  
   120  func (st apiState) Charm(charmURL string) (Charm, error) {
   121  	return st.State.Charm(charmURL)
   122  }
   123  
   124  func (u apiUnit) Application() (Application, error) {
   125  	s, err := u.Unit.Application()
   126  	return apiApplication{s}, err
   127  }