-
Notifications
You must be signed in to change notification settings - Fork 23
/
charmorigin.go
187 lines (159 loc) · 4.52 KB
/
charmorigin.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
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"fmt"
"strings"
"github.com/juju/errors"
"github.com/juju/os/v2/series"
"github.com/juju/schema"
)
// CharmOriginArgs is an argument struct used to add information about the
// charm origin.
type CharmOriginArgs struct {
Source string
ID string
Hash string
Revision int
Channel string
Platform string
}
func newCharmOrigin(args CharmOriginArgs) *charmOrigin {
return &charmOrigin{
Version_: 2,
Source_: args.Source,
ID_: args.ID,
Hash_: args.Hash,
Revision_: args.Revision,
Channel_: args.Channel,
Platform_: args.Platform,
}
}
// charmOrigin represents the origin of a charm.
type charmOrigin struct {
Version_ int `yaml:"version"`
Source_ string `yaml:"source"`
ID_ string `yaml:"id"`
Hash_ string `yaml:"hash"`
Revision_ int `yaml:"revision"`
Channel_ string `yaml:"channel"`
Platform_ string `yaml:"platform"`
}
func platformFromSeries(s string) (string, error) {
if s == "" {
return "", errors.New("cannot convert empty series to a platform")
}
os, err := series.GetOSFromSeries(s)
if err != nil {
return "", fmt.Errorf("extracting os from series %q: %w", s, err)
}
version, err := series.SeriesVersion(s)
if err != nil {
return "", fmt.Errorf("extracting os version from series %q: %w", s, err)
}
return fmt.Sprintf("unknown/%s/%s", strings.ToLower(os.String()), version), nil
}
// Source implements CharmOrigin.
func (a *charmOrigin) Source() string {
return a.Source_
}
// ID implements CharmOrigin.
func (a *charmOrigin) ID() string {
return a.ID_
}
// Hash implements CharmOrigin.
func (a *charmOrigin) Hash() string {
return a.Hash_
}
// Revision implements CharmOrigin.
func (a *charmOrigin) Revision() int {
return a.Revision_
}
// Channel implements CharmOrigin.
func (a *charmOrigin) Channel() string {
return a.Channel_
}
// Platform implements CharmOrigin.
func (a *charmOrigin) Platform() string {
return a.Platform_
}
func importCharmOrigin(source map[string]interface{}) (*charmOrigin, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "charmOrigin version schema check failed")
}
importFunc, ok := charmOriginDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type charmOriginDeserializationFunc func(map[string]interface{}) (*charmOrigin, error)
var charmOriginDeserializationFuncs = map[int]charmOriginDeserializationFunc{
1: importCharmOriginV1,
2: importCharmOriginV2,
}
func importCharmOriginV1(source map[string]interface{}) (*charmOrigin, error) {
return importCharmOriginVersion(source, 1)
}
func importCharmOriginV2(source map[string]interface{}) (*charmOrigin, error) {
return importCharmOriginVersion(source, 2)
}
func importCharmOriginVersion(source map[string]interface{}, importVersion int) (*charmOrigin, error) {
fields := schema.Fields{
"source": schema.String(),
"id": schema.String(),
"hash": schema.String(),
"revision": schema.Int(),
"channel": schema.String(),
"platform": schema.String(),
}
defaults := schema.Defaults{
"source": "unknown",
"id": schema.Omit,
"hash": schema.Omit,
"revision": schema.Omit,
"channel": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "charmOrigin v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
var revision int
switch t := valid["revision"].(type) {
case int:
revision = t
case int64:
revision = int(t)
default:
return nil, errors.Errorf("unexpected revision type %T", valid["revision"])
}
platform := valid["platform"].(string)
if importVersion < 2 {
parts := strings.Split(platform, "/")
if len(parts) < 3 {
return nil, errors.NotValidf("platform %q", platform)
}
pSeries := parts[2]
vers, err := series.SeriesVersion(pSeries)
if err != nil {
return nil, errors.NotValidf("platform series %q", pSeries)
}
parts[2] = vers
parts = append(parts, "stable")
platform = strings.Join(parts, "/")
}
return &charmOrigin{
Version_: 2,
Source_: valid["source"].(string),
ID_: valid["id"].(string),
Hash_: valid["hash"].(string),
Revision_: revision,
Channel_: valid["channel"].(string),
Platform_: platform,
}, nil
}