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