From ba5497aec459583c24f2d7867d9647cd95bc6542 Mon Sep 17 00:00:00 2001 From: danaelhe <42972711+danaelhe@users.noreply.github.com> Date: Tue, 20 Jun 2023 16:48:18 -0400 Subject: [PATCH 1/3] DB Connection Pool: Add skip_if_exists Addresses #925 --- .../resource_database_connection_pool.go | 16 +++++ .../resource_database_connection_pool_test.go | 65 ++++++++++++++++++- docs/resources/database_connection_pool.md | 1 + 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/digitalocean/database/resource_database_connection_pool.go b/digitalocean/database/resource_database_connection_pool.go index cf31475a4..16da79ecb 100644 --- a/digitalocean/database/resource_database_connection_pool.go +++ b/digitalocean/database/resource_database_connection_pool.go @@ -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, + }, }, } } @@ -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 { diff --git a/digitalocean/database/resource_database_connection_pool_test.go b/digitalocean/database/resource_database_connection_pool_test.go index 974923767..d043c1ea7 100644 --- a/digitalocean/database/resource_database_connection_pool_test.go +++ b/digitalocean/database/resource_database_connection_pool_test.go @@ -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] @@ -197,7 +238,7 @@ const testAccCheckDigitalOceanDatabaseConnectionPoolConfigBasic = ` resource "digitalocean_database_cluster" "foobar" { name = "%s" engine = "pg" - version = "11" + version = "14" size = "db-s-1vcpu-1gb" region = "nyc1" node_count = 1 @@ -212,11 +253,31 @@ 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" engine = "pg" - version = "11" + version = "15" size = "db-s-1vcpu-1gb" region = "nyc1" node_count = 1 diff --git a/docs/resources/database_connection_pool.md b/docs/resources/database_connection_pool.md index 372b2137a..ceab805b3 100644 --- a/docs/resources/database_connection_pool.md +++ b/docs/resources/database_connection_pool.md @@ -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. Only use if do-terraform-provider returned a 5xx on a previous create request that successfully created a pool. ## Attributes Reference From 667fc6ede8c51aa1956cf4b4e64aed15612c2758 Mon Sep 17 00:00:00 2001 From: danaelhe <42972711+danaelhe@users.noreply.github.com> Date: Tue, 20 Jun 2023 17:02:28 -0400 Subject: [PATCH 2/3] terrafmt --- .../resource_database_connection_pool_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/digitalocean/database/resource_database_connection_pool_test.go b/digitalocean/database/resource_database_connection_pool_test.go index d043c1ea7..fa25055a0 100644 --- a/digitalocean/database/resource_database_connection_pool_test.go +++ b/digitalocean/database/resource_database_connection_pool_test.go @@ -264,12 +264,12 @@ resource "digitalocean_database_cluster" "foobar" { } 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" + cluster_id = digitalocean_database_cluster.foobar.id + name = "%s" + mode = "transaction" + size = 10 + db_name = "defaultdb" + user = "doadmin" skip_if_exists = true }` From 781dd9fee87b3bebb6bc8730a6eb6ef369774bd7 Mon Sep 17 00:00:00 2001 From: danaelhe <42972711+danaelhe@users.noreply.github.com> Date: Tue, 20 Jun 2023 17:03:58 -0400 Subject: [PATCH 3/3] update docs --- docs/resources/database_connection_pool.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/database_connection_pool.md b/docs/resources/database_connection_pool.md index ceab805b3..8ea856a27 100644 --- a/docs/resources/database_connection_pool.md +++ b/docs/resources/database_connection_pool.md @@ -39,7 +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. Only use if do-terraform-provider returned a 5xx on a previous create request that successfully created a pool. +* `skip_if_exists` - (Optional) Skips creating a new connection pool if the connection pool already exists. ## Attributes Reference