github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/agent/state.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"launchpad.net/juju-core/instance"
    10  	"launchpad.net/juju-core/state/api/base"
    11  	"launchpad.net/juju-core/state/api/params"
    12  )
    13  
    14  // State provides access to an agent's view of the state.
    15  type State struct {
    16  	caller base.Caller
    17  }
    18  
    19  // NewState returns a version of the state that provides functionality
    20  // required by agent code.
    21  func NewState(caller base.Caller) *State {
    22  	return &State{caller}
    23  }
    24  
    25  func (st *State) getEntity(tag string) (*params.AgentGetEntitiesResult, error) {
    26  	var results params.AgentGetEntitiesResults
    27  	args := params.Entities{
    28  		Entities: []params.Entity{{Tag: tag}},
    29  	}
    30  	err := st.caller.Call("Agent", "", "GetEntities", args, &results)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	if len(results.Entities) != 1 {
    35  		return nil, fmt.Errorf("expected one result, got %d", len(results.Entities))
    36  	}
    37  	if err := results.Entities[0].Error; err != nil {
    38  		return nil, err
    39  	}
    40  	return &results.Entities[0], nil
    41  }
    42  
    43  type Entity struct {
    44  	st  *State
    45  	tag string
    46  	doc params.AgentGetEntitiesResult
    47  }
    48  
    49  func (st *State) Entity(tag string) (*Entity, error) {
    50  	doc, err := st.getEntity(tag)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return &Entity{
    55  		st:  st,
    56  		tag: tag,
    57  		doc: *doc,
    58  	}, nil
    59  }
    60  
    61  // Tag returns the entity's tag.
    62  func (m *Entity) Tag() string {
    63  	return m.tag
    64  }
    65  
    66  // Life returns the current life cycle state of the entity.
    67  func (m *Entity) Life() params.Life {
    68  	return m.doc.Life
    69  }
    70  
    71  // Jobs returns the set of configured jobs
    72  // if the API is running on behalf of a machine agent.
    73  // When running for other agents, it will return
    74  // the empty list.
    75  func (m *Entity) Jobs() []params.MachineJob {
    76  	return m.doc.Jobs
    77  }
    78  
    79  // ContainerType returns the type of container hosting this entity.
    80  // If the entity is not a machine, it returns an empty string.
    81  func (m *Entity) ContainerType() instance.ContainerType {
    82  	return m.doc.ContainerType
    83  }
    84  
    85  // SetPassword sets the password associated with the agent's entity.
    86  func (m *Entity) SetPassword(password string) error {
    87  	var results params.ErrorResults
    88  	args := params.PasswordChanges{
    89  		Changes: []params.PasswordChange{{
    90  			Tag:      m.tag,
    91  			Password: password,
    92  		}},
    93  	}
    94  	err := m.st.caller.Call("Agent", "", "SetPasswords", args, &results)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	return results.OneError()
    99  }