Skip to content

Commit bbbc925

Browse files
modular-magicianScottSuarez
authored andcommitted
fix: do not store the ssl_mode field locally if it has never been used. (#9428) (#16486)
* fix: do not store the ssl_mode field locally if it has never been used. This is to make sure the irrelevant workflows doesn't need to set this ssl_mode field. See [PR](GoogleCloudPlatform/magic-modules#9396) for details. * fix: fix a lint error in resource_sql_database_instance_test.go [upstream:f729df4775ff38c7560141faf4fcdd488216556f] Signed-off-by: Modular Magician <[email protected]>
1 parent 44ecb78 commit bbbc925

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

.changelog/9428.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
Do not store the ssl_mode field locally if it has never been used.
3+
```

google/services/sql/data_source_sql_database_instances.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func dataSourceSqlDatabaseInstancesRead(d *schema.ResourceData, meta interface{}
114114
return err
115115
}
116116

117-
pageInstances := flattenDatasourceGoogleDatabaseInstancesList(instances.Items, project)
117+
pageInstances := flattenDatasourceGoogleDatabaseInstancesList(instances.Items, project, d)
118118
databaseInstances = append(databaseInstances, pageInstances...)
119119

120120
pageToken = instances.NextPageToken
@@ -132,7 +132,7 @@ func dataSourceSqlDatabaseInstancesRead(d *schema.ResourceData, meta interface{}
132132
return nil
133133
}
134134

135-
func flattenDatasourceGoogleDatabaseInstancesList(fetchedInstances []*sqladmin.DatabaseInstance, project string) []map[string]interface{} {
135+
func flattenDatasourceGoogleDatabaseInstancesList(fetchedInstances []*sqladmin.DatabaseInstance, project string, d *schema.ResourceData) []map[string]interface{} {
136136
if fetchedInstances == nil {
137137
return make([]map[string]interface{}, 0)
138138
}
@@ -148,7 +148,7 @@ func flattenDatasourceGoogleDatabaseInstancesList(fetchedInstances []*sqladmin.D
148148
instance["available_maintenance_versions"] = rawInstance.AvailableMaintenanceVersions
149149
instance["instance_type"] = rawInstance.InstanceType
150150
instance["service_account_email_address"] = rawInstance.ServiceAccountEmailAddress
151-
instance["settings"] = flattenSettings(rawInstance.Settings)
151+
instance["settings"] = flattenSettings(rawInstance.Settings, d)
152152

153153
if rawInstance.DiskEncryptionConfiguration != nil {
154154
instance["encryption_key_name"] = rawInstance.DiskEncryptionConfiguration.KmsKeyName

google/services/sql/resource_sql_database_instance.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ is set to true. Defaults to ZONAL.`,
437437
Type: schema.TypeBool,
438438
Optional: true,
439439
AtLeastOneOf: ipConfigurationKeys,
440-
Description: `Whether SSL connections over IP are enforced or not. To change this field, also set the corresponding value in ssl_mode.`,
440+
Description: `Whether SSL connections over IP are enforced or not. To change this field, also set the corresponding value in ssl_mode if it has been set too.`,
441441
},
442442
"private_network": {
443443
Type: schema.TypeString,
@@ -1616,7 +1616,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
16161616
if err := d.Set("instance_type", instance.InstanceType); err != nil {
16171617
return fmt.Errorf("Error setting instance_type: %s", err)
16181618
}
1619-
if err := d.Set("settings", flattenSettings(instance.Settings)); err != nil {
1619+
if err := d.Set("settings", flattenSettings(instance.Settings, d)); err != nil {
16201620
log.Printf("[WARN] Failed to set SQL Database Instance Settings")
16211621
}
16221622

@@ -2012,7 +2012,7 @@ func resourceSqlDatabaseInstanceImport(d *schema.ResourceData, meta interface{})
20122012
return []*schema.ResourceData{d}, nil
20132013
}
20142014

2015-
func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} {
2015+
func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[string]interface{} {
20162016
data := map[string]interface{}{
20172017
"version": settings.SettingsVersion,
20182018
"tier": settings.Tier,
@@ -2051,7 +2051,7 @@ func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} {
20512051
}
20522052

20532053
if settings.IpConfiguration != nil {
2054-
data["ip_configuration"] = flattenIpConfiguration(settings.IpConfiguration)
2054+
data["ip_configuration"] = flattenIpConfiguration(settings.IpConfiguration, d)
20552055
}
20562056

20572057
if settings.LocationPreference != nil {
@@ -2191,14 +2191,13 @@ func flattenDatabaseFlags(databaseFlags []*sqladmin.DatabaseFlags) []map[string]
21912191
return flags
21922192
}
21932193

2194-
func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration) interface{} {
2194+
func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration, d *schema.ResourceData) interface{} {
21952195
data := map[string]interface{}{
21962196
"ipv4_enabled": ipConfiguration.Ipv4Enabled,
21972197
"private_network": ipConfiguration.PrivateNetwork,
21982198
"allocated_ip_range": ipConfiguration.AllocatedIpRange,
21992199
"require_ssl": ipConfiguration.RequireSsl,
22002200
"enable_private_path_for_google_cloud_services": ipConfiguration.EnablePrivatePathForGoogleCloudServices,
2201-
"ssl_mode": ipConfiguration.SslMode,
22022201
}
22032202

22042203
if ipConfiguration.AuthorizedNetworks != nil {
@@ -2209,6 +2208,11 @@ func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration) interface
22092208
data["psc_config"] = flattenPscConfigs(ipConfiguration.PscConfig)
22102209
}
22112210

2211+
// We store the ssl_mode value only if the customer already uses `ssl_mode`.
2212+
if _, ok := d.GetOk("settings.0.ip_configuration.0.ssl_mode"); ok {
2213+
data["ssl_mode"] = ipConfiguration.SslMode
2214+
}
2215+
22122216
return []map[string]interface{}{data}
22132217
}
22142218

