Skip to content
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
Merged
2 changes: 1 addition & 1 deletion .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- 'mongodbatlas/**backup_schedule**.go'
project:
- 'mongodbatlas/data_source_mongodbatlas_project_invitation*.go'
- 'mongodbatlas/data_source_mongodbatlas_project_ip_access_list*.go'
- 'mongodbatlas/fw_data_source_mongodbatlas_project_ip_access_list*.go'
- 'mongodbatlas/data_source_mongodbatlas_project.go'
- 'mongodbatlas/data_source_mongodbatlas_projects.go'
- 'mongodbatlas/resource_mongodbatlas_access_list_api_key*.go'
Expand Down
107 changes: 0 additions & 107 deletions mongodbatlas/data_source_mongodbatlas_project_ip_access_list.go

This file was deleted.

50 changes: 50 additions & 0 deletions mongodbatlas/framework/validator/cidr_validator.go
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{}
}
61 changes: 61 additions & 0 deletions mongodbatlas/framework/validator/cidr_validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package validator
Copy link
Collaborator Author

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


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 mongodbatlas/framework/validator/duration_validator_test.go
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 mongodbatlas/framework/validator/json_string_validator_test.go
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)
}
})
}
}
Loading