-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwms_validation.go
More file actions
220 lines (193 loc) · 8.23 KB
/
wms_validation.go
File metadata and controls
220 lines (193 loc) · 8.23 KB
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
package v3
import (
"fmt"
sharedValidation "github.com/pdok/smooth-operator/pkg/validation"
"k8s.io/utils/strings/slices"
"maps"
"strings"
)
func (wms *WMS) ValidateCreate() ([]string, error) {
var warnings []string
var reasons []string
err := sharedValidation.ValidateLabelsOnCreate(wms.Labels)
if err != nil {
reasons = append(reasons, fmt.Sprintf("%v", err))
}
validateWMS(wms, &warnings, &reasons)
if len(reasons) > 0 {
return warnings, fmt.Errorf("%s", strings.Join(reasons, ". "))
}
return warnings, nil
}
func (wms *WMS) ValidateUpdate(wmsOld *WMS) ([]string, error) {
var warnings []string
var reasons []string
// Check labels did not change
err := sharedValidation.ValidateLabelsOnUpdate(wmsOld.Labels, wms.Labels)
if err != nil {
reasons = append(reasons, fmt.Sprintf("%v", err))
}
// Check service.baseURL did not change
if wms.Spec.Service.URL != wmsOld.Spec.Service.URL {
reasons = append(reasons, "service.baseURL is immutable")
}
if (wms.Spec.Service.Inspire == nil && wmsOld.Spec.Service.Inspire != nil) || (wms.Spec.Service.Inspire != nil && wmsOld.Spec.Service.Inspire == nil) {
reasons = append(reasons, "services cannot change from inspire to not inspire or the other way around")
}
validateWMS(wms, &warnings, &reasons)
if len(reasons) > 0 {
return warnings, fmt.Errorf("%s", strings.Join(reasons, ". "))
}
return warnings, nil
}
func validateWMS(wms *WMS, warnings *[]string, reasons *[]string) {
if strings.Contains(wms.GetName(), "wms") {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("name should not contain wms", wms.GroupVersionKind(), wms.GetName()))
}
service := wms.Spec.Service
err := sharedValidation.ValidateBaseURL(service.URL)
if err != nil {
*reasons = append(*reasons, fmt.Sprintf("%v", err))
}
if service.Mapfile != nil {
if service.Resolution != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("service.resolution is not used when service.mapfile is configured", wms.GroupVersionKind(), wms.GetName()))
}
if service.DefResolution != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("service.defResolution is not used when service.mapfile is configured", wms.GroupVersionKind(), wms.GetName()))
}
}
var rewriteGroupToDataLayers = false
if wms.Spec.Options.RewriteGroupToDataLayers != nil {
rewriteGroupToDataLayers = *wms.Spec.Options.RewriteGroupToDataLayers
}
var validateChildStyleNameEqual = true
if wms.Spec.Options.ValidateChildStyleNameEqual != nil {
validateChildStyleNameEqual = *wms.Spec.Options.ValidateChildStyleNameEqual
}
equalChildStyleNames := map[string][]string{}
if rewriteGroupToDataLayers && validateChildStyleNameEqual {
findEqualChildStyleNames(&wms.Spec.Service.Layer, &equalChildStyleNames)
}
var names []string
hasVisibleLayer := false
wms.Spec.Service.Layer.setInheritedBoundingBoxes()
for _, layer := range wms.Spec.Service.Layer.GetAllLayers() {
var layerReasons []string
if slices.Contains(names, layer.Name) {
*reasons = append(*reasons, fmt.Sprintf("layer names must be unique, layer.name '%s' is duplicated", layer.Name))
}
names = append(names, layer.Name)
if service.Mapfile != nil && layer.BoundingBoxes != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("layer.boundingBoxes is not used when service.mapfile is configured", wms.GroupVersionKind(), wms.GetName()))
}
if service.Mapfile == nil && service.DataEPSG != "EPSG:28992" && !layer.hasBoundingBoxForCRS(service.DataEPSG) {
*reasons = append(*reasons, "layer.boundingBoxes must contain a boundingBox for CRS '"+service.DataEPSG+"' when service.dataEPSG is not 'EPSG:28992'")
}
if !*layer.Visible {
if layer.Title != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("layer.title is not used when layer.visible=false", wms.GroupVersionKind(), wms.GetName()))
}
if layer.Abstract != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("layer.abstract is not used when layer.visible=false", wms.GroupVersionKind(), wms.GetName()))
}
if layer.Keywords != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("layer.keywords is not used when layer.visible=false", wms.GroupVersionKind(), wms.GetName()))
}
if layer.DatasetMetadataURL != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("layer.datasetMetadataURL is not used when layer.visible=false", wms.GroupVersionKind(), wms.GetName()))
}
if layer.Authority != nil && layer.Authority.SpatialDatasetIdentifier != "" {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("layer.authority.spatialDatasetIdentifier is not used when layer.visible=false", wms.GroupVersionKind(), wms.GetName()))
}
for _, style := range layer.Styles {
if style.Title != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("style.title is not used when layer.visible=false", wms.GroupVersionKind(), wms.GetName()))
}
if style.Abstract != nil {
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("style.abstract is not used when layer.visible=false", wms.GroupVersionKind(), wms.GetName()))
}
}
}
if layer.Visible != nil && *layer.Visible {
var fields []string
hasVisibleLayer = true
if layer.Title == nil {
fields = append(fields, "layer.title")
}
if layer.Abstract == nil {
fields = append(fields, "layer.abstract")
}
if layer.Keywords == nil {
fields = append(fields, "layer.keywords")
}
if len(fields) != 0 {
layerReasons = append(layerReasons, "layer.visible=true; missing required fields: "+strings.Join(fields, ", "))
}
for _, style := range layer.Styles {
if style.Title == nil {
layerReasons = append(layerReasons, fmt.Sprintf("invalid style: '%s': style.title must be set on a visible layer", style.Name))
}
}
if !rewriteGroupToDataLayers && validateChildStyleNameEqual {
equalStylesNames, ok := equalChildStyleNames[layer.Name]
if ok {
for _, styleName := range equalStylesNames {
layerReasons = append(layerReasons, fmt.Sprintf("invalid style: '%s': style.name from parent layer must not be set on a child layer", styleName))
}
}
}
}
if layer.IsDataLayer(&wms.Spec.Service) {
for _, style := range layer.Styles {
if wms.Spec.Service.Mapfile == nil && style.Visualization == nil {
layerReasons = append(layerReasons, fmt.Sprintf("invalid style: '%s': style.visualization must be set on a dataLayer", style.Name))
}
if wms.Spec.Service.Mapfile != nil && style.Visualization != nil {
layerReasons = append(layerReasons, fmt.Sprintf("invalid style: '%s': style.visualization must not be set on a layer when a static mapfile is used", style.Name))
}
}
}
layerType := layer.GetLayerType(&service)
if layerType == GroupLayer || layerType == TopLayer {
if !*layer.Visible {
layerReasons = append(layerReasons, layerType+" must be visible")
}
if layer.Data != nil {
layerReasons = append(layerReasons, "layer.data must not be set on a groupLayer")
}
for _, style := range layer.Styles {
if style.Visualization != nil {
layerReasons = append(layerReasons, fmt.Sprintf("invalid style: '%s': style.visualization must not be set on a groupLayer", style.Name))
}
}
}
if len(layerReasons) != 0 {
*reasons = append(*reasons, fmt.Sprintf("%s '%s' is invalid: ", layerType, layer.Name)+strings.Join(layerReasons, ", "))
}
}
if !hasVisibleLayer {
*reasons = append(*reasons, "at least one layer must be visible")
}
}
func findEqualChildStyleNames(layer *Layer, equalStyleNames *map[string][]string) {
if layer.Layers == nil || len(*layer.Layers) == 0 {
return
}
equalChildStyleNames := map[string][]string{}
for _, childLayer := range *layer.Layers {
var equalStyles []string
for _, style := range layer.Styles {
for _, childStyle := range childLayer.Styles {
if style.Name == childStyle.Name {
equalStyles = append(equalStyles, style.Name)
}
}
}
if len(equalStyles) > 0 {
equalChildStyleNames[childLayer.Name] = equalStyles
}
findEqualChildStyleNames(&childLayer, equalStyleNames)
}
maps.Copy(*equalStyleNames, equalChildStyleNames)
}