github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/unitassigner/unitassigner.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package unitassigner
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	apiwatcher "github.com/juju/juju/api/watcher"
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/watcher"
    14  )
    15  
    16  const uaFacade = "UnitAssigner"
    17  
    18  // API provides access to the UnitAssigner API facade.
    19  type API struct {
    20  	facade base.FacadeCaller
    21  }
    22  
    23  // New creates a new client-side UnitAssigner facade.
    24  func New(caller base.APICaller) API {
    25  	fc := base.NewFacadeCaller(caller, uaFacade)
    26  	return API{facade: fc}
    27  }
    28  
    29  // AssignUnits tells the controller to run whatever unit assignments it has.
    30  // Unit assignments for units that no longer exist will return an error that
    31  // satisfies errors.IsNotFound.
    32  func (a API) AssignUnits(tags []names.UnitTag) ([]error, error) {
    33  	entities := make([]params.Entity, len(tags))
    34  	for i, tag := range tags {
    35  		entities[i] = params.Entity{Tag: tag.String()}
    36  	}
    37  	args := params.Entities{Entities: entities}
    38  	var result params.ErrorResults
    39  	if err := a.facade.FacadeCall("AssignUnits", args, &result); err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	errs := make([]error, len(result.Results))
    44  	for i, e := range result.Results {
    45  		if e.Error != nil {
    46  			errs[i] = convertNotFound(e.Error)
    47  		}
    48  	}
    49  	return errs, nil
    50  }
    51  
    52  // convertNotFound converts param notfound errors into errors.notfound values.
    53  func convertNotFound(err error) error {
    54  	if params.IsCodeNotFound(err) {
    55  		return errors.NewNotFound(err, "")
    56  	}
    57  	return err
    58  }
    59  
    60  // WatchUnitAssignments watches the server for new unit assignments to be
    61  // created.
    62  func (a API) WatchUnitAssignments() (watcher.StringsWatcher, error) {
    63  	var result params.StringsWatchResult
    64  	err := a.facade.FacadeCall("WatchUnitAssignments", nil, &result)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	if result.Error != nil {
    69  		return nil, result.Error
    70  	}
    71  	w := apiwatcher.NewStringsWatcher(a.facade.RawAPICaller(), result)
    72  	return w, nil
    73  }
    74  
    75  // SetAgentStatus sets the status of the unit agents.
    76  func (a API) SetAgentStatus(args params.SetStatus) error {
    77  	var result params.ErrorResults
    78  	err := a.facade.FacadeCall("SetAgentStatus", args, &result)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	return result.Combine()
    83  }