@@ -5,6 +5,7 @@ package provider
5
5
6
6
import (
7
7
"context"
8
+ "net"
8
9
9
10
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
10
11
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -14,6 +15,7 @@ import (
14
15
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
15
16
"github.com/hashicorp/terraform-plugin-framework/types"
16
17
18
+ "github.com/terraform-providers/terraform-provider-random/internal/diagnostics"
17
19
mapplanmodifiers "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers/map"
18
20
)
19
21
@@ -92,6 +94,16 @@ func (r *ipResource) Create(ctx context.Context, req resource.CreateRequest, res
92
94
addressType := plan .AddressType .ValueString ()
93
95
cidrRange := plan .CIDRRange .ValueString ()
94
96
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
+
95
107
u := & ipModelV0 {
96
108
ID : types .StringValue ("-" ),
97
109
Keepers : plan .Keepers ,
@@ -135,3 +147,8 @@ type ipModelV0 struct {
135
147
CIDRRange types.String `tfsdk:"cidr_range"`
136
148
Result types.String `tfsdk:"result"`
137
149
}
150
+
151
+ func isValidCIDRRange (cidrRange string ) (bool , error ) {
152
+ _ , _ , err := net .ParseCIDR (cidrRange )
153
+ return err == nil , err
154
+ }
0 commit comments