/
data.go
202 lines (163 loc) · 5.76 KB
/
data.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api
// TODO(ericsnow) Eliminate the dependence on apiserver if possible.
import (
"strings"
"time"
"github.com/juju/errors"
charmresource "gopkg.in/juju/charm.v6-unstable/resource"
"gopkg.in/juju/names.v2"
"gopkg.in/macaroon.v1"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/charmstore"
)
// ListResourcesArgs are the arguments for the ListResources endpoint.
type ListResourcesArgs params.Entities
// NewListResourcesArgs returns the arguments for the ListResources endpoint.
func NewListResourcesArgs(services []string) (ListResourcesArgs, error) {
var args ListResourcesArgs
var errs []error
for _, service := range services {
if !names.IsValidApplication(service) {
err := errors.Errorf("invalid application %q", service)
errs = append(errs, err)
continue
}
args.Entities = append(args.Entities, params.Entity{
Tag: names.NewApplicationTag(service).String(),
})
}
if err := resolveErrors(errs); err != nil {
return args, errors.Trace(err)
}
return args, nil
}
// AddPendingResourcesArgs holds the arguments to the AddPendingResources
// API endpoint.
type AddPendingResourcesArgs struct {
params.Entity
params.AddCharmWithAuthorization
// Resources is the list of resources to add as pending.
Resources []CharmResource
}
// NewAddPendingResourcesArgs returns the arguments for the
// AddPendingResources API endpoint.
func NewAddPendingResourcesArgs(applicationID string, chID charmstore.CharmID, csMac *macaroon.Macaroon, resources []charmresource.Resource) (AddPendingResourcesArgs, error) {
var args AddPendingResourcesArgs
if !names.IsValidApplication(applicationID) {
return args, errors.Errorf("invalid application %q", applicationID)
}
tag := names.NewApplicationTag(applicationID).String()
var apiResources []CharmResource
for _, res := range resources {
if err := res.Validate(); err != nil {
return args, errors.Trace(err)
}
apiRes := CharmResource2API(res)
apiResources = append(apiResources, apiRes)
}
args.Tag = tag
args.Resources = apiResources
if chID.URL != nil {
args.URL = chID.URL.String()
args.Channel = string(chID.Channel)
args.CharmStoreMacaroon = csMac
}
return args, nil
}
// AddPendingResourcesResult holds the result of the AddPendingResources
// API endpoint.
type AddPendingResourcesResult struct {
params.ErrorResult
// PendingIDs holds the "pending ID" for each of the requested
// resources.
PendingIDs []string `json:"pending-ids"`
}
// ResourcesResults holds the resources that result
// from a bulk API call.
type ResourcesResults struct {
// Results is the list of resource results.
Results []ResourcesResult `json:"results"`
}
// ResourcesResult holds the resources that result from an API call
// for a single application.
type ResourcesResult struct {
params.ErrorResult
// Resources is the list of resources for the application.
Resources []Resource `json:"resources"`
// CharmStoreResources is the list of resources associated with the charm in
// the charmstore.
CharmStoreResources []CharmResource `json:"charm-store-resources"`
// UnitResources contains a list of the resources for each unit in the
// application.
UnitResources []UnitResources `json:"unit-resources"`
}
// A UnitResources contains a list of the resources the unit defined by Entity.
type UnitResources struct {
params.Entity
// Resources is a list of resources for the unit.
Resources []Resource `json:"resources"`
// DownloadProgress indicates the number of bytes of a resource file
// have been downloaded so far the uniter. Only currently downloading
// resources are included.
DownloadProgress map[string]int64 `json:"download-progress"`
}
// UploadResult is the response from an upload request.
type UploadResult struct {
params.ErrorResult
// Resource describes the resource that was stored in the model.
Resource Resource `json:"resource"`
}
// Resource contains info about a Resource.
type Resource struct {
CharmResource
// ID uniquely identifies a resource-application pair within the model.
// Note that the model ignores pending resources (those with a
// pending ID) except for in a few clearly pending-related places.
ID string `json:"id"`
// PendingID identifies that this resource is pending and
// distinguishes it from other pending resources with the same model
// ID (and from the active resource).
PendingID string `json:"pending-id"`
// ApplicationID identifies the application for the resource.
ApplicationID string `json:"application"`
// Username is the ID of the user that added the revision
// to the model (whether implicitly or explicitly).
Username string `json:"username"`
// Timestamp indicates when the resource was added to the model.
Timestamp time.Time `json:"timestamp"`
}
// CharmResource contains the definition for a resource.
type CharmResource struct {
// Name identifies the resource.
Name string `json:"name"`
// Type is the name of the resource type.
Type string `json:"type"`
// Path is where the resource will be stored.
Path string `json:"path"`
// Description contains user-facing info about the resource.
Description string `json:"description,omitempty"`
// Origin is where the resource will come from.
Origin string `json:"origin"`
// Revision is the revision, if applicable.
Revision int `json:"revision"`
// Fingerprint is the SHA-384 checksum for the resource blob.
Fingerprint []byte `json:"fingerprint"`
// Size is the size of the resource, in bytes.
Size int64 `json:"size"`
}
func resolveErrors(errs []error) error {
switch len(errs) {
case 0:
return nil
case 1:
return errs[0]
default:
msgs := make([]string, len(errs))
for i, err := range errs {
msgs[i] = err.Error()
}
return errors.New(strings.Join(msgs, "\n"))
}
}