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