~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/charms/client.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package charms
 
5
 
 
6
import (
 
7
        "github.com/juju/errors"
 
8
        "github.com/juju/utils/set"
 
9
        "gopkg.in/juju/charm.v6-unstable"
 
10
        "gopkg.in/juju/charm.v6-unstable/resource"
 
11
 
 
12
        "github.com/juju/juju/apiserver/common"
 
13
        "github.com/juju/juju/apiserver/facade"
 
14
        "github.com/juju/juju/apiserver/params"
 
15
        "github.com/juju/juju/state"
 
16
)
 
17
 
 
18
func init() {
 
19
        common.RegisterStandardFacade("Charms", 2, NewAPI)
 
20
}
 
21
 
 
22
var getState = func(st *state.State) charmsAccess {
 
23
        return stateShim{st}
 
24
}
 
25
 
 
26
// Charms defines the methods on the charms API end point.
 
27
type Charms interface {
 
28
        List(args params.CharmsList) (params.CharmsListResult, error)
 
29
        CharmInfo(args params.CharmURL) (params.CharmInfo, error)
 
30
        IsMetered(args params.CharmURL) (bool, error)
 
31
}
 
32
 
 
33
// API implements the charms interface and is the concrete
 
34
// implementation of the api end point.
 
35
type API struct {
 
36
        access     charmsAccess
 
37
        authorizer facade.Authorizer
 
38
}
 
39
 
 
40
// NewAPI returns a new charms API facade.
 
41
func NewAPI(
 
42
        st *state.State,
 
43
        resources facade.Resources,
 
44
        authorizer facade.Authorizer,
 
45
) (*API, error) {
 
46
        if !authorizer.AuthClient() {
 
47
                return nil, common.ErrPerm
 
48
        }
 
49
 
 
50
        return &API{
 
51
                access:     getState(st),
 
52
                authorizer: authorizer,
 
53
        }, nil
 
54
}
 
55
 
 
56
// CharmInfo returns information about the requested charm.
 
57
// NOTE: thumper 2016-06-29, this is not a bulk call and probably should be.
 
58
func (a *API) CharmInfo(args params.CharmURL) (params.CharmInfo, error) {
 
59
        curl, err := charm.ParseURL(args.URL)
 
60
        if err != nil {
 
61
                return params.CharmInfo{}, err
 
62
        }
 
63
        aCharm, err := a.access.Charm(curl)
 
64
        if err != nil {
 
65
                return params.CharmInfo{}, err
 
66
        }
 
67
        info := params.CharmInfo{
 
68
                Revision: aCharm.Revision(),
 
69
                URL:      curl.String(),
 
70
                Config:   convertCharmConfig(aCharm.Config()),
 
71
                Meta:     convertCharmMeta(aCharm.Meta()),
 
72
                Actions:  convertCharmActions(aCharm.Actions()),
 
73
                Metrics:  convertCharmMetrics(aCharm.Metrics()),
 
74
        }
 
75
        return info, nil
 
76
}
 
77
 
 
78
// List returns a list of charm URLs currently in the state.
 
79
// If supplied parameter contains any names, the result will be filtered
 
80
// to return only the charms with supplied names.
 
81
func (a *API) List(args params.CharmsList) (params.CharmsListResult, error) {
 
82
        charms, err := a.access.AllCharms()
 
83
        if err != nil {
 
84
                return params.CharmsListResult{}, errors.Annotatef(err, " listing charms ")
 
85
        }
 
86
 
 
87
        names := set.NewStrings(args.Names...)
 
88
        checkName := !names.IsEmpty()
 
89
        charmURLs := []string{}
 
90
        for _, aCharm := range charms {
 
91
                charmURL := aCharm.URL()
 
92
                if checkName {
 
93
                        if !names.Contains(charmURL.Name) {
 
94
                                continue
 
95
                        }
 
96
                }
 
97
                charmURLs = append(charmURLs, charmURL.String())
 
98
        }
 
99
        return params.CharmsListResult{CharmURLs: charmURLs}, nil
 
100
}
 
101
 
 
102
// IsMetered returns whether or not the charm is metered.
 
