|
7 | 7 | "strings" |
8 | 8 | "time" |
9 | 9 |
|
10 | | - "github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator" |
11 | 10 | loadbalancerUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/loadbalancer/utils" |
12 | 11 |
|
13 | 12 | "github.com/google/uuid" |
@@ -239,12 +238,34 @@ func (r *loadBalancerResource) ModifyPlan(ctx context.Context, req resource.Modi |
239 | 238 | } |
240 | 239 |
|
241 | 240 | // 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.")) |
248 | 269 | } |
249 | 270 | } |
250 | 271 |
|
|
0 commit comments