github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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 "github.com/juju/juju/instance" 10 "github.com/juju/juju/state/api/base" 11 "github.com/juju/juju/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 1 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 func (st *State) StateServingInfo() (params.StateServingInfo, error) { 44 var results params.StateServingInfo 45 err := st.caller.Call("Agent", "", "StateServingInfo", nil, &results) 46 return results, err 47 } 48 49 // IsMaster reports whether the connected machine 50 // agent lives at the same network address as the primary 51 // mongo server for the replica set. 52 // This call will return an error if the connected 53 // agent is not a machine agent with environment-manager 54 // privileges. 55 func (st *State) IsMaster() (bool, error) { 56 var results params.IsMasterResult 57 err := st.caller.Call("Agent", "", "IsMaster", nil, &results) 58 return results.Master, err 59 } 60 61 type Entity struct { 62 st *State 63 tag string 64 doc params.AgentGetEntitiesResult 65 } 66 67 func (st *State) Entity(tag string) (*Entity, error) { 68 doc, err := st.getEntity(tag) 69 if err != nil { 70 return nil, err 71 } 72 return &Entity{ 73 st: st, 74 tag: tag, 75 doc: *doc, 76 }, nil 77 } 78 79 // Tag returns the entity's tag. 80 func (m *Entity) Tag() string { 81 return m.tag 82 } 83 84 // Life returns the current life cycle state of the entity. 85 func (m *Entity) Life() params.Life { 86 return m.doc.Life 87 } 88 89 // Jobs returns the set of configured jobs 90 // if the API is running on behalf of a machine agent. 91 // When running for other agents, it will return 92 // the empty list. 93 func (m *Entity) Jobs() []params.MachineJob { 94 return m.doc.Jobs 95 } 96 97 // ContainerType returns the type of container hosting this entity. 98 // If the entity is not a machine, it returns an empty string. 99 func (m *Entity) ContainerType() instance.ContainerType { 100 return m.doc.ContainerType 101 } 102 103 // SetPassword sets the password associated with the agent's entity. 104 func (m *Entity) SetPassword(password string) error { 105 var results params.ErrorResults 106 args := params.EntityPasswords{ 107 Changes: []params.EntityPassword{{ 108 Tag: m.tag, 109 Password: password, 110 }}, 111 } 112 err := m.st.caller.Call("Agent", "", "SetPasswords", args, &results) 113 if err != nil { 114 return err 115 } 116 return results.OneError() 117 }