Skip to content

Commit 7c37bd7

Browse files
Remove first-gen settings from SQL (#5364) (#10412)
Signed-off-by: Modular Magician <[email protected]>
1 parent a69be42 commit 7c37bd7

5 files changed

+90
-164
lines changed

.changelog/5364.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:breaking-change
2+
sql: removed the following `google_sql_database_instance` fields: `authorized_gae_applications`, `crash_safe_replication`, `replication_type`
3+
```
4+
```release-note:breaking-change
5+
sql: added drift detection to the following `google_sql_database_instance` fields: `activation_policy` (defaults `ALWAYS`), `availability_type` (defaults `ZONAL`), `disk_type` (defaults `PD_SSD`), `encryption_key_name`
6+
```

google/data_source_google_sql_ca_certs_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ resource "google_sql_database_instance" "foo" {
7777
database_version = "MYSQL_5_7"
7878
settings {
7979
tier = "db-f1-micro"
80-
crash_safe_replication = false
8180
}
8281
8382
deletion_protection = false

google/resource_sql_database_instance.go

+51-125
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"log"
8-
"regexp"
98
"strings"
109
"time"
1110

@@ -139,28 +138,15 @@ func resourceSqlDatabaseInstance() *schema.Resource {
139138
Description: `The machine type to use. See tiers for more details and supported versions. Postgres supports only shared-core machine types, and custom machine types such as db-custom-2-13312. See the Custom Machine Type Documentation to learn about specifying custom machine types.`,
140139
},
141140
"activation_policy": {
142-
Type: schema.TypeString,
143-
Optional: true,
144-
// Defaults differ between first and second gen instances
145-
Computed: true,
146-
Description: `This specifies when the instance should be active. Can be either ALWAYS, NEVER or ON_DEMAND.`,
147-
},
148-
"authorized_gae_applications": {
149-
Type: schema.TypeList,
141+
Type: schema.TypeString,
150142
Optional: true,
151-
Computed: true,
152-
Elem: &schema.Schema{Type: schema.TypeString},
153-
Deprecated: "This property is only applicable to First Generation instances, and First Generation instances are now deprecated.",
154-
Description: `This property is only applicable to First Generation instances. First Generation instances are now deprecated, see https://cloud.google.com/sql/docs/mysql/deprecation-notice for information on how to upgrade to Second Generation instances. A list of Google App Engine project names that are allowed to access this instance.`,
143+
Default: "ALWAYS",
144+
Description: `This specifies when the instance should be active. Can be either ALWAYS, NEVER or ON_DEMAND.`,
155145
},
156146
"availability_type": {
157-
Type: schema.TypeString,
158-
Optional: true,
159-
DiffSuppressFunc: suppressFirstGen,
160-
// Set computed instead of default because this property is for second-gen
161-
// only. The default when not provided is ZONAL, which means no explicit HA
162-
// configuration.
163-
Computed: true,
147+
Type: schema.TypeString,
148+
Optional: true,
149+
Default: "ZONAL",
164150
ValidateFunc: validation.StringInSlice([]string{"REGIONAL", "ZONAL"}, false),
165151
Description: `The availability type of the Cloud SQL instance, high availability
166152
(REGIONAL) or single zone (ZONAL). For MySQL instances, ensure that
@@ -243,13 +229,6 @@ settings.backup_configuration.binary_log_enabled are both set to true.`,
243229
Optional: true,
244230
Description: `The name of server instance collation.`,
245231
},
246-
"crash_safe_replication": {
247-
Type: schema.TypeBool,
248-
Optional: true,
249-
Computed: true,
250-
Deprecated: "This property is only applicable to First Generation instances, and First Generation instances are now deprecated.",
251-
Description: `This property is only applicable to First Generation instances. First Generation instances are now deprecated, see here for information on how to upgrade to Second Generation instances. Specific to read instances, indicates when crash-safe replication flags are enabled.`,
252-
},
253232
"database_flags": {
254233
Type: schema.TypeList,
255234
Optional: true,
@@ -269,31 +248,28 @@ settings.backup_configuration.binary_log_enabled are both set to true.`,
269248
},
270249
},
271250
"disk_autoresize": {
272-
Type: schema.TypeBool,
273-
Optional: true,
274-
Default: true,
275-
DiffSuppressFunc: suppressFirstGen,
276-
Description: `Configuration to increase storage size automatically. Note that future terraform apply calls will attempt to resize the disk to the value specified in disk_size - if this is set, do not set disk_size.`,
251+
Type: schema.TypeBool,
252+
Optional: true,
253+
Default: true,
254+
Description: `Configuration to increase storage size automatically. Note that future terraform apply calls will attempt to resize the disk to the value specified in disk_size - if this is set, do not set disk_size.`,
277255
},
278256
"disk_autoresize_limit": {
279-
Type: schema.TypeInt,
280-
Optional: true,
281-
Default: 0,
282-
DiffSuppressFunc: suppressFirstGen,
283-
Description: `The maximum size, in GB, to which storage capacity can be automatically increased. The default value is 0, which specifies that there is no limit.`,
257+
Type: schema.TypeInt,
258+
Optional: true,
259+
Default: 0,
260+
Description: `The maximum size, in GB, to which storage capacity can be automatically increased. The default value is 0, which specifies that there is no limit.`,
284261
},
285262
"disk_size": {
286263
Type: schema.TypeInt,
287264
Optional: true,
288-
// Defaults differ between first and second gen instances
265+
// Default is likely 10gb, but it is undocumented and may change.
289266
Computed: true,
290267
Description: `The size of data disk, in GB. Size of a running instance cannot be reduced but can be increased.`,
291268
},
292269
"disk_type": {
293-
Type: schema.TypeString,
294-
Optional: true,
295-
// Set computed instead of default because this property is for second-gen only.
296-
Computed: true,
270+
Type: schema.TypeString,
271+
Optional: true,
272+
Default: "PD_SSD",
297273
Description: `The type of data disk: PD_SSD or PD_HDD.`,
298274
},
299275
"ip_configuration": {
@@ -391,13 +367,6 @@ settings.backup_configuration.binary_log_enabled are both set to true.`,
391367
Default: "PER_USE",
392368
Description: `Pricing plan for this instance, can only be PER_USE.`,
393369
},
394-
"replication_type": {
395-
Type: schema.TypeString,
396-
Optional: true,
397-
Deprecated: "This property is only applicable to First Generation instances, and First Generation instances are now deprecated.",
398-
Computed: true,
399-
Description: `This property is only applicable to First Generation instances. First Generation instances are now deprecated, see here for information on how to upgrade to Second Generation instances. Replication type for this instance, can be one of ASYNCHRONOUS or SYNCHRONOUS.`,
400-
},
401370
"user_labels": {
402371
Type: schema.TypeMap,
403372
Optional: true,
@@ -715,31 +684,6 @@ settings.backup_configuration.binary_log_enabled are both set to true.`,
715684
}
716685
}
717686

718-
// Suppress diff with any attribute value that is not supported on 1st Generation
719-
// Instances
720-
func suppressFirstGen(k, old, new string, d *schema.ResourceData) bool {
721-
if isFirstGen(d) {
722-
log.Printf("[DEBUG] suppressing diff on %s due to 1st gen instance type", k)
723-
return true
724-
}
725-
726-
return false
727-
}
728-
729-
// Detects whether a database is 1st Generation by inspecting the tier name
730-
func isFirstGen(d *schema.ResourceData) bool {
731-
settingsList := d.Get("settings").([]interface{})
732-
if len(settingsList) == 0 {
733-
return false
734-
}
735-
settings := settingsList[0].(map[string]interface{})
736-
tier := settings["tier"].(string)
737-
738-
// 1st Generation databases have tiers like 'D0', as opposed to 2nd Generation which are
739-
// prefixed with 'db'
740-
return !regexp.MustCompile("db*").Match([]byte(tier))
741-
}
742-
743687
// Makes private_network ForceNew if it is changing from set to nil. The API returns an error
744688
// if this change is attempted in-place.
745689
func privateNetworkCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
@@ -825,7 +769,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
825769
cloneContext, cloneSource := expandCloneContext(d.Get("clone").([]interface{}))
826770

827771
s, ok := d.GetOk("settings")
828-
desiredSettings := expandSqlDatabaseInstanceSettings(s.([]interface{}), !isFirstGen(d))
772+
desiredSettings := expandSqlDatabaseInstanceSettings(s.([]interface{}))
829773
if ok {
830774
instance.Settings = desiredSettings
831775
}
@@ -969,42 +913,35 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
969913
return nil
970914
}
971915

972-
func expandSqlDatabaseInstanceSettings(configured []interface{}, secondGen bool) *sqladmin.Settings {
916+
func expandSqlDatabaseInstanceSettings(configured []interface{}) *sqladmin.Settings {
973917
if len(configured) == 0 || configured[0] == nil {
974918
return nil
975919
}
976920

977921
_settings := configured[0].(map[string]interface{})
978922
settings := &sqladmin.Settings{
979923
// Version is unset in Create but is set during update
980-
SettingsVersion: int64(_settings["version"].(int)),
981-
Tier: _settings["tier"].(string),
982-
ForceSendFields: []string{"StorageAutoResize"},
983-
ActivationPolicy: _settings["activation_policy"].(string),
984-
AvailabilityType: _settings["availability_type"].(string),
985-
Collation: _settings["collation"].(string),
986-
CrashSafeReplicationEnabled: _settings["crash_safe_replication"].(bool),
987-
DataDiskSizeGb: int64(_settings["disk_size"].(int)),
988-
DataDiskType: _settings["disk_type"].(string),
989-
PricingPlan: _settings["pricing_plan"].(string),
990-
ReplicationType: _settings["replication_type"].(string),
991-
UserLabels: convertStringMap(_settings["user_labels"].(map[string]interface{})),
992-
BackupConfiguration: expandBackupConfiguration(_settings["backup_configuration"].([]interface{})),
993-
DatabaseFlags: expandDatabaseFlags(_settings["database_flags"].([]interface{})),
994-
AuthorizedGaeApplications: expandAuthorizedGaeApplications(_settings["authorized_gae_applications"].([]interface{})),
995-
IpConfiguration: expandIpConfiguration(_settings["ip_configuration"].([]interface{})),
996-
LocationPreference: expandLocationPreference(_settings["location_preference"].([]interface{})),
997-
MaintenanceWindow: expandMaintenanceWindow(_settings["maintenance_window"].([]interface{})),
998-
InsightsConfig: expandInsightsConfig(_settings["insights_config"].([]interface{})),
999-
}
1000-
1001-
// 1st Generation instances don't support the disk_autoresize parameter
1002-
// and it defaults to true - so we shouldn't set it if this is first gen
1003-
if secondGen {
1004-
resize := _settings["disk_autoresize"].(bool)
1005-
settings.StorageAutoResize = &resize
1006-
settings.StorageAutoResizeLimit = int64(_settings["disk_autoresize_limit"].(int))
1007-
}
924+
SettingsVersion: int64(_settings["version"].(int)),
925+
Tier: _settings["tier"].(string),
926+
ForceSendFields: []string{"StorageAutoResize"},
927+
ActivationPolicy: _settings["activation_policy"].(string),
928+
AvailabilityType: _settings["availability_type"].(string),
929+
Collation: _settings["collation"].(string),
930+
DataDiskSizeGb: int64(_settings["disk_size"].(int)),
931+
DataDiskType: _settings["disk_type"].(string),
932+
PricingPlan: _settings["pricing_plan"].(string),
933+
UserLabels: convertStringMap(_settings["user_labels"].(map[string]interface{})),
934+
BackupConfiguration: expandBackupConfiguration(_settings["backup_configuration"].([]interface{})),
935+
DatabaseFlags: expandDatabaseFlags(_settings["database_flags"].([]interface{})),
936+
IpConfiguration: expandIpConfiguration(_settings["ip_configuration"].([]interface{})),
937+
LocationPreference: expandLocationPreference(_settings["location_preference"].([]interface{})),
938+
MaintenanceWindow: expandMaintenanceWindow(_settings["maintenance_window"].([]interface{})),
939+
InsightsConfig: expandInsightsConfig(_settings["insights_config"].([]interface{})),
940+
}
941+
942+
resize := _settings["disk_autoresize"].(bool)
943+
settings.StorageAutoResize = &resize
944+
settings.StorageAutoResizeLimit = int64(_settings["disk_autoresize_limit"].(int))
1008945

1009946
return settings
1010947
}
@@ -1102,14 +1039,6 @@ func expandAuthorizedNetworks(configured []interface{}) []*sqladmin.AclEntry {
11021039
return an
11031040
}
11041041

1105-
func expandAuthorizedGaeApplications(configured []interface{}) []string {
1106-
aga := make([]string, 0, len(configured))
1107-
for _, app := range configured {
1108-
aga = append(aga, app.(string))
1109-
}
1110-
return aga
1111-
}
1112-
11131042
func expandDatabaseFlags(configured []interface{}) []*sqladmin.DatabaseFlags {
11141043
databaseFlags := make([]*sqladmin.DatabaseFlags, 0, len(configured))
11151044
for _, _flag := range configured {
@@ -1273,7 +1202,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
12731202

12741203
// Update only updates the settings, so they are all we need to set.
12751204
instance := &sqladmin.DatabaseInstance{
1276-
Settings: expandSqlDatabaseInstanceSettings(d.Get("settings").([]interface{}), !isFirstGen(d)),
1205+
Settings: expandSqlDatabaseInstanceSettings(d.Get("settings").([]interface{})),
12771206
}
12781207

12791208
// Lock on the master_instance_name just in case updating any replica
@@ -1378,18 +1307,15 @@ func resourceSqlDatabaseInstanceImport(d *schema.ResourceData, meta interface{})
13781307

13791308
func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} {
13801309
data := map[string]interface{}{
1381-
"version": settings.SettingsVersion,
1382-
"tier": settings.Tier,
1383-
"activation_policy": settings.ActivationPolicy,
1384-
"authorized_gae_applications": settings.AuthorizedGaeApplications,
1385-
"availability_type": settings.AvailabilityType,
1386-
"collation": settings.Collation,
1387-
"crash_safe_replication": settings.CrashSafeReplicationEnabled,
1388-
"disk_type": settings.DataDiskType,
1389-
"disk_size": settings.DataDiskSizeGb,
1390-
"pricing_plan": settings.PricingPlan,
1391-
"replication_type": settings.ReplicationType,
1392-
"user_labels": settings.UserLabels,
1310+
"version": settings.SettingsVersion,
1311+
"tier": settings.Tier,
1312+
"activation_policy": settings.ActivationPolicy,
1313+
"availability_type": settings.AvailabilityType,
1314+
"collation": settings.Collation,
1315+
"disk_type": settings.DataDiskType,
1316+
"disk_size": settings.DataDiskSizeGb,
1317+
"pricing_plan": settings.PricingPlan,
1318+
"user_labels": settings.UserLabels,
13931319
}
13941320

13951321
if settings.BackupConfiguration != nil {

website/docs/guides/version_4_upgrade.html.markdown

+33
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ description: |-
7373
- [`bigquery-json.googleapis.com` is no longer a valid service name](#bigquery-jsongoogleapiscom-is-no-longer-a-valid-service-name)
7474
- [Resource: `google_spanner_instance`](#resource-google_spanner_instance)
7575
- [Exactly one of `num_nodes` or `processing_units` is required](#exactly-one-of-num_nodes-or-processing_units-is-required)
76+
- [Resource: `google_sql_database_instance`](#resource-google_sql_database_instance)
77+
- [First-generation fields have been removed](#first-generation-fields-have-been-removed)
78+
- [Drift detection and defaults enabled on fields](#drift-detection-and-defaults-enabled-on-fields)
7679
- [Resource: `google_storage_bucket`](#resource-google_storage_bucket)
7780
- [`bucket_policy_only` field is now removed](#bucket_policy_only-field-is-now-removed)
7881
- [`location` field is now required.](#location-field-is-now-required)
@@ -471,6 +474,36 @@ the provider will no longer convert the service name. Use `bigquery.googleapis.c
471474

472475
The provider will now enforce at plan time that one of these fields be set.
473476

477+
### Resource: `google_sql_database_instance`
478+
479+
### First-generation fields have been removed
480+
481+
Removed fields specific to first-generation SQL instances:
482+
`authorized_gae_applications`, `crash_safe_replication`, `replication_type`
483+
484+
### Drift detection and defaults enabled on fields
485+
486+
Added drift detection and plan-time defaults to several fields used to configure
487+
second-generation SQL instances. If you see changes flagged by Terraform after
488+
running `terraform plan`, amend your config to resolve them.
489+
490+
The affected fields are:
491+
492+
* `activation_policy` will now default to `ALWAYS` at plan time, and detect
493+
drift even when unset. Previously, Terraform only detected drift when the field
494+
had been set in config explicitly.
495+
496+
* `availability_type` will now default to `ZONAL` at plan time, and detect
497+
drift even when unset. Previously, Terraform only detected drift when the field
498+
had been set in config explicitly.
499+
500+
* `disk_type` will now default to `PD_SSD` at plan time, and detect
501+
drift even when unset. Previously, Terraform only detected drift when the field
502+
had been set in config explicitly.
503+
504+
* `encryption_key_name` will now detect drift even when unset. Previously,
505+
Terraform only detected drift when the field had been set in config explicitly.
506+
474507
## Resource: `google_storage_bucket`
475508

476509
### `bucket_policy_only` field is now removed

0 commit comments

Comments
 (0)