/
client.go
306 lines (281 loc) · 8.67 KB
/
client.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package caasoperator
import (
"github.com/juju/charm/v12"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/version/v2"
"github.com/juju/juju/api/base"
"github.com/juju/juju/api/common"
apiwatcher "github.com/juju/juju/api/watcher"
"github.com/juju/juju/caas"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/status"
"github.com/juju/juju/core/watcher"
"github.com/juju/juju/rpc/params"
)
// Client allows access to the CAAS operator API endpoint.
type Client struct {
facade base.FacadeCaller
*common.APIAddresser
}
// NewClient returns a client used to access the CAAS Operator API.
func NewClient(caller base.APICaller) *Client {
facadeCaller := base.NewFacadeCaller(caller, "CAASOperator")
return &Client{
facade: facadeCaller,
APIAddresser: common.NewAPIAddresser(facadeCaller),
}
}
func (c *Client) appTag(application string) (names.ApplicationTag, error) {
if !names.IsValidApplication(application) {
return names.ApplicationTag{}, errors.NotValidf("application name %q", application)
}
return names.NewApplicationTag(application), nil
}
// Model returns the model entity.
func (c *Client) Model() (*model.Model, error) {
var result params.ModelResult
err := c.facade.FacadeCall("CurrentModel", nil, &result)
if err != nil {
return nil, err
}
if err := result.Error; err != nil {
return nil, err
}
modelType := model.ModelType(result.Type)
if modelType == "" {
modelType = model.IAAS
}
return &model.Model{
Name: result.Name,
UUID: result.UUID,
ModelType: modelType,
}, nil
}
// SetStatus sets the status of the specified application.
func (c *Client) SetStatus(
application string,
status status.Status,
info string,
data map[string]interface{},
) error {
tag, err := c.appTag(application)
if err != nil {
return errors.Trace(err)
}
var result params.ErrorResults
args := params.SetStatus{
Entities: []params.EntityStatusArgs{{
Tag: tag.String(),
Status: status.String(),
Info: info,
Data: data,
}},
}
if err := c.facade.FacadeCall("SetStatus", args, &result); err != nil {
return errors.Trace(err)
}
return result.OneError()
}
// CharmInfo holds info about the charm for an application.
type CharmInfo struct {
// URL holds the URL of the charm assigned to the
// application.
URL *charm.URL
// ForceUpgrade indicates whether or not application
// units should upgrade to the charm even if they
// are in an error state.
ForceUpgrade bool
// SHA256 holds the SHA256 hash of the charm archive.
SHA256 string
// CharmModifiedVersion increases when the charm changes in some way.
CharmModifiedVersion int
// DeploymentMode is either "operator" or "workload"
DeploymentMode caas.DeploymentMode
}
// Charm returns information about the charm currently assigned
// to the application, including url, force upgrade and sha etc.
func (c *Client) Charm(application string) (*CharmInfo, error) {
tag, err := c.appTag(application)
if err != nil {
return nil, errors.Trace(err)
}
var results params.ApplicationCharmResults
args := params.Entities{
Entities: []params.Entity{{Tag: tag.String()}},
}
if err := c.facade.FacadeCall("Charm", args, &results); err != nil {
return nil, errors.Trace(err)
}
if n := len(results.Results); n != 1 {
return nil, errors.Trace(err)
}
if err := results.Results[0].Error; err != nil {
if params.IsCodeNotFound(err) {
return nil, errors.NotFoundf("application %q", application)
}
return nil, errors.Trace(err)
}
result := results.Results[0].Result
curl, err := charm.ParseURL(result.URL)
if err != nil {
return nil, errors.Trace(err)
}
// Older controllers don't set DeploymentMode, all charms are workload charms.
if result.DeploymentMode == "" {
result.DeploymentMode = string(caas.ModeWorkload)
}
return &CharmInfo{
URL: curl,
ForceUpgrade: result.ForceUpgrade,
SHA256: result.SHA256,
CharmModifiedVersion: result.CharmModifiedVersion,
DeploymentMode: caas.DeploymentMode(result.DeploymentMode),
}, nil
}
func applicationTag(application string) (names.ApplicationTag, error) {
if !names.IsValidApplication(application) {
return names.ApplicationTag{}, errors.NotValidf("application name %q", application)
}
return names.NewApplicationTag(application), nil
}
// Watch returns a watcher for observing changes to an application.
func (c *Client) Watch(application string) (watcher.NotifyWatcher, error) {
tag, err := c.appTag(application)
if err != nil {
return nil, errors.Trace(err)
}
return common.Watch(c.facade, "Watch", tag)
}
func entities(tags ...names.Tag) params.Entities {
entities := params.Entities{
Entities: make([]params.Entity, len(tags)),
}
for i, tag := range tags {
entities.Entities[i].Tag = tag.String()
}
return entities
}
// WatchUnits returns a StringsWatcher that notifies of
// changes to the lifecycles of units of the specified
// CAAS application in the current model.
func (c *Client) WatchUnits(application string) (watcher.StringsWatcher, error) {
applicationTag, err := applicationTag(application)
if err != nil {
return nil, errors.Trace(err)
}
args := entities(applicationTag)
var results params.StringsWatchResults
if err := c.facade.FacadeCall("WatchUnits", args, &results); err != nil {
return nil, err
}
if n := len(results.Results); n != 1 {
return nil, errors.Errorf("expected 1 result, got %d", n)
}
if err := results.Results[0].Error; err != nil {
return nil, errors.Trace(err)
}
w := apiwatcher.NewStringsWatcher(c.facade.RawAPICaller(), results.Results[0])
return w, nil
}
// RemoveUnit removes the specified unit from the current model.
func (c *Client) RemoveUnit(unitName string) error {
if !names.IsValidUnit(unitName) {
return errors.NotValidf("unit name %q", unitName)
}
var result params.ErrorResults
args := params.Entities{
Entities: []params.Entity{{Tag: names.NewUnitTag(unitName).String()}},
}
err := c.facade.FacadeCall("Remove", args, &result)
if err != nil {
return err
}
resultErr := result.OneError()
if params.IsCodeNotFound(resultErr) {
return nil
}
return resultErr
}
// Life returns the lifecycle state for the specified CAAS application
// or unit in the current model.
func (c *Client) Life(entityName string) (life.Value, error) {
var tag names.Tag
switch {
case names.IsValidApplication(entityName):
tag = names.NewApplicationTag(entityName)
case names.IsValidUnit(entityName):
tag = names.NewUnitTag(entityName)
default:
return "", errors.NotValidf("application or unit name %q", entityName)
}
args := entities(tag)
var results params.LifeResults
if err := c.facade.FacadeCall("Life", args, &results); err != nil {
return "", err
}
if n := len(results.Results); n != 1 {
return "", errors.Errorf("expected 1 result, got %d", n)
}
if err := results.Results[0].Error; err != nil {
return "", maybeNotFound(err)
}
return results.Results[0].Life, nil
}
// maybeNotFound returns an error satisfying errors.IsNotFound
// if the supplied error has a CodeNotFound error.
func maybeNotFound(err *params.Error) error {
if err == nil || !params.IsCodeNotFound(err) {
return err
}
return errors.NewNotFound(err, "")
}
// SetVersion sets the tools version associated with
// the given application.
func (c *Client) SetVersion(appName string, v version.Binary) error {
if !names.IsValidApplication(appName) {
return errors.NotValidf("application name %q", appName)
}
var results params.ErrorResults
args := params.EntitiesVersion{
AgentTools: []params.EntityVersion{{
Tag: names.NewApplicationTag(appName).String(),
Tools: ¶ms.Version{v},
}},
}
err := c.facade.FacadeCall("SetTools", args, &results)
if err != nil {
return errors.Trace(err)
}
return results.OneError()
}
// WatchContainerStart watchs for Unit starts via the CAAS provider.
func (c *Client) WatchContainerStart(application string, containerName string) (watcher.StringsWatcher, error) {
applicationTag, err := applicationTag(application)
if err != nil {
return nil, errors.Trace(err)
}
args := params.WatchContainerStartArgs{
Args: []params.WatchContainerStartArg{{
Entity: params.Entity{
Tag: applicationTag.String(),
},
Container: containerName,
}},
}
var results params.StringsWatchResults
if err := c.facade.FacadeCall("WatchContainerStart", args, &results); err != nil {
return nil, err
}
if n := len(results.Results); n != 1 {
return nil, errors.Errorf("expected 1 result, got %d", n)
}
if err := results.Results[0].Error; err != nil {
return nil, errors.Trace(err)
}
w := apiwatcher.NewStringsWatcher(c.facade.RawAPICaller(), results.Results[0])
return w, nil
}