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