terraform/provider: reject unknown provider settings instead of using the environment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-05 14:18:57 -08:00
1 parent 6d8903236e
commit 7c2501f44c
3 files changed
+53 -1

No files matched your search

+1 -1
View File
@@ -8,6 +8,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.97.3
github.com/aws/smithy-go v1.28.13
github.com/hashicorp/terraform-plugin-framework v1.17.0
github.com/hashicorp/terraform-plugin-go v0.29.0
)
require (
@@ -24,7 +25,6 @@ require (
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-plugin v1.7.0 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/terraform-plugin-go v0.29.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.10.0 // indirect
github.com/hashicorp/terraform-registry-address v0.4.0 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
+22
View File
@@ -8,6 +8,7 @@ import (
"sync"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"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"
@@ -137,6 +138,27 @@ func (p *seaweedfsProvider) Configure(ctx context.Context, req provider.Configur
if resp.Diagnostics.HasError() {
return
}
for _, setting := range []struct {
name string
unknown bool
}{
{"endpoint", config.Endpoint.IsUnknown()},
{"region", config.Region.IsUnknown()},
{"access_key", config.AccessKey.IsUnknown()},
{"secret_key", config.SecretKey.IsUnknown()},
{"insecure", config.Insecure.IsUnknown()},
} {
if setting.unknown {
resp.Diagnostics.AddAttributeError(
path.Root(setting.name),
"Unknown SeaweedFS provider setting",
fmt.Sprintf("The provider cannot be configured while %q is unknown. Set it to a value known at plan time, or leave it unset to use the environment variable.", setting.name),
)
}
}
if resp.Diagnostics.HasError() {
return
}
region := stringConfigValue(config.Region, "SEAWEEDFS_REGION", os.Getenv)
if region == "" {
@@ -1,10 +1,14 @@
package seaweedfs
import (
"context"
"testing"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func TestStringConfigValue(t *testing.T) {
@@ -57,6 +61,32 @@ func TestBoolConfigValue(t *testing.T) {
}
}
func TestConfigureRejectsUnknownEndpoint(t *testing.T) {
t.Setenv("SEAWEEDFS_ENDPOINT", "http://127.0.0.1:8333")
ctx := context.Background()
p := &seaweedfsProvider{}
var schemaResp provider.SchemaResponse
p.Schema(ctx, provider.SchemaRequest{}, &schemaResp)
objectType := schemaResp.Schema.Type().TerraformType(ctx).(tftypes.Object)
values := map[string]tftypes.Value{}
for name, attrType := range objectType.AttributeTypes {
values[name] = tftypes.NewValue(attrType, nil)
}
values["endpoint"] = tftypes.NewValue(tftypes.String, tftypes.UnknownValue)
var resp provider.ConfigureResponse
p.Configure(ctx, provider.ConfigureRequest{Config: tfsdk.Config{
Schema: schemaResp.Schema,
Raw: tftypes.NewValue(objectType, values),
}}, &resp)
if !resp.Diagnostics.HasError() {
t.Fatal("expected an unknown endpoint to be rejected")
}
if resp.ResourceData != nil {
t.Fatal("expected no client when the endpoint is unknown")
}
}
func TestRegisteredResourcesHaveSchemasAndImport(t *testing.T) {
t.Parallel()