Skip to content

Commit d4064e4

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

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/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",
@@ -432,6 +437,28 @@ is set to true. Defaults to ZONAL.`,
432437
DiffSuppressFunc: caseDiffDashSuppress,
433438
Description: `The type of supported data disk is tier dependent and can be PD_SSD or PD_HDD or HYPERDISK_BALANCED.`,
434439
},
440+
"connection_pool_config": {
441+
Type: schema.TypeSet,
442+
Optional: true,
443+
Computed: true,
444+
Description: `The managed connection pool setting for a Cloud SQL instance.`,
445+
Elem: &schema.Resource{
446+
Schema: map[string]*schema.Schema{
447+
"connection_pooling_enabled": {
448+
Type: schema.TypeBool,
449+
Optional: true,
450+
Description: `Whether Managed Connection Pool is enabled for this instance.`,
451+
},
452+
"flags": {
453+
Type: schema.TypeSet,
454+
Optional: true,
455+
Set: schema.HashResource(sqlDatabaseFlagSchemaElem),
456+
Elem: sqlDatabaseFlagSchemaElem,
457+
Description: `List of connection pool configuration flags`,
458+
},
459+
},
460+
},
461+
},
435462
"ip_configuration": {
436463
Type: schema.TypeList,
437464
Optional: true,
@@ -1409,6 +1436,7 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, databaseVersion
14091436
UserLabels: tpgresource.ConvertStringMap(_settings["user_labels"].(map[string]interface{})),
14101437
BackupConfiguration: expandBackupConfiguration(_settings["backup_configuration"].([]interface{})),
14111438
DatabaseFlags: expandDatabaseFlags(_settings["database_flags"].(*schema.Set).List()),
1439+
ConnectionPoolConfig: expandConnectionPoolConfig(_settings["connection_pool_config"].(*schema.Set).List()),
14121440
IpConfiguration: expandIpConfiguration(_settings["ip_configuration"].([]interface{}), databaseVersion),
14131441
LocationPreference: expandLocationPreference(_settings["location_preference"].([]interface{})),
14141442
MaintenanceWindow: expandMaintenanceWindow(_settings["maintenance_window"].([]interface{})),
@@ -1558,6 +1586,35 @@ func expandPscConfig(configured []interface{}) *sqladmin.PscConfig {
15581586
return nil
15591587
}
15601588

1589+
func expandFlags(configured []interface{}) []*sqladmin.ConnectionPoolFlags {
1590+
connectionPoolFlags := make([]*sqladmin.ConnectionPoolFlags, 0, len(configured))
1591+
for _, _flag := range configured {
1592+
if _flag == nil {
1593+
continue
1594+
}
1595+
_entry := _flag.(map[string]interface{})
1596+
1597+
connectionPoolFlags = append(connectionPoolFlags, &sqladmin.ConnectionPoolFlags{
1598+
Name: _entry["name"].(string),
1599+
Value: _entry["value"].(string),
1600+
})
1601+
}
1602+
return connectionPoolFlags
1603+
}
1604+
1605+
func expandConnectionPoolConfig(configured []interface{}) *sqladmin.ConnectionPoolConfig {
1606+
if len(configured) == 0 || configured[0] == nil {
1607+
return nil
1608+
}
1609+
1610+
_connectionPoolConfig := configured[0].(map[string]interface{})
1611+
1612+
return &sqladmin.ConnectionPoolConfig{
1613+
ConnectionPoolingEnabled: _connectionPoolConfig["connection_pooling_enabled"].(bool),
1614+
Flags: expandFlags(_connectionPoolConfig["flags"].(*schema.Set).List()),
1615+
}
1616+
}
1617+
15611618
func expandAuthorizedNetworks(configured []interface{}) []*sqladmin.AclEntry {
15621619
an := make([]*sqladmin.AclEntry, 0, len(configured))
15631620
for _, _acl := range configured {
@@ -2315,6 +2372,10 @@ func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[
23152372
data["database_flags"] = flattenDatabaseFlags(settings.DatabaseFlags)
23162373
}
23172374

2375+
if settings.ConnectionPoolConfig != nil {
2376+
data["connection_pool_config"] = flattenConnectionPoolConfig(settings.ConnectionPoolConfig)
2377+
}
2378+
23182379
if settings.IpConfiguration != nil {
23192380
data["ip_configuration"] = flattenIpConfiguration(settings.IpConfiguration, d)
23202381
}
@@ -2485,6 +2546,38 @@ func flattenReplicationCluster(replicationCluster *sqladmin.ReplicationCluster,
24852546
return []map[string]interface{}{data}
24862547
}
24872548

2549+
func flattenConnectionPoolFlags(connectionPoolFlags []*sqladmin.ConnectionPoolFlags) []interface{} {
2550+
if len(connectionPoolFlags) == 0 { // Handles nil or empty slice
2551+
return make([]interface{}, 0) // Explicitly return empty slice
2552+
}
2553+
2554+
mcpflags := make([]interface{}, len(connectionPoolFlags)) // Pre-allocate for efficiency
2555+
for i, mcpflag := range connectionPoolFlags {
2556+
data := map[string]interface{}{
2557+
"name": mcpflag.Name,
2558+
"value": mcpflag.Value,
2559+
}
2560+
mcpflags[i] = data
2561+
}
2562+
return mcpflags
2563+
}
2564+
2565+
func flattenConnectionPoolConfig(connectionPoolConfig *sqladmin.ConnectionPoolConfig) []interface{} {
2566+
if connectionPoolConfig == nil {
2567+
return []interface{}{
2568+
map[string]interface{}{
2569+
"connection_pooling_enabled": false,
2570+
"flags": make([]interface{}, 0), // Default to empty flags
2571+
},
2572+
}
2573+
}
2574+
data := map[string]interface{}{
2575+
"connection_pooling_enabled": connectionPoolConfig.ConnectionPoolingEnabled, // Corrected key
2576+
"flags": flattenConnectionPoolFlags(connectionPoolConfig.Flags), // Corrected key
2577+
}
2578+
return []interface{}{data}
2579+
}
2580+
24882581
func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration, d *schema.ResourceData) interface{} {
24892582
data := map[string]interface{}{
24902583
"ipv4_enabled": ipConfiguration.Ipv4Enabled,

google/services/sql/resource_sql_database_instance_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,52 @@ func TestAccSqlDatabaseInstance_withPrivateNetwork_withoutAllocatedIpRange(t *te
813813
})
814814
}
815815

816+
func TestAccSqlDatabaseInstance_withMCPEnabled(t *testing.T) {
817+
t.Parallel()
818+
819+
instanceName := "tf-test-" + acctest.RandString(t, 10)
820+
821+
acctest.VcrTest(t, resource.TestCase{
822+
PreCheck: func() { acctest.AccTestPreCheck(t) },
823+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
824+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
825+
Steps: []resource.TestStep{
826+
{
827+
Config: testAccSqlDatabaseInstance_withMCPEnabled(instanceName),
828+
},
829+
{
830+
ResourceName: "google_sql_database_instance.instance",
831+
ImportState: true,
832+
ImportStateVerify: true,
833+
ImportStateVerifyIgnore: []string{"deletion_protection"},
834+
},
835+
},
836+
})
837+
}
838+
839+
func TestAccSqlDatabaseInstance_withoutMCPEnabled(t *testing.T) {
840+
t.Parallel()
841+
842+
instanceName := "tf-test-" + acctest.RandString(t, 10)
843+
844+
acctest.VcrTest(t, resource.TestCase{
845+
PreCheck: func() { acctest.AccTestPreCheck(t) },
846+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
847+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
848+
Steps: []resource.TestStep{
849+
{
850+
Config: testAccSqlDatabaseInstance_withoutMCPEnabled(instanceName),
851+
},
852+
{
853+
ResourceName: "google_sql_database_instance.instance",
854+
ImportState: true,
855+
ImportStateVerify: true,
856+
ImportStateVerifyIgnore: []string{"deletion_protection"},
857+
},
858+
},
859+
})
860+
}
861+
816862
func TestAccSqlDatabaseInstance_withPSCEnabled_withoutAllowedConsumerProjects(t *testing.T) {
817863
t.Parallel()
818864

@@ -4727,6 +4773,41 @@ func verifyPscAutoConnectionsOperation(resourceName string, isPscConfigExpected
47274773
}
47284774
}
47294775

4776+
func testAccSqlDatabaseInstance_withoutMCPEnabled(instanceName string) string {
4777+
return fmt.Sprintf(`
4778+
resource "google_sql_database_instance" "instance" {
4779+
name = "%s"
4780+
region = "us-central1"
4781+
database_version = "POSTGRES_16"
4782+
deletion_protection = false
4783+
settings {
4784+
tier = "db-perf-optimized-N-2"
4785+
}
4786+
}
4787+
`, instanceName)
4788+
}
4789+
4790+
func testAccSqlDatabaseInstance_withMCPEnabled(instanceName string) string {
4791+
return fmt.Sprintf(`
4792+
resource "google_sql_database_instance" "instance" {
4793+
name = "%s"
4794+
region = "us-central1"
4795+
database_version = "POSTGRES_16"
4796+
deletion_protection = false
4797+
settings {
4798+
tier = "db-perf-optimized-N-2"
4799+
connection_pool_config {
4800+
connection_pooling_enabled = true
4801+
flags {
4802+
name = "max_client_connections"
4803+
value = "1980"
4804+
}
4805+
}
4806+
}
4807+
}
4808+
`, instanceName)
4809+
}
4810+
47304811
func testAccSqlDatabaseInstance_withPSCEnabled_withoutPscAutoConnections(instanceName string) string {
47314812
return fmt.Sprintf(`
47324813
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)