-
Notifications
You must be signed in to change notification settings - Fork 5
/
encoder.go
92 lines (74 loc) · 2.12 KB
/
encoder.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
package dsapi
import (
"github.com/MerlinDMC/dsapid"
"github.com/MerlinDMC/dsapid/converter"
"github.com/MerlinDMC/dsapid/storage"
"net/url"
"path"
)
type dsapiEncoder struct {
base *url.URL
users storage.UserStorage
}
func NewEncoder(base_url string, users storage.UserStorage) (encoder *dsapiEncoder) {
encoder = new(dsapiEncoder)
if base, err := url.Parse(base_url); err == nil {
encoder.base = base
}
encoder.users = users
return encoder
}
func (me *dsapiEncoder) Encode(manifest *dsapid.ManifestResource) interface{} {
out := dsapiManifest{
Uuid: manifest.Uuid,
Name: manifest.Name,
Version: manifest.Version,
Description: manifest.Description,
Os: manifest.Os,
Type: manifest.Type,
Homepage: manifest.Homepage,
Urn: manifest.Urn,
PublishedAt: manifest.PublishedAt,
CreatedAt: manifest.CreatedAt,
Requirements: manifest.Requirements,
Users: manifest.Users,
Tags: manifest.Tags,
}
if owner, ok := me.users.GetOK(manifest.Owner); ok {
out.CreatorUuid = owner.Uuid
out.CreatorName = owner.Name
out.VendorUuid = owner.Uuid
}
if manifest.Type == dsapid.ManifestTypeZvol {
if v, ok := manifest.Options["cpu_type"]; ok {
out.CpuType = v.(string)
}
if v, ok := manifest.Options["image_size"]; ok {
out.ImageSize = converter.DecodeToInt64(v)
}
if v, ok := manifest.Options["nic_driver"]; ok {
out.NicDriver = v.(string)
}
if v, ok := manifest.Options["disk_driver"]; ok {
out.DiskDriver = v.(string)
}
}
for file_idx, file := range manifest.Files {
out.Files = append(out.Files, dsapiManifestFile{
Path: file.Path,
Size: file.Size,
Md5: file.Md5,
Sha1: file.Sha1,
})
if file_url, err := url.Parse(path.Join("", "datasets", out.Uuid, file.Path)); err == nil {
out.Files[file_idx].Url = me.base.ResolveReference(file_url).String()
}
}
return out
}
func (me *dsapiEncoder) EncodeWithExtra(manifest *dsapid.ManifestResource) interface{} {
out := me.Encode(manifest).(dsapiManifest)
out.BuilderInfo = manifest.BuilderInfo
out.MetadataInfo = manifest.MetadataInfo
return out
}