github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/state/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/state/api/params"
    10  )
    11  
    12  // Unit represents a juju unit as seen by the deployer worker.
    13  type Unit struct {
    14  	tag  string
    15  	life params.Life
    16  	st   *State
    17  }
    18  
    19  // Tag returns the unit's tag.
    20  func (u *Unit) Tag() string {
    21  	return u.tag
    22  }
    23  
    24  // Name returns the unit's name.
    25  func (u *Unit) Name() string {
    26  	_, name, err := names.ParseTag(u.tag, names.UnitTagKind)
    27  	if err != nil {
    28  		panic(err)
    29  	}
    30  	return name
    31  }
    32  
    33  // Life returns the unit's lifecycle value.
    34  func (u *Unit) Life() params.Life {
    35  	return u.life
    36  }
    37  
    38  // Refresh updates the cached local copy of the unit's data.
    39  func (u *Unit) Refresh() error {
    40  	life, err := u.st.unitLife(u.tag)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	u.life = life
    45  	return nil
    46  }
    47  
    48  // Remove removes the unit from state, calling EnsureDead first, then Remove.
    49  // It will fail if the unit is not present.
    50  func (u *Unit) Remove() error {
    51  	var result params.ErrorResults
    52  	args := params.Entities{
    53  		Entities: []params.Entity{{Tag: u.tag}},
    54  	}
    55  	err := u.st.call("Remove", args, &result)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	return result.OneError()
    60  }
    61  
    62  // SetPassword sets the unit's password.
    63  func (u *Unit) SetPassword(password string) error {
    64  	var result params.ErrorResults
    65  	args := params.EntityPasswords{
    66  		Changes: []params.EntityPassword{
    67  			{Tag: u.tag, Password: password},
    68  		},
    69  	}
    70  	err := u.st.call("SetPasswords", args, &result)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	return result.OneError()
    75  }