-
Notifications
You must be signed in to change notification settings - Fork 494
/
client.go
232 lines (213 loc) · 7.95 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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// Package charms provides a client for accessing the charms API.
package charms
import (
"github.com/juju/charm/v9"
charmresource "github.com/juju/charm/v9/resource"
"github.com/juju/errors"
"gopkg.in/macaroon.v2"
"github.com/juju/juju/api/base"
api "github.com/juju/juju/api/client/resources"
apicharm "github.com/juju/juju/api/common/charm"
commoncharms "github.com/juju/juju/api/common/charms"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/rpc/params"
)
// Client allows access to the charms API end point.
type Client struct {
base.ClientFacade
*commoncharms.CharmInfoClient
facade base.FacadeCaller
}
// NewClient creates a new client for accessing the charms API.
func NewClient(st base.APICallCloser) *Client {
frontend, backend := base.NewClientFacade(st, "Charms")
commonClient := commoncharms.NewCharmInfoClient(backend)
return &Client{ClientFacade: frontend, CharmInfoClient: commonClient, facade: backend}
}
// IsMetered returns whether or not the charm is metered.
func (c *Client) IsMetered(charmURL string) (bool, error) {
args := params.CharmURL{URL: charmURL}
var metered params.IsMeteredResult
if err := c.facade.FacadeCall("IsMetered", args, &metered); err != nil {
return false, errors.Trace(err)
}
return metered.Metered, nil
}
// CharmToResolve holds the charm url and it's channel to be resolved.
type CharmToResolve struct {
URL *charm.URL
Origin apicharm.Origin
SwitchCharm bool
}
// ResolvedCharm holds resolved charm data.
type ResolvedCharm struct {
URL *charm.URL
Origin apicharm.Origin
SupportedSeries []string
Error error
}
// ResolveCharms resolves the given charm URLs with an optionally specified
// preferred channel.
// ResolveCharms is only supported in version 3 and above, it is expected that
// the consumer of the client is intended to handle the fallback.
func (c *Client) ResolveCharms(charms []CharmToResolve) ([]ResolvedCharm, error) {
args := params.ResolveCharmsWithChannel{
Resolve: make([]params.ResolveCharmWithChannel, len(charms)),
}
for i, ch := range charms {
args.Resolve[i] = params.ResolveCharmWithChannel{
Reference: ch.URL.String(),
Origin: ch.Origin.ParamsCharmOrigin(),
SwitchCharm: ch.SwitchCharm,
}
}
var result params.ResolveCharmWithChannelResults
if err := c.facade.FacadeCall("ResolveCharms", args, &result); err != nil {
return nil, errors.Trace(apiservererrors.RestoreError(err))
}
resolvedCharms := make([]ResolvedCharm, len(charms))
for i, r := range result.Results {
if r.Error != nil {
resolvedCharms[i] = ResolvedCharm{Error: apiservererrors.RestoreError(r.Error)}
continue
}
curl, err := charm.ParseURL(r.URL)
if err != nil {
resolvedCharms[i] = ResolvedCharm{Error: err}
}
origin := apicharm.APICharmOrigin(r.Origin)
resolvedCharms[i] = ResolvedCharm{
URL: curl,
Origin: origin,
SupportedSeries: r.SupportedSeries,
}
}
return resolvedCharms, nil
}
// DownloadInfo holds the URL and Origin for a charm that requires downloading
// on the client side. This is mainly for bundles as we don't resolve bundles
// on the server.
type DownloadInfo struct {
URL string
Origin apicharm.Origin
}
// GetDownloadInfo will get a download information from the given charm URL
// using the appropriate charm store.
func (c *Client) GetDownloadInfo(curl *charm.URL, origin apicharm.Origin, mac *macaroon.Macaroon) (DownloadInfo, error) {
args := params.CharmURLAndOrigins{
Entities: []params.CharmURLAndOrigin{{
CharmURL: curl.String(),
Origin: origin.ParamsCharmOrigin(),
Macaroon: mac,
}},
}
var results params.DownloadInfoResults
if err := c.facade.FacadeCall("GetDownloadInfos", args, &results); err != nil {
return DownloadInfo{}, errors.Trace(err)
}
if num := len(results.Results); num != 1 {
return DownloadInfo{}, errors.Errorf("expected one result, received %d", num)
}
result := results.Results[0]
return DownloadInfo{
URL: result.URL,
Origin: apicharm.APICharmOrigin(result.Origin),
}, nil
}
// AddCharm adds the given charm URL (which must include revision) to
// the model, if it does not exist yet. Local charms are not
// supported, only charm store and charm hub URLs. See also AddLocalCharm()
// in the client-side API.
//
// If the AddCharm API call fails because of an authorization error
// when retrieving the charm from the charm store, an error
// satisfying params.IsCodeUnauthorized will be returned.
func (c *Client) AddCharm(curl *charm.URL, origin apicharm.Origin, force bool) (apicharm.Origin, error) {
args := params.AddCharmWithOrigin{
URL: curl.String(),
Origin: origin.ParamsCharmOrigin(),
Force: force,
// Deprecated: Series is used here to communicate with older
// controllers and instead we use Origin to describe the platform.
Series: origin.Series,
}
var result params.CharmOriginResult
if err := c.facade.FacadeCall("AddCharm", args, &result); err != nil {
return apicharm.Origin{}, errors.Trace(err)
}
return apicharm.APICharmOrigin(result.Origin), nil
}
// AddCharmWithAuthorization is like AddCharm except it also provides
// the given charmstore macaroon for the juju server to use when
// obtaining the charm from the charm store or from charm hub. The
// macaroon is conventionally obtained from the /delegatable-macaroon
// endpoint in the charm store.
//
// If the AddCharmWithAuthorization API call fails because of an
// authorization error when retrieving the charm from the charm store,
// an error satisfying params.IsCodeUnauthorized will be returned.
// Force is used to overload any validation errors that could occur during
// a deploy
func (c *Client) AddCharmWithAuthorization(curl *charm.URL, origin apicharm.Origin, csMac *macaroon.Macaroon, force bool) (apicharm.Origin, error) {
args := params.AddCharmWithAuth{
URL: curl.String(),
Origin: origin.ParamsCharmOrigin(),
CharmStoreMacaroon: csMac,
Force: force,
// Deprecated: Series is used here to communicate with older
// controllers and instead we use Origin to describe the platform.
Series: origin.Series,
}
var result params.CharmOriginResult
if err := c.facade.FacadeCall("AddCharmWithAuthorization", args, &result); err != nil {
return apicharm.Origin{}, errors.Trace(err)
}
return apicharm.APICharmOrigin(result.Origin), nil
}
// CheckCharmPlacement checks to see if a charm can be placed into the
// application. If the application doesn't exist then it is considered fine to
// be placed there.
func (c *Client) CheckCharmPlacement(applicationName string, curl *charm.URL) error {
args := params.ApplicationCharmPlacements{
Placements: []params.ApplicationCharmPlacement{{
Application: applicationName,
CharmURL: curl.String(),
}},
}
var result params.ErrorResults
if err := c.facade.FacadeCall("CheckCharmPlacement", args, &result); err != nil {
return errors.Trace(err)
}
return result.OneError()
}
// ListCharmResources returns a list of associated resources for a given charm.
func (c *Client) ListCharmResources(curl *charm.URL, origin apicharm.Origin) ([]charmresource.Resource, error) {
args := params.CharmURLAndOrigins{
Entities: []params.CharmURLAndOrigin{{
CharmURL: curl.String(),
Origin: origin.ParamsCharmOrigin(),
}},
}
var results params.CharmResourcesResults
if err := c.facade.FacadeCall("ListCharmResources", args, &results); err != nil {
return nil, errors.Trace(err)
}
if n := len(results.Results); n != 1 {
return nil, errors.Errorf("expected 1 result, received %d", n)
}
result := results.Results[0]
resources := make([]charmresource.Resource, len(result))
for i, res := range result {
if res.Error != nil {
return nil, errors.Trace(res.Error)
}
chRes, err := api.API2CharmResource(res.CharmResource)
if err != nil {
return nil, errors.Annotate(err, "unexpected charm resource")
}
resources[i] = chRes
}
return resources, nil
}