103
func (a *API) IsMetered(args params.CharmURL) (params.IsMeteredResult, error) {
 
104
        curl, err := charm.ParseURL(args.URL)
 
105
        if err != nil {
 
106
                return params.IsMeteredResult{false}, err
 
107
        }
 
108
        aCharm, err := a.access.Charm(curl)
 
109
        if err != nil {
 
110
                return params.IsMeteredResult{false}, err
 
111
        }
 
112
        if aCharm.Metrics() != nil && len(aCharm.Metrics().Metrics) > 0 {
 
113
                return params.IsMeteredResult{true}, nil
 
114
        }
 
115
        return params.IsMeteredResult{false}, nil
 
116
}
 
117
 
 
118
func convertCharmConfig(config *charm.Config) map[string]params.CharmOption {
 
119
        if config == nil {
 
120
                return nil
 
121
        }
 
122
        result := make(map[string]params.CharmOption)
 
123
        for key, value := range config.Options {
 
124
                result[key] = convertCharmOption(value)
 
125
        }
 
126
        return result
 
127
}
 
128
 
 
129
func convertCharmOption(opt charm.Option) params.CharmOption {
 
130
        return params.CharmOption{
 
131
                Type:        opt.Type,
 
132
                Description: opt.Description,
 
133
                Default:     opt.Default,
 
134
        }
 
135
}
 
136
 
 
137
func convertCharmMeta(meta *charm.Meta) *params.CharmMeta {
 
138
        if meta == nil {
 
139
                return nil
 
140
        }
 
141
        result := &params.CharmMeta{
 
142
                Name:           meta.Name,
 
143
                Summary:        meta.Summary,
 
144
                Description:    meta.Description,
 
145
                Subordinate:    meta.Subordinate,
 
146
                Provides:       convertCharmRelationMap(meta.Provides),
 
147
                Requires:       convertCharmRelationMap(meta.Requires),
 
148
                Peers:          convertCharmRelationMap(meta.Peers),
 
149
                ExtraBindings:  convertCharmExtraBindingMap(meta.ExtraBindings),
 
150
                Categories:     meta.Categories,
 
151
                Tags:           meta.Tags,
 
152
                Series:         meta.Series,
 
153
                Storage:        convertCharmStorageMap(meta.Storage),
 
154
                PayloadClasses: convertCharmPayloadClassMap(meta.PayloadClasses),
 
155
                Resources:      convertCharmResourceMetaMap(meta.Resources),
 
156
                Terms:          meta.Terms,
 
157
                MinJujuVersion: meta.MinJujuVersion.String(),
 
158
        }
 
159
 
 
160
        return result
 
161
}
 
162
 
 
163
func convertCharmRelationMap(relations map[string]charm.Relation) map[string]params.CharmRelation {
 
164
        if len(relations) == 0 {
 
165
                return nil
 
166
        }
 
167
        result := make(map[string]params.CharmRelation)
 
168
        for key, value := range relations {
 
169
                result[key] = convertCharmRelation(value)
 
170
        }
 
171
        return result
 
172
}
 
173
 
 
174
func convertCharmRelation(relation charm.Relation) params.CharmRelation {
 
175
        return params.CharmRelation{
 
176
                Name:      relation.Name,
 
177
                Role:      string(relation.Role),
 
178
                Interface: relation.Interface,
 
179
                Optional:  relation.Optional,
 
180
                Limit:     relation.Limit,
 
181
                Scope:     string(relation.Scope),
 
182
        }
 
183
}
 
184
 
 
185
func convertCharmStorageMap(storage map[string]charm.Storage) map[string]params.CharmStorage {
 
186
        if len(storage) == 0 {
 
187
                return nil
 
188
        }
 
189
        result := make(map[string]params.CharmStorage)
 
190
        for key, value := range storage {
 
191
                result[key] = convertCharmStorage(value)
 
192
        }
 
193
        return result
 
194
}
 
195
 
 
196
func convertCharmStorage(storage charm.Storage) params.CharmStorage {
 
197
        return params.CharmStorage{
 
198
                Name:        storage.Name,
 
199
                Description: storage.Description,
 
200
                Type:        string(storage.Type),
 
201
                Shared:      storage.Shared,
 
202
                ReadOnly:    storage.ReadOnly,
 
203
                CountMin:    storage.CountMin,
 
204
                CountMax:    storage.CountMax,
 
205
                MinimumSize: storage.MinimumSize,
 
206
                Location:    storage.Location,
 
207
                Properties:  storage.Properties,
 
208
        }
 
209
}
 
