-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Migrate DataSource: mongodbatlas_project_ip_access_list to Terraform Plugin Framework #1395
Merged
andreaangiolillo
merged 9 commits into
CLOUDP-189585-plugin-framework-migration
from
INTMDB-975_2
Aug 22, 2023
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1b09dda
feat: Migrate DataSource: mongodbatlas_project_ip_access_list to Terr…
andreaangiolillo 673049e
Added tests
andreaangiolillo 38a4e92
Added test
andreaangiolillo 00fda09
Merged branch
andreaangiolillo 39651ca
Update mongodbatlas/fw_data_source_mongodbatlas_project_ip_access_lis…
andreaangiolillo b484723
Addressed PR comments (Part-1)
andreaangiolillo 1f731ef
fix
andreaangiolillo 8a1adfd
merged origin
andreaangiolillo 37a363e
Addressed PR comments (Part 2)
andreaangiolillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 URL", | ||
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) | ||
} | ||
}) | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
mongodbatlas/framework/validator/json_string_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,65 @@ | ||
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 TestStringIsJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
json string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid JSON", | ||
json: `{ | ||
"test": "value" | ||
}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid value", | ||
json: "12312321", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing comma", | ||
json: `{ | ||
"test" "value" | ||
}`, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "empty", | ||
json: "", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
val := tt.json | ||
wantErr := tt.wantErr | ||
cidrValidator := jsonStringValidator{} | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added unit tests to the validators