|
| 1 | +package validation |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | +) |
| 10 | + |
| 11 | +var _ validator.List = panelPositionsConsistencyValidator{} |
| 12 | + |
| 13 | +// panelPositionsConsistencyValidator validates that either all panels have positions or none of them do. |
| 14 | +type panelPositionsConsistencyValidator struct{} |
| 15 | + |
| 16 | +func (v panelPositionsConsistencyValidator) Description(_ context.Context) string { |
| 17 | + return "either all panels must have positions or none of them should have positions" |
| 18 | +} |
| 19 | + |
| 20 | +func (v panelPositionsConsistencyValidator) MarkdownDescription(ctx context.Context) string { |
| 21 | + return v.Description(ctx) |
| 22 | +} |
| 23 | + |
| 24 | +func (v panelPositionsConsistencyValidator) ValidateList(ctx context.Context, request validator.ListRequest, response *validator.ListResponse) { |
| 25 | + if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + elements := request.ConfigValue.Elements() |
| 30 | + if len(elements) == 0 { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + var hasPositionCount int |
| 35 | + var noPositionCount int |
| 36 | + |
| 37 | + for _, element := range elements { |
| 38 | + if element.IsNull() || element.IsUnknown() { |
| 39 | + continue |
| 40 | + } |
| 41 | + |
| 42 | + panelObj, ok := element.(types.Object) |
| 43 | + if !ok { |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + attrs := panelObj.Attributes() |
| 48 | + position, exists := attrs["position"] |
| 49 | + if !exists { |
| 50 | + continue |
| 51 | + } |
| 52 | + |
| 53 | + // Check if position is set (not null and not unknown) |
| 54 | + if position.IsNull() || position.IsUnknown() { |
| 55 | + noPositionCount++ |
| 56 | + } else { |
| 57 | + // Check if position object has any actual values set |
| 58 | + positionObj, ok := position.(types.Object) |
| 59 | + if !ok { |
| 60 | + noPositionCount++ |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + positionAttrs := positionObj.Attributes() |
| 65 | + hasAnyCoordinate := false |
| 66 | + |
| 67 | + // Check if any coordinate is set |
| 68 | + for _, coordName := range []string{"x_coordinate", "y_coordinate"} { |
| 69 | + if coord, exists := positionAttrs[coordName]; exists { |
| 70 | + if !coord.IsNull() && !coord.IsUnknown() { |
| 71 | + hasAnyCoordinate = true |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if hasAnyCoordinate { |
| 78 | + hasPositionCount++ |
| 79 | + } else { |
| 80 | + noPositionCount++ |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // If we have both panels with positions and panels without positions, that's invalid |
| 86 | + if hasPositionCount > 0 && noPositionCount > 0 { |
| 87 | + response.Diagnostics.Append( |
| 88 | + validatordiag.InvalidAttributeValueDiagnostic( |
| 89 | + request.Path, |
| 90 | + v.Description(ctx), |
| 91 | + "Found panels with positions and panels without positions. All panels must have consistent position configuration.", |
| 92 | + ), |
| 93 | + ) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// RequireConsistentPanelPositions returns a ListValidator which ensures that either all panels |
| 98 | +// have positions or none of them do. |
| 99 | +// |
| 100 | +// Null (unconfigured) and unknown (known after apply) values are skipped. |
| 101 | +func RequireConsistentPanelPositions() validator.List { |
| 102 | + return panelPositionsConsistencyValidator{} |
| 103 | +} |
0 commit comments