-
Notifications
You must be signed in to change notification settings - Fork 2
/
label_validator.go
59 lines (51 loc) · 1.7 KB
/
label_validator.go
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
/*
* SPDX-FileCopyrightText: The terraform-provider-k8s Authors
* SPDX-License-Identifier: 0BSD
*/
package validators
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
utilValidation "k8s.io/apimachinery/pkg/util/validation"
)
type labelValidator struct{}
var _ validator.Map = labelValidator{}
func LabelValidator() validator.Map {
return labelValidator{}
}
func (validator labelValidator) Description(_ context.Context) string {
return "labels must match upstream k8s spec"
}
func (validator labelValidator) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}
func (validator labelValidator) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) {
for key, value := range req.ConfigValue.Elements() {
for _, msg := range utilValidation.IsQualifiedName(key) {
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
req.Path,
fmt.Sprintf("Invalid Label Key '%s'", key),
msg,
))
}
val, isString := value.(types.String)
if !isString {
resp.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
req.Path,
fmt.Sprintf("Invalid Type in Label '%s'", key),
fmt.Sprintf("Label values must be types.String but was %s", value.Type(ctx).String()),
))
continue
}
for _, msg := range utilValidation.IsValidLabelValue(val.ValueString()) {
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
req.Path,
fmt.Sprintf("Invalid Value in Label '%s'", key),
msg,
))
}
}
}