Skip to content

Commit

Permalink
chore: parse given cidr_range
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Jan 20, 2024
1 parent 9a8afd6 commit 0665205
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/provider/random_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"context"
"net"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/terraform-providers/terraform-provider-random/internal/diagnostics"
mapplanmodifiers "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers/map"
)

Expand Down Expand Up @@ -92,6 +94,16 @@ func (r *ipResource) Create(ctx context.Context, req resource.CreateRequest, res
addressType := plan.AddressType.ValueString()
cidrRange := plan.CIDRRange.ValueString()

if valid, err := isValidCIDRRange(cidrRange); !valid {
resp.Diagnostics.AddError(
"Create Random IP error",
"There was an error when validating the CIDR range.\n\n"+
diagnostics.RetryMsg+
"Original Error: "+err.Error(),
)
return
}

u := &ipModelV0{
ID: types.StringValue("-"),
Keepers: plan.Keepers,
Expand Down Expand Up @@ -135,3 +147,8 @@ type ipModelV0 struct {
CIDRRange types.String `tfsdk:"cidr_range"`
Result types.String `tfsdk:"result"`
}

func isValidCIDRRange(cidrRange string) (bool, error) {
_, _, err := net.ParseCIDR(cidrRange)
return err == nil, err
}

0 comments on commit 0665205

Please sign in to comment.