forked from juju/juju
/
crossmodel.go
369 lines (335 loc) · 11.5 KB
/
crossmodel.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// Package crossmodel provides an API server facade for managing
// cross model relations.
package crossmodel
import (
"fmt"
"sort"
"github.com/juju/errors"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
jujucrossmodel "github.com/juju/juju/core/crossmodel"
"github.com/juju/juju/feature"
)
func init() {
common.RegisterStandardFacadeForFeature("CrossModelRelations", 1, NewAPI, feature.CrossModelRelations)
}
// API implements the cross model interface and is the concrete
// implementation of the api end point.
type API struct {
authorizer facade.Authorizer
getApplicationOffers func(interface{}) jujucrossmodel.ApplicationOffers
backend Backend
statePool StatePool
}
// createAPI returns a new cross model API facade.
func createAPI(
getApplicationOffers func(interface{}) jujucrossmodel.ApplicationOffers,
backend Backend,
statePool StatePool,
authorizer facade.Authorizer,
) (*API, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
}
api := &API{
authorizer: authorizer,
getApplicationOffers: getApplicationOffers,
backend: backend,
statePool: statePool,
}
return api, nil
}
// NewAPI returns a new cross model API facade.
func NewAPI(ctx facade.Context) (*API, error) {
return createAPI(getApplicationOffers, getStateAccess(ctx.State()), getStatePool(ctx.StatePool()), ctx.Auth())
}
// Offer makes application endpoints available for consumption at a specified URL.
func (api *API) Offer(all params.AddApplicationOffers) (params.ErrorResults, error) {
result := make([]params.ErrorResult, len(all.Offers))
for i, one := range all.Offers {
applicationOfferParams, err := api.makeAddOfferArgsFromParams(one)
if err != nil {
result[i].Error = common.ServerError(err)
continue
}
_, err = api.getApplicationOffers(api.backend).AddOffer(applicationOfferParams)
result[i].Error = common.ServerError(err)
}
return params.ErrorResults{Results: result}, nil
}
func (api *API) makeAddOfferArgsFromParams(addOfferParams params.AddApplicationOffer) (jujucrossmodel.AddApplicationOfferArgs, error) {
result := jujucrossmodel.AddApplicationOfferArgs{
OfferName: addOfferParams.OfferName,
ApplicationName: addOfferParams.ApplicationName,
ApplicationDescription: addOfferParams.ApplicationDescription,
Endpoints: addOfferParams.Endpoints,
}
if result.OfferName == "" {
result.OfferName = result.ApplicationName
}
application, err := api.backend.Application(addOfferParams.ApplicationName)
if err != nil {
return result, errors.Annotatef(err, "getting offered application %v", addOfferParams.ApplicationName)
}
if result.ApplicationDescription == "" {
ch, _, err := application.Charm()
if err != nil {
return result,
errors.Annotatef(err, "getting charm for application %v", addOfferParams.ApplicationName)
}
result.ApplicationDescription = ch.Meta().Description
}
return result, nil
}
// ApplicationOffers gets details about remote applications that match given URLs.
func (api *API) ApplicationOffers(urls params.ApplicationURLs) (params.ApplicationOffersResults, error) {
var results params.ApplicationOffersResults
results.Results = make([]params.ApplicationOfferResult, len(urls.ApplicationURLs))
for i, urlStr := range urls.ApplicationURLs {
offer, err := api.offerForURL(urlStr)
if err != nil {
results.Results[i].Error = common.ServerError(err)
continue
}
results.Results[i].Result = offer.ApplicationOffer
}
return results, nil
}
// modelForName looks up the model details for the named model.
func (api *API) modelForName(modelName, userName string) (Model, bool, error) {
user := api.authorizer.GetAuthTag().(names.UserTag)
if userName != "" {
user = names.NewUserTag(userName)
}
var model Model
models, err := api.backend.ModelsForUser(user)
if err != nil {
return nil, false, err
}
for _, m := range models {
if m.Model().Name() == modelName {
model = m.Model()
break
}
}
return model, model != nil, nil
}
// offerForURL finds the single offer for a specified (possibly relative) URL,
// returning the offer and full URL.
func (api *API) offerForURL(urlStr string) (params.ApplicationOfferDetails, error) {
fail := func(err error) (params.ApplicationOfferDetails, error) {
return params.ApplicationOfferDetails{}, errors.Trace(err)
}
url, err := jujucrossmodel.ParseApplicationURL(urlStr)
if err != nil {
return fail(errors.Trace(err))
}
if url.Source != "" {
err = errors.NotSupportedf("query for non-local application offers")
return fail(errors.Trace(err))
}
model, ok, err := api.modelForName(url.ModelName, url.User)
if err != nil {
return fail(errors.Trace(err))
}
if !ok {
err = errors.NotFoundf("model %q", url.ModelName)
return fail(err)
}
filter := jujucrossmodel.ApplicationOfferFilter{
OfferName: url.ApplicationName,
}
offers, err := api.applicationOffersFromModel(model.UUID(), filter)
if err != nil {
return fail(errors.Trace(err))
}
if len(offers) == 0 {
err := errors.NotFoundf("application offer %q", url.ApplicationName)
return fail(err)
}
if len(offers) > 1 {
err := errors.Errorf("too many application offers for %q", url.ApplicationName)
return fail(err)
}
fullURL := fmt.Sprintf("%s/%s.%s", model.Owner().Name(), model.Name(), url.ApplicationName)
offer := offers[0]
offer.OfferURL = fullURL
return offer, nil
}
// ApplicationOffers gets details about remote applications that match given URLs.
func (api *API) applicationOffersFromModel(modelUUID string, filters ...jujucrossmodel.ApplicationOfferFilter) ([]params.ApplicationOfferDetails, error) {
backend := api.backend
if modelUUID != api.backend.ModelUUID() {
st, releaser, err := api.statePool.Get(modelUUID)
if err != nil {
return nil, errors.Trace(err)
}
backend = st
defer releaser()
}
offers, err := api.getApplicationOffers(backend).ListOffers(filters...)
if err != nil {
return nil, errors.Trace(err)
}
var results []params.ApplicationOfferDetails
for _, offer := range offers {
app, err := backend.Application(offer.ApplicationName)
if err != nil {
return nil, errors.Trace(err)
}
curl, _ := app.CharmURL()
status, err := backend.RemoteConnectionStatus(offer.OfferName)
if err != nil {
return nil, errors.Trace(err)
}
offerDetails := params.ApplicationOfferDetails{
ApplicationOffer: makeOfferParamsFromOffer(offer),
ApplicationName: app.Name(),
CharmName: curl.Name,
ConnectedCount: status.ConnectionCount(),
}
results = append(results, offerDetails)
}
return results, nil
}
func makeOfferParamsFromOffer(offer jujucrossmodel.ApplicationOffer) params.ApplicationOffer {
result := params.ApplicationOffer{
OfferName: offer.OfferName,
ApplicationDescription: offer.ApplicationDescription,
}
for alias, ep := range offer.Endpoints {
result.Endpoints = append(result.Endpoints, params.RemoteEndpoint{
Name: alias,
Interface: ep.Interface,
Role: ep.Role,
Scope: ep.Scope,
Limit: ep.Limit,
})
}
return result
}
// FindApplicationOffers gets details about remote applications that match given filter.
func (api *API) FindApplicationOffers(filters params.OfferFilters) (params.FindApplicationOffersResults, error) {
var result params.FindApplicationOffersResults
offers, err := api.getApplicationOffersDetails(filters)
if err != nil {
return result, err
}
for _, offer := range offers {
result.Results = append(result.Results, offer.ApplicationOffer)
}
return result, nil
}
// ListApplicationOffers gets deployed details about application offers that match given filter.
// The results contain details about the deployed applications such as connection count.
func (api *API) ListApplicationOffers(filters params.OfferFilters) (params.ListApplicationOffersResults, error) {
var result params.ListApplicationOffersResults
offers, err := api.getApplicationOffersDetails(filters)
if err != nil {
return result, err
}
result.Results = offers
return result, nil
}
// getModelFilters splits the specified filters per model and returns
// the model and filter details for each.
func (api *API) getModelFilters(filters params.OfferFilters) (
models map[string]Model,
filtersPerModel map[string][]jujucrossmodel.ApplicationOfferFilter,
_ error,
) {
models = make(map[string]Model)
filtersPerModel = make(map[string][]jujucrossmodel.ApplicationOfferFilter)
// Group the filters per model and then query each model with the relevant filters
// for that model.
modelUUIDs := make(map[string]string)
for _, f := range filters.Filters {
// Default model is the current model.
modelUUID := api.backend.ModelUUID()
model, ok := models[modelUUID]
if !ok {
var err error
model, err = api.backend.Model()
if err != nil {
return nil, nil, errors.Trace(err)
}
models[modelUUID] = model
}
// If the filter contains a model name, look up the details.
if f.ModelName != "" {
if modelUUID, ok = modelUUIDs[f.ModelName]; !ok {
var err error
model, ok, err := api.modelForName(f.ModelName, "")
if err != nil {
return nil, nil, errors.Trace(err)
}
if !ok {
err := errors.NotFoundf("model %q", f.ModelName)
return nil, nil, errors.Trace(err)
}
// Record the UUID and model for next time.
modelUUID = model.UUID()
modelUUIDs[f.ModelName] = modelUUID
models[modelUUID] = model
}
}
// Record the filter and model details against the model UUID.
filters := filtersPerModel[modelUUID]
filters = append(filters, makeOfferFilterFromParams(f))
filtersPerModel[modelUUID] = filters
}
return models, filtersPerModel, nil
}
// getApplicationOffersDetails gets details about remote applications that match given filter.
func (api *API) getApplicationOffersDetails(filters params.OfferFilters) ([]params.ApplicationOfferDetails, error) {
// Gather all the filter details for doing a query for each model.
models, filtersPerModel, err := api.getModelFilters(filters)
if err != nil {
return nil, common.ServerError(errors.Trace(err))
}
if len(filtersPerModel) == 0 {
thisModelUUID := api.backend.ModelUUID()
filtersPerModel[thisModelUUID] = []jujucrossmodel.ApplicationOfferFilter{}
model, err := api.backend.Model()
if err != nil {
return nil, common.ServerError(errors.Trace(err))
}
models[thisModelUUID] = model
}
// Ensure the result is deterministic.
var allUUIDs []string
for modelUUID := range filtersPerModel {
allUUIDs = append(allUUIDs, modelUUID)
}
sort.Strings(allUUIDs)
// Do the per model queries.
var result []params.ApplicationOfferDetails
for _, modelUUID := range allUUIDs {
filters := filtersPerModel[modelUUID]
offers, err := api.applicationOffersFromModel(modelUUID, filters...)
if err != nil {
return nil, common.ServerError(errors.Trace(err))
}
model := models[modelUUID]
baseURL := fmt.Sprintf("%s/%s", model.Owner().Name(), model.Name())
for _, offer := range offers {
url := fmt.Sprintf("%s.%s", baseURL, offer.OfferName)
offer.OfferURL = url
result = append(result, offer)
}
}
return result, nil
}
func makeOfferFilterFromParams(filter params.OfferFilter) jujucrossmodel.ApplicationOfferFilter {
offerFilter := jujucrossmodel.ApplicationOfferFilter{
OfferName: filter.OfferName,
ApplicationName: filter.ApplicationName,
ApplicationDescription: filter.ApplicationDescription,
}
// TODO(wallyworld) - add support for Endpoint filter attribute
return offerFilter
}