google/services/sql/resource_sql_database_instance_test.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -2159,42 +2159,60 @@ func TestAccSqlDatabaseInstance_updateSslOptionsForPostgreSQL(t *testing.T) {
21592159
PreCheck: func() { acctest.AccTestPreCheck(t) },
21602160
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
21612161
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
2162+
2163+
// We don't do ImportStateVerify for the ssl_mode because of the implementation. The ssl_mode is expected to be discarded if the local state doesn't have it.
21622164
Steps: []resource.TestStep{
21632165
{
21642166
Config: testGoogleSqlDatabaseInstance_setSslOptionsForPostgreSQL(databaseName, databaseVersion, false, "ALLOW_UNENCRYPTED_AND_ENCRYPTED"),
2167+
Check: resource.ComposeTestCheckFunc(
2168+
resource.TestCheckResourceAttr(resourceName, "settings.0.ip_configuration.0.require_ssl", "false"),
2169+
resource.TestCheckResourceAttr(resourceName, "settings.0.ip_configuration.0.ssl_mode", "ALLOW_UNENCRYPTED_AND_ENCRYPTED"),
2170+
),
21652171
},
21662172
{
21672173
ResourceName: resourceName,
21682174
ImportState: true,
21692175
ImportStateVerify: true,
2170-
ImportStateVerifyIgnore: []string{"deletion_protection"},
2176+
ImportStateVerifyIgnore: []string{"deletion_protection", "settings.0.ip_configuration.0.ssl_mode"},
21712177
},
21722178
{
21732179
Config: testGoogleSqlDatabaseInstance_setSslOptionsForPostgreSQL(databaseName, databaseVersion, false, "ENCRYPTED_ONLY"),
2180+
Check: resource.ComposeTestCheckFunc(
2181+
resource.TestCheckResourceAttr(resourceName, "settings.0.ip_configuration.0.require_ssl", "false"),
2182+
resource.TestCheckResourceAttr(resourceName, "settings.0.ip_configuration.0.ssl_mode", "ENCRYPTED_ONLY"),
2183+
),
21742184
},
21752185
{
21762186
ResourceName: resourceName,
21772187
ImportState: true,
21782188
ImportStateVerify: true,
2179-
ImportStateVerifyIgnore: []string{"deletion_protection"},
2189+
ImportStateVerifyIgnore: []string{"deletion_protection", "settings.0.ip_configuration.0.ssl_mode"},
21802190
},
21812191
{
21822192
Config: testGoogleSqlDatabaseInstance_setSslOptionsForPostgreSQL(databaseName, databaseVersion, true, "TRUSTED_CLIENT_CERTIFICATE_REQUIRED"),
2193+
Check: resource.ComposeTestCheckFunc(
2194+
resource.TestCheckResourceAttr(resourceName, "settings.0.ip_configuration.0.require_ssl", "true"),
2195+
resource.TestCheckResourceAttr(resourceName, "settings.0.ip_configuration.0.ssl_mode", "TRUSTED_CLIENT_CERTIFICATE_REQUIRED"),
2196+
),
21832197
},
21842198
{
21852199
ResourceName: resourceName,
21862200
ImportState: true,
21872201
ImportStateVerify: true,
2188-
ImportStateVerifyIgnore: []string{"deletion_protection"},
2202+
ImportStateVerifyIgnore: []string{"deletion_protection", "settings.0.ip_configuration.0.ssl_mode"},
21892203
},
21902204
{
21912205
Config: testGoogleSqlDatabaseInstance_setSslOptionsForPostgreSQL(databaseName, databaseVersion, false, "ALLOW_UNENCRYPTED_AND_ENCRYPTED"),
2206+
Check: resource.ComposeTestCheckFunc(
2207+
resource.TestCheckResourceAttr(resourceName, "settings.0.ip_configuration.0.require_ssl", "false"),
2208+
resource.TestCheckResourceAttr(resourceName, "settings.0.ip_configuration.0.ssl_mode", "ALLOW_UNENCRYPTED_AND_ENCRYPTED"),
2209+
),
21922210
},
21932211
{
21942212
ResourceName: resourceName,
21952213
ImportState: true,
21962214
ImportStateVerify: true,
2197-
ImportStateVerifyIgnore: []string{"deletion_protection"},
2215+
ImportStateVerifyIgnore: []string{"deletion_protection", "settings.0.ip_configuration.0.ssl_mode"},
21982216
},
21992217
},
22002218
})

0 commit comments

Comments
 (0)