Skip to content

Commit 125ee30

Browse files
chore: parse given cidr_range
1 parent 9a8afd6 commit 125ee30

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

internal/provider/random_ip.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package provider
55

66
import (
77
"context"
8+
"net"
89

910
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1011
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -14,6 +15,7 @@ import (
1415
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1516
"github.com/hashicorp/terraform-plugin-framework/types"
1617

18+
"github.com/terraform-providers/terraform-provider-random/internal/diagnostics"
1719
mapplanmodifiers "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers/map"
1820
)
1921

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

97+
if valid, err := isValidCIDRRange(cidrRange); !valid {
98+
resp.Diagnostics.AddError(
99+
"Create Random IP error",
100+
"There was an error during generation of a random IP address.\n\n"+
101+
diagnostics.RetryMsg+
102+
"Original Error: "+err.Error(),
103+
)
104+
return
105+
}
106+
95107
u := &ipModelV0{
96108
ID: types.StringValue("-"),
97109
Keepers: plan.Keepers,
@@ -135,3 +147,8 @@ type ipModelV0 struct {
135147
CIDRRange types.String `tfsdk:"cidr_range"`
136148
Result types.String `tfsdk:"result"`
137149
}
150+
151+
func isValidCIDRRange(cidrRange string) (bool, error) {
152+
_, _, err := net.ParseCIDR(cidrRange)
153+
return err == nil, err
154+
}

0 commit comments

Comments
 (0)