Skip to content

Commit eb70bcb

Browse files
author
Abdelkarim Boujida
committed
refactor to have multiple db connections on provider level
1 parent a529fb5 commit eb70bcb

17 files changed

Lines changed: 654 additions & 394 deletions

docs/index.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,24 @@ provider "cloudsqlpostgresql" {
3232

3333
<!-- schema generated by tfplugindocs -->
3434
## Schema
35+
36+
### Required
37+
38+
- `connection_configs` (Attributes Map) A map of connections of Postgresql database instances (see [below for nested schema](#nestedatt--connection_configs))
39+
40+
<a id="nestedatt--connection_configs"></a>
41+
### Nested Schema for `connection_configs`
42+
43+
Required:
44+
45+
- `connection_name` (String) The connection name of the Google Cloud SQL Postgresql instance. The `connection_name` format should be `<project>:<region>:<instance>`
46+
- `password` (String, Sensitive) The password to use to authenticate using the built-in database authentication
47+
- `username` (String) The username to use to authenticate with the Cloud SQL Postgresql instance
48+
49+
Optional:
50+
51+
- `database` (String) The database to connect to. Defaults to `postgres`.
52+
- `private_ip` (Boolean) Use the private IP address of the Cloud SQL Postgresql instance to connect to
53+
- `proxy` (String) Proxy socks url if used. Format needs to be `socks5://<ip>:<port>`
54+
- `psc` (Boolean) Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to
55+
- `ssl_mode` (String) Determine the security of the connection to the Cloud SQL Postgresql instance

docs/resources/default_privileges.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The `cloudsqlpostgresql_default_privileges` resource allows to set the privilege
1717

1818
### Required
1919

20-
- `connection_config` (Attributes) The connection properties for the Cloud SQL instance. (see [below for nested schema](#nestedatt--connection_config))
20+
- `connection_config` (String) The key of the connection defined in the provider
2121
- `owner` (String) The target role
2222
- `privileges` (Attributes Set) A list of privileges (see [below for nested schema](#nestedatt--privileges))
2323
- `role` (String) The role
@@ -27,24 +27,6 @@ The `cloudsqlpostgresql_default_privileges` resource allows to set the privilege
2727
- `object_type` (String) The object type, can be `TABLES`, `SEQUENCES`, `FUNCTIONS`, `ROUTINES`, `TYPES` or `SCHEMAS`. Defaults to `TABLES`.
2828
- `schema` (String) The schema
2929

30-
<a id="nestedatt--connection_config"></a>
31-
### Nested Schema for `connection_config`
32-
33-
Required:
34-
35-
- `connection_name` (String) The connection name of the Google Cloud SQL Postgresql instance. The `connection_name` format should be `<project>:<region>:<instance>`
36-
- `password` (String, Sensitive) The password to use to authenticate using the built-in database authentication
37-
- `username` (String) The username to use to authenticate with the Cloud SQL Postgresql instance
38-
39-
Optional:
40-
41-
- `database` (String) The database to connect to. Defaults to `postgres`.
42-
- `private_ip` (Boolean) Use the private IP address of the Cloud SQL Postgresql instance to connect to
43-
- `proxy` (String) Proxy socks url if used. Format needs to be `socks5://<ip>:<port>`
44-
- `psc` (Boolean) Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to
45-
- `ssl_mode` (String) Determine the security of the connection to the Cloud SQL Postgresql instance
46-
47-
4830
<a id="nestedatt--privileges"></a>
4931
### Nested Schema for `privileges`
5032

docs/resources/grant_database.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,11 @@ resource "cloudsqlpostgresql_grant_database" "default" {
2929

3030
### Required
3131

32-
- `connection_config` (Attributes) The connection properties for the Cloud SQL instance. (see [below for nested schema](#nestedatt--connection_config))
32+
- `connection_config` (String) The key of the connection defined in the provider
3333
- `database` (String) The database on which the privileges will be granted for this role.
3434
- `privileges` (Attributes Set) A list of privileges to grant on the database for this role. (see [below for nested schema](#nestedatt--privileges))
3535
- `role` (String) The name of the role to grant privileges on the database. Can be username or role.
3636

37-
<a id="nestedatt--connection_config"></a>
38-
### Nested Schema for `connection_config`
39-
40-
Required:
41-
42-
- `connection_name` (String) The connection name of the Google Cloud SQL Postgresql instance. The `connection_name` format should be `<project>:<region>:<instance>`
43-
- `password` (String, Sensitive) The password to use to authenticate using the built-in database authentication
44-
- `username` (String) The username to use to authenticate with the Cloud SQL Postgresql instance
45-
46-
Optional:
47-
48-
- `database` (String) The database to connect to. Defaults to `postgres`.
49-
- `private_ip` (Boolean) Use the private IP address of the Cloud SQL Postgresql instance to connect to
50-
- `proxy` (String) Proxy socks url if used. Format needs to be `socks5://<ip>:<port>`
51-
- `psc` (Boolean) Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to
52-
- `ssl_mode` (String) Determine the security of the connection to the Cloud SQL Postgresql instance
53-
54-
5537
<a id="nestedatt--privileges"></a>
5638
### Nested Schema for `privileges`
5739

docs/resources/grant_role.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,10 @@ The `cloudsqlpostgresql_grant_role` resource creates and manages role membership
1717

1818
### Required
1919

20-
- `connection_config` (Attributes) The connection properties for the Cloud SQL instance. (see [below for nested schema](#nestedatt--connection_config))
20+
- `connection_config` (String) The key of the connection defined in the provider
2121
- `group_role` (String) The `group_role` that will get the `role` as member
2222
- `role` (String) The `role` that will be a member of the `group_role`
2323

2424
### Optional
2525

2626
- `admin_option` (Boolean) Enable admin option
27-
28-
<a id="nestedatt--connection_config"></a>
29-
### Nested Schema for `connection_config`
30-
31-
Required:
32-
33-
- `connection_name` (String) The connection name of the Google Cloud SQL Postgresql instance. The `connection_name` format should be `<project>:<region>:<instance>`
34-
- `password` (String, Sensitive) The password to use to authenticate using the built-in database authentication
35-
- `username` (String) The username to use to authenticate with the Cloud SQL Postgresql instance
36-
37-
Optional:
38-
39-
- `database` (String) The database to connect to. Defaults to `postgres`.
40-
- `private_ip` (Boolean) Use the private IP address of the Cloud SQL Postgresql instance to connect to
41-
- `proxy` (String) Proxy socks url if used. Format needs to be `socks5://<ip>:<port>`
42-
- `psc` (Boolean) Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to
43-
- `ssl_mode` (String) Determine the security of the connection to the Cloud SQL Postgresql instance

docs/resources/grant_schema.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,11 @@ resource "cloudsqlpostgresql_grant_database" "default" {
3030

3131
### Required
3232

33-
- `connection_config` (Attributes) The connection properties for the Cloud SQL instance. (see [below for nested schema](#nestedatt--connection_config))
33+
- `connection_config` (String) The key of the connection defined in the provider
3434
- `privileges` (Attributes Set) A list of privileges to grant on the schema for this role. (see [below for nested schema](#nestedatt--privileges))
3535
- `role` (String) The name of the role to grant privileges on the schema. Can be username or role.
3636
- `schema` (String) The schema on which the privileges will be granted for this role.
3737

38-
<a id="nestedatt--connection_config"></a>
39-
### Nested Schema for `connection_config`
40-
41-
Required:
42-
43-
- `connection_name` (String) The connection name of the Google Cloud SQL Postgresql instance. The `connection_name` format should be `<project>:<region>:<instance>`
44-
- `password` (String, Sensitive) The password to use to authenticate using the built-in database authentication
45-
- `username` (String) The username to use to authenticate with the Cloud SQL Postgresql instance
46-
47-
Optional:
48-
49-
- `database` (String) The database to connect to. Defaults to `postgres`.
50-
- `private_ip` (Boolean) Use the private IP address of the Cloud SQL Postgresql instance to connect to
51-
- `proxy` (String) Proxy socks url if used. Format needs to be `socks5://<ip>:<port>`
52-
- `psc` (Boolean) Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to
53-
- `ssl_mode` (String) Determine the security of the connection to the Cloud SQL Postgresql instance
54-
55-
5638
<a id="nestedatt--privileges"></a>
5739
### Nested Schema for `privileges`
5840

docs/resources/grant_table.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,12 @@ resource "cloudsqlpostgresql_grant_table" "default" {
3434

3535
### Required
3636

37-
- `connection_config` (Attributes) The connection properties for the Cloud SQL instance. (see [below for nested schema](#nestedatt--connection_config))
37+
- `connection_config` (String) The key of the connection defined in the provider
3838
- `privileges` (Attributes Set) A list of privileges to grant on the table for this role. (see [below for nested schema](#nestedatt--privileges))
3939
- `role` (String) The name of the role to grant privileges on the table. Can be username or role.
4040
- `schema` (String) The schema where the table resides.
4141
- `table` (String) The table on which the privileges will be granted for this role.
4242

43-
<a id="nestedatt--connection_config"></a>
44-
### Nested Schema for `connection_config`
45-
46-
Required:
47-
48-
- `connection_name` (String) The connection name of the Google Cloud SQL Postgresql instance. The `connection_name` format should be `<project>:<region>:<instance>`
49-
- `password` (String, Sensitive) The password to use to authenticate using the built-in database authentication
50-
- `username` (String) The username to use to authenticate with the Cloud SQL Postgresql instance
51-
52-
Optional:
53-
54-
- `database` (String) The database to connect to. Defaults to `postgres`.
55-
- `private_ip` (Boolean) Use the private IP address of the Cloud SQL Postgresql instance to connect to
56-
- `proxy` (String) Proxy socks url if used. Format needs to be `socks5://<ip>:<port>`
57-
- `psc` (Boolean) Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to
58-
- `ssl_mode` (String) Determine the security of the connection to the Cloud SQL Postgresql instance
59-
60-
6143
<a id="nestedatt--privileges"></a>
6244
### Nested Schema for `privileges`
6345

docs/resources/role.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The `cloudsqlpostgresql_role` resource creates and manages a role. The superuser
1717

1818
### Required
1919

20-
- `connection_config` (Attributes) The connection properties for the Cloud SQL instance. (see [below for nested schema](#nestedatt--connection_config))
20+
- `connection_config` (String) The key of the connection defined in the provider
2121
- `name` (String) The name of the role
2222

2323
### Optional
@@ -31,20 +31,3 @@ The `cloudsqlpostgresql_role` resource creates and manages a role. The superuser
3131
- `is_user` (Boolean) Is this role a user that can login
3232
- `is_valid_until` (String) Sets a date and time after which the role's password is no longer valid
3333
- `password` (String, Sensitive) Sets the role's password
34-
35-
<a id="nestedatt--connection_config"></a>
36-
### Nested Schema for `connection_config`
37-
38-
Required:
39-
40-
- `connection_name` (String) The connection name of the Google Cloud SQL Postgresql instance. The `connection_name` format should be `<project>:<region>:<instance>`
41-
- `password` (String, Sensitive) The password to use to authenticate using the built-in database authentication
42-
- `username` (String) The username to use to authenticate with the Cloud SQL Postgresql instance
43-
44-
Optional:
45-
46-
- `database` (String) The database to connect to. Defaults to `postgres`.
47-
- `private_ip` (Boolean) Use the private IP address of the Cloud SQL Postgresql instance to connect to
48-
- `proxy` (String) Proxy socks url if used. Format needs to be `socks5://<ip>:<port>`
49-
- `psc` (Boolean) Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to
50-
- `ssl_mode` (String) Determine the security of the connection to the Cloud SQL Postgresql instance

internal/provider/config.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,53 @@ import (
1717
type Config struct {
1818
dbRegistry map[string]*sql.DB
1919
dbRegistryMutex sync.Mutex
20+
dbDrivers map[string]bool
21+
dbDriversMutex sync.Mutex
22+
connections map[string]*ConnectionConfig
2023
}
2124

2225
func NewConfig() *Config {
2326
return &Config{
24-
dbRegistry: make(map[string]*sql.DB),
27+
dbRegistry: make(map[string]*sql.DB),
28+
dbDrivers: make(map[string]bool),
29+
connections: make(map[string]*ConnectionConfig),
2530
}
2631
}
2732

2833
func (c *Config) connectToPostgresql(ctx context.Context, cc *ConnectionConfig) (*sql.DB, error) {
2934
c.dbRegistryMutex.Lock()
3035
defer c.dbRegistryMutex.Unlock()
3136

32-
id, err := cc.Id()
33-
if err != nil {
34-
return nil, err
35-
}
37+
key := cc.DsnKey()
3638

37-
if c.dbRegistry[id] != nil {
38-
return c.dbRegistry[id], nil
39+
if c.dbRegistry[key] != nil {
40+
return c.dbRegistry[key], nil
3941
}
4042

41-
err = createSqlDriver(ctx, cc)
43+
err := c.registerDriver(ctx, cc)
4244
if err != nil {
4345
return nil, err
4446
}
4547

46-
db, err := sql.Open(id, cc.Dsn())
48+
db, err := sql.Open(cc.DriverKey(), cc.Dsn())
4749
if err != nil {
4850
return nil, err
4951
}
5052

51-
c.dbRegistry[id] = db
52-
return c.dbRegistry[id], nil
53+
c.dbRegistry[key] = db
54+
return c.dbRegistry[key], nil
5355
}
5456

55-
func createSqlDriver(ctx context.Context, cc *ConnectionConfig) error {
57+
func (c *Config) registerDriver(ctx context.Context, cc *ConnectionConfig) error {
58+
c.dbDriversMutex.Lock()
59+
defer c.dbDriversMutex.Unlock()
60+
61+
key := cc.DriverKey()
62+
63+
if c.dbDrivers[key] {
64+
return nil
65+
}
66+
5667
var (
5768
dialOptions []cloudsqlconn.DialOption
5869
options []cloudsqlconn.Option
@@ -72,14 +83,13 @@ func createSqlDriver(ctx context.Context, cc *ConnectionConfig) error {
7283
options = append(options, cloudsqlconn.WithDialFunc(createDialer(cc.Proxy.ValueString(), ctx)))
7384
}
7485

75-
id, err := cc.Id()
86+
_, err := pgxv4.RegisterDriver(key, options...)
7687
if err != nil {
7788
return err
7889
}
7990

80-
_, err = pgxv4.RegisterDriver(id, options...)
81-
82-
return err
91+
c.dbDrivers[key] = true
92+
return nil
8393
}
8494

8595
func createDialer(proxyInput string, ctxProvider context.Context) func(ctx context.Context, network, addr string) (net.Conn, error) {

0 commit comments

Comments
 (0)