github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  	"github.com/juju/names"
    10  
    11  	"github.com/juju/juju/api/base"
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/instance"
    14  	"github.com/juju/juju/state/multiwatcher"
    15  )
    16  
    17  // State provides access to an agent's view of the state.
    18  type State struct {
    19  	facade base.FacadeCaller
    20  }
    21  
    22  // NewState returns a version of the state that provides functionality
    23  // required by agent code.
    24  func NewState(caller base.APICaller) *State {
    25  	facadeCaller := base.NewFacadeCaller(caller, "Agent")
    26  	return &State{facadeCaller}
    27  }
    28  
    29  func (st *State) getEntity(tag names.Tag) (*params.AgentGetEntitiesResult, error) {
    30  	var results params.AgentGetEntitiesResults
    31  	args := params.Entities{
    32  		Entities: []params.Entity{{Tag: tag.String()}},
    33  	}
    34  	err := st.facade.FacadeCall("GetEntities", args, &results)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	if len(results.Entities) != 1 {
    39  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Entities))
    40  	}
    41  	if err := results.Entities[0].Error; err != nil {
    42  		return nil, err
    43  	}
    44  	return &results.Entities[0], nil
    45  }
    46  
    47  func (st *State) StateServingInfo() (params.StateServingInfo, error) {
    48  	var results params.StateServingInfo
    49  	err := st.facade.FacadeCall("StateServingInfo", nil, &results)
    50  	return results, err
    51  }
    52  
    53  // IsMaster reports whether the connected machine
    54  // agent lives at the same network address as the primary
    55  // mongo server for the replica set.
    56  // This call will return an error if the connected
    57  // agent is not a machine agent with environment-manager
    58  // privileges.
    59  func (st *State) IsMaster() (bool, error) {
    60  	var results params.IsMasterResult
    61  	err := st.facade.FacadeCall("IsMaster", nil, &results)
    62  	return results.Master, err
    63  }
    64  
    65  type Entity struct {
    66  	st  *State
    67  	tag names.Tag
    68  	doc params.AgentGetEntitiesResult
    69  }
    70  
    71  func (st *State) Entity(tag names.Tag) (*Entity, error) {
    72  	doc, err := st.getEntity(tag)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	return &Entity{
    77  		st:  st,
    78  		tag: tag,
    79  		doc: *doc,
    80  	}, nil
    81  }
    82  
    83  // Tag returns the entity's tag.
    84  func (m *Entity) Tag() string {
    85  	return m.tag.String()
    86  }
    87  
    88  // Life returns the current life cycle state of the entity.
    89  func (m *Entity) Life() params.Life {
    90  	return m.doc.Life
    91  }
    92  
    93  // Jobs returns the set of configured jobs
    94  // if the API is running on behalf of a machine agent.
    95  // When running for other agents, it will return
    96  // the empty list.
    97  func (m *Entity) Jobs() []multiwatcher.MachineJob {
    98  	return m.doc.Jobs
    99  }
   100  
   101  // ContainerType returns the type of container hosting this entity.
   102  // If the entity is not a machine, it returns an empty string.
   103  func (m *Entity) ContainerType() instance.ContainerType {
   104  	return m.doc.ContainerType
   105  }
   106  
   107  // SetPassword sets the password associated with the agent's entity.
   108  func (m *Entity) SetPassword(password string) error {
   109  	var results params.ErrorResults
   110  	args := params.EntityPasswords{
   111  		Changes: []params.EntityPassword{{
   112  			Tag:      m.tag.String(),
   113  			Password: password,
   114  		}},
   115  	}
   116  	err := m.st.facade.FacadeCall("SetPasswords", args, &results)
   117  	if err != nil {
   118  		return err
   119  	}
   120  	return results.OneError()
   121  }
   122  
   123  // ClearReboot clears the reboot flag of the machine.
   124  func (m *Entity) ClearReboot() error {
   125  	var result params.ErrorResults
   126  	args := params.SetStatus{
   127  		Entities: []params.EntityStatusArgs{
   128  			{Tag: m.tag.String()},
   129  		},
   130  	}
   131  	err := m.st.facade.FacadeCall("ClearReboot", args, &result)
   132  	if err != nil {
   133  		return err
   134  	}
   135  	return result.OneError()
   136  }