David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2016 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package gomaasapi |
| 5 | |
| 6 | import "github.com/juju/schema" |
| 7 | |
| 8 | type filesystem struct { |
| 9 | fstype string |
| 10 | mountPoint string |
| 11 | label string |
| 12 | uuid string |
| 13 | // no idea what the mount_options are as a value type, so ignoring for now. |
| 14 | } |
| 15 | |
| 16 | // Type implements FileSystem. |
| 17 | func (f *filesystem) Type() string { |
| 18 | return f.fstype |
| 19 | } |
| 20 | |
| 21 | // MountPoint implements FileSystem. |
| 22 | func (f *filesystem) MountPoint() string { |
| 23 | return f.mountPoint |
| 24 | } |
| 25 | |
| 26 | // Label implements FileSystem. |
| 27 | func (f *filesystem) Label() string { |
| 28 | return f.label |
| 29 | } |
| 30 | |
| 31 | // UUID implements FileSystem. |
| 32 | func (f *filesystem) UUID() string { |
| 33 | return f.uuid |
| 34 | } |
| 35 | |
| 36 | // There is no need for controller based parsing of filesystems until we need it. |
| 37 | // Currently the filesystem reading is only called by the Partition parsing. |
| 38 | |
| 39 | func filesystem2_0(source map[string]interface{}) (*filesystem, error) { |
| 40 | fields := schema.Fields{ |
| 41 | "fstype": schema.String(), |
| 42 | "mount_point": schema.OneOf(schema.Nil(""), schema.String()), |
| 43 | "label": schema.OneOf(schema.Nil(""), schema.String()), |
| 44 | "uuid": schema.String(), |
| 45 | // TODO: mount_options when we know the type (note it can be |
| 46 | // nil). |
| 47 | } |
| 48 | defaults := schema.Defaults{ |
| 49 | "mount_point": "", |
| 50 | "label": "", |
| 51 | } |
| 52 | checker := schema.FieldMap(fields, defaults) |
| 53 | coerced, err := checker.Coerce(source, nil) |
| 54 | if err != nil { |
| 55 | return nil, WrapWithDeserializationError(err, "filesystem 2.0 schema check failed") |
| 56 | } |
| 57 | valid := coerced.(map[string]interface{}) |
| 58 | // From here we know that the map returned from the schema coercion |
| 59 | // contains fields of the right type. |
| 60 | mount_point, _ := valid["mount_point"].(string) |
| 61 | label, _ := valid["label"].(string) |
| 62 | result := &filesystem{ |
| 63 | fstype: valid["fstype"].(string), |
| 64 | mountPoint: mount_point, |
| 65 | label: label, |
| 66 | uuid: valid["uuid"].(string), |
| 67 | } |
| 68 | return result, nil |
| 69 | } |