github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/agent/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/v5"
     8  
     9  	"github.com/juju/juju/api/common"
    10  	"github.com/juju/juju/core/life"
    11  	"github.com/juju/juju/core/status"
    12  	"github.com/juju/juju/rpc/params"
    13  )
    14  
    15  // Unit represents a juju unit as seen by the deployer worker.
    16  type Unit struct {
    17  	tag  names.UnitTag
    18  	life life.Value
    19  	st   *State
    20  }
    21  
    22  // Tag returns the unit's tag.
    23  func (u *Unit) Tag() string {
    24  	return u.tag.String()
    25  }
    26  
    27  // Name returns the unit's name.
    28  func (u *Unit) Name() string {
    29  	return u.tag.Id()
    30  }
    31  
    32  // Life returns the unit's lifecycle value.
    33  func (u *Unit) Life() life.Value {
    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 := common.OneLife(u.st.facade, 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.String()}},
    53  	}
    54  	err := u.st.facade.FacadeCall("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.EntityPasswords{
    65  		Changes: []params.EntityPassword{
    66  			{Tag: u.tag.String(), Password: password},
    67  		},
    68  	}
    69  	err := u.st.facade.FacadeCall("SetPasswords", args, &result)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	return result.OneError()
    74  }
    75  
    76  // SetStatus sets the status of the unit.
    77  func (u *Unit) SetStatus(unitStatus status.Status, info string, data map[string]interface{}) error {
    78  	var result params.ErrorResults
    79  	args := params.SetStatus{
    80  		Entities: []params.EntityStatusArgs{
    81  			{Tag: u.tag.String(), Status: unitStatus.String(), Info: info, Data: data},
    82  		},
    83  	}
    84  	err := u.st.facade.FacadeCall("SetStatus", args, &result)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	return result.OneError()
    89  }