Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Query Insights #4464

Merged
merged 3 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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": {
Expand Down Expand Up @@ -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.`,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -1448,4 +1492,4 @@ data "google_sql_backup_run" "backup" {
most_recent = true
}
`, context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down