github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/allwatcher.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api
     5  
     6  import (
     7  	"github.com/juju/juju/api/base"
     8  	"github.com/juju/juju/apiserver/params"
     9  	"github.com/juju/juju/state/multiwatcher"
    10  )
    11  
    12  // AllWatcher holds information allowing us to get Deltas describing
    13  // changes to the entire model or all models (depending on
    14  // the watcher type).
    15  type AllWatcher struct {
    16  	objType string
    17  	caller  base.APICaller
    18  	id      *string
    19  }
    20  
    21  // NewAllWatcher returns an AllWatcher instance which interacts with a
    22  // watcher created by the WatchAll API call.
    23  //
    24  // There should be no need to call this from outside of the api
    25  // package. It is only used by Client.WatchAll in this package.
    26  func NewAllWatcher(caller base.APICaller, id *string) *AllWatcher {
    27  	return newAllWatcher("AllWatcher", caller, id)
    28  }
    29  
    30  // NewAllModelWatcher returns an AllWatcher instance which interacts
    31  // with a watcher created by the WatchAllModels API call.
    32  //
    33  // There should be no need to call this from outside of the api
    34  // package. It is only used by Client.WatchAllModels in
    35  // api/controller.
    36  func NewAllModelWatcher(caller base.APICaller, id *string) *AllWatcher {
    37  	return newAllWatcher("AllModelWatcher", caller, id)
    38  }
    39  
    40  func newAllWatcher(objType string, caller base.APICaller, id *string) *AllWatcher {
    41  	return &AllWatcher{
    42  		objType: objType,
    43  		caller:  caller,
    44  		id:      id,
    45  	}
    46  }
    47  
    48  // Next returns a new set of deltas from a watcher previously created
    49  // by the WatchAll or WatchAllModels API calls. It will block until
    50  // there are deltas to return.
    51  func (watcher *AllWatcher) Next() ([]multiwatcher.Delta, error) {
    52  	var info params.AllWatcherNextResults
    53  	err := watcher.caller.APICall(
    54  		watcher.objType,
    55  		watcher.caller.BestFacadeVersion(watcher.objType),
    56  		*watcher.id,
    57  		"Next",
    58  		nil, &info,
    59  	)
    60  	return info.Deltas, err
    61  }
    62  
    63  // Stop shutdowns down a watcher previously created by the WatchAll or
    64  // WatchAllModels API calls
    65  func (watcher *AllWatcher) Stop() error {
    66  	return watcher.caller.APICall(
    67  		watcher.objType,
    68  		watcher.caller.BestFacadeVersion(watcher.objType),
    69  		*watcher.id,
    70  		"Stop",
    71  		nil, nil,
    72  	)
    73  }