-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner.go
61 lines (51 loc) · 1.46 KB
/
cleaner.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// The cleaner package implements the API interface
// used by the cleaner worker.
package cleaner
import (
"github.com/juju/loggo"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
)
func init() {
common.RegisterStandardFacade("Cleaner", 1, NewCleanerAPI)
}
var logger = loggo.GetLogger("juju.apiserver.cleaner")
// CleanerAPI implements the API used by the cleaner worker.
type CleanerAPI struct {
st StateInterface
resources *common.Resources
}
// NewCleanerAPI creates a new instance of the Cleaner API.
func NewCleanerAPI(
st *state.State,
res *common.Resources,
authorizer common.Authorizer,
) (*CleanerAPI, error) {
if !authorizer.AuthEnvironManager() {
return nil, common.ErrPerm
}
return &CleanerAPI{
st: getState(st),
resources: res,
}, nil
}
// Cleanup triggers a state cleanup
func (api *CleanerAPI) Cleanup() error {
return api.st.Cleanup()
}
// WatchChanges watches for cleanups to be perfomed in state
func (api *CleanerAPI) WatchCleanups() (params.NotifyWatchResult, error) {
watch := api.st.WatchCleanups()
if _, ok := <-watch.Changes(); ok {
return params.NotifyWatchResult{
NotifyWatcherId: api.resources.Register(watch),
}, nil
}
return params.NotifyWatchResult{
Error: common.ServerError(watcher.EnsureErr(watch)),
}, nil
}