Skip to content

Commit 9e112a1

Browse files
feat: support Cloud SQL\'s new field connection_pool_config. (#13497) (#9918)
[upstream:db2dbb701f705e2936c76e9cff368038ca0385f6] Signed-off-by: Modular Magician <[email protected]>
1 parent 29e22ba commit 9e112a1

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

.changelog/13497.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
sql: added `connection_pool_config` field.
3+
```

google-beta/services/sql/resource_sql_database_instance.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ var (
7575
"settings.0.backup_configuration.0.transaction_log_retention_days",
7676
}
7777

78+
connectionPoolConfigKeys = []string{
79+
"settings.0.connection_pool_config.0.connection_pooling_enabled",
80+
"settings.0.connection_pool_config.0.flags",
81+
}
82+
7883
ipConfigurationKeys = []string{
7984
"settings.0.ip_configuration.0.authorized_networks",
8085
"settings.0.ip_configuration.0.ipv4_enabled",
@@ -444,6 +449,28 @@ is set to true. Defaults to ZONAL.`,
444449
Computed: true,
445450
Description: `Provisioned throughput measured in MiB per second for the data disk. This field is only used for HYPERDISK_BALANCED disk types.`,
446451
},
452+
"connection_pool_config": {
453+
Type: schema.TypeSet,
454+
Optional: true,
455+
Computed: true,
456+
Description: `The managed connection pool setting for a Cloud SQL instance.`,
457+
Elem: &schema.Resource{
458+
Schema: map[string]*schema.Schema{
459+
"connection_pooling_enabled": {
460+
Type: schema.TypeBool,
461+
Optional: true,
462+
Description: `Whether Managed Connection Pool is enabled for this instance.`,
463+
},
464+
"flags": {
465+
Type: schema.TypeSet,
466+
Optional: true,
467+
Set: schema.HashResource(sqlDatabaseFlagSchemaElem),
468+
Elem: sqlDatabaseFlagSchemaElem,
469+
Description: `List of connection pool configuration flags`,
470+
},
471+
},
472+
},
473+
},
447474
"ip_configuration": {
448475
Type: schema.TypeList,
449476
Optional: true,
@@ -1423,6 +1450,7 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, databaseVersion
14231450
UserLabels: tpgresource.ConvertStringMap(_settings["user_labels"].(map[string]interface{})),
14241451
BackupConfiguration: expandBackupConfiguration(_settings["backup_configuration"].([]interface{})),
14251452
DatabaseFlags: expandDatabaseFlags(_settings["database_flags"].(*schema.Set).List()),
1453+
ConnectionPoolConfig: expandConnectionPoolConfig(_settings["connection_pool_config"].(*schema.Set).List()),
14261454
IpConfiguration: expandIpConfiguration(_settings["ip_configuration"].([]interface{}), databaseVersion),
14271455
LocationPreference: expandLocationPreference(_settings["location_preference"].([]interface{})),
14281456
MaintenanceWindow: expandMaintenanceWindow(_settings["maintenance_window"].([]interface{})),
@@ -1572,6 +1600,35 @@ func expandPscConfig(configured []interface{}) *sqladmin.PscConfig {
15721600
return nil
15731601
}
15741602

1603+
func expandFlags(configured []interface{}) []*sqladmin.ConnectionPoolFlags {
1604+
connectionPoolFlags := make([]*sqladmin.ConnectionPoolFlags, 0, len(configured))
1605+
for _, _flag := range configured {
1606+
if _flag == nil {
1607+
continue
1608+
}
1609+
_entry := _flag.(map[string]interface{})
1610+
1611+
connectionPoolFlags = append(connectionPoolFlags, &sqladmin.ConnectionPoolFlags{
1612+
Name: _entry["name"].(string),
1613+
Value: _entry["value"].(string),
1614+
})
1615+
}
1616+
return connectionPoolFlags
1617+
}
1618+
1619+
func expandConnectionPoolConfig(configured []interface{}) *sqladmin.ConnectionPoolConfig {
1620+
if len(configured) == 0 || configured[0] == nil {
1621+
return nil
1622+
}
1623+
1624+
_connectionPoolConfig := configured[0].(map[string]interface{})
1625+
1626+
return &sqladmin.ConnectionPoolConfig{
1627+
ConnectionPoolingEnabled: _connectionPoolConfig["connection_pooling_enabled"].(bool),
1628+
Flags: expandFlags(_connectionPoolConfig["flags"].(*schema.Set).List()),
1629+
}
1630+
}
1631+
15751632
func expandAuthorizedNetworks(configured []interface{}) []*sqladmin.AclEntry {
15761633
an := make([]*sqladmin.AclEntry, 0, len(configured))
15771634
for _, _acl := range configured {
@@ -2331,6 +2388,10 @@ func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[
23312388
data["database_flags"] = flattenDatabaseFlags(settings.DatabaseFlags)
23322389
}
23332390

2391+
if settings.ConnectionPoolConfig != nil {
2392+
data["connection_pool_config"] = flattenConnectionPoolConfig(settings.ConnectionPoolConfig)
2393+
}
2394+
23342395
if settings.IpConfiguration != nil {
23352396
data["ip_configuration"] = flattenIpConfiguration(settings.IpConfiguration, d)
23362397
}
@@ -2501,6 +2562,38 @@ func flattenReplicationCluster(replicationCluster *sqladmin.ReplicationCluster,
25012562
return []map[string]interface{}{data}
25022563
}
25032564

2565+
func flattenConnectionPoolFlags(connectionPoolFlags []*sqladmin.ConnectionPoolFlags) []interface{} {
2566+
if len(connectionPoolFlags) == 0 { // Handles nil or empty slice
2567+
return make([]interface{}, 0) // Explicitly return empty slice
2568+
}
2569+
2570+
mcpflags := make([]interface{}, len(connectionPoolFlags)) // Pre-allocate for efficiency
2571+
for i, mcpflag := range connectionPoolFlags {
2572+
data := map[string]interface{}{
2573+
"name": mcpflag.Name,
2574+
"value": mcpflag.Value,
2575+
}
2576+
mcpflags[i] = data
2577+
}
2578+
return mcpflags
2579+
}
2580+
2581+
func flattenConnectionPoolConfig(connectionPoolConfig *sqladmin.ConnectionPoolConfig) []interface{} {
2582+
if connectionPoolConfig == nil {
2583+
return []interface{}{
2584+
map[string]interface{}{
2585+
"connection_pooling_enabled": false,
2586+
"flags": make([]interface{}, 0), // Default to empty flags
2587+
},
2588+
}
2589+
}
2590+
data := map[string]interface{}{
2591+
"connection_pooling_enabled": connectionPoolConfig.ConnectionPoolingEnabled, // Corrected key
2592+
"flags": flattenConnectionPoolFlags(connectionPoolConfig.Flags), // Corrected key
2593+
}
2594+
return []interface{}{data}
2595+
}
2596+
25042597
func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration, d *schema.ResourceData) interface{} {
25052598
data := map[string]interface{}{
25062599
"ipv4_enabled": ipConfiguration.Ipv4Enabled,

google-beta/services/sql/resource_sql_database_instance_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,52 @@ func TestAccSqlDatabaseInstance_withPrivateNetwork_withoutAllocatedIpRange(t *te
848848
})
849849
}
850850

851+
func TestAccSqlDatabaseInstance_withMCPEnabled(t *testing.T) {
852+
t.Parallel()
853+
854+
instanceName := "tf-test-" + acctest.RandString(t, 10)
855+
856+
acctest.VcrTest(t, resource.TestCase{
857+
PreCheck: func() { acctest.AccTestPreCheck(t) },
858+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
859+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
860+
Steps: []resource.TestStep{
861+
{
862+
Config: testAccSqlDatabaseInstance_withMCPEnabled(instanceName),
863+
},
864+
{
865+
ResourceName: "google_sql_database_instance.instance",
866+
ImportState: true,
867+
ImportStateVerify: true,
868+
ImportStateVerifyIgnore: []string{"deletion_protection"},
869+
},
870+
},
871+
})
872+
}
873+
874+
func TestAccSqlDatabaseInstance_withoutMCPEnabled(t *testing.T) {
875+
t.Parallel()
876+
877+
instanceName := "tf-test-" + acctest.RandString(t, 10)
878+
879+
acctest.VcrTest(t, resource.TestCase{
880+
PreCheck: func() { acctest.AccTestPreCheck(t) },
881+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
882+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
883+
Steps: []resource.TestStep{
884+
{
885+
Config: testAccSqlDatabaseInstance_withoutMCPEnabled(instanceName),
886+
},
887+
{
888+
ResourceName: "google_sql_database_instance.instance",
889+
ImportState: true,
890+
ImportStateVerify: true,
891+
ImportStateVerifyIgnore: []string{"deletion_protection"},
892+
},
893+
},
894+
})
895+
}
896+
851897
func TestAccSqlDatabaseInstance_withPSCEnabled_withoutAllowedConsumerProjects(t *testing.T) {
852898
t.Parallel()
853899

@@ -4762,6 +4808,41 @@ func verifyPscAutoConnectionsOperation(resourceName string, isPscConfigExpected
47624808
}
47634809
}
47644810

4811+
func testAccSqlDatabaseInstance_withoutMCPEnabled(instanceName string) string {
4812+
return fmt.Sprintf(`
4813+
resource "google_sql_database_instance" "instance" {
4814+
name = "%s"
4815+
region = "us-central1"
4816+
database_version = "POSTGRES_16"
4817+
deletion_protection = false
4818+
settings {
4819+
tier = "db-perf-optimized-N-2"
4820+
}
4821+
}
4822+
`, instanceName)
4823+
}
4824+
4825+
func testAccSqlDatabaseInstance_withMCPEnabled(instanceName string) string {
4826+
return fmt.Sprintf(`
4827+
resource "google_sql_database_instance" "instance" {
4828+
name = "%s"
4829+
region = "us-central1"
4830+
database_version = "POSTGRES_16"
4831+
deletion_protection = false
4832+
settings {
4833+
tier = "db-perf-optimized-N-2"
4834+
connection_pool_config {
4835+
connection_pooling_enabled = true
4836+
flags {
4837+
name = "max_client_connections"
4838+
value = "1980"
4839+
}
4840+
}
4841+
}
4842+
}
4843+
`, instanceName)
4844+
}
4845+
47654846
func testAccSqlDatabaseInstance_withPSCEnabled_withoutPscAutoConnections(instanceName string) string {
47664847
return fmt.Sprintf(`
47674848
resource "google_sql_database_instance" "instance" {

website/docs/r/sql_database_instance.html.markdown

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,25 @@ resource "google_sql_database_instance" "main" {
173173
}
174174
```
175175

176+
### Cloud SQL Instance with MCP
177+
```hcl
178+
resource "google_sql_database_instance" "instance" {
179+
name: = "mcp-enabled-main-instance"
180+
region = "us-central1"
181+
database_version = "POSTGRES_16"
182+
settings {
183+
tier = "db-perf-optimized-N-2"
184+
connection_pool_config {
185+
connection_pooling_enabled = true
186+
flags {
187+
name = "max_client_connections"
188+
value = "1980"
189+
}
190+
}
191+
}
192+
}
193+
```
194+
176195
### Cloud SQL Instance with PSC connectivity
177196

178197
```hcl
@@ -571,6 +590,16 @@ The optional, computed `replication_cluster` block represents a primary instance
571590

572591
* `dr_replica`: Read-only field that indicates whether the replica is a DR replica.
573592

593+
The optional `settings.connection_pool_config` subblock supports:
594+
595+
* `connection_pooling_enabled`: (Optional) True if the manager connection pooling configuration is enabled.
596+
597+
The optional `settings.connection_pool_config.flags` sublist supports:
598+
599+
* `name` - (Required) Name of the flag.
600+
601+
* `value` - (Required) Value of the flag.
602+
574603
## Attributes Reference
575604

576605
In addition to the arguments listed above, the following computed attributes are

0 commit comments

Comments
 (0)