github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"gopkg.in/juju/charm.v6"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/api/uniter"
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/core/model"
    15  	"github.com/juju/juju/core/watcher"
    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  	Relation(names.RelationTag) (Relation, error)
    26  	StorageAttachment(names.StorageTag, names.UnitTag) (params.StorageAttachment, error)
    27  	StorageAttachmentLife([]params.StorageAttachmentId) ([]params.LifeResult, error)
    28  	Unit(names.UnitTag) (Unit, error)
    29  	WatchRelationUnits(names.RelationTag, names.UnitTag) (watcher.RelationUnitsWatcher, error)
    30  	WatchStorageAttachment(names.StorageTag, names.UnitTag) (watcher.NotifyWatcher, error)
    31  	WatchUpdateStatusHookInterval() (watcher.NotifyWatcher, error)
    32  	UpdateStatusHookInterval() (time.Duration, error)
    33  }
    34  
    35  type Unit interface {
    36  	Life() params.Life
    37  	Refresh() error
    38  	Resolved() params.ResolvedMode
    39  	Application() (Application, error)
    40  	Tag() names.UnitTag
    41  	Watch() (watcher.NotifyWatcher, error)
    42  	WatchAddressesHash() (watcher.StringsWatcher, error)
    43  	WatchConfigSettingsHash() (watcher.StringsWatcher, error)
    44  	WatchTrustConfigSettingsHash() (watcher.StringsWatcher, error)
    45  	WatchUpgradeSeriesNotifications() (watcher.NotifyWatcher, error)
    46  	WatchLXDProfileUpgradeNotifications() (watcher.StringsWatcher, error)
    47  	WatchStorage() (watcher.StringsWatcher, error)
    48  	WatchActionNotifications() (watcher.StringsWatcher, error)
    49  	// WatchRelation returns a watcher that fires when relations
    50  	// relevant for this unit change.
    51  	WatchRelations() (watcher.StringsWatcher, error)
    52  	UpgradeSeriesStatus() (model.UpgradeSeriesStatus, error)
    53  }
    54  
    55  type Application interface {
    56  	// CharmModifiedVersion returns a revision number for the charm that
    57  	// increments whenever the charm or a resource for the charm changes.
    58  	CharmModifiedVersion() (int, error)
    59  	// CharmURL returns the url for the charm for this application.
    60  	CharmURL() (*charm.URL, bool, error)
    61  	// Life returns whether the application is alive.
    62  	Life() params.Life
    63  	// Refresh syncs this value with the api server.
    64  	Refresh() error
    65  	// Tag returns the tag for this application.
    66  	Tag() names.ApplicationTag
    67  	// Watch returns a watcher that fires when this application changes.
    68  	Watch() (watcher.NotifyWatcher, error)
    69  	// WatchLeadershipSettings returns a watcher that fires when the leadership
    70  	// settings for this application change.
    71  	WatchLeadershipSettings() (watcher.NotifyWatcher, error)
    72  }
    73  
    74  type Relation interface {
    75  	Id() int
    76  	Life() params.Life
    77  	Suspended() bool
    78  	UpdateSuspended(bool)
    79  }
    80  
    81  func NewAPIState(st *uniter.State) State {
    82  	return apiState{st}
    83  }
    84  
    85  type apiState struct {
    86  	*uniter.State
    87  }
    88  
    89  type apiUnit struct {
    90  	*uniter.Unit
    91  }
    92  
    93  type apiApplication struct {
    94  	*uniter.Application
    95  }
    96  
    97  type apiRelation struct {
    98  	*uniter.Relation
    99  }
   100  
   101  func (st apiState) Relation(tag names.RelationTag) (Relation, error) {
   102  	r, err := st.State.Relation(tag)
   103  	return apiRelation{r}, err
   104  }
   105  
   106  func (st apiState) Unit(tag names.UnitTag) (Unit, error) {
   107  	u, err := st.State.Unit(tag)
   108  	return apiUnit{u}, err
   109  }
   110  
   111  func (u apiUnit) Application() (Application, error) {
   112  	s, err := u.Unit.Application()
   113  	return apiApplication{s}, err
   114  }