/
machiner.go
176 lines (157 loc) · 5.17 KB
/
machiner.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package machine
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/common/networkingcommon"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/core/model"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
)
// MachinerAPI implements the API used by the machiner worker.
type MachinerAPI struct {
*common.LifeGetter
*common.StatusSetter
*common.DeadEnsurer
*common.AgentEntityWatcher
*common.APIAddresser
*networkingcommon.NetworkConfigAPI
st *state.State
auth facade.Authorizer
getCanModify common.GetAuthFunc
getCanRead common.GetAuthFunc
}
// NewMachinerAPIForState creates a new instance of the Machiner API.
func NewMachinerAPIForState(ctrlSt, st *state.State, resources facade.Resources, authorizer facade.Authorizer) (*MachinerAPI, error) {
if !authorizer.AuthMachineAgent() {
return nil, apiservererrors.ErrPerm
}
getCanAccess := func() (common.AuthFunc, error) {
return authorizer.AuthOwner, nil
}
netConfigAPI, err := networkingcommon.NewNetworkConfigAPI(st, getCanAccess)
if err != nil {
return nil, errors.Annotate(err, "instantiating network config API")
}
return &MachinerAPI{
LifeGetter: common.NewLifeGetter(st, getCanAccess),
StatusSetter: common.NewStatusSetter(st, getCanAccess),
DeadEnsurer: common.NewDeadEnsurer(st, nil, getCanAccess),
AgentEntityWatcher: common.NewAgentEntityWatcher(st, resources, getCanAccess),
APIAddresser: common.NewAPIAddresser(ctrlSt, resources),
NetworkConfigAPI: netConfigAPI,
st: st,
auth: authorizer,
getCanModify: getCanAccess,
getCanRead: getCanAccess,
}, nil
}
func (api *MachinerAPI) getMachine(tag string, authChecker common.AuthFunc) (*state.Machine, error) {
mtag, err := names.ParseMachineTag(tag)
if err != nil {
return nil, apiservererrors.ErrPerm
} else if !authChecker(mtag) {
return nil, apiservererrors.ErrPerm
}
entity, err := api.st.FindEntity(mtag)
if err != nil {
return nil, err
}
return entity.(*state.Machine), nil
}
func (api *MachinerAPI) SetMachineAddresses(args params.SetMachinesAddresses) (params.ErrorResults, error) {
results := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.MachineAddresses)),
}
canModify, err := api.getCanModify()
if err != nil {
return results, err
}
for i, arg := range args.MachineAddresses {
m, err := api.getMachine(arg.Tag, canModify)
if err != nil {
results.Results[i].Error = apiservererrors.ServerError(err)
continue
}
addresses, err := params.ToProviderAddresses(arg.Addresses...).ToSpaceAddresses(api.st)
if err != nil {
results.Results[i].Error = apiservererrors.ServerError(err)
continue
}
if err := m.SetMachineAddresses(addresses...); err != nil {
results.Results[i].Error = apiservererrors.ServerError(err)
}
}
return results, nil
}
// Jobs returns the jobs assigned to the given entities.
func (api *MachinerAPI) Jobs(args params.Entities) (params.JobsResults, error) {
result := params.JobsResults{
Results: make([]params.JobsResult, len(args.Entities)),
}
canRead, err := api.getCanRead()
if err != nil {
return result, err
}
for i, agent := range args.Entities {
machine, err := api.getMachine(agent.Tag, canRead)
if err != nil {
result.Results[i].Error = apiservererrors.ServerError(err)
continue
}
machineJobs := machine.Jobs()
jobs := make([]model.MachineJob, len(machineJobs))
for i, job := range machineJobs {
jobs[i] = job.ToParams()
}
result.Results[i].Jobs = jobs
}
return result, nil
}
// RecordAgentStartTime updates the agent start time field in the machine doc.
func (api *MachinerAPI) RecordAgentStartTime(args params.Entities) (params.ErrorResults, error) {
results := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Entities)),
}
canModify, err := api.getCanModify()
if err != nil {
return results, err
}
for i, entity := range args.Entities {
m, err := api.getMachine(entity.Tag, canModify)
if err != nil {
results.Results[i].Error = apiservererrors.ServerError(err)
continue
}
if err := m.RecordAgentStartInformation(""); err != nil {
results.Results[i].Error = apiservererrors.ServerError(err)
}
}
return results, nil
}
// RecordAgentStartInformation syncs the machine model with information
// reported by a machine agent when it starts.
func (api *MachinerAPI) RecordAgentStartInformation(args params.RecordAgentStartInformationArgs) (params.ErrorResults, error) {
results := params.ErrorResults{
Results: make([]params.ErrorResult, len(args.Args)),
}
canModify, err := api.getCanModify()
if err != nil {
return results, err
}
for i, arg := range args.Args {
m, err := api.getMachine(arg.Tag, canModify)
if err != nil {
results.Results[i].Error = apiservererrors.ServerError(err)
continue
}
if err := m.RecordAgentStartInformation(arg.Hostname); err != nil {
results.Results[i].Error = apiservererrors.ServerError(err)
}
}
return results, nil
}