Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import (
"github.com/circlefin/terraform-provider-quicknode/api/streams"
"github.com/circlefin/terraform-provider-quicknode/internal/client/transport"
"github.com/circlefin/terraform-provider-quicknode/internal/utils"
"github.com/circlefin/terraform-provider-quicknode/internal/validators"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand Down Expand Up @@ -88,6 +90,9 @@ func (p *QuickNodeProvider) Schema(ctx context.Context, req provider.SchemaReque
"requests_per_second": schema.Int64Attribute{
MarkdownDescription: "Maximum requests per second to limit requests to quicknode api",
Optional: true,
Validators: []validator.Int64{
validators.RequestsPerSecondValidator,
},
},
},
}
Expand Down
70 changes: 70 additions & 0 deletions internal/provider/provider_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2026 Circle Internet Group, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package provider

import (
"context"
"testing"

frameworkprovider "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRequestsPerSecondValidation(t *testing.T) {
ctx := context.Background()
var schemaResponse frameworkprovider.SchemaResponse
(&QuickNodeProvider{}).Schema(ctx, frameworkprovider.SchemaRequest{}, &schemaResponse)

attribute, ok := schemaResponse.Schema.Attributes["requests_per_second"].(schema.Int64Attribute)
require.True(t, ok)
require.Len(t, attribute.Validators, 1)

tests := []struct {
name string
value types.Int64
wantError bool
}{
{name: "null", value: types.Int64Null()},
{name: "unknown", value: types.Int64Unknown()},
{name: "negative", value: types.Int64Value(-1), wantError: true},
{name: "zero", value: types.Int64Value(0), wantError: true},
{name: "one", value: types.Int64Value(1)},
{name: "positive", value: types.Int64Value(5)},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
request := validator.Int64Request{ConfigValue: test.value}
var response validator.Int64Response

attribute.Validators[0].ValidateInt64(ctx, request, &response)

if test.wantError {
require.Len(t, response.Diagnostics, 1)
assert.Equal(t, "Invalid value", response.Diagnostics[0].Summary())
assert.Contains(t, response.Diagnostics[0].Detail(), "Expected value to be at least 1")
return
}

assert.Empty(t, response.Diagnostics)
})
}
}
56 changes: 56 additions & 0 deletions internal/validators/provider_validators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2026 Circle Internet Group, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package validators

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

type Int64AtLeastValidator struct {
min int64
}

func (v Int64AtLeastValidator) Description(ctx context.Context) string {
return fmt.Sprintf("value must be at least %d", v.min)
}

func (v Int64AtLeastValidator) MarkdownDescription(ctx context.Context) string {
return fmt.Sprintf("value must be at least %d", v.min)
}

func (v Int64AtLeastValidator) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

value := req.ConfigValue.ValueInt64()

if value < v.min {
resp.Diagnostics.AddAttributeError(
req.Path,
"Invalid value",
fmt.Sprintf("Expected value to be at least %d, got: %d", v.min, value),
)
}
}

var RequestsPerSecondValidator = Int64AtLeastValidator{
min: 1,
}