...
1 package v2
2
3 import (
4 "errors"
5 "fmt"
6
7 "edge-infra.dev/pkg/sds/lib/set"
8 )
9
10 var (
11 errV1AndV2SpecSpecified = errors.New("V1 spec cannot be specified alongside V2 spec")
12 errStandByLongerThanSuspendTime = errors.New("stand-by time cannot be longer than suspend time")
13 errSuspendLongerThanOffTime = errors.New("suspend time cannot be longer than off time")
14 )
15
16
17
18
19
20
21
22 func (displayConfig *DisplayConfig) Validate() error {
23 if displayConfig == nil {
24 return nil
25 }
26
27 if displayConfig.V1 != nil {
28 return validateV1(displayConfig)
29 }
30
31 if err := validateNonSettableFields(displayConfig); err != nil {
32 return err
33 }
34
35 if err := validateDisplayPorts(displayConfig); err != nil {
36 return err
37 }
38
39 if err := validatePrimary(displayConfig); err != nil {
40 return err
41 }
42
43 return validateDPMS(displayConfig)
44 }
45
46
47
48
49 func validateV1(displayConfig *DisplayConfig) error {
50 if displayConfig.Displays != nil || displayConfig.Layout != nil || displayConfig.DPMS != nil {
51 return errV1AndV2SpecSpecified
52 }
53 return displayConfig.V1.Validate()
54 }
55
56
57 func validateDisplayPorts(displayConfig *DisplayConfig) error {
58 displayDPs := set.Set[DisplayPort]{}
59 for _, dp := range displayConfig.Displays.DisplayPorts() {
60 if _, ok := displayDPs[dp]; ok {
61 return fmt.Errorf("display display-ports must be unique, multiple displays use '%s'", dp)
62 }
63 displayDPs.Add(dp)
64 }
65
66 layoutDPs := set.Set[DisplayPort]{}
67 for _, dp := range displayConfig.Layout {
68 if _, ok := layoutDPs[dp]; ok {
69 return fmt.Errorf("layout display-ports must be unique, multiple entries for '%s'", dp)
70 }
71 layoutDPs.Add(dp)
72 }
73
74 return nil
75 }
76
77
78 func validateNonSettableFields(displayConfig *DisplayConfig) error {
79 for _, display := range displayConfig.Displays {
80 if display.MPID != nil {
81 return fmt.Errorf("display %s specified 'MPID': this must not be configured in the NodeDisplayConfig spec", display.DisplayPort)
82 }
83 if display.SupportedResolutions != nil {
84 return fmt.Errorf("display %s specified 'supportedResolutions': resolution must be configured via the 'resolution' field", display.DisplayPort)
85 }
86 }
87 return nil
88 }
89
90
91 func validatePrimary(displayConfig *DisplayConfig) error {
92 primaries := []DisplayPort{}
93 for _, display := range displayConfig.Displays {
94 if display.IsPrimary() {
95 primaries = append(primaries, display.DisplayPort)
96 }
97 }
98
99 if len(primaries) > 1 {
100 return fmt.Errorf("there can only be one primary, found %d: %v", len(primaries), primaries)
101 }
102
103 return nil
104 }
105
106
107
108
109
110
111
112 func validateDPMS(displayConfig *DisplayConfig) error {
113 if displayConfig.DPMS == nil {
114 return nil
115 }
116
117 var standybyTime, suspendTimeTime, offTimeTime int
118 if displayConfig.DPMS.StandbyTime != nil {
119 standybyTime = *displayConfig.DPMS.StandbyTime
120 }
121 if displayConfig.DPMS.SuspendTime != nil {
122 suspendTimeTime = *displayConfig.DPMS.SuspendTime
123 }
124 if displayConfig.DPMS.OffTime != nil {
125 offTimeTime = *displayConfig.DPMS.OffTime
126 }
127
128 if standybyTime > suspendTimeTime {
129 return errStandByLongerThanSuspendTime
130 } else if suspendTimeTime > offTimeTime {
131 return errSuspendLongerThanOffTime
132 }
133
134 return nil
135 }
136
View as plain text