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

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package deployer
     5  
     6  import (
     7  	"github.com/juju/names"
     8  
     9  	"github.com/juju/juju/api/common"
    10  	"github.com/juju/juju/apiserver/params"
    11  )
    12  
    13  // Unit represents a juju unit as seen by the deployer worker.
    14  type Unit struct {
    15  	tag  names.UnitTag
    16  	life params.Life
    17  	st   *State
    18  }
    19  
    20  // Tag returns the unit's tag.
    21  func (u *Unit) Tag() string {
    22  	return u.tag.String()
    23  }
    24  
    25  // Name returns the unit's name.
    26  func (u *Unit) Name() string {
    27  	return u.tag.Id()
    28  }
    29  
    30  // Life returns the unit's lifecycle value.
    31  func (u *Unit) Life() params.Life {
    32  	return u.life
    33  }
    34  
    35  // Refresh updates the cached local copy of the unit's data.
    36  func (u *Unit) Refresh() error {
    37  	life, err := common.Life(u.st.facade, u.tag)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	u.life = life
    42  	return nil
    43  }
    44  
    45  // Remove removes the unit from state, calling EnsureDead first, then Remove.
    46  // It will fail if the unit is not present.
    47  func (u *Unit) Remove() error {
    48  	var result params.ErrorResults
    49  	args := params.Entities{
    50  		Entities: []params.Entity{{Tag: u.tag.String()}},
    51  	}
    52  	err := u.st.facade.FacadeCall("Remove", args, &result)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return result.OneError()
    57  }
    58  
    59  // SetPassword sets the unit's password.
    60  func (u *Unit) SetPassword(password string) error {
    61  	var result params.ErrorResults
    62  	args := params.EntityPasswords{
    63  		Changes: []params.EntityPassword{
    64  			{Tag: u.tag.String(), Password: password},
    65  		},
    66  	}
    67  	err := u.st.facade.FacadeCall("SetPasswords", args, &result)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	return result.OneError()
    72  }