|
| 1 | +package v3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + sharedValidation "github.com/pdok/smooth-operator/pkg/validation" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +func (wfs *WFS) ValidateCreate() ([]string, error) { |
| 10 | + warnings := []string{} |
| 11 | + reasons := []string{} |
| 12 | + |
| 13 | + err := sharedValidation.ValidateLabelsOnCreate(wfs.Labels) |
| 14 | + if err != nil { |
| 15 | + reasons = append(reasons, fmt.Sprintf("%v", err)) |
| 16 | + } |
| 17 | + |
| 18 | + validateWFS(wfs, &warnings, &reasons) |
| 19 | + |
| 20 | + if len(reasons) > 0 { |
| 21 | + return warnings, fmt.Errorf("%s", strings.Join(reasons, ". ")) |
| 22 | + } else { |
| 23 | + return warnings, nil |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func (wfs *WFS) ValidateUpdate(wfsOld *WFS) ([]string, error) { |
| 28 | + warnings := []string{} |
| 29 | + reasons := []string{} |
| 30 | + |
| 31 | + // Check labels did not change |
| 32 | + err := sharedValidation.ValidateLabelsOnUpdate(wfsOld.Labels, wfs.Labels) |
| 33 | + if err != nil { |
| 34 | + reasons = append(reasons, fmt.Sprintf("%v", err)) |
| 35 | + } |
| 36 | + |
| 37 | + // Check service.baseURL did not change |
| 38 | + if wfs.Spec.Service.BaseURL != wfsOld.Spec.Service.BaseURL { |
| 39 | + reasons = append(reasons, fmt.Sprintf("service.baseURL is immutable")) |
| 40 | + } |
| 41 | + |
| 42 | + if (wfs.Spec.Service.Inspire == nil && wfsOld.Spec.Service.Inspire != nil) || (wfs.Spec.Service.Inspire != nil && wfsOld.Spec.Service.Inspire == nil) { |
| 43 | + reasons = append(reasons, fmt.Sprintf("services cannot change from inspire to not inspire or the other way around")) |
| 44 | + } |
| 45 | + |
| 46 | + validateWFS(wfs, &warnings, &reasons) |
| 47 | + |
| 48 | + if len(reasons) > 0 { |
| 49 | + return warnings, fmt.Errorf("%s", strings.Join(reasons, ". ")) |
| 50 | + } else { |
| 51 | + return warnings, nil |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func validateWFS(wfs *WFS, warnings *[]string, reasons *[]string) { |
| 56 | + if strings.Contains(wfs.GetName(), "wfs") { |
| 57 | + *warnings = append(*warnings, sharedValidation.FormatValidationWarning("name should not contain wfs", wfs.GroupVersionKind(), wfs.GetName())) |
| 58 | + } |
| 59 | + |
| 60 | + service := wfs.Spec.Service |
| 61 | + |
| 62 | + err := sharedValidation.ValidateBaseURL(service.BaseURL) |
| 63 | + if err != nil { |
| 64 | + *reasons = append(*reasons, fmt.Sprintf("%v", err)) |
| 65 | + } |
| 66 | + |
| 67 | + if service.Mapfile == nil && service.DefaultCrs != "EPSG:28992" && service.Bbox == nil { |
| 68 | + *reasons = append(*reasons, fmt.Sprintf("service.bbox.defaultCRS is required when service.defaultCRS is not 'EPSG:28992'")) |
| 69 | + } |
| 70 | + |
| 71 | + if service.Mapfile != nil { |
| 72 | + if service.Bbox != nil { |
| 73 | + *warnings = append(*warnings, sharedValidation.FormatValidationWarning("service.bbox is not used when service.mapfile is configured", wfs.GroupVersionKind(), wfs.GetName())) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments