Skip to content

Commit

Permalink
feat: Migrate DataSource: mongodbatlas_project_ip_access_list to Terr…
Browse files Browse the repository at this point in the history
…aform Plugin Framework (#1395)
  • Loading branch information
andreaangiolillo authored Aug 22, 2023
1 parent b1d63dd commit 7aaa9f2
Show file tree
Hide file tree
Showing 14 changed files with 678 additions and 111 deletions.
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

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 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)
}
})
}
}
40 changes: 40 additions & 0 deletions mongodbatlas/framework/validator/ip_validator.go
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{}
}
Loading

0 comments on commit 7aaa9f2

Please sign in to comment.