github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/common/life.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/apiserver/params"
    12  )
    13  
    14  // Life requests the life cycle of the given entities from the given
    15  // server-side API facade via the given caller.
    16  func Life(caller base.FacadeCaller, tags []names.Tag) ([]params.LifeResult, error) {
    17  	if len(tags) == 0 {
    18  		return []params.LifeResult{}, nil
    19  	}
    20  	var result params.LifeResults
    21  	entities := make([]params.Entity, len(tags))
    22  	for i, t := range tags {
    23  		entities[i] = params.Entity{t.String()}
    24  	}
    25  	args := params.Entities{Entities: entities}
    26  	if err := caller.FacadeCall("Life", args, &result); err != nil {
    27  		return []params.LifeResult{}, err
    28  	}
    29  	return result.Results, nil
    30  }
    31  
    32  // OneLife requests the life cycle of the given entity from the given
    33  // server-side API facade via the given caller.
    34  func OneLife(caller base.FacadeCaller, tag names.Tag) (params.Life, error) {
    35  	result, err := Life(caller, []names.Tag{tag})
    36  	if err != nil {
    37  		return "", err
    38  	}
    39  	if len(result) != 1 {
    40  		return "", errors.Errorf("expected 1 result, got %d", len(result))
    41  	}
    42  	if err := result[0].Error; err != nil {
    43  		return "", err
    44  	}
    45  	return result[0].Life, nil
    46  }