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
11 changes: 11 additions & 0 deletions postgresql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ func Provider() *schema.Provider {
Description: "Specify the expected version of PostgreSQL.",
ValidateFunc: validateExpectedVersion,
},
"disabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Disable the provider from making any changes to PostgreSQL. When set to true, the provider will not connect to the database.",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -331,6 +337,11 @@ func acquireAzureOauthToken(tenantId string) (string, error) {
}

func providerConfigure(d *schema.ResourceData) (any, error) {
// If disabled, return nil to prevent any database connections
if d.Get("disabled").(bool) {
return nil, nil
}

var sslMode string
if sslModeRaw, ok := d.GetOk("sslmode"); ok {
sslMode = sslModeRaw.(string)
Expand Down
50 changes: 50 additions & 0 deletions postgresql/provider_disabled_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package postgresql

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func TestProviderConfigure_Disabled(t *testing.T) {
raw := map[string]interface{}{
"disabled": true,
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "test",
}

p := Provider()
d := schema.TestResourceDataRaw(t, p.Schema, raw)

client, err := providerConfigure(d)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}

if client != nil {
t.Fatal("expected client to be nil when disabled=true")
}
}

func TestProviderConfigure_NotDisabled(t *testing.T) {
raw := map[string]interface{}{
"disabled": false,
"host": "localhost",
"port": 25432,
"username": "postgres",
"password": "postgres",
"database": "postgres",
}

p := Provider()
d := schema.TestResourceDataRaw(t, p.Schema, raw)

client, err := providerConfigure(d)

// Client creation will likely fail without a real DB, but that's ok
// We just want to verify it doesn't return early like disabled=true does
_ = client
_ = err
}