forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo_snap_yaml.go
746 lines (679 loc) · 20.7 KB
/
info_snap_yaml.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package snap
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"gopkg.in/yaml.v2"
"github.com/snapcore/snapd/metautil"
"github.com/snapcore/snapd/strutil"
"github.com/snapcore/snapd/timeout"
)
type snapYaml struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
Type Type `yaml:"type"`
Architectures []string `yaml:"architectures,omitempty"`
Assumes []string `yaml:"assumes"`
Title string `yaml:"title"`
Description string `yaml:"description"`
Summary string `yaml:"summary"`
License string `yaml:"license,omitempty"`
Epoch Epoch `yaml:"epoch,omitempty"`
Base string `yaml:"base,omitempty"`
Confinement ConfinementType `yaml:"confinement,omitempty"`
Environment strutil.OrderedMap `yaml:"environment,omitempty"`
Plugs map[string]interface{} `yaml:"plugs,omitempty"`
Slots map[string]interface{} `yaml:"slots,omitempty"`
Apps map[string]appYaml `yaml:"apps,omitempty"`
Hooks map[string]hookYaml `yaml:"hooks,omitempty"`
Layout map[string]layoutYaml `yaml:"layout,omitempty"`
SystemUsernames map[string]interface{} `yaml:"system-usernames,omitempty"`
Links map[string][]string `yaml:"links,omitempty"`
// TypoLayouts is used to detect the use of the incorrect plural form of "layout"
TypoLayouts typoDetector `yaml:"layouts,omitempty"`
}
type typoDetector struct {
Hint string
}
func (td *typoDetector) UnmarshalYAML(func(interface{}) error) error {
return fmt.Errorf("typo detected: %s", td.Hint)
}
type appYaml struct {
Aliases []string `yaml:"aliases,omitempty"`
Command string `yaml:"command"`
CommandChain []string `yaml:"command-chain,omitempty"`
Daemon string `yaml:"daemon"`
DaemonScope DaemonScope `yaml:"daemon-scope"`
StopCommand string `yaml:"stop-command,omitempty"`
ReloadCommand string `yaml:"reload-command,omitempty"`
PostStopCommand string `yaml:"post-stop-command,omitempty"`
StopTimeout timeout.Timeout `yaml:"stop-timeout,omitempty"`
StartTimeout timeout.Timeout `yaml:"start-timeout,omitempty"`
WatchdogTimeout timeout.Timeout `yaml:"watchdog-timeout,omitempty"`
Completer string `yaml:"completer,omitempty"`
RefreshMode string `yaml:"refresh-mode,omitempty"`
StopMode StopModeType `yaml:"stop-mode,omitempty"`
InstallMode string `yaml:"install-mode,omitempty"`
RestartCond RestartCondition `yaml:"restart-condition,omitempty"`
RestartDelay timeout.Timeout `yaml:"restart-delay,omitempty"`
SlotNames []string `yaml:"slots,omitempty"`
PlugNames []string `yaml:"plugs,omitempty"`
BusName string `yaml:"bus-name,omitempty"`
ActivatesOn []string `yaml:"activates-on,omitempty"`
CommonID string `yaml:"common-id,omitempty"`
Environment strutil.OrderedMap `yaml:"environment,omitempty"`
Sockets map[string]socketsYaml `yaml:"sockets,omitempty"`
After []string `yaml:"after,omitempty"`
Before []string `yaml:"before,omitempty"`
Timer string `yaml:"timer,omitempty"`
Autostart string `yaml:"autostart,omitempty"`
}
type hookYaml struct {
PlugNames []string `yaml:"plugs,omitempty"`
SlotNames []string `yaml:"slots,omitempty"`
Environment strutil.OrderedMap `yaml:"environment,omitempty"`
CommandChain []string `yaml:"command-chain,omitempty"`
}
type layoutYaml struct {
Bind string `yaml:"bind,omitempty"`
BindFile string `yaml:"bind-file,omitempty"`
Type string `yaml:"type,omitempty"`
User string `yaml:"user,omitempty"`
Group string `yaml:"group,omitempty"`
Mode string `yaml:"mode,omitempty"`
Symlink string `yaml:"symlink,omitempty"`
}
type socketsYaml struct {
ListenStream string `yaml:"listen-stream,omitempty"`
SocketMode os.FileMode `yaml:"socket-mode,omitempty"`
}
// InfoFromSnapYaml creates a new info based on the given snap.yaml data
func InfoFromSnapYaml(yamlData []byte) (*Info, error) {
return infoFromSnapYaml(yamlData, new(scopedTracker))
}
// scopedTracker helps keeping track of which slots/plugs are scoped
// to apps and hooks.
type scopedTracker struct {
plugs map[*PlugInfo]bool
slots map[*SlotInfo]bool
}
func (strk *scopedTracker) init(sizeGuess int) {
strk.plugs = make(map[*PlugInfo]bool, sizeGuess)
strk.slots = make(map[*SlotInfo]bool, sizeGuess)
}
func (strk *scopedTracker) markPlug(plug *PlugInfo) {
strk.plugs[plug] = true
}
func (strk *scopedTracker) markSlot(slot *SlotInfo) {
strk.slots[slot] = true
}
func (strk *scopedTracker) plug(plug *PlugInfo) bool {
return strk.plugs[plug]
}
func (strk *scopedTracker) slot(slot *SlotInfo) bool {
return strk.slots[slot]
}
func infoFromSnapYaml(yamlData []byte, strk *scopedTracker) (*Info, error) {
var y snapYaml
// Customize hints for the typo detector.
y.TypoLayouts.Hint = `use singular "layout" instead of plural "layouts"`
err := yaml.Unmarshal(yamlData, &y)
if err != nil {
return nil, fmt.Errorf("cannot parse snap.yaml: %s", err)
}
snap := infoSkeletonFromSnapYaml(y)
// Collect top-level definitions of plugs and slots
if err := setPlugsFromSnapYaml(y, snap); err != nil {
return nil, err
}
if err := setSlotsFromSnapYaml(y, snap); err != nil {
return nil, err
}
strk.init(len(y.Apps) + len(y.Hooks))
// Collect all apps, their aliases and hooks
if err := setAppsFromSnapYaml(y, snap, strk); err != nil {
return nil, err
}
setHooksFromSnapYaml(y, snap, strk)
// Bind plugs and slots that are not scoped to all known apps and hooks.
bindUnscopedPlugs(snap, strk)
bindUnscopedSlots(snap, strk)
// Collect layout elements.
if y.Layout != nil {
snap.Layout = make(map[string]*Layout, len(y.Layout))
for path, l := range y.Layout {
var mode os.FileMode = 0755
if l.Mode != "" {
m, err := strconv.ParseUint(l.Mode, 8, 32)
if err != nil {
return nil, err
}
mode = os.FileMode(m)
}
user := "root"
if l.User != "" {
user = l.User
}
group := "root"
if l.Group != "" {
group = l.Group
}
snap.Layout[path] = &Layout{
Snap: snap, Path: path,
Bind: l.Bind, Type: l.Type, Symlink: l.Symlink, BindFile: l.BindFile,
User: user, Group: group, Mode: mode,
}
}
}
// Rename specific plugs on the core snap.
snap.renameClashingCorePlugs()
snap.BadInterfaces = make(map[string]string)
SanitizePlugsSlots(snap)
// Collect system usernames
if err := setSystemUsernamesFromSnapYaml(y, snap); err != nil {
return nil, err
}
if err := setLinksFromSnapYaml(y, snap); err != nil {
return nil, err
}
// FIXME: validation of the fields
return snap, nil
}
// infoSkeletonFromSnapYaml initializes an Info without apps, hook, plugs, or
// slots
func infoSkeletonFromSnapYaml(y snapYaml) *Info {
// Prepare defaults
architectures := []string{"all"}
if len(y.Architectures) != 0 {
architectures = y.Architectures
}
typ := TypeApp
if y.Type != "" {
typ = y.Type
}
// TODO: once we have epochs transition to the snapd type for real
if y.Name == "snapd" {
typ = TypeSnapd
}
if len(y.Epoch.Read) == 0 {
// normalize
y.Epoch.Read = []uint32{0}
y.Epoch.Write = []uint32{0}
}
confinement := StrictConfinement
if y.Confinement != "" {
confinement = y.Confinement
}
// Construct snap skeleton without apps, hooks, plugs, or slots
snap := &Info{
SuggestedName: y.Name,
Version: y.Version,
SnapType: typ,
Architectures: architectures,
Assumes: y.Assumes,
OriginalTitle: y.Title,
OriginalDescription: y.Description,
OriginalSummary: y.Summary,
License: y.License,
Epoch: y.Epoch,
Confinement: confinement,
Base: y.Base,
Apps: make(map[string]*AppInfo),
LegacyAliases: make(map[string]*AppInfo),
Hooks: make(map[string]*HookInfo),
Plugs: make(map[string]*PlugInfo),
Slots: make(map[string]*SlotInfo),
Environment: y.Environment,
SystemUsernames: make(map[string]*SystemUsernameInfo),
OriginalLinks: make(map[string][]string),
}
sort.Strings(snap.Assumes)
return snap
}
func setPlugsFromSnapYaml(y snapYaml, snap *Info) error {
for name, data := range y.Plugs {
iface, label, attrs, err := convertToSlotOrPlugData("plug", name, data)
if err != nil {
return err
}
snap.Plugs[name] = &PlugInfo{
Snap: snap,
Name: name,
Interface: iface,
Attrs: attrs,
Label: label,
}
if len(y.Apps) > 0 {
snap.Plugs[name].Apps = make(map[string]*AppInfo)
}
if len(y.Hooks) > 0 {
snap.Plugs[name].Hooks = make(map[string]*HookInfo)
}
}
return nil
}
func setSlotsFromSnapYaml(y snapYaml, snap *Info) error {
for name, data := range y.Slots {
iface, label, attrs, err := convertToSlotOrPlugData("slot", name, data)
if err != nil {
return err
}
snap.Slots[name] = &SlotInfo{
Snap: snap,
Name: name,
Interface: iface,
Attrs: attrs,
Label: label,
}
if len(y.Apps) > 0 {
snap.Slots[name].Apps = make(map[string]*AppInfo)
}
if len(y.Hooks) > 0 {
snap.Slots[name].Hooks = make(map[string]*HookInfo)
}
}
return nil
}
func setAppsFromSnapYaml(y snapYaml, snap *Info, strk *scopedTracker) error {
for appName, yApp := range y.Apps {
// Collect all apps
app := &AppInfo{
Snap: snap,
Name: appName,
LegacyAliases: yApp.Aliases,
Command: yApp.Command,
CommandChain: yApp.CommandChain,
StartTimeout: yApp.StartTimeout,
Daemon: yApp.Daemon,
DaemonScope: yApp.DaemonScope,
StopTimeout: yApp.StopTimeout,
StopCommand: yApp.StopCommand,
ReloadCommand: yApp.ReloadCommand,
PostStopCommand: yApp.PostStopCommand,
RestartCond: yApp.RestartCond,
RestartDelay: yApp.RestartDelay,
BusName: yApp.BusName,
CommonID: yApp.CommonID,
Environment: yApp.Environment,
Completer: yApp.Completer,
StopMode: yApp.StopMode,
RefreshMode: yApp.RefreshMode,
InstallMode: yApp.InstallMode,
Before: yApp.Before,
After: yApp.After,
Autostart: yApp.Autostart,
WatchdogTimeout: yApp.WatchdogTimeout,
}
if len(y.Plugs) > 0 || len(yApp.PlugNames) > 0 {
app.Plugs = make(map[string]*PlugInfo)
}
if len(y.Slots) > 0 || len(yApp.SlotNames) > 0 {
app.Slots = make(map[string]*SlotInfo)
}
if len(yApp.Sockets) > 0 {
app.Sockets = make(map[string]*SocketInfo, len(yApp.Sockets))
}
if len(yApp.ActivatesOn) > 0 {
app.ActivatesOn = make([]*SlotInfo, 0, len(yApp.ActivatesOn))
}
// Daemons default to being system daemons
if app.Daemon != "" && app.DaemonScope == "" {
app.DaemonScope = SystemDaemon
}
snap.Apps[appName] = app
for _, alias := range app.LegacyAliases {
if snap.LegacyAliases[alias] != nil {
return fmt.Errorf("cannot set %q as alias for both %q and %q", alias, snap.LegacyAliases[alias].Name, appName)
}
snap.LegacyAliases[alias] = app
}
// Bind all plugs/slots listed in this app
for _, plugName := range yApp.PlugNames {
plug, ok := snap.Plugs[plugName]
if !ok {
// Create implicit plug definitions if required
plug = &PlugInfo{
Snap: snap,
Name: plugName,
Interface: plugName,
Apps: make(map[string]*AppInfo),
}
snap.Plugs[plugName] = plug
}
// Mark the plug as scoped.
strk.markPlug(plug)
app.Plugs[plugName] = plug
plug.Apps[appName] = app
}
for _, slotName := range yApp.SlotNames {
slot, ok := snap.Slots[slotName]
if !ok {
slot = &SlotInfo{
Snap: snap,
Name: slotName,
Interface: slotName,
Apps: make(map[string]*AppInfo),
}
snap.Slots[slotName] = slot
}
// Mark the slot as scoped.
strk.markSlot(slot)
app.Slots[slotName] = slot
slot.Apps[appName] = app
}
for _, slotName := range yApp.ActivatesOn {
slot, ok := snap.Slots[slotName]
if !ok {
return fmt.Errorf("invalid activates-on value %q on app %q: slot not found", slotName, appName)
}
app.ActivatesOn = append(app.ActivatesOn, slot)
// Implicitly add the slot to the app
strk.markSlot(slot)
app.Slots[slotName] = slot
slot.Apps[appName] = app
}
for name, data := range yApp.Sockets {
app.Sockets[name] = &SocketInfo{
App: app,
Name: name,
ListenStream: data.ListenStream,
SocketMode: data.SocketMode,
}
}
if yApp.Timer != "" {
app.Timer = &TimerInfo{
App: app,
Timer: yApp.Timer,
}
}
// collect all common IDs
if app.CommonID != "" {
snap.CommonIDs = append(snap.CommonIDs, app.CommonID)
}
}
return nil
}
func setHooksFromSnapYaml(y snapYaml, snap *Info, strk *scopedTracker) {
for hookName, yHook := range y.Hooks {
if !IsHookSupported(hookName) {
continue
}
// Collect all hooks
hook := &HookInfo{
Snap: snap,
Name: hookName,
Environment: yHook.Environment,
CommandChain: yHook.CommandChain,
Explicit: true,
}
if len(y.Plugs) > 0 || len(yHook.PlugNames) > 0 {
hook.Plugs = make(map[string]*PlugInfo)
}
if len(y.Slots) > 0 || len(yHook.SlotNames) > 0 {
hook.Slots = make(map[string]*SlotInfo)
}
snap.Hooks[hookName] = hook
// Bind all plugs/slots listed in this hook
for _, plugName := range yHook.PlugNames {
plug, ok := snap.Plugs[plugName]
if !ok {
// Create implicit plug definitions if required
plug = &PlugInfo{
Snap: snap,
Name: plugName,
Interface: plugName,
Hooks: make(map[string]*HookInfo),
}
snap.Plugs[plugName] = plug
}
// Mark the plug as scoped.
strk.markPlug(plug)
if plug.Hooks == nil {
plug.Hooks = make(map[string]*HookInfo)
}
hook.Plugs[plugName] = plug
plug.Hooks[hookName] = hook
}
for _, slotName := range yHook.SlotNames {
slot, ok := snap.Slots[slotName]
if !ok {
// Create implicit slot definitions if required
slot = &SlotInfo{
Snap: snap,
Name: slotName,
Interface: slotName,
Hooks: make(map[string]*HookInfo),
}
snap.Slots[slotName] = slot
}
// Mark the slot as scoped.
strk.markSlot(slot)
if slot.Hooks == nil {
slot.Hooks = make(map[string]*HookInfo)
}
hook.Slots[slotName] = slot
slot.Hooks[hookName] = hook
}
}
}
func setSystemUsernamesFromSnapYaml(y snapYaml, snap *Info) error {
for user, data := range y.SystemUsernames {
if user == "" {
return fmt.Errorf("system username cannot be empty")
}
scope, attrs, err := convertToUsernamesData(user, data)
if err != nil {
return err
}
if scope == "" {
return fmt.Errorf("system username %q does not specify a scope", user)
}
snap.SystemUsernames[user] = &SystemUsernameInfo{
Name: user,
Scope: scope,
Attrs: attrs,
}
}
return nil
}
func setLinksFromSnapYaml(y snapYaml, snap *Info) error {
for linksKey, links := range y.Links {
if linksKey == "" {
return fmt.Errorf("links key cannot be empty")
}
if !isValidLinksKey(linksKey) {
return fmt.Errorf("links key is invalid: %s", linksKey)
}
snap.OriginalLinks[linksKey] = links
}
return nil
}
func bindUnscopedPlugs(snap *Info, strk *scopedTracker) {
for plugName, plug := range snap.Plugs {
if strk.plug(plug) {
continue
}
for appName, app := range snap.Apps {
app.Plugs[plugName] = plug
plug.Apps[appName] = app
}
for hookName, hook := range snap.Hooks {
hook.Plugs[plugName] = plug
plug.Hooks[hookName] = hook
}
}
}
func bindUnscopedSlots(snap *Info, strk *scopedTracker) {
for slotName, slot := range snap.Slots {
if strk.slot(slot) {
continue
}
for appName, app := range snap.Apps {
app.Slots[slotName] = slot
slot.Apps[appName] = app
}
for hookName, hook := range snap.Hooks {
hook.Slots[slotName] = slot
slot.Hooks[hookName] = hook
}
}
}
// bindImplicitHooks binds all global plugs and slots to implicit hooks
func bindImplicitHooks(snap *Info, strk *scopedTracker) {
for hookName, hook := range snap.Hooks {
if hook.Explicit {
continue
}
for _, plug := range snap.Plugs {
if strk.plug(plug) {
continue
}
if hook.Plugs == nil {
hook.Plugs = make(map[string]*PlugInfo)
}
hook.Plugs[plug.Name] = plug
if plug.Hooks == nil {
plug.Hooks = make(map[string]*HookInfo)
}
plug.Hooks[hookName] = hook
}
for _, slot := range snap.Slots {
if strk.slot(slot) {
continue
}
if hook.Slots == nil {
hook.Slots = make(map[string]*SlotInfo)
}
hook.Slots[slot.Name] = slot
if slot.Hooks == nil {
slot.Hooks = make(map[string]*HookInfo)
}
slot.Hooks[hookName] = hook
}
}
}
func convertToSlotOrPlugData(plugOrSlot, name string, data interface{}) (iface, label string, attrs map[string]interface{}, err error) {
iface = name
switch data.(type) {
case string:
return data.(string), "", nil, nil
case nil:
return name, "", nil, nil
case map[interface{}]interface{}:
for keyData, valueData := range data.(map[interface{}]interface{}) {
key, ok := keyData.(string)
if !ok {
err := fmt.Errorf("%s %q has attribute key that is not a string (found %T)",
plugOrSlot, name, keyData)
return "", "", nil, err
}
if strings.HasPrefix(key, "$") {
err := fmt.Errorf("%s %q uses reserved attribute %q", plugOrSlot, name, key)
return "", "", nil, err
}
switch key {
case "":
return "", "", nil, fmt.Errorf("%s %q has an empty attribute key", plugOrSlot, name)
case "interface":
value, ok := valueData.(string)
if !ok {
err := fmt.Errorf("interface name on %s %q is not a string (found %T)",
plugOrSlot, name, valueData)
return "", "", nil, err
}
iface = value
case "label":
value, ok := valueData.(string)
if !ok {
err := fmt.Errorf("label of %s %q is not a string (found %T)",
plugOrSlot, name, valueData)
return "", "", nil, err
}
label = value
default:
if attrs == nil {
attrs = make(map[string]interface{})
}
value, err := metautil.NormalizeValue(valueData)
if err != nil {
return "", "", nil, fmt.Errorf("attribute %q of %s %q: %v", key, plugOrSlot, name, err)
}
attrs[key] = value
}
}
return iface, label, attrs, nil
default:
err := fmt.Errorf("%s %q has malformed definition (found %T)", plugOrSlot, name, data)
return "", "", nil, err
}
}
// Short form:
// system-usernames:
// snap_daemon: shared # 'scope' is 'shared'
// lxd: external # currently unsupported
// foo: private # currently unsupported
// Attributes form:
// system-usernames:
// snap_daemon:
// scope: shared
// attrib1: ...
// attrib2: ...
func convertToUsernamesData(user string, data interface{}) (scope string, attrs map[string]interface{}, err error) {
switch data.(type) {
case string:
return data.(string), nil, nil
case nil:
return "", nil, nil
case map[interface{}]interface{}:
for keyData, valueData := range data.(map[interface{}]interface{}) {
key, ok := keyData.(string)
if !ok {
err := fmt.Errorf("system username %q has attribute key that is not a string (found %T)", user, keyData)
return "", nil, err
}
switch key {
case "scope":
value, ok := valueData.(string)
if !ok {
err := fmt.Errorf("scope on system username %q is not a string (found %T)", user, valueData)
return "", nil, err
}
scope = value
case "":
return "", nil, fmt.Errorf("system username %q has an empty attribute key", user)
default:
if attrs == nil {
attrs = make(map[string]interface{})
}
value, err := metautil.NormalizeValue(valueData)
if err != nil {
return "", nil, fmt.Errorf("attribute %q of system username %q: %v", key, user, err)
}
attrs[key] = value
}
}
return scope, attrs, nil
default:
err := fmt.Errorf("system username %q has malformed definition (found %T)", user, data)
return "", nil, err
}
}