/
machineactions.go
109 lines (94 loc) · 3.73 KB
/
machineactions.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
// Copyright 2016 Canonical Ltd.
// Copyright 2016 Cloudbase Solutions
// Licensed under the AGPLv3, see LICENCE file for details.
// machineactions implements the the apiserver side of
// running actions on machines
package machineactions
import (
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
"gopkg.in/juju/names.v2"
)
type Backend interface {
ActionByTag(tag names.ActionTag) (state.Action, error)
FindEntity(tag names.Tag) (state.Entity, error)
TagToActionReceiverFn(findEntity func(names.Tag) (state.Entity, error)) func(string) (state.ActionReceiver, error)
ConvertActions(ar state.ActionReceiver, fn common.GetActionsFn) ([]params.ActionResult, error)
}
// Facade implements the machineactions interface and is the concrete
// implementation of the api end point.
type Facade struct {
backend Backend
resources facade.Resources
accessMachine common.AuthFunc
}
// NewFacade creates a new server-side machineactions API end point.
func NewFacade(
backend Backend,
resources facade.Resources,
authorizer facade.Authorizer,
) (*Facade, error) {
if !authorizer.AuthMachineAgent() {
return nil, common.ErrPerm
}
return &Facade{
backend: backend,
resources: resources,
accessMachine: authorizer.AuthOwner,
}, nil
}
// Actions returns the Actions by Tags passed and ensures that the machine asking
// for them is the machine that has the actions
func (f *Facade) Actions(args params.Entities) params.ActionResults {
actionFn := common.AuthAndActionFromTagFn(f.accessMachine, f.backend.ActionByTag)
return common.Actions(args, actionFn)
}
// BeginActions marks the actions represented by the passed in Tags as running.
func (f *Facade) BeginActions(args params.Entities) params.ErrorResults {
actionFn := common.AuthAndActionFromTagFn(f.accessMachine, f.backend.ActionByTag)
return common.BeginActions(args, actionFn)
}
// FinishActions saves the result of a completed Action
func (f *Facade) FinishActions(args params.ActionExecutionResults) params.ErrorResults {
actionFn := common.AuthAndActionFromTagFn(f.accessMachine, f.backend.ActionByTag)
return common.FinishActions(args, actionFn)
}
// WatchActionNotifications returns a StringsWatcher for observing
// incoming action calls to a machine.
func (f *Facade) WatchActionNotifications(args params.Entities) params.StringsWatchResults {
tagToActionReceiver := f.backend.TagToActionReceiverFn(f.backend.FindEntity)
watchOne := common.WatchOneActionReceiverNotifications(tagToActionReceiver, f.resources.Register)
return common.WatchActionNotifications(args, f.accessMachine, watchOne)
}
// RunningActions lists the actions running for the entities passed in.
// If we end up needing more than ListRunning at some point we could follow/abstract
// what's done in the client actions package.
func (f *Facade) RunningActions(args params.Entities) params.ActionsByReceivers {
canAccess := f.accessMachine
tagToActionReceiver := f.backend.TagToActionReceiverFn(f.backend.FindEntity)
response := params.ActionsByReceivers{
Actions: make([]params.ActionsByReceiver, len(args.Entities)),
}
for i, entity := range args.Entities {
currentResult := &response.Actions[i]
receiver, err := tagToActionReceiver(entity.Tag)
if err != nil {
currentResult.Error = common.ServerError(common.ErrBadId)
continue
}
currentResult.Receiver = receiver.Tag().String()
if !canAccess(receiver.Tag()) {
currentResult.Error = common.ServerError(common.ErrPerm)
continue
}
results, err := f.backend.ConvertActions(receiver, receiver.RunningActions)
if err != nil {
currentResult.Error = common.ServerError(err)
continue
}
currentResult.Actions = results
}
return response
}