diff --git a/terraform/provider/go.mod b/terraform/provider/go.mod index a4bfad9e94..c7e5245557 100644 --- a/terraform/provider/go.mod +++ b/terraform/provider/go.mod @@ -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 diff --git a/terraform/provider/seaweedfs/provider.go b/terraform/provider/seaweedfs/provider.go index 76eb3fe239..aaf9cef36d 100644 --- a/terraform/provider/seaweedfs/provider.go +++ b/terraform/provider/seaweedfs/provider.go @@ -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 == "" { diff --git a/terraform/provider/seaweedfs/provider_test.go b/terraform/provider/seaweedfs/provider_test.go index 7a2281a36f..c7778f5287 100644 --- a/terraform/provider/seaweedfs/provider_test.go +++ b/terraform/provider/seaweedfs/provider_test.go @@ -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()