forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charmmetadata.go
1046 lines (925 loc) · 29.9 KB
/
charmmetadata.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// CharmMetadataArgs is an argument struct used to create a new
// CharmMetadata.
type CharmMetadataArgs struct {
Name string
Summary string
Description string
Subordinate bool
MinJujuVersion string
RunAs string
Assumes string
Provides map[string]CharmMetadataRelation
Peers map[string]CharmMetadataRelation
Requires map[string]CharmMetadataRelation
ExtraBindings map[string]string
Categories []string
Tags []string
Storage map[string]CharmMetadataStorage
Devices map[string]CharmMetadataDevice
Payloads map[string]CharmMetadataPayload
Resources map[string]CharmMetadataResource
Terms []string
Containers map[string]CharmMetadataContainer
LXDProfile string
}
func newCharmMetadata(args CharmMetadataArgs) *charmMetadata {
var provides map[string]charmMetadataRelation
if args.Provides != nil {
provides = make(map[string]charmMetadataRelation, len(args.Provides))
for k, v := range args.Provides {
provides[k] = charmMetadataRelation{
Name_: v.Name(),
Role_: v.Role(),
Interface_: v.Interface(),
Optional_: v.Optional(),
Limit_: v.Limit(),
Scope_: v.Scope(),
}
}
}
var peers map[string]charmMetadataRelation
if args.Peers != nil {
peers = make(map[string]charmMetadataRelation, len(args.Peers))
for k, v := range args.Peers {
peers[k] = charmMetadataRelation{
Name_: v.Name(),
Role_: v.Role(),
Interface_: v.Interface(),
Optional_: v.Optional(),
Limit_: v.Limit(),
Scope_: v.Scope(),
}
}
}
var requires map[string]charmMetadataRelation
if args.Requires != nil {
requires = make(map[string]charmMetadataRelation, len(args.Requires))
for k, v := range args.Requires {
requires[k] = charmMetadataRelation{
Name_: v.Name(),
Role_: v.Role(),
Interface_: v.Interface(),
Optional_: v.Optional(),
Limit_: v.Limit(),
Scope_: v.Scope(),
}
}
}
var storage map[string]charmMetadataStorage
if args.Storage != nil {
storage = make(map[string]charmMetadataStorage, len(args.Storage))
for k, v := range args.Storage {
storage[k] = charmMetadataStorage{
Name_: v.Name(),
Description_: v.Description(),
Type_: v.Type(),
Shared_: v.Shared(),
Readonly_: v.Readonly(),
CountMin_: v.CountMin(),
CountMax_: v.CountMax(),
MinimumSize_: v.MinimumSize(),
Location_: v.Location(),
Properties_: v.Properties(),
}
}
}
var devices map[string]charmMetadataDevice
if args.Devices != nil {
devices = make(map[string]charmMetadataDevice, len(args.Devices))
for k, v := range args.Devices {
devices[k] = charmMetadataDevice{
Name_: v.Name(),
Description_: v.Description(),
Type_: v.Type(),
CountMin_: v.CountMin(),
CountMax_: v.CountMax(),
}
}
}
var payloads map[string]charmMetadataPayload
if args.Payloads != nil {
payloads = make(map[string]charmMetadataPayload, len(args.Payloads))
for k, v := range args.Payloads {
payloads[k] = charmMetadataPayload{
Name_: v.Name(),
Type_: v.Type(),
}
}
}
var resources map[string]charmMetadataResource
if args.Resources != nil {
resources = make(map[string]charmMetadataResource, len(args.Resources))
for k, v := range args.Resources {
resources[k] = charmMetadataResource{
Name_: v.Name(),
Type_: v.Type(),
Path_: v.Path(),
Description_: v.Description(),
}
}
}
var containers map[string]charmMetadataContainer
if args.Containers != nil {
containers = make(map[string]charmMetadataContainer, len(args.Containers))
for k, v := range args.Containers {
mounts := make([]charmMetadataContainerMount, len(v.Mounts()))
for i, m := range v.Mounts() {
mounts[i] = charmMetadataContainerMount{
Storage_: m.Storage(),
Location_: m.Location(),
}
}
containers[k] = charmMetadataContainer{
Resource_: v.Resource(),
Mounts_: mounts,
Uid_: v.Uid(),
Gid_: v.Gid(),
}
}
}
return &charmMetadata{
Version_: 1,
Name_: args.Name,
Summary_: args.Summary,
Description_: args.Description,
Subordinate_: args.Subordinate,
MinJujuVersion_: args.MinJujuVersion,
RunAs_: args.RunAs,
Assumes_: args.Assumes,
Provides_: provides,
Requires_: requires,
Peers_: peers,
ExtraBindings_: args.ExtraBindings,
Categories_: args.Categories,
Tags_: args.Tags,
Storage_: storage,
Devices_: devices,
Payloads_: payloads,
Resources_: resources,
Terms_: args.Terms,
Containers_: containers,
LXDProfile_: args.LXDProfile,
}
}
// charmMetadata represents the metadata of a charm.
type charmMetadata struct {
Version_ int `yaml:"version"`
Name_ string `yaml:"name,omitempty"`
Summary_ string `yaml:"summary,omitempty"`
Description_ string `yaml:"description,omitempty"`
Subordinate_ bool `yaml:"subordinate,omitempty"`
MinJujuVersion_ string `yaml:"min-juju-version,omitempty"`
RunAs_ string `yaml:"run-as,omitempty"`
Assumes_ string `yaml:"assumes,omitempty"`
Provides_ map[string]charmMetadataRelation `yaml:"provides,omitempty"`
Requires_ map[string]charmMetadataRelation `yaml:"requires,omitempty"`
Peers_ map[string]charmMetadataRelation `yaml:"peers,omitempty"`
ExtraBindings_ map[string]string `yaml:"extra-bindings,omitempty"`
Categories_ []string `yaml:"categories,omitempty"`
Tags_ []string `yaml:"tags,omitempty"`
Storage_ map[string]charmMetadataStorage `yaml:"storage,omitempty"`
Devices_ map[string]charmMetadataDevice `yaml:"devices,omitempty"`
Payloads_ map[string]charmMetadataPayload `yaml:"payloads,omitempty"`
Resources_ map[string]charmMetadataResource `yaml:"resources,omitempty"`
Terms_ []string `yaml:"terms,omitempty"`
Containers_ map[string]charmMetadataContainer `yaml:"containers,omitempty"`
LXDProfile_ string `yaml:"lxd-profile,omitempty"`
}
// Name returns the name of the charm.
func (m *charmMetadata) Name() string {
return m.Name_
}
// Summary returns the summary of the charm.
func (m *charmMetadata) Summary() string {
return m.Summary_
}
// Description returns the description of the charm.
func (m *charmMetadata) Description() string {
return m.Description_
}
// Subordinate returns whether the charm is a subordinate charm.
func (m *charmMetadata) Subordinate() bool {
return m.Subordinate_
}
// MinJujuVersion returns the minimum Juju version required by the charm.
func (m *charmMetadata) MinJujuVersion() string {
return m.MinJujuVersion_
}
// RunAs returns the user the charm should run as.
func (m *charmMetadata) RunAs() string {
return m.RunAs_
}
// Assumes returns the charm the charm assumes.
func (m *charmMetadata) Assumes() string {
return m.Assumes_
}
// Provides returns the relations of the charm.
func (m *charmMetadata) Provides() map[string]CharmMetadataRelation {
relations := make(map[string]CharmMetadataRelation, len(m.Provides_))
for k, v := range m.Provides_ {
relations[k] = v
}
return relations
}
// Requires returns the relations of the charm.
func (m *charmMetadata) Requires() map[string]CharmMetadataRelation {
relations := make(map[string]CharmMetadataRelation, len(m.Requires_))
for k, v := range m.Requires_ {
relations[k] = v
}
return relations
}
// Peers returns the relations of the charm.
func (m *charmMetadata) Peers() map[string]CharmMetadataRelation {
relations := make(map[string]CharmMetadataRelation, len(m.Peers_))
for k, v := range m.Peers_ {
relations[k] = v
}
return relations
}
// ExtraBindings returns the extra bindings of the charm.
func (m *charmMetadata) ExtraBindings() map[string]string {
return m.ExtraBindings_
}
// Categories returns the categories of the charm.
func (m *charmMetadata) Categories() []string {
return m.Categories_
}
// Tags returns the tags of the charm.
func (m *charmMetadata) Tags() []string {
return m.Tags_
}
// Storage returns the storage of the charm.
func (m *charmMetadata) Storage() map[string]CharmMetadataStorage {
storage := make(map[string]CharmMetadataStorage, len(m.Storage_))
for k, v := range m.Storage_ {
storage[k] = v
}
return storage
}
// Devices returns the devices of the charm.
func (m *charmMetadata) Devices() map[string]CharmMetadataDevice {
devices := make(map[string]CharmMetadataDevice, len(m.Devices_))
for k, v := range m.Devices_ {
devices[k] = v
}
return devices
}
// Payloads returns the payloads of the charm.
func (m *charmMetadata) Payloads() map[string]CharmMetadataPayload {
payloads := make(map[string]CharmMetadataPayload, len(m.Payloads_))
for k, v := range m.Payloads_ {
payloads[k] = v
}
return payloads
}
// Resources returns the resources of the charm.
func (m *charmMetadata) Resources() map[string]CharmMetadataResource {
resources := make(map[string]CharmMetadataResource, len(m.Resources_))
for k, v := range m.Resources_ {
resources[k] = v
}
return resources
}
// Terms returns the terms of the charm.
func (m *charmMetadata) Terms() []string {
return m.Terms_
}
// Containers returns the containers of the charm.
func (m *charmMetadata) Containers() map[string]CharmMetadataContainer {
containers := make(map[string]CharmMetadataContainer, len(m.Containers_))
for k, v := range m.Containers_ {
containers[k] = v
}
return containers
}
// LXDPofile returns the LXD profile of the charm.
func (m *charmMetadata) LXDProfile() string {
return m.LXDProfile_
}
func importCharmMetadata(source map[string]interface{}) (*charmMetadata, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "charmMetadata version schema check failed")
}
importFunc, ok := charmMetadataDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type charmMetadataDeserializationFunc func(map[string]interface{}) (*charmMetadata, error)
var charmMetadataDeserializationFuncs = map[int]charmMetadataDeserializationFunc{
1: importCharmMetadataV1,
}
func importCharmMetadataV1(source map[string]interface{}) (*charmMetadata, error) {
return importCharmMetadataVersion(source, 1)
}
func importCharmMetadataVersion(source map[string]interface{}, importVersion int) (*charmMetadata, error) {
fields := schema.Fields{
"name": schema.String(),
"summary": schema.String(),
"description": schema.String(),
"subordinate": schema.Bool(),
"min-juju-version": schema.String(),
"run-as": schema.String(),
"assumes": schema.String(),
"provides": schema.StringMap(schema.Any()),
"requires": schema.StringMap(schema.Any()),
"peers": schema.StringMap(schema.Any()),
"extra-bindings": schema.StringMap(schema.String()),
"categories": schema.List(schema.String()),
"tags": schema.List(schema.String()),
"storage": schema.StringMap(schema.Any()),
"devices": schema.StringMap(schema.Any()),
"payloads": schema.StringMap(schema.Any()),
"resources": schema.StringMap(schema.Any()),
"terms": schema.List(schema.String()),
"containers": schema.StringMap(schema.Any()),
"lxd-profile": schema.String(),
}
defaults := schema.Defaults{
"summary": schema.Omit,
"description": schema.Omit,
"subordinate": schema.Omit,
"min-juju-version": schema.Omit,
"run-as": schema.Omit,
"assumes": schema.Omit,
"provides": schema.Omit,
"requires": schema.Omit,
"peers": schema.Omit,
"extra-bindings": schema.Omit,
"categories": schema.Omit,
"tags": schema.Omit,
"storage": schema.Omit,
"devices": schema.Omit,
"payloads": schema.Omit,
"resources": schema.Omit,
"terms": schema.Omit,
"containers": schema.Omit,
"lxd-profile": 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{})
var provides map[string]charmMetadataRelation
if valid["provides"] != nil {
provides = make(map[string]charmMetadataRelation)
for k, v := range valid["provides"].(map[string]interface{}) {
var err error
if provides[k], err = importCharmMetadataRelation(v, importVersion); err != nil {
return nil, errors.Annotatef(err, "provides relation %q", k)
}
}
}
var requires map[string]charmMetadataRelation
if valid["requires"] != nil {
requires = make(map[string]charmMetadataRelation)
for k, v := range valid["requires"].(map[string]interface{}) {
var err error
if requires[k], err = importCharmMetadataRelation(v, importVersion); err != nil {
return nil, errors.Annotatef(err, "requires relation %q", k)
}
}
}
var peers map[string]charmMetadataRelation
if valid["peers"] != nil {
peers = make(map[string]charmMetadataRelation)
for k, v := range valid["peers"].(map[string]interface{}) {
var err error
if peers[k], err = importCharmMetadataRelation(v, importVersion); err != nil {
return nil, errors.Annotatef(err, "peers relation %q", k)
}
}
}
var extraBindings map[string]string
if valid["extra-bindings"] != nil {
extraBindings = make(map[string]string)
for k, v := range valid["extra-bindings"].(map[string]interface{}) {
extraBindings[k] = v.(string)
}
}
var categories []string
if valid["categories"] != nil {
categories = make([]string, 0)
for _, v := range valid["categories"].([]interface{}) {
categories = append(categories, v.(string))
}
}
var tags []string
if valid["tags"] != nil {
tags = make([]string, 0)
for _, v := range valid["tags"].([]interface{}) {
tags = append(tags, v.(string))
}
}
var storage map[string]charmMetadataStorage
if valid["storage"] != nil {
storage = make(map[string]charmMetadataStorage)
for k, v := range valid["storage"].(map[string]interface{}) {
var err error
if storage[k], err = importCharmMetadataStorage(v, importVersion); err != nil {
return nil, errors.Annotatef(err, "storage %q", k)
}
}
}
var devices map[string]charmMetadataDevice
if valid["devices"] != nil {
devices = make(map[string]charmMetadataDevice)
for k, v := range valid["devices"].(map[string]interface{}) {
var err error
if devices[k], err = importCharmMetadataDevice(v, importVersion); err != nil {
return nil, errors.Annotatef(err, "device %q", k)
}
}
}
var payloads map[string]charmMetadataPayload
if valid["payloads"] != nil {
payloads = make(map[string]charmMetadataPayload)
for k, v := range valid["payloads"].(map[string]interface{}) {
var err error
if payloads[k], err = importCharmMetadataPayload(v, importVersion); err != nil {
return nil, errors.Annotatef(err, "payload %q", k)
}
}
}
var resources map[string]charmMetadataResource
if valid["resources"] != nil {
resources = make(map[string]charmMetadataResource)
for k, v := range valid["resources"].(map[string]interface{}) {
var err error
if resources[k], err = importCharmMetadataResource(v, importVersion); err != nil {
return nil, errors.Annotatef(err, "resource %q", k)
}
}
}
var containers map[string]charmMetadataContainer
if valid["containers"] != nil {
containers = make(map[string]charmMetadataContainer)
for k, v := range valid["containers"].(map[string]interface{}) {
var err error
if containers[k], err = importCharmMetadataContainer(v, importVersion); err != nil {
return nil, errors.Annotatef(err, "container %q", k)
}
}
}
var terms []string
if valid["terms"] != nil {
terms = make([]string, 0)
for _, v := range valid["terms"].([]interface{}) {
terms = append(terms, v.(string))
}
}
var (
summary string
description string
subordinate bool
minJujuVersion string
runAs string
assumes string
lxdProfile string
)
if valid["summary"] != nil {
summary = valid["summary"].(string)
}
if valid["description"] != nil {
description = valid["description"].(string)
}
if valid["subordinate"] != nil {
subordinate = valid["subordinate"].(bool)
}
if valid["min-juju-version"] != nil {
minJujuVersion = valid["min-juju-version"].(string)
}
if valid["run-as"] != nil {
runAs = valid["run-as"].(string)
}
if valid["assumes"] != nil {
assumes = valid["assumes"].(string)
}
if valid["lxd-profile"] != nil {
lxdProfile = valid["lxd-profile"].(string)
}
return &charmMetadata{
Version_: 1,
Name_: valid["name"].(string),
Summary_: summary,
Description_: description,
Subordinate_: subordinate,
MinJujuVersion_: minJujuVersion,
RunAs_: runAs,
Assumes_: assumes,
Provides_: provides,
Requires_: requires,
Peers_: peers,
ExtraBindings_: extraBindings,
Categories_: categories,
Tags_: tags,
Storage_: storage,
Devices_: devices,
Resources_: resources,
Payloads_: payloads,
Containers_: containers,
Terms_: terms,
LXDProfile_: lxdProfile,
}, nil
}
func importCharmMetadataRelation(source interface{}, importVersion int) (charmMetadataRelation, error) {
fields := schema.Fields{
"name": schema.String(),
"role": schema.String(),
"interface": schema.String(),
"optional": schema.Bool(),
"limit": schema.Int(),
"scope": schema.String(),
}
defaults := schema.Defaults{
"optional": schema.Omit,
"limit": schema.Omit,
"scope": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return charmMetadataRelation{}, errors.Annotate(err, "charmMetadataRelation schema check failed")
}
valid := coerced.(map[string]interface{})
return charmMetadataRelation{
Name_: valid["name"].(string),
Role_: valid["role"].(string),
Interface_: valid["interface"].(string),
Optional_: valid["optional"].(bool),
Limit_: int(valid["limit"].(int64)),
Scope_: valid["scope"].(string),
}, nil
}
func importCharmMetadataStorage(source interface{}, importVersion int) (charmMetadataStorage, error) {
fields := schema.Fields{
"name": schema.String(),
"description": schema.String(),
"type": schema.String(),
"shared": schema.Bool(),
"readonly": schema.Bool(),
"count-min": schema.Int(),
"count-max": schema.Int(),
"minimum-size": schema.Int(),
"location": schema.String(),
"properties": schema.List(schema.String()),
}
defaults := schema.Defaults{
"description": schema.Omit,
"shared": schema.Omit,
"readonly": schema.Omit,
"count-min": schema.Omit,
"count-max": schema.Omit,
"minimum-size": schema.Omit,
"location": schema.Omit,
"properties": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return charmMetadataStorage{}, errors.Annotate(err, "charmMetadataStorage schema check failed")
}
valid := coerced.(map[string]interface{})
properties := make([]string, 0)
for _, v := range valid["properties"].([]interface{}) {
properties = append(properties, v.(string))
}
return charmMetadataStorage{
Name_: valid["name"].(string),
Description_: valid["description"].(string),
Type_: valid["type"].(string),
Shared_: valid["shared"].(bool),
Readonly_: valid["readonly"].(bool),
CountMin_: int(valid["count-min"].(int64)),
CountMax_: int(valid["count-max"].(int64)),
MinimumSize_: int(valid["minimum-size"].(int64)),
Location_: valid["location"].(string),
Properties_: properties,
}, nil
}
func importCharmMetadataDevice(source interface{}, importVersion int) (charmMetadataDevice, error) {
fields := schema.Fields{
"name": schema.String(),
"description": schema.String(),
"type": schema.String(),
"count-min": schema.Int(),
"count-max": schema.Int(),
}
defaults := schema.Defaults{
"description": schema.Omit,
"count-min": schema.Omit,
"count-max": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return charmMetadataDevice{}, errors.Annotate(err, "charmMetadataDevice schema check failed")
}
valid := coerced.(map[string]interface{})
return charmMetadataDevice{
Name_: valid["name"].(string),
Description_: valid["description"].(string),
Type_: valid["type"].(string),
CountMin_: int(valid["count-min"].(int64)),
CountMax_: int(valid["count-max"].(int64)),
}, nil
}
func importCharmMetadataPayload(source interface{}, importVersion int) (charmMetadataPayload, error) {
fields := schema.Fields{
"name": schema.String(),
"type": schema.String(),
}
defaults := schema.Defaults{}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return charmMetadataPayload{}, errors.Annotate(err, "charmMetadataPayload schema check failed")
}
valid := coerced.(map[string]interface{})
return charmMetadataPayload{
Name_: valid["name"].(string),
Type_: valid["type"].(string),
}, nil
}
func importCharmMetadataResource(source interface{}, importVersion int) (charmMetadataResource, error) {
fields := schema.Fields{
"name": schema.String(),
"type": schema.String(),
"path": schema.String(),
"description": schema.String(),
}
defaults := schema.Defaults{
"description": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return charmMetadataResource{}, errors.Annotate(err, "charmMetadataResource schema check failed")
}
valid := coerced.(map[string]interface{})
return charmMetadataResource{
Name_: valid["name"].(string),
Type_: valid["type"].(string),
Path_: valid["path"].(string),
Description_: valid["description"].(string),
}, nil
}
func importCharmMetadataContainer(source interface{}, importVersion int) (charmMetadataContainer, error) {
fields := schema.Fields{
"resource": schema.String(),
"mounts": schema.List(schema.Any()),
"uid": schema.Int(),
"gid": schema.Int(),
}
defaults := schema.Defaults{
"uid": schema.Omit,
"gid": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return charmMetadataContainer{}, errors.Annotate(err, "charmMetadataContainer schema check failed")
}
valid := coerced.(map[string]interface{})
mounts := make([]charmMetadataContainerMount, 0)
for _, v := range valid["mounts"].([]interface{}) {
mount, err := importCharmMetadataContainerMount(v, importVersion)
if err != nil {
return charmMetadataContainer{}, errors.Annotate(err, "mount")
}
mounts = append(mounts, mount)
}
var uid *int
if valid["uid"] != nil {
uid = int64ToIntPtr(valid["uid"].(*int64))
}
var gid *int
if valid["gid"] != nil {
uid = int64ToIntPtr(valid["gid"].(*int64))
}
return charmMetadataContainer{
Resource_: valid["resource"].(string),
Mounts_: mounts,
Uid_: uid,
Gid_: gid,
}, nil
}
func importCharmMetadataContainerMount(source interface{}, importVersion int) (charmMetadataContainerMount, error) {
fields := schema.Fields{
"storage": schema.String(),
"location": schema.String(),
}
defaults := schema.Defaults{}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return charmMetadataContainerMount{}, errors.Annotate(err, "charmMetadataContainerMount schema check failed")
}
valid := coerced.(map[string]interface{})
return charmMetadataContainerMount{
Storage_: valid["storage"].(string),
Location_: valid["location"].(string),
}, nil
}
type charmMetadataRelation struct {
Name_ string `yaml:"name"`
Role_ string `yaml:"role"`
Interface_ string `yaml:"interface"`
Optional_ bool `yaml:"optional"`
Limit_ int `yaml:"limit"`
Scope_ string `yaml:"scope"`
}
// Name returns the name of the relation.
func (r charmMetadataRelation) Name() string {
return r.Name_
}
// Role returns the role of the relation.
func (r charmMetadataRelation) Role() string {
return r.Role_
}
// Interface returns the interface of the relation.
func (r charmMetadataRelation) Interface() string {
return r.Interface_
}
// Optional returns whether the relation is optional.
func (r charmMetadataRelation) Optional() bool {
return r.Optional_
}
// Limit returns the limit of the relation.
func (r charmMetadataRelation) Limit() int {
return r.Limit_
}
// Scope returns the scope of the relation.
func (r charmMetadataRelation) Scope() string {
return r.Scope_
}
type charmMetadataStorage struct {
Name_ string `yaml:"name"`
Description_ string `yaml:"description"`
Type_ string `yaml:"type"`
Shared_ bool `yaml:"shared"`
Readonly_ bool `yaml:"readonly"`
CountMin_ int `yaml:"count-min"`
CountMax_ int `yaml:"count-max"`
MinimumSize_ int `yaml:"minimum-size"`
Location_ string `yaml:"location"`
Properties_ []string `yaml:"properties"`
}
// Name returns the name of the storage.
func (s charmMetadataStorage) Name() string {
return s.Name_
}
// Description returns the description of the storage.
func (s charmMetadataStorage) Description() string {
return s.Description_
}
// Type returns the type of the storage.
func (s charmMetadataStorage) Type() string {
return s.Type_
}
// Shared returns whether the storage is shared.
func (s charmMetadataStorage) Shared() bool {
return s.Shared_
}
// Readonly returns whether the storage is readonly.
func (s charmMetadataStorage) Readonly() bool {
return s.Readonly_
}
// CountMin returns the minimum count of the storage.
func (s charmMetadataStorage) CountMin() int {
return s.CountMin_
}
// CountMax returns the maximum count of the storage.
func (s charmMetadataStorage) CountMax() int {
return s.CountMax_
}
// MinimumSize returns the minimum size of the storage.
func (s charmMetadataStorage) MinimumSize() int {
return s.MinimumSize_
}
// Location returns the location of the storage.
func (s charmMetadataStorage) Location() string {
return s.Location_
}
// Properties returns the properties of the storage.
func (s charmMetadataStorage) Properties() []string {
return s.Properties_
}
type charmMetadataDevice struct {
Name_ string `yaml:"name"`
Description_ string `yaml:"description"`
Type_ string `yaml:"type"`
CountMin_ int `yaml:"count-min"`
CountMax_ int `yaml:"count-max"`
}
// Name returns the name of the device.
func (d charmMetadataDevice) Name() string {
return d.Name_
}
// Description returns the description of the device.
func (d charmMetadataDevice) Description() string {
return d.Description_
}
// Type returns the type of the device.
func (d charmMetadataDevice) Type() string {
return d.Type_
}
// CountMin returns the minimum count of the device.
func (d charmMetadataDevice) CountMin() int {
return d.CountMin_
}
// CountMax returns the maximum count of the device.
func (d charmMetadataDevice) CountMax() int {
return d.CountMax_
}
type charmMetadataPayload struct {
Name_ string `yaml:"name"`
Type_ string `yaml:"type"`
}
// Name returns the name of the payload.
func (p charmMetadataPayload) Name() string {
return p.Name_
}
// Type returns the type of the payload.
func (p charmMetadataPayload) Type() string {
return p.Type_
}
type charmMetadataResource struct {
Name_ string `yaml:"name"`
Type_ string `yaml:"type"`
Path_ string `yaml:"path"`
Description_ string `yaml:"description"`
}
// Name returns the name of the resource.
func (r charmMetadataResource) Name() string {
return r.Name_
}
// Type returns the type of the resource.
func (r charmMetadataResource) Type() string {
return r.Type_
}
// Path returns the path of the resource.
func (r charmMetadataResource) Path() string {
return r.Path_
}
// Description returns the description of the resource.
func (r charmMetadataResource) Description() string {
return r.Description_
}
type charmMetadataContainer struct {
Resource_ string `yaml:"resource"`
Mounts_ []charmMetadataContainerMount `yaml:"mounts"`
Uid_ *int `yaml:"uid,omitempty"`
Gid_ *int `yaml:"gid,omitempty"`
}