diff --git a/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb b/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb index 1d1b24d22ad9..968f350a0216 100644 --- a/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb +++ b/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb @@ -72,6 +72,13 @@ var ( "replica_configuration.0.username", "replica_configuration.0.verify_server_certificate", } + + insightsConfigKeys = []string{ + "settings.0.insights_config.0.query_insights_enabled", + "settings.0.insights_config.0.query_string_length", + "settings.0.insights_config.0.record_application_tags", + "settings.0.insights_config.0.record_client_address", + } ) func resourceSqlDatabaseInstance() *schema.Resource { @@ -93,7 +100,9 @@ func resourceSqlDatabaseInstance() *schema.Resource { CustomizeDiff: customdiff.All( customdiff.ForceNewIfChange("settings.0.disk_size", isDiskShrinkage), privateNetworkCustomizeDiff, - pitrPostgresOnlyCustomizeDiff), + pitrPostgresOnlyCustomizeDiff, + insightsPostgresOnlyCustomizeDiff, + ), Schema: map[string]*schema.Schema{ "region": { @@ -353,6 +362,42 @@ settings.backup_configuration.binary_log_enabled are both set to true.`, Elem: &schema.Schema{Type: schema.TypeString}, Description: `A set of key/value user label pairs to assign to the instance.`, }, + "insights_config": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "query_insights_enabled": { + Type: schema.TypeBool, + Optional: true, + AtLeastOneOf: insightsConfigKeys, + Description: `True if Query Insights feature is enabled.`, + }, + "query_string_length": { + Type: schema.TypeInt, + Optional: true, + Default: 1024, + ValidateFunc: validation.IntBetween(256, 4500), + AtLeastOneOf: insightsConfigKeys, + Description: `Maximum query length stored in bytes. Between 256 and 4500. Default to 1024.`, + }, + "record_application_tags": { + Type: schema.TypeBool, + Optional: true, + AtLeastOneOf: insightsConfigKeys, + Description: `True if Query Insights will record application tags from query when enabled.`, + }, + "record_client_address": { + Type: schema.TypeBool, + Optional: true, + AtLeastOneOf: insightsConfigKeys, + Description: `True if Query Insights will record client address when enabled.`, + }, + }, + }, + Description: `Configuration of Query Insights.`, + }, }, }, Description: `The settings to use for the database. The configuration is detailed below.`, @@ -691,6 +736,15 @@ func pitrPostgresOnlyCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, return nil } +func insightsPostgresOnlyCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error { + insights := diff.Get("settings.0.insights_config.0.query_insights_enabled").(bool) + dbVersion := diff.Get("database_version").(string) + if insights && !strings.Contains(dbVersion, "POSTGRES") { + return fmt.Errorf("query_insights_enabled is only available for Postgres now.") + } + return nil +} + func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) userAgent, err := generateUserAgentString(d, config.userAgent) @@ -890,6 +944,7 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, secondGen bool) IpConfiguration: expandIpConfiguration(_settings["ip_configuration"].([]interface{})), LocationPreference: expandLocationPreference(_settings["location_preference"].([]interface{})), MaintenanceWindow: expandMaintenanceWindow(_settings["maintenance_window"].([]interface{})), + InsightsConfig: expandInsightsConfig(_settings["insights_config"].([]interface{})), } // 1st Generation instances don't support the disk_autoresize parameter @@ -1032,6 +1087,20 @@ func expandBackupConfiguration(configured []interface{}) *sqladmin.BackupConfigu } } +func expandInsightsConfig(configured []interface{}) *sqladmin.InsightsConfig { + if len(configured) == 0 || configured[0] == nil { + return nil + } + + _insightsConfig := configured[0].(map[string]interface{}) + return &sqladmin.InsightsConfig{ + QueryInsightsEnabled: _insightsConfig["query_insights_enabled"].(bool), + QueryStringLength: int64(_insightsConfig["query_string_length"].(int)), + RecordApplicationTags: _insightsConfig["record_application_tags"].(bool), + RecordClientAddress: _insightsConfig["record_client_address"].(bool), + } +} + func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) userAgent, err := generateUserAgentString(d, config.userAgent) @@ -1283,6 +1352,10 @@ func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} { data["maintenance_window"] = flattenMaintenanceWindow(settings.MaintenanceWindow) } + if settings.InsightsConfig != nil { + data["insights_config"] = flattenInsightsConfig(settings.InsightsConfig) + } + data["disk_autoresize"] = settings.StorageAutoResize if settings.UserLabels != nil { @@ -1432,6 +1505,17 @@ func flattenServerCaCerts(caCerts []*sqladmin.SslCert) []map[string]interface{} return certs } +func flattenInsightsConfig(insightsConfig *sqladmin.InsightsConfig) interface{} { + data := map[string]interface{}{ + "query_insights_enabled": insightsConfig.QueryInsightsEnabled, + "query_string_length": insightsConfig.QueryStringLength, + "record_application_tags": insightsConfig.RecordApplicationTags, + "record_client_address": insightsConfig.RecordClientAddress, + } + + return []map[string]interface{}{data} +} + func instanceMutexKey(project, instance_name string) string { return fmt.Sprintf("google-sql-database-instance-%s-%s", project, instance_name) } diff --git a/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb b/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb index 80b404a4bcf1..414295f07650 100644 --- a/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb @@ -884,6 +884,30 @@ func TestAccSqlDatabaseInstance_PointInTimeRecoveryEnabled(t *testing.T) { }) } +func TestAccSqlDatabaseInstance_insights(t *testing.T) { + t.Parallel() + + masterID := randInt(t) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: fmt.Sprintf( + testGoogleSqlDatabaseInstance_insights, masterID), + }, + resource.TestStep{ + ResourceName: "google_sql_database_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + }, + }) +} + var testGoogleSqlDatabaseInstance_basic2 = ` resource "google_sql_database_instance" "instance" { region = "us-central1" @@ -1322,6 +1346,26 @@ resource "google_sql_database_instance" "instance" { } ` +var testGoogleSqlDatabaseInstance_insights = ` +resource "google_sql_database_instance" "instance" { + name = "tf-test-%d" + region = "us-central1" + database_version = "POSTGRES_9_6" + deletion_protection = false + + settings { + tier = "db-f1-micro" + + insights_config { + query_insights_enabled = true + query_string_length = 256 + record_application_tags = true + record_client_address = true + } + } +} +` + func testGoogleSqlDatabaseInstance_PointInTimeRecoveryEnabled(masterID int, pointInTimeRecoveryEnabled bool) string { return fmt.Sprintf(` resource "google_sql_database_instance" "instance" { @@ -1453,4 +1497,4 @@ data "google_sql_backup_run" "backup" { most_recent = true } `, context) -} \ No newline at end of file +} diff --git a/mmv1/third_party/terraform/website/docs/d/sql_database_instance.html.markdown b/mmv1/third_party/terraform/website/docs/d/sql_database_instance.html.markdown index 6efb9a317a11..76d7065aae2a 100644 --- a/mmv1/third_party/terraform/website/docs/d/sql_database_instance.html.markdown +++ b/mmv1/third_party/terraform/website/docs/d/sql_database_instance.html.markdown @@ -126,6 +126,16 @@ when an Instance can automatically restart to apply updates. The maintenance win * `update_track` - Receive updates earlier (`canary`) or later (`stable`). +The `settings.insights_config` subblock for instances declares [Query Insights](https://cloud.google.com/sql/docs/postgres/insights-overview) configuration. It contains: + +* `query_insights_enabled` - True if Query Insights feature is enabled. + +* `query_string_length` - Maximum query length stored in bytes. Between 256 and 4500. Default to 1024. + +* `record_application_tags` - True if Query Insights will record application tags from query when enabled. + +* `record_client_address` - True if Query Insights will record client address when enabled. + The `replica_configuration` block contains: * `ca_certificate` - PEM representation of the trusted CA's x509 certificate.