Skip to content

Commit f157ceb

Browse files
Marcel JacekMarcel Jacek
authored andcommitted
implement review findings
1 parent a940fcc commit f157ceb

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

  • stackit/internal/services/loadbalancer/loadbalancer

stackit/internal/services/loadbalancer/loadbalancer/resource.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
1110
loadbalancerUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/loadbalancer/utils"
1211

1312
"github.com/google/uuid"
@@ -239,12 +238,34 @@ func (r *loadBalancerResource) ModifyPlan(ctx context.Context, req resource.Modi
239238
}
240239

241240
// ConfigValidators validates the resource configuration
242-
func (r *loadBalancerResource) ConfigValidators(_ context.Context) []resource.ConfigValidator {
243-
return []resource.ConfigValidator{
244-
resourcevalidator.AtLeastOneOf(
245-
path.MatchRoot("external_address"),
246-
path.MatchRoot("options").AtName("private_network_only"),
247-
),
241+
func (r *loadBalancerResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
242+
var model Model
243+
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
244+
if resp.Diagnostics.HasError() {
245+
return
246+
}
247+
248+
externalAddressIsSet := !utils.IsUndefined(model.ExternalAddress)
249+
250+
lbOptions, err := toOptionsPayload(ctx, &model)
251+
if err != nil || lbOptions == nil {
252+
// private_network_only is not set and external_address is not set
253+
if !externalAddressIsSet {
254+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring load balancer", fmt.Sprintf("You need to provide either the `options.private_network_only = true` or `external_address` field. %v", err))
255+
}
256+
return
257+
}
258+
if lbOptions.PrivateNetworkOnly == nil || *lbOptions.PrivateNetworkOnly == false {
259+
// private_network_only is not set or false and external_address is not set
260+
if !externalAddressIsSet {
261+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring load balancer", fmt.Sprintf("You need to provide either the `options.private_network_only = true` or `external_address` field."))
262+
}
263+
return
264+
}
265+
266+
// Both are set
267+
if *lbOptions.PrivateNetworkOnly && externalAddressIsSet {
268+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring load balancer", fmt.Sprintf("You need to provide either the `options.private_network_only = true` or `external_address` field."))
248269
}
249270
}
250271

0 commit comments

Comments
 (0)