-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Migrate DataSource: mongodbatlas_project_ip_access_list to Terr…
…aform Plugin Framework (#1395)
- Loading branch information
1 parent
b1d63dd
commit 7aaa9f2
Showing
14 changed files
with
678 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 0 additions & 107 deletions
107
mongodbatlas/data_source_mongodbatlas_project_ip_access_list.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
type CIDRValidator struct{} | ||
|
||
func (v CIDRValidator) Description(_ context.Context) string { | ||
return "string value must be defined as a valid cidr." | ||
} | ||
|
||
func (v CIDRValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
func (v CIDRValidator) ValidateString(ctx context.Context, req validator.StringRequest, response *validator.StringResponse) { | ||
// If the value is unknown or null, there is nothing to validate. | ||
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { | ||
return | ||
} | ||
|
||
value := req.ConfigValue.ValueString() | ||
_, ipnet, err := net.ParseCIDR(value) | ||
if err != nil { | ||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
req.Path, | ||
v.Description(ctx), | ||
req.ConfigValue.ValueString(), | ||
)) | ||
return | ||
} | ||
|
||
if ipnet == nil || ipnet.String() != value { | ||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
req.Path, | ||
v.Description(ctx), | ||
req.ConfigValue.ValueString(), | ||
)) | ||
return | ||
} | ||
} | ||
|
||
func ValidCIDR() validator.String { | ||
return CIDRValidator{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func TestValidCIDR(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cidr string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid Value", | ||
cidr: "192.0.0.0/28", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid value", | ||
cidr: "12312321", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing slash", | ||
cidr: "192.0.0.8", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "empty", | ||
cidr: "", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
val := tt.cidr | ||
wantErr := tt.wantErr | ||
cidrValidator := CIDRValidator{} | ||
|
||
validatorRequest := validator.StringRequest{ | ||
ConfigValue: types.StringValue(val), | ||
} | ||
|
||
validatorResponse := validator.StringResponse{ | ||
Diagnostics: diag.Diagnostics{}, | ||
} | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
cidrValidator.ValidateString(context.Background(), validatorRequest, &validatorResponse) | ||
|
||
if validatorResponse.Diagnostics.HasError() && !wantErr { | ||
t.Errorf("URL() error = %v, wantErr %v", validatorResponse.Diagnostics.Errors(), wantErr) | ||
} | ||
}) | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
mongodbatlas/framework/validator/duration_validator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func TestValidDurationBetween(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
minutes string | ||
maxMinutes int | ||
minMinutes int | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid minutes", | ||
minutes: "11m", | ||
minMinutes: 10, | ||
maxMinutes: 12, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "out of range", | ||
minutes: "11h45m", | ||
minMinutes: 10, | ||
maxMinutes: 12, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "unvalid minutes", | ||
minutes: "1m", | ||
minMinutes: 10, | ||
maxMinutes: 12, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "max minutes smaller than min minutes", | ||
minutes: "11", | ||
minMinutes: 10, | ||
maxMinutes: 1, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "negative number", | ||
minutes: "-11", | ||
minMinutes: 10, | ||
maxMinutes: 1, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "empty", | ||
minutes: "", | ||
minMinutes: 10, | ||
maxMinutes: 12, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
wantErr := tt.wantErr | ||
cidrValidator := durationValidator{ | ||
MinMinutes: tt.minMinutes, | ||
MaxMinutes: tt.maxMinutes, | ||
} | ||
|
||
val := tt.minutes | ||
validatorRequest := validator.StringRequest{ | ||
ConfigValue: types.StringValue(val), | ||
} | ||
|
||
validatorResponse := validator.StringResponse{ | ||
Diagnostics: diag.Diagnostics{}, | ||
} | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
cidrValidator.ValidateString(context.Background(), validatorRequest, &validatorResponse) | ||
|
||
if validatorResponse.Diagnostics.HasError() && !wantErr { | ||
t.Errorf("URL() error = %v, wantErr %v", validatorResponse.Diagnostics.Errors(), wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
type IPValidator struct{} | ||
|
||
func (v IPValidator) Description(_ context.Context) string { | ||
return "string value must be defined as a valid IP Address." | ||
} | ||
|
||
func (v IPValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
func (v IPValidator) ValidateString(ctx context.Context, req validator.StringRequest, response *validator.StringResponse) { | ||
// If the value is unknown or null, there is nothing to validate. | ||
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { | ||
return | ||
} | ||
|
||
value := req.ConfigValue.ValueString() | ||
ip := net.ParseIP(value) | ||
if ip == nil { | ||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
req.Path, | ||
v.Description(ctx), | ||
req.ConfigValue.ValueString(), | ||
)) | ||
} | ||
} | ||
|
||
func ValidIP() validator.String { | ||
return IPValidator{} | ||
} |
Oops, something went wrong.