github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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  	"github.com/juju/names"
    10  
    11  	"github.com/juju/juju/api/base"
    12  	"github.com/juju/juju/api/watcher"
    13  	"github.com/juju/juju/apiserver/params"
    14  )
    15  
    16  // Watch starts a NotifyWatcher for the entity with the specified tag.
    17  func Watch(facade base.FacadeCaller, tag names.Tag) (watcher.NotifyWatcher, error) {
    18  	var results params.NotifyWatchResults
    19  	args := params.Entities{
    20  		Entities: []params.Entity{{Tag: tag.String()}},
    21  	}
    22  	err := facade.FacadeCall("Watch", args, &results)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	if len(results.Results) != 1 {
    27  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
    28  	}
    29  	result := results.Results[0]
    30  	if result.Error != nil {
    31  		return nil, result.Error
    32  	}
    33  	return watcher.NewNotifyWatcher(facade.RawAPICaller(), result), nil
    34  }