/
upgradeseries.go
107 lines (96 loc) · 3.31 KB
/
upgradeseries.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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/api/base"
apiwatcher "github.com/juju/juju/api/watcher"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/watcher"
"github.com/juju/juju/rpc/params"
)
// UpgradeSeriesAPI provides common agent-side API functions to
// call into apiserver.common/UpgradeSeries
type UpgradeSeriesAPI struct {
facade base.FacadeCaller
tag names.Tag
}
// NewUpgradeSeriesAPI creates a UpgradeSeriesAPI on the specified facade,
// and uses this name when calling through the caller.
func NewUpgradeSeriesAPI(facade base.FacadeCaller, tag names.Tag) *UpgradeSeriesAPI {
return &UpgradeSeriesAPI{facade: facade, tag: tag}
}
// WatchUpgradeSeriesNotifications returns a NotifyWatcher for observing the state of
// a series upgrade.
func (u *UpgradeSeriesAPI) WatchUpgradeSeriesNotifications() (watcher.NotifyWatcher, error) {
var results params.NotifyWatchResults
args := params.Entities{
Entities: []params.Entity{{Tag: u.tag.String()}},
}
err := u.facade.FacadeCall("WatchUpgradeSeriesNotifications", args, &results)
if err != nil {
return nil, err
}
if len(results.Results) != 1 {
return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return nil, result.Error
}
w := apiwatcher.NewNotifyWatcher(u.facade.RawAPICaller(), result)
return w, nil
}
// UpgradeSeriesUnitStatus returns the upgrade series status of a
// unit from remote state.
func (u *UpgradeSeriesAPI) UpgradeSeriesUnitStatus() (model.UpgradeSeriesStatus, string, error) {
var results params.UpgradeSeriesStatusResults
args := params.Entities{
Entities: []params.Entity{{Tag: u.tag.String()}},
}
err := u.facade.FacadeCall("UpgradeSeriesUnitStatus", args, &results)
if err != nil {
return "", "", err
}
if len(results.Results) != 1 {
return "", "", errors.Errorf("expected 1 result, got %d", len(results.Results))
}
res := results.Results[0]
if res.Error != nil {
// TODO (externalreality) The code to convert api errors (with
// error codes) back to normal Go errors is in bad spot and
// causes import cycles which is why we don't use it here and may
// be the reason why it has few uses despite being useful.
if params.IsCodeNotFound(res.Error) {
return "", "", errors.NewNotFound(res.Error, "")
}
return "", "", res.Error
}
return res.Status, res.Target, nil
}
// SetUpgradeSeriesUnitStatus sets the upgrade series status of the
// unit in the remote state.
func (u *UpgradeSeriesAPI) SetUpgradeSeriesUnitStatus(status model.UpgradeSeriesStatus, reason string) error {
var results params.ErrorResults
args := params.UpgradeSeriesStatusParams{
Params: []params.UpgradeSeriesStatusParam{{
Entity: params.Entity{Tag: u.tag.String()},
Status: status,
Message: reason,
}},
}
err := u.facade.FacadeCall("SetUpgradeSeriesUnitStatus", args, &results)
if err != nil {
return err
}
if len(results.Results) != 1 {
return errors.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if result.Error != nil {
return apiservererrors.RestoreError(result.Error)
}
return nil
}