Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB Connection Pool: Add skip_if_exists #1000

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions digitalocean/database/resource_database_connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func ResourceDigitalOceanDatabaseConnectionPool() *schema.Resource {
Computed: true,
Sensitive: true,
},
// doesn't create a connection pool if it already exists.
// only use if terraform returned a 5xx on a previous create request that successfully created a pool.
"skip_if_exists": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
}
}
Expand All @@ -117,6 +124,15 @@ func resourceDigitalOceanDatabaseConnectionPoolCreate(ctx context.Context, d *sc
Database: d.Get("db_name").(string),
}

skipIfExists := d.Get("skip_if_exists").(bool)
if skipIfExists {
pool, _, err := client.Databases.GetPool(context.Background(), clusterID, opts.Name)
if err == nil {
log.Printf("[INFO] DatabaseConnectionPool Create Request Skipped because skipIfExists argument is passed and DatabaseConnectionPool already exists: %s", pool.Name)
return resourceDigitalOceanDatabaseConnectionPoolRead(ctx, d, meta)
}
}

log.Printf("[DEBUG] DatabaseConnectionPool create configuration: %#v", opts)
pool, _, err := client.Databases.CreatePool(context.Background(), clusterID, opts)
if err != nil {
Expand Down
61 changes: 61 additions & 0 deletions digitalocean/database/resource_database_connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,47 @@ func testAccCheckDigitalOceanDatabaseConnectionPoolDestroy(s *terraform.State) e
return nil
}

func TestAccDigitalOceanDatabaseConnectionPool_SkipIfExistIsPassed(t *testing.T) {
databaseName := acceptance.RandomTestName()
databaseConnectionPoolName := acceptance.RandomTestName()
var databaseConnectionPool godo.DatabasePool

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanDatabaseConnectionPoolDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseConnectionPoolConfigBasicSkipIfExists, databaseName, databaseConnectionPoolName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanDatabaseConnectionPoolExists("digitalocean_database_connection_pool.pool-01", &databaseConnectionPool),
testAccCheckDigitalOceanDatabaseConnectionPoolAttributes(&databaseConnectionPool, databaseConnectionPoolName),
resource.TestCheckResourceAttr(
"digitalocean_database_connection_pool.pool-01", "name", databaseConnectionPoolName),
resource.TestCheckResourceAttr(
"digitalocean_database_connection_pool.pool-01", "size", "10"),
resource.TestCheckResourceAttr(
"digitalocean_database_connection_pool.pool-01", "mode", "transaction"),
resource.TestCheckResourceAttr(
"digitalocean_database_connection_pool.pool-01", "db_name", "defaultdb"),
resource.TestCheckResourceAttr(
"digitalocean_database_connection_pool.pool-01", "user", "doadmin"),
resource.TestCheckResourceAttrSet(
"digitalocean_database_connection_pool.pool-01", "host"),
resource.TestCheckResourceAttrSet(
"digitalocean_database_connection_pool.pool-01", "private_host"),
resource.TestCheckResourceAttrSet(
"digitalocean_database_connection_pool.pool-01", "port"),
resource.TestCheckResourceAttrSet(
"digitalocean_database_connection_pool.pool-01", "uri"),
resource.TestCheckResourceAttrSet(
"digitalocean_database_connection_pool.pool-01", "private_uri"),
),
},
},
})
}

func testAccCheckDigitalOceanDatabaseConnectionPoolExists(n string, database *godo.DatabasePool) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -212,6 +253,26 @@ resource "digitalocean_database_connection_pool" "pool-01" {
user = "doadmin"
}`

const testAccCheckDigitalOceanDatabaseConnectionPoolConfigBasicSkipIfExists = `
resource "digitalocean_database_cluster" "foobar" {
name = "%s"
engine = "pg"
version = "14"
size = "db-s-1vcpu-1gb"
region = "nyc1"
node_count = 1
}

resource "digitalocean_database_connection_pool" "pool-01" {
cluster_id = digitalocean_database_cluster.foobar.id
name = "%s"
mode = "transaction"
size = 10
db_name = "defaultdb"
user = "doadmin"
skip_if_exists = true
}`

const testAccCheckDigitalOceanDatabaseConnectionPoolConfigUpdated = `
resource "digitalocean_database_cluster" "foobar" {
name = "%s"
Expand Down
1 change: 1 addition & 0 deletions docs/resources/database_connection_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The following arguments are supported:
* `size` - (Required) The desired size of the PGBouncer connection pool.
* `db_name` - (Required) The database for use with the connection pool.
* `user` - (Optional) The name of the database user for use with the connection pool. When excluded, all sessions connect to the database as the inbound user.
* `skip_if_exists` - (Optional) Skips creating a new connection pool if the connection pool already exists.

## Attributes Reference

Expand Down