210
 
 
211
func convertCharmPayloadClassMap(payload map[string]charm.PayloadClass) map[string]params.CharmPayloadClass {
 
212
        if len(payload) == 0 {
 
213
                return nil
 
214
        }
 
215
        result := make(map[string]params.CharmPayloadClass)
 
216
        for key, value := range payload {
 
217
                result[key] = convertCharmPayloadClass(value)
 
218
        }
 
219
        return result
 
220
}
 
221
 
 
222
func convertCharmPayloadClass(payload charm.PayloadClass) params.CharmPayloadClass {
 
223
        return params.CharmPayloadClass{
 
224
                Name: payload.Name,
 
225
                Type: payload.Type,
 
226
        }
 
227
}
 
228
 
 
229
func convertCharmResourceMetaMap(resources map[string]resource.Meta) map[string]params.CharmResourceMeta {
 
230
        if len(resources) == 0 {
 
231
                return nil
 
232
        }
 
233
        result := make(map[string]params.CharmResourceMeta)
 
234
        for key, value := range resources {
 
235
                result[key] = convertCharmResourceMeta(value)
 
236
        }
 
237
        return result
 
238
}
 
239
 
 
240
func convertCharmResourceMeta(meta resource.Meta) params.CharmResourceMeta {
 
241
        return params.CharmResourceMeta{
 
242
                Name:        meta.Name,
 
243
                Type:        meta.Type.String(),
 
244
                Path:        meta.Path,
 
245
                Description: meta.Description,
 
246
        }
 
247
}
 
248
 
 
249
func convertCharmActions(actions *charm.Actions) *params.CharmActions {
 
250
        if actions == nil {
 
251
                return nil
 
252
        }
 
253
        result := &params.CharmActions{
 
254
                ActionSpecs: convertCharmActionSpecMap(actions.ActionSpecs),
 
255
        }
 
256
 
 
257
        return result
 
258
}
 
259
 
 
260
func convertCharmActionSpecMap(specs map[string]charm.ActionSpec) map[string]params.CharmActionSpec {
 
261
        if len(specs) == 0 {
 
262
                return nil
 
263
        }
 
264
        result := make(map[string]params.CharmActionSpec)
 
265
        for key, value := range specs {
 
266
                result[key] = convertCharmActionSpec(value)
 
267
        }
 
268
        return result
 
269
}
 
270
 
 
271
func convertCharmActionSpec(spec charm.ActionSpec) params.CharmActionSpec {
 
272
        return params.CharmActionSpec{
 
273
                Description: spec.Description,
 
274
                Params:      spec.Params,
 
275
        }
 
276
}
 
277
 
 
278
func convertCharmMetrics(metrics *charm.Metrics) *params.CharmMetrics {
 
279
        if metrics == nil {
 
280
                return nil
 
281
        }
 
282
        return &params.CharmMetrics{
 
283
                Metrics: convertCharmMetricMap(metrics.Metrics),
 
284
        }
 
285
}
 
286
 
 
287
func convertCharmMetricMap(metrics map[string]charm.Metric) map[string]params.CharmMetric {
 
288
        if len(metrics) == 0 {
 
289
                return nil
 
290
        }
 
291
        result := make(map[string]params.CharmMetric)
 
292
        for key, value := range metrics {
 
293
                result[key] = convertCharmMetric(value)
 
294
        }
 
295
        return result
 
296
}
 
297
 
 
298
func convertCharmMetric(metric charm.Metric) params.CharmMetric {
 
299
        return params.CharmMetric{
 
300
                Type:        string(metric.Type),
 
301
                Description: metric.Description,
 
302
        }
 
303
}
 
304
 
 
305
func convertCharmExtraBindingMap(bindings map[string]charm.ExtraBinding) map[string]string {
 
306
        if len(bindings) == 0 {
 
307
                return nil
 
308
        }
 
309
        result := make(map[string]string)
 
310
        for key, value := range bindings {
 
311
                result[key] = value.Name
 
312
        }
 
313
        return result
 
314
}