github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/lxdprofile/profile.go (about) 1 // Copyright 2019 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package lxdprofile 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/juju/collections/set" 11 ) 12 13 type LXDProfiles struct { 14 Profile Profile 15 } 16 17 // LXDProfile implements LXDProfiler interface. 18 func (p LXDProfiles) LXDProfile() LXDProfile { 19 return p.Profile 20 } 21 22 // ProfilePost is a close representation of lxd api 23 // ProfilesPost 24 type ProfilePost struct { 25 Name string 26 Profile *Profile 27 } 28 29 // Profile is a representation of charm.v6 LXDProfile 30 type Profile struct { 31 Config map[string]string 32 Description string 33 Devices map[string]map[string]string 34 } 35 36 // Empty implements LXDProfile interface. 37 func (p Profile) Empty() bool { 38 return len(p.Devices) < 1 && len(p.Config) < 1 39 } 40 41 // ValidateConfigDevices implements LXDProfile interface. 42 func (p Profile) ValidateConfigDevices() error { 43 for _, val := range p.Devices { 44 goodDevs := set.NewStrings("unix-char", "unix-block", "gpu", "usb") 45 if devType, ok := val["type"]; ok { 46 if !goodDevs.Contains(devType) { 47 return fmt.Errorf("invalid lxd-profile: contains device type %q", devType) 48 } 49 } 50 } 51 for key := range p.Config { 52 if strings.HasPrefix(key, "boot") || 53 strings.HasPrefix(key, "limits") || 54 strings.HasPrefix(key, "migration") { 55 return fmt.Errorf("invalid lxd-profile: contains config value %q", key) 56 } 57 } 58 return nil 59 }