Skip to content

Commit 4e061da

Browse files
Support Cloud SQL read pool (#14088) (#23897)
[upstream:4f9e22f16eac321b3db15eebb7e270dac4e3d4e5] Signed-off-by: Modular Magician <[email protected]>
1 parent f1b43e3 commit 4e061da

File tree

5 files changed

+320
-9
lines changed

5 files changed

+320
-9
lines changed

.changelog/14088.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 `node_count` field to `sql_database_instance` resource, and added new value `READ_POOL_INSTANCE` to the `instance_type` field of `sql_database_instance` resource
3+
```

google/services/sql/data_source_sql_database_instances.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func flattenDatasourceGoogleDatabaseInstancesList(fetchedInstances []*sqladmin.D
162162
instance["available_maintenance_versions"] = rawInstance.AvailableMaintenanceVersions
163163
instance["instance_type"] = rawInstance.InstanceType
164164
instance["service_account_email_address"] = rawInstance.ServiceAccountEmailAddress
165-
instance["settings"] = flattenSettings(rawInstance.Settings, d)
165+
instance["settings"] = flattenSettings(rawInstance.Settings, rawInstance.InstanceType, d)
166166

167167
if rawInstance.DiskEncryptionConfiguration != nil {
168168
instance["encryption_key_name"] = rawInstance.DiskEncryptionConfiguration.KmsKeyName

google/services/sql/resource_sql_database_instance.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,16 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
330330
settings.backup_configuration.enabled is set to true.
331331
For MySQL instances, ensure that settings.backup_configuration.binary_log_enabled is set to true.
332332
For Postgres instances, ensure that settings.backup_configuration.point_in_time_recovery_enabled
333-
is set to true. Defaults to ZONAL.`,
333+
is set to true. Defaults to ZONAL.
334+
For read pool instances, this field is read-only. The availability type is changed by specifying
335+
the number of nodes (node_count).`,
336+
},
337+
"effective_availability_type": {
338+
Type: schema.TypeString,
339+
Computed: true,
340+
Description: `The availability type of the Cloud SQL instance, high availability
341+
(REGIONAL) or single zone (ZONAL). This field always contains the value that is reported by the
342+
API (for read pools, effective_availability_type may differ from availability_type).`,
334343
},
335344
"backup_configuration": {
336345
Type: schema.TypeList,
@@ -881,7 +890,14 @@ is set to true. Defaults to ZONAL.`,
881890
Type: schema.TypeString,
882891
Computed: true,
883892
Optional: true,
884-
Description: `The type of the instance. The valid values are:- 'SQL_INSTANCE_TYPE_UNSPECIFIED', 'CLOUD_SQL_INSTANCE', 'ON_PREMISES_INSTANCE' and 'READ_REPLICA_INSTANCE'.`,
893+
Description: `The type of the instance. See https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1/instances#SqlInstanceType for supported values.`,
894+
},
895+
896+
"node_count": {
897+
Type: schema.TypeInt,
898+
Computed: true,
899+
Optional: true,
900+
Description: `For a read pool instance, the number of nodes in the read pool.`,
885901
},
886902

887903
"replica_configuration": {
@@ -1264,6 +1280,10 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
12641280
instance.InstanceType = d.Get("instance_type").(string)
12651281
}
12661282

1283+
if _, ok := d.GetOk("node_count"); ok {
1284+
instance.NodeCount = int64(d.Get("node_count").(int))
1285+
}
1286+
12671287
instance.RootPassword = d.Get("root_password").(string)
12681288

12691289
// Modifying a replica during Create can cause problems if the master is
@@ -1848,10 +1868,12 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
18481868
if err := d.Set("instance_type", instance.InstanceType); err != nil {
18491869
return fmt.Errorf("Error setting instance_type: %s", err)
18501870
}
1851-
if err := d.Set("settings", flattenSettings(instance.Settings, d)); err != nil {
1871+
if err := d.Set("node_count", instance.NodeCount); err != nil {
1872+
return fmt.Errorf("Error setting node_count: %s", err)
1873+
}
1874+
if err := d.Set("settings", flattenSettings(instance.Settings, instance.InstanceType, d)); err != nil {
18521875
log.Printf("[WARN] Failed to set SQL Database Instance Settings")
18531876
}
1854-
18551877
if instance.DiskEncryptionConfiguration != nil {
18561878
if err := d.Set("encryption_key_name", instance.DiskEncryptionConfiguration.KmsKeyName); err != nil {
18571879
return fmt.Errorf("Error setting encryption_key_name: %s", err)
@@ -2199,6 +2221,10 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
21992221
instance.InstanceType = d.Get("instance_type").(string)
22002222
}
22012223

2224+
if _, ok := d.GetOk("node_count"); ok {
2225+
instance.NodeCount = int64(d.Get("node_count").(int))
2226+
}
2227+
22022228
// Database Version is required for all calls with Google ML integration enabled or it will be rejected by the API.
22032229
if d.Get("settings.0.enable_google_ml_integration").(bool) || len(_settings["connection_pool_config"].(*schema.Set).List()) > 0 {
22042230
instance.DatabaseVersion = databaseVersion
@@ -2358,13 +2384,14 @@ func resourceSqlDatabaseInstanceImport(d *schema.ResourceData, meta interface{})
23582384
return []*schema.ResourceData{d}, nil
23592385
}
23602386

2361-
func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[string]interface{} {
2387+
func flattenSettings(settings *sqladmin.Settings, iType string, d *schema.ResourceData) []map[string]interface{} {
23622388
data := map[string]interface{}{
23632389
"version": settings.SettingsVersion,
23642390
"tier": settings.Tier,
23652391
"edition": flattenEdition(settings.Edition),
23662392
"activation_policy": settings.ActivationPolicy,
2367-
"availability_type": settings.AvailabilityType,
2393+
"availability_type": d.Get("settings.0.availability_type"),
2394+
"effective_availability_type": settings.AvailabilityType,
23682395
"collation": settings.Collation,
23692396
"connector_enforcement": settings.ConnectorEnforcement,
23702397
"disk_type": settings.DataDiskType,
@@ -2377,6 +2404,18 @@ func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[
23772404
"retain_backups_on_delete": settings.RetainBackupsOnDelete,
23782405
}
23792406

2407+
if data["availability_type"] == "" {
2408+
data["availability_type"] = "ZONAL"
2409+
}
2410+
// For read pools, availability type is server managed. Above, we
2411+
// pull it from the old TF resource so that it never shows a
2412+
// diff. Now, here, for non-pool instances, we overwrite it with the
2413+
// value obtained from the API (which would be the typical way to
2414+
// populate the field).
2415+
if iType != "READ_POOL_INSTANCE" {
2416+
data["availability_type"] = settings.AvailabilityType
2417+
}
2418+
23802419
if settings.ActiveDirectoryConfig != nil {
23812420
data["active_directory_config"] = flattenActiveDirectoryConfig(settings.ActiveDirectoryConfig)
23822421
}

0 commit comments

Comments
 (0)