/
unitfacade.go
217 lines (182 loc) · 5.75 KB
/
unitfacade.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package payloadshookcontext
import (
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v5"
api "github.com/juju/juju/api/client/payloads"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/core/payloads"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
)
var logger = loggo.GetLogger("juju.apiserver.payloadshookcontext")
// NewHookContextFacade returns a new payloads hook context facade for
// the State and Unit given. It is used for facade registration.
func NewHookContextFacade(st *state.State, unit *state.Unit) (*UnitFacade, error) {
up, err := st.UnitPayloads(unit)
if err != nil {
return nil, errors.Trace(err)
}
return NewUnitFacade(up), nil
}
// UnitPayloadBackend exposes the State functionality for a unit's payloads.
type UnitPayloadBackend interface {
// Track tracks a payload for the unit and info.
Track(info payloads.Payload) error
// List returns information on the payload with the id on the unit.
List(ids ...string) ([]payloads.Result, error)
// SetStatus sets the status for the payload with the given id on the unit.
SetStatus(id, status string) error
// LookUp returns the payload ID for the given name/rawID pair.
LookUp(name, rawID string) (string, error)
// Untrack removes the information for the payload with the given id.
Untrack(id string) error
}
// UnitFacade serves payload-specific API methods.
type UnitFacade struct {
backend UnitPayloadBackend
}
// NewUnitFacade builds a new facade for the given backend.
func NewUnitFacade(backend UnitPayloadBackend) *UnitFacade {
return &UnitFacade{backend: backend}
}
// Track stores a payload to be tracked in state.
func (uf UnitFacade) Track(args params.TrackPayloadArgs) (params.PayloadResults, error) {
logger.Debugf("tracking %d payloads from API", len(args.Payloads))
var r params.PayloadResults
for _, apiPayload := range args.Payloads {
pl, err := api.API2Payload(apiPayload)
if err != nil {
return r, errors.Trace(err)
}
logger.Debugf("tracking payload from API: %#v", pl)
id, err := uf.track(pl.Payload)
res := newPayloadResult(id, err)
r.Results = append(r.Results, res)
}
return r, nil
}
func (uf UnitFacade) track(pl payloads.Payload) (string, error) {
if err := uf.backend.Track(pl); err != nil {
return "", errors.Trace(err)
}
id, err := uf.backend.LookUp(pl.Name, pl.ID)
if err != nil {
return "", errors.Trace(err)
}
return id, nil
}
// List builds the list of payload being tracked for
// the given unit and IDs. If no IDs are provided then all tracked
// payloads for the unit are returned.
func (uf UnitFacade) List(args params.Entities) (params.PayloadResults, error) {
if len(args.Entities) == 0 {
return uf.listAll()
}
var ids []string
for _, entity := range args.Entities {
id, err := api.API2ID(entity.Tag)
if err != nil {
return params.PayloadResults{}, errors.Trace(err)
}
ids = append(ids, id)
}
results, err := uf.backend.List(ids...)
if err != nil {
return params.PayloadResults{}, errors.Trace(err)
}
var r params.PayloadResults
for _, result := range results {
res := Result2api(result)
r.Results = append(r.Results, res)
}
return r, nil
}
func (uf UnitFacade) listAll() (params.PayloadResults, error) {
var r params.PayloadResults
results, err := uf.backend.List()
if err != nil {
return r, errors.Trace(err)
}
for _, result := range results {
pl := result.Payload
id, err := uf.backend.LookUp(pl.Name, pl.ID)
if err != nil {
logger.Errorf("failed to look up ID for %q: %v", pl.FullID(), err)
id = ""
}
apipl := api.Payload2api(*pl)
res := newPayloadResult(id, nil)
res.Payload = &apipl
r.Results = append(r.Results, res)
}
return r, nil
}
// LookUp identifies the payload with the provided name and raw ID.
func (uf UnitFacade) LookUp(args params.LookUpPayloadArgs) (params.PayloadResults, error) {
var r params.PayloadResults
for _, arg := range args.Args {
id, err := uf.backend.LookUp(arg.Name, arg.ID)
res := newPayloadResult(id, err)
r.Results = append(r.Results, res)
}
return r, nil
}
// SetStatus sets the raw status of a payload.
func (uf UnitFacade) SetStatus(args params.SetPayloadStatusArgs) (params.PayloadResults, error) {
var r params.PayloadResults
for _, arg := range args.Args {
id, err := api.API2ID(arg.Tag)
if err != nil {
return r, errors.Trace(err)
}
err = uf.backend.SetStatus(id, arg.Status)
res := newPayloadResult(id, err)
r.Results = append(r.Results, res)
}
return r, nil
}
// Untrack marks the identified payload as no longer being tracked.
func (uf UnitFacade) Untrack(args params.Entities) (params.PayloadResults, error) {
var r params.PayloadResults
for _, entity := range args.Entities {
id, err := api.API2ID(entity.Tag)
if err != nil {
return r, errors.Trace(err)
}
err = uf.backend.Untrack(id)
res := newPayloadResult(id, err)
r.Results = append(r.Results, res)
}
return r, nil
}
// newPayloadResult builds a new PayloadResult from the provided tag
// and error. NotFound is also set based on the error.
func newPayloadResult(id string, err error) params.PayloadResult {
result := payloads.Result{
ID: id,
Payload: nil,
NotFound: errors.IsNotFound(err),
Error: err,
}
return Result2api(result)
}
// Result2api converts the payloads.Result into a PayloadResult.
func Result2api(result payloads.Result) params.PayloadResult {
res := params.PayloadResult{
NotFound: result.NotFound,
}
if result.ID != "" {
res.Tag = names.NewPayloadTag(result.ID).String()
}
if result.Payload != nil {
pl := api.Payload2api(*result.Payload)
res.Payload = &pl
}
if result.Error != nil {
res.Error = apiservererrors.ServerError(result.Error)
}
return res
}