/
helpers.go
71 lines (61 loc) · 1.8 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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package params // import "gopkg.in/juju/charmrepo.v4/csclient/params"
import (
"gopkg.in/errgo.v1"
"gopkg.in/juju/charm.v6/resource"
)
// Resource2API converts a charm resource into an API Resource struct.
func Resource2API(res resource.Resource) Resource {
return Resource{
Name: res.Name,
Type: res.Type.String(),
Path: res.Path,
Description: res.Description,
Revision: res.Revision,
Fingerprint: res.Fingerprint.Bytes(),
Size: res.Size,
}
}
// API2Resource converts an API Resource struct into
// a charm resource.
func API2Resource(apiInfo Resource) (resource.Resource, error) {
var res resource.Resource
rtype, err := resource.ParseType(apiInfo.Type)
if err != nil {
return res, errgo.Mask(err, errgo.Any)
}
fp, err := deserializeFingerprint(apiInfo.Fingerprint)
if err != nil {
return res, errgo.Mask(err, errgo.Any)
}
// Charmstore doesn't set Origin, so we just default it to OriginStore.
res = resource.Resource{
Meta: resource.Meta{
Name: apiInfo.Name,
Type: rtype,
Path: apiInfo.Path,
Description: apiInfo.Description,
},
Origin: resource.OriginStore,
Revision: apiInfo.Revision,
Fingerprint: fp,
Size: apiInfo.Size,
}
if err := res.Validate(); err != nil {
return res, errgo.Mask(err, errgo.Any)
}
return res, nil
}
// deserializeFingerprint converts the serialized fingerprint back into
// a Fingerprint. "zero" values are treated appropriately.
func deserializeFingerprint(fpSum []byte) (resource.Fingerprint, error) {
if len(fpSum) == 0 {
return resource.Fingerprint{}, nil
}
fp, err := resource.NewFingerprint(fpSum)
if err != nil {
return fp, errgo.Mask(err, errgo.Any)
}
return fp, nil
}