forked from juju/juju
/
helpers.go
199 lines (173 loc) · 5.76 KB
/
helpers.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
// 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 (
"github.com/juju/errors"
charmresource "gopkg.in/juju/charm.v6-unstable/resource"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/resource"
)
// Resource2API converts a resource.Resource into
// a Resource struct.
func Resource2API(res resource.Resource) Resource {
return Resource{
CharmResource: CharmResource2API(res.Resource),
ID: res.ID,
PendingID: res.PendingID,
ApplicationID: res.ApplicationID,
Username: res.Username,
Timestamp: res.Timestamp,
}
}
// APIResult2ServiceResources converts a ResourcesResult into a resource.ServiceResources.
func APIResult2ServiceResources(apiResult ResourcesResult) (resource.ServiceResources, error) {
var result resource.ServiceResources
if apiResult.Error != nil {
// TODO(ericsnow) Return the resources too?
err := common.RestoreError(apiResult.Error)
return resource.ServiceResources{}, errors.Trace(err)
}
for _, apiRes := range apiResult.Resources {
res, err := API2Resource(apiRes)
if err != nil {
// This could happen if the server is misbehaving
// or non-conforming.
// TODO(ericsnow) Aggregate errors?
return resource.ServiceResources{}, errors.Annotate(err, "got bad data from server")
}
result.Resources = append(result.Resources, res)
}
for _, unitRes := range apiResult.UnitResources {
tag, err := names.ParseUnitTag(unitRes.Tag)
if err != nil {
return resource.ServiceResources{}, errors.Annotate(err, "got bad data from server")
}
resNames := map[string]bool{}
unitResources := resource.UnitResources{Tag: tag}
for _, apiRes := range unitRes.Resources {
res, err := API2Resource(apiRes)
if err != nil {
return resource.ServiceResources{}, errors.Annotate(err, "got bad data from server")
}
resNames[res.Name] = true
unitResources.Resources = append(unitResources.Resources, res)
}
if len(unitRes.DownloadProgress) > 0 {
unitResources.DownloadProgress = make(map[string]int64)
for resName, progress := range unitRes.DownloadProgress {
if _, ok := resNames[resName]; !ok {
err := errors.Errorf("got progress from unrecognized resource %q", resName)
return resource.ServiceResources{}, errors.Annotate(err, "got bad data from server")
}
unitResources.DownloadProgress[resName] = progress
}
}
result.UnitResources = append(result.UnitResources, unitResources)
}
for _, chRes := range apiResult.CharmStoreResources {
res, err := API2CharmResource(chRes)
if err != nil {
return resource.ServiceResources{}, errors.Annotate(err, "got bad data from server")
}
result.CharmStoreResources = append(result.CharmStoreResources, res)
}
return result, nil
}
func ServiceResources2APIResult(svcRes resource.ServiceResources) ResourcesResult {
var result ResourcesResult
for _, res := range svcRes.Resources {
result.Resources = append(result.Resources, Resource2API(res))
}
for _, unitResources := range svcRes.UnitResources {
tag := unitResources.Tag
apiRes := UnitResources{
Entity: params.Entity{Tag: tag.String()},
}
for _, unitRes := range unitResources.Resources {
apiRes.Resources = append(apiRes.Resources, Resource2API(unitRes))
}
if len(unitResources.DownloadProgress) > 0 {
apiRes.DownloadProgress = make(map[string]int64)
for resName, progress := range unitResources.DownloadProgress {
apiRes.DownloadProgress[resName] = progress
}
}
result.UnitResources = append(result.UnitResources, apiRes)
}
result.CharmStoreResources = make([]CharmResource, len(svcRes.CharmStoreResources))
for i, chRes := range svcRes.CharmStoreResources {
result.CharmStoreResources[i] = CharmResource2API(chRes)
}
return result
}
// API2Resource converts an API Resource struct into
// a resource.Resource.
func API2Resource(apiRes Resource) (resource.Resource, error) {
var res resource.Resource
charmRes, err := API2CharmResource(apiRes.CharmResource)
if err != nil {
return res, errors.Trace(err)
}
res = resource.Resource{
Resource: charmRes,
ID: apiRes.ID,
PendingID: apiRes.PendingID,
ApplicationID: apiRes.ApplicationID,
Username: apiRes.Username,
Timestamp: apiRes.Timestamp,
}
if err := res.Validate(); err != nil {
return res, errors.Trace(err)
}
return res, nil
}
// CharmResource2API converts a charm resource into
// a CharmResource struct.
func CharmResource2API(res charmresource.Resource) CharmResource {
return CharmResource{
Name: res.Name,
Type: res.Type.String(),
Path: res.Path,
Description: res.Description,
Origin: res.Origin.String(),
Revision: res.Revision,
Fingerprint: res.Fingerprint.Bytes(),
Size: res.Size,
}
}
// API2CharmResource converts an API CharmResource struct into
// a charm resource.
func API2CharmResource(apiInfo CharmResource) (charmresource.Resource, error) {
var res charmresource.Resource
rtype, err := charmresource.ParseType(apiInfo.Type)
if err != nil {
return res, errors.Trace(err)
}
origin, err := charmresource.ParseOrigin(apiInfo.Origin)
if err != nil {
return res, errors.Trace(err)
}
fp, err := resource.DeserializeFingerprint(apiInfo.Fingerprint)
if err != nil {
return res, errors.Trace(err)
}
res = charmresource.Resource{
Meta: charmresource.Meta{
Name: apiInfo.Name,
Type: rtype,
Path: apiInfo.Path,
Description: apiInfo.Description,
},
Origin: origin,
Revision: apiInfo.Revision,
Fingerprint: fp,
Size: apiInfo.Size,
}
if err := res.Validate(); err != nil {
return res, errors.Trace(err)
}
return res, nil
}