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