Skip to content

Commit 39768c5

Browse files
authored
rework connection pool (#607)
* rework connection pool
1 parent 49ad04c commit 39768c5

13 files changed

+390
-168
lines changed

ovh/data_cloud_project_database_log_subscription.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func dataSourceCloudProjectDatabaseLogSubscriptionRead(ctx context.Context, d *s
9494
res := &CloudProjectDatabaseLogSubscriptionResponse{}
9595

9696
log.Printf("[DEBUG] Will read log subscrition %s from cluster %s from project %s", id, clusterID, serviceName)
97-
if err := config.OVHClient.Get(endpoint, res); err != nil {
97+
if err := config.OVHClient.GetWithContext(ctx, endpoint, res); err != nil {
9898
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
9999
}
100100

ovh/data_cloud_project_database_log_subscriptions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func dataSourceCloudProjectDatabaseLogSubscriptionsRead(ctx context.Context, d *
5959
res := make([]string, 0)
6060

6161
log.Printf("[DEBUG] Will read log subscritions from cluster %s from project %s", clusterID, serviceName)
62-
if err := config.OVHClient.Get(endpoint, &res); err != nil {
62+
if err := config.OVHClient.GetWithContext(ctx, endpoint, &res); err != nil {
6363
return diag.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
6464
}
6565

ovh/data_cloud_project_database_postgresql_connection_pool.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -73,36 +73,36 @@ func dataSourceCloudProjectDatabasePostgresqlConnectionPool() *schema.Resource {
7373
func dataSourceCloudProjectDatabasePostgresqlConnectionPoolRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
7474
config := meta.(*Config)
7575
serviceName := d.Get("service_name").(string)
76-
clusterId := d.Get("cluster_id").(string)
76+
clusterID := d.Get("cluster_id").(string)
7777
name := d.Get("name").(string)
7878

7979
listEndpoint := fmt.Sprintf("/cloud/project/%s/database/postgresql/%s/connectionPool",
8080
url.PathEscape(serviceName),
81-
url.PathEscape(clusterId),
81+
url.PathEscape(clusterID),
8282
)
8383

8484
listRes := make([]string, 0)
8585

86-
log.Printf("[DEBUG] Will read connectionPools from cluster %s from project %s", clusterId, serviceName)
86+
log.Printf("[DEBUG] Will read connectionPools from cluster %s from project %s", clusterID, serviceName)
8787
if err := config.OVHClient.GetWithContext(ctx, listEndpoint, &listRes); err != nil {
8888
return diag.Errorf("Error calling GET %s:\n\t %q", listEndpoint, err)
8989
}
9090

9191
for _, id := range listRes {
9292
endpoint := fmt.Sprintf("/cloud/project/%s/database/postgresql/%s/connectionPool/%s",
9393
url.PathEscape(serviceName),
94-
url.PathEscape(clusterId),
94+
url.PathEscape(clusterID),
9595
url.PathEscape(id),
9696
)
9797
res := &CloudProjectDatabasePostgresqlConnectionPoolResponse{}
9898

99-
log.Printf("[DEBUG] Will read connectionPool %s from cluster %s from project %s", id, clusterId, serviceName)
99+
log.Printf("[DEBUG] Will read connectionPool %s from cluster %s from project %s", id, clusterID, serviceName)
100100
if err := config.OVHClient.GetWithContext(ctx, endpoint, res); err != nil {
101101
return diag.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
102102
}
103103

104104
if res.Name == name {
105-
for k, v := range res.ToMap() {
105+
for k, v := range res.toMap() {
106106
if k != "id" {
107107
d.Set(k, v)
108108
} else {
@@ -114,5 +114,5 @@ func dataSourceCloudProjectDatabasePostgresqlConnectionPoolRead(ctx context.Cont
114114
}
115115
}
116116

117-
return diag.Errorf("ConnectionPool name %s not found for cluster %s from project %s", name, clusterId, serviceName)
117+
return diag.Errorf("ConnectionPool name %s not found for cluster %s from project %s", name, clusterID, serviceName)
118118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/url"
8+
"sort"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
13+
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
14+
)
15+
16+
func dataSourceCloudProjectDatabasePostgresqlConnectionPools() *schema.Resource {
17+
return &schema.Resource{
18+
ReadContext: dataSourceCloudProjectDatabasePostgresqlConnectionPoolsRead,
19+
Schema: map[string]*schema.Schema{
20+
"service_name": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
24+
},
25+
"cluster_id": {
26+
Type: schema.TypeString,
27+
Description: "Cluster ID",
28+
Required: true,
29+
},
30+
31+
//Computed
32+
"connection_pool_ids": {
33+
Type: schema.TypeList,
34+
Description: "List of connection pools ids",
35+
Computed: true,
36+
Elem: &schema.Schema{Type: schema.TypeString},
37+
},
38+
},
39+
}
40+
}
41+
42+
func dataSourceCloudProjectDatabasePostgresqlConnectionPoolsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
43+
config := meta.(*Config)
44+
serviceName := d.Get("service_name").(string)
45+
clusterID := d.Get("cluster_id").(string)
46+
47+
endpoint := fmt.Sprintf("/cloud/project/%s/database/postgresql/%s/connectionPool",
48+
url.PathEscape(serviceName),
49+
url.PathEscape(clusterID),
50+
)
51+
res := make([]string, 0)
52+
53+
log.Printf("[DEBUG] Will read connection pools from cluster %s from project %s", clusterID, serviceName)
54+
if err := config.OVHClient.GetWithContext(ctx, endpoint, &res); err != nil {
55+
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
56+
}
57+
58+
// sort.Strings sorts in place, returns nothing
59+
sort.Strings(res)
60+
61+
d.SetId(hashcode.Strings(res))
62+
d.Set("connection_pool_ids", res)
63+
return nil
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
)
11+
12+
const testAccCloudProjectDatabasePostgresqlConnectionPoolsDatasourceConfig = testAccCloudProjectDatabasePostgresqlConnectionPoolConfig + `
13+
14+
data "ovh_cloud_project_database_postgresql_connection_pools" "connection_pools" {
15+
service_name = ovh_cloud_project_database_postgresql_connection_pool.connection_pool.service_name
16+
cluster_id = ovh_cloud_project_database_postgresql_connection_pool.connection_pool.cluster_id
17+
}
18+
`
19+
20+
func TestAccCloudProjectDatabasePostgresqlConnectionPoolsDataSource_basic(t *testing.T) {
21+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
22+
version := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_POSTGRESQL_VERSION_TEST")
23+
if version == "" {
24+
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
25+
}
26+
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
27+
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
28+
description := acctest.RandomWithPrefix(test_prefix)
29+
name := "johndoe"
30+
replication := "replication"
31+
connectionPoolName := "test_connection_pool"
32+
mode := "session"
33+
size := 13
34+
35+
config := fmt.Sprintf(
36+
testAccCloudProjectDatabasePostgresqlConnectionPoolsDatasourceConfig,
37+
serviceName,
38+
description,
39+
version,
40+
region,
41+
flavor,
42+
name,
43+
replication,
44+
connectionPoolName,
45+
mode,
46+
size,
47+
)
48+
49+
resource.Test(t, resource.TestCase{
50+
PreCheck: func() { testAccPreCheckCloudDatabaseNoEngine(t) },
51+
Providers: testAccProviders,
52+
Steps: []resource.TestStep{
53+
{
54+
Config: config,
55+
Check: resource.ComposeTestCheckFunc(
56+
resource.TestCheckResourceAttrSet(
57+
"data.ovh_cloud_project_database_postgresql_connection_pools.connection_pools",
58+
"connection_pool_ids.#",
59+
),
60+
),
61+
},
62+
},
63+
})
64+
}

ovh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func Provider() *schema.Provider {
7373
"ovh_cloud_project_database_opensearch_user": dataSourceCloudProjectDatabaseOpensearchUser(),
7474
"ovh_cloud_project_database_postgresql_user": dataSourceCloudProjectDatabasePostgresqlUser(),
7575
"ovh_cloud_project_database_postgresql_connection_pool": dataSourceCloudProjectDatabasePostgresqlConnectionPool(),
76+
"ovh_cloud_project_database_postgresql_connection_pools": dataSourceCloudProjectDatabasePostgresqlConnectionPools(),
7677
"ovh_cloud_project_database_redis_user": dataSourceCloudProjectDatabaseRedisUser(),
7778
"ovh_cloud_project_database_user": dataSourceCloudProjectDatabaseUser(),
7879
"ovh_cloud_project_database_users": dataSourceCloudProjectDatabaseUsers(),

ovh/resource_cloud_project_database.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func resourceCloudProjectDatabaseUpdate(ctx context.Context, d *schema.ResourceD
371371
return diag.Errorf("service update failed : %q", err)
372372
}
373373
log.Printf("[DEBUG] Will update database: %+v", params)
374-
err = config.OVHClient.Put(endpoint, params, nil)
374+
err = config.OVHClient.PutWithContext(ctx, endpoint, params, nil)
375375
if err != nil {
376376
return diag.Errorf("calling Put %s with params %v:\n\t %q", endpoint, params, err)
377377
}
@@ -387,7 +387,7 @@ func resourceCloudProjectDatabaseUpdate(ctx context.Context, d *schema.ResourceD
387387

388388
advancedConfigEndpoint := fmt.Sprintf("%s/advancedConfiguration", endpoint)
389389

390-
err = config.OVHClient.Put(advancedConfigEndpoint, acParams, nil)
390+
err = config.OVHClient.PutWithContext(ctx, advancedConfigEndpoint, acParams, nil)
391391
if err != nil {
392392
return diag.Errorf("calling Put %s with params %v:\n\t %q", advancedConfigEndpoint, acParams, err)
393393
}

ovh/resource_cloud_project_database_log_subscription.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func resourceCloudProjectDatabaseLogSubscriptionCreate(ctx context.Context, d *s
131131
res := &CloudProjectDatabaseLogSubscriptionResponse{}
132132

133133
log.Printf("[DEBUG] Will create Log subscrition : %+v for cluster %s from project %s", params, clusterID, serviceName)
134-
err := config.OVHClient.Post(endpoint, params, res)
134+
err := config.OVHClient.PostWithContext(ctx, endpoint, params, res)
135135
if err != nil {
136136
diag.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, err)
137137
}
@@ -165,7 +165,7 @@ func resourceCloudProjectDatabaseLogSubscriptionRead(ctx context.Context, d *sch
165165
res := &CloudProjectDatabaseLogSubscriptionResponse{}
166166

167167
log.Printf("[DEBUG] Will read log subscrition %s from cluster %s from project %s", id, clusterID, serviceName)
168-
if err := config.OVHClient.Get(endpoint, res); err != nil {
168+
if err := config.OVHClient.GetWithContext(ctx, endpoint, res); err != nil {
169169
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
170170
}
171171

@@ -200,7 +200,7 @@ func resourceCloudProjectDatabaseLogSubscriptionDelete(ctx context.Context, d *s
200200
res := &CloudProjectDatabaseLogSubscriptionResponse{}
201201

202202
log.Printf("[DEBUG] Will delete Log subscrition %s from cluster %s from project %s", id, clusterID, serviceName)
203-
err := config.OVHClient.Delete(endpoint, res)
203+
err := config.OVHClient.DeleteWithContext(ctx, endpoint, res)
204204
if err != nil {
205205
diag.Errorf("calling DELETE %s:\n\t %q", endpoint, err)
206206
}

ovh/resource_cloud_project_database_m3db_namespace.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func resourceCloudProjectDatabaseM3dbNamespaceUpdate(ctx context.Context, d *sch
209209
params := (&CloudProjectDatabaseM3dbNamespaceUpdateOpts{}).FromResource(d)
210210

211211
log.Printf("[DEBUG] Will update namespace: %+v from cluster %s from project %s", params, clusterId, serviceName)
212-
err := config.OVHClient.Put(endpoint, params, nil)
212+
err := config.OVHClient.PutWithContext(ctx, endpoint, params, nil)
213213
if err != nil {
214214
return diag.Errorf("calling Put %s with params %+v:\n\t %q", endpoint, params, err)
215215
}

ovh/resource_cloud_project_database_mongodb_user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func resourceCloudProjectDatabaseMongodbUserUpdate(ctx context.Context, d *schem
194194
retry.RetryContext(ctx, d.Timeout(schema.TimeoutUpdate),
195195
func() *retry.RetryError {
196196
log.Printf("[DEBUG] Will update user: %+v from cluster %s from project %s", params, clusterId, serviceName)
197-
rErr := config.OVHClient.Put(endpoint, params, nil)
197+
rErr := config.OVHClient.PutWithContext(ctx, endpoint, params, nil)
198198
if rErr != nil {
199199
if errOvh, ok := rErr.(*ovh.APIError); ok && (errOvh.Code == 409) {
200200
return retry.RetryableError(rErr)

0 commit comments

Comments
 (0)