forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
79 lines (70 loc) · 2.18 KB
/
api.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package applicationscaler
import (
"github.com/juju/errors"
"github.com/juju/loggo"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api/base"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/watcher"
)
var logger = loggo.GetLogger("juju.api.applicationscaler")
// NewWatcherFunc exists to let us test Watch properly.
type NewWatcherFunc func(base.APICaller, params.StringsWatchResult) watcher.StringsWatcher
// API makes calls to the ApplicationScaler facade.
type API struct {
caller base.FacadeCaller
newWatcher NewWatcherFunc
}
// NewAPI returns a new API using the supplied caller.
func NewAPI(caller base.APICaller, newWatcher NewWatcherFunc) *API {
return &API{
caller: base.NewFacadeCaller(caller, "ApplicationScaler"),
newWatcher: newWatcher,
}
}
// Watch returns a StringsWatcher that delivers the names of applications
// that may need to be rescaled.
func (api *API) Watch() (watcher.StringsWatcher, error) {
var result params.StringsWatchResult
err := api.caller.FacadeCall("Watch", nil, &result)
if err != nil {
return nil, errors.Trace(err)
}
if result.Error != nil {
return nil, errors.Trace(result.Error)
}
w := api.newWatcher(api.caller.RawAPICaller(), result)
return w, nil
}
// Rescale requests that all supplied application names be rescaled to
// their minimum configured sizes. It returns the first error it
// encounters.
func (api *API) Rescale(applications []string) error {
args := params.Entities{
Entities: make([]params.Entity, len(applications)),
}
for i, application := range applications {
if !names.IsValidApplication(application) {
return errors.NotValidf("application name %q", application)
}
tag := names.NewApplicationTag(application)
args.Entities[i].Tag = tag.String()
}
var results params.ErrorResults
err := api.caller.FacadeCall("Rescale", args, &results)
if err != nil {
return errors.Trace(err)
}
for _, result := range results.Results {
if result.Error != nil {
if err == nil {
err = result.Error
} else {
logger.Errorf("additional rescale error: %v", err)
}
}
}
return errors.Trace(err)
}