config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package filesystem
  2. import (
  3. "encoding/json"
  4. "errors"
  5. )
  6. //FileSystem configuration. Append more lines if required.
  7. type FileSystemOption struct {
  8. Name string `json:"name"` //Display name of this device
  9. Uuid string `json:"uuid"` //UUID of this device, e.g. S1
  10. Path string `json:"path"` //Path for the storage root
  11. Access string `json:"access,omitempty"` //Access right, allow {readonly, readwrite}
  12. Hierarchy string `json:"hierarchy"` //Folder hierarchy, allow {public, user}
  13. Automount bool `json:"automount"` //Automount this device if exists
  14. Filesystem string `json:"filesystem,omitempty"` //Support {"ext4","ext2", "ext3", "fat", "vfat", "ntfs"}
  15. Mountdev string `json:"mountdev,omitempty"` //Device file (e.g. /dev/sda1)
  16. Mountpt string `json:"mountpt,omitempty"` //Device mount point (e.g. /media/storage1)
  17. //Backup Hierarchy Options
  18. Parentuid string `json:"parentuid,omitempty"` //The parent mount point for backup source, backup disk only
  19. BackupMode string `json:"backupmode,omitempty"` //Backup mode of the virtual disk
  20. Username string `json:"username,omitempty"` //Username if the storage require auth
  21. Password string `json:"password,omitempty"` //Password if the storage require auth
  22. }
  23. //Parse a list of StorageConfig from the given json content
  24. func loadConfigFromJSON(jsonContent []byte) ([]FileSystemOption, error) {
  25. storageInConfig := []FileSystemOption{}
  26. //Try to parse the JSON content
  27. err := json.Unmarshal(jsonContent, &storageInConfig)
  28. return storageInConfig, err
  29. }
  30. //Validate if the given options are correct
  31. func ValidateOption(options *FileSystemOption) error {
  32. //Check if path exists
  33. if options.Name == "" {
  34. return errors.New("File System Handler name cannot be empty")
  35. }
  36. if options.Uuid == "" {
  37. return errors.New("File System Handler uuid cannot be empty")
  38. }
  39. if !fileExists(options.Path) {
  40. return errors.New("Path not exists, given: " + options.Path)
  41. }
  42. if !inSlice([]string{"readonly", "readwrite"}, options.Access) {
  43. return errors.New("Not supported access mode: " + options.Access)
  44. }
  45. if !inSlice([]string{"user", "public", "backup"}, options.Hierarchy) {
  46. return errors.New("Not supported hierarchy: " + options.Hierarchy)
  47. }
  48. if !inSlice([]string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs"}, options.Filesystem) {
  49. return errors.New("Not supported file system type: " + options.Filesystem)
  50. }
  51. if options.Mountpt != "" && !fileExists(options.Mountpt) {
  52. return errors.New("Mount point not exists: " + options.Mountpt)
  53. }
  54. return nil
  55. }