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