forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machiner.go
83 lines (75 loc) · 2.38 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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// The machiner package implements the API interface
// used by the machiner worker.
package machine
import (
"github.com/juju/errors"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state/apiserver/common"
)
// MachinerAPI implements the API used by the machiner worker.
type MachinerAPI struct {
*common.LifeGetter
*common.StatusSetter
*common.DeadEnsurer
*common.AgentEntityWatcher
*common.APIAddresser
st *state.State
auth common.Authorizer
getCanModify common.GetAuthFunc
getCanRead common.GetAuthFunc
}
// NewMachinerAPI creates a new instance of the Machiner API.
func NewMachinerAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*MachinerAPI, error) {
if !authorizer.AuthMachineAgent() {
return nil, common.ErrPerm
}
getCanModify := func() (common.AuthFunc, error) {
return authorizer.AuthOwner, nil
}
getCanRead := func() (common.AuthFunc, error) {
return authorizer.AuthOwner, nil
}
return &MachinerAPI{
LifeGetter: common.NewLifeGetter(st, getCanRead),
StatusSetter: common.NewStatusSetter(st, getCanModify),
DeadEnsurer: common.NewDeadEnsurer(st, getCanModify),
AgentEntityWatcher: common.NewAgentEntityWatcher(st, resources, getCanRead),
APIAddresser: common.NewAPIAddresser(st, resources),
st: st,
auth: authorizer,
getCanModify: getCanModify,
}, nil
}
func (api *MachinerAPI) getMachine(tag string) (*state.Machine, error) {
entity, err := api.st.FindEntity(tag)
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 params.ErrorResults{}, err
}
for i, arg := range args.MachineAddresses {
err := common.ErrPerm
if canModify(arg.Tag) {
var m *state.Machine
m, err = api.getMachine(arg.Tag)
if err == nil {
err = m.SetMachineAddresses(arg.Addresses...)
} else if errors.IsNotFound(err) {
err = common.ErrPerm
}
}
results.Results[i].Error = common.ServerError(err)
}
return results, nil
}