forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
258 lines (238 loc) · 7.31 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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package caasoperator
import (
"github.com/juju/errors"
"github.com/juju/version"
"gopkg.in/juju/charm.v6"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api/base"
"github.com/juju/juju/api/common"
apiwatcher "github.com/juju/juju/api/watcher"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/status"
"github.com/juju/juju/core/watcher"
)
// 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()
}
// Charm returns information about the charm currently assigned
// to the application, including url, force upgrade and sha etc.
func (c *Client) Charm(application string) (_ *charm.URL, forceUpgrade bool, sha256 string, vers int, _ error) {
tag, err := c.appTag(application)
if err != nil {
return nil, false, "", 0, 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, false, "", 0, errors.Trace(err)
}
if n := len(results.Results); n != 1 {
return nil, false, "", 0, errors.Errorf("expected 1 result, got %d", n)
}
if err := results.Results[0].Error; err != nil {
return nil, false, "", 0, errors.Trace(err)
}
result := results.Results[0].Result
curl, err := charm.ParseURL(result.URL)
if err != nil {
return nil, false, "", 0, errors.Trace(err)
}
return curl, result.ForceUpgrade, result.SHA256, result.CharmModifiedVersion, nil
}
// SetPodSpec sets the pod spec of the specified application.
func (c *Client) SetPodSpec(appName string, spec string) error {
tag, err := applicationTag(appName)
if err != nil {
return errors.Trace(err)
}
var result params.ErrorResults
args := params.SetPodSpecParams{
Specs: []params.EntityString{{
Tag: tag.String(),
Value: spec,
}},
}
if err := c.facade.FacadeCall("SetPodSpec", args, &result); err != nil {
return errors.Trace(err)
}
return result.OneError()
}
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
}
return result.OneError()
}
// 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 life.Value(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()
}