Skip to content

Commit cc01b33

Browse files
remove custom validators
1 parent 83f0874 commit cc01b33

File tree

3 files changed

+36
-217
lines changed

3 files changed

+36
-217
lines changed

internal/helper/validation/position_coordinates.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

internal/helper/validation/position_coordinates_test.go

Lines changed: 0 additions & 101 deletions
This file was deleted.

internal/provider/flexible_board_resource.go

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ import (
1717
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1818
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1919
"github.com/hashicorp/terraform-plugin-framework/types"
20-
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2120

2221
"github.com/honeycombio/terraform-provider-honeycombio/client"
2322
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper"
24-
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper/validation"
2523
"github.com/honeycombio/terraform-provider-honeycombio/internal/models"
2624
)
2725

@@ -101,9 +99,6 @@ func (*flexibleBoardResource) Schema(_ context.Context, _ resource.SchemaRequest
10199
Blocks: map[string]schema.Block{
102100
"panel": schema.ListNestedBlock{
103101
Description: "List of panels to render on the board.",
104-
Validators: []validator.List{
105-
validation.RequireConsistentPanelPositions(),
106-
},
107102
NestedObject: schema.NestedBlockObject{
108103
Attributes: map[string]schema.Attribute{
109104
"type": schema.StringAttribute{
@@ -125,6 +120,7 @@ func (*flexibleBoardResource) Schema(_ context.Context, _ resource.SchemaRequest
125120
Description: "The X coordinate of the panel.",
126121
Validators: []validator.Int64{
127122
int64validator.AtLeast(0),
123+
int64validator.AlsoRequires(path.MatchRelative().AtParent().AtName("y_coordinate")),
128124
},
129125
},
130126
"y_coordinate": schema.Int64Attribute{
@@ -134,6 +130,7 @@ func (*flexibleBoardResource) Schema(_ context.Context, _ resource.SchemaRequest
134130
Description: "The Y coordinate of the panel.",
135131
Validators: []validator.Int64{
136132
int64validator.AtLeast(0),
133+
int64validator.AlsoRequires(path.MatchRelative().AtParent().AtName("x_coordinate")),
137134
},
138135
},
139136
"height": schema.Int64Attribute{
@@ -155,9 +152,6 @@ func (*flexibleBoardResource) Schema(_ context.Context, _ resource.SchemaRequest
155152
},
156153
},
157154
},
158-
Validators: []validator.Object{
159-
validation.RequireBothCoordinates(),
160-
},
161155
},
162156
"slo_panel": schema.ListNestedBlock{
163157
Description: "A Service Level Objective(SLO) panel to be displayed on the Board.",
@@ -580,26 +574,31 @@ func expandBoardPanels(
580574
// This is a workaround for the limitations of the terraform v5 protocol.
581575
func expandPanelPosition(
582576
ctx context.Context,
583-
panelPosition types.Object,
577+
panelPosition types.List,
584578
diags *diag.Diagnostics,
585579
) client.BoardPanelPosition {
586580
if panelPosition.IsNull() || panelPosition.IsUnknown() {
587581
return client.BoardPanelPosition{
588-
X: -1,
589-
Y: -1,
590-
Width: -1,
591-
Height: -1,
582+
X: -1,
583+
Y: -1,
592584
}
593585
}
594586

595-
var position models.BoardPanelPositionModel
596-
diags.Append(panelPosition.As(ctx, &position, basetypes.ObjectAsOptions{})...)
587+
var position []models.BoardPanelPositionModel
588+
diags.Append(panelPosition.ElementsAs(ctx, &position, false)...)
589+
590+
if len(position) == 0 {
591+
return client.BoardPanelPosition{
592+
X: -1,
593+
Y: -1,
594+
}
595+
}
597596

598597
return client.BoardPanelPosition{
599-
X: int(position.XCoordinate.ValueInt64()),
600-
Y: int(position.YCoordinate.ValueInt64()),
601-
Height: int(position.Height.ValueInt64()),
602-
Width: int(position.Width.ValueInt64()),
598+
X: int(position[0].XCoordinate.ValueInt64()),
599+
Y: int(position[0].YCoordinate.ValueInt64()),
600+
Height: int(position[0].Height.ValueInt64()),
601+
Width: int(position[0].Width.ValueInt64()),
603602
}
604603
}
605604

@@ -717,38 +716,33 @@ func flattenBoardPanel(
717716
}
718717

719718
func flattenBoardPanelPosition(
720-
_ context.Context,
719+
ctx context.Context,
721720
position client.BoardPanelPosition,
722721
diags *diag.Diagnostics,
723722
statePosition client.BoardPanelPosition,
724-
) types.Object {
723+
) types.List {
725724
// we use negative numbers to indicate that the panel position was never set. We use this to not write to state when panel position is not set.
726725
// This is a workaround for the various limitations that terraform v5 protocol presents.
727726
// Without this workaround, whenever the API generates a default position, terraform would complain about a schema mismatch between config and applied results.
728-
if statePosition.Height == -1 && statePosition.Width == -1 && statePosition.X == -1 && statePosition.Y == -1 {
729-
return types.ObjectNull(models.BoardPanelPositionModelAttrType)
730-
}
731-
x := position.X
732-
y := position.Y
733-
width := position.Width
734-
height := position.Height
735-
736-
attrs := map[string]attr.Value{}
737-
if statePosition.X != -1 && statePosition.Y != -1 {
738-
attrs["x_coordinate"] = types.Int64Value(int64(x))
739-
attrs["y_coordinate"] = types.Int64Value(int64(y))
740-
}
741-
if statePosition.Width != -1 {
742-
attrs["width"] = types.Int64Value(int64(width))
743-
}
744-
if statePosition.Height != -1 {
745-
attrs["height"] = types.Int64Value(int64(height))
746-
}
727+
if statePosition.Height == 0 && statePosition.Width == 0 && statePosition.X == -1 && statePosition.Y == -1 {
728+
return types.ListNull(types.ObjectType{AttrTypes: models.BoardPanelPositionModelAttrType})
729+
}
730+
obj, d := types.ObjectValue(models.BoardPanelPositionModelAttrType, map[string]attr.Value{
731+
"x_coordinate": types.Int64Value(int64(position.X)),
732+
"y_coordinate": types.Int64Value(int64(position.Y)),
733+
"height": types.Int64Value(int64(position.Height)),
734+
"width": types.Int64Value(int64(position.Width)),
735+
})
736+
diags.Append(d...)
747737

748-
obj, d := types.ObjectValue(models.BoardPanelPositionModelAttrType, attrs)
738+
result, d := types.ListValueFrom(
739+
ctx,
740+
types.ObjectType{AttrTypes: models.BoardPanelPositionModelAttrType},
741+
[]attr.Value{obj},
742+
)
749743
diags.Append(d...)
750744

751-
return obj
745+
return result
752746
}
753747

754748
func flattenBoardQueryPanel(
@@ -883,12 +877,6 @@ func removeDefaultNegativeNumbers(panels []client.BoardPanel) []client.BoardPane
883877
resp[i].PanelPosition.X = 0
884878
resp[i].PanelPosition.Y = 0
885879
}
886-
if resp[i].PanelPosition.Width == -1 {
887-
resp[i].PanelPosition.Width = 0
888-
}
889-
if resp[i].PanelPosition.Height == -1 {
890-
resp[i].PanelPosition.Height = 0
891-
}
892880
}
893881

894882
return resp

0 commit comments

Comments
 (0)