github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/common/watch.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "fmt" 8 9 "gopkg.in/juju/names.v2" 10 11 "github.com/juju/juju/api/base" 12 apiwatcher "github.com/juju/juju/api/watcher" 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/watcher" 15 ) 16 17 // Watch starts a NotifyWatcher for the entity with the specified tag. 18 func Watch(facade base.FacadeCaller, tag names.Tag) (watcher.NotifyWatcher, error) { 19 var results params.NotifyWatchResults 20 args := params.Entities{ 21 Entities: []params.Entity{{Tag: tag.String()}}, 22 } 23 err := facade.FacadeCall("Watch", args, &results) 24 if err != nil { 25 return nil, err 26 } 27 if len(results.Results) != 1 { 28 return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results)) 29 } 30 result := results.Results[0] 31 if result.Error != nil { 32 return nil, result.Error 33 } 34 return apiwatcher.NewNotifyWatcher(facade.RawAPICaller(), result), nil 35 }