Skip to content

Commit 8d7edf5

Browse files
authored
boolean fields in observability_Config were being incorrectly omitted from the API request when set to false (#15757)
1 parent 5f1a13f commit 8d7edf5

File tree

3 files changed

+267
-4
lines changed

3 files changed

+267
-4
lines changed

mmv1/products/alloydb/Instance.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,38 +266,55 @@ properties:
266266
description: 'Configuration for enhanced query insights.'
267267
min_version: 'beta'
268268
default_from_api: true
269+
custom_expand: 'templates/terraform/custom_expand/alloydb_instance_observability_config.go.tmpl'
269270
properties:
270271
- name: 'enabled'
271272
type: Boolean
272273
description: 'Observability feature status for an instance.'
273274
include_empty_value_in_cai: true # Default value is false in CAI asset
275+
send_empty_value: true
276+
default_from_api: true
274277
- name: 'preserveComments'
275278
type: Boolean
276279
description: 'Preserve comments in the query string.'
277280
include_empty_value_in_cai: true # Default value is false in CAI asset
281+
send_empty_value: true
282+
default_from_api: true
278283
- name: 'trackWaitEvents'
279284
type: Boolean
280285
description: 'Record wait events during query execution for an instance.'
286+
send_empty_value: true
287+
default_from_api: true
281288
- name: 'trackWaitEventTypes'
282289
type: Boolean
283290
description: 'Record wait event types during query execution for an instance.'
291+
send_empty_value: true
292+
default_from_api: true
284293
- name: 'maxQueryStringLength'
285294
type: Integer
286295
description: 'Query string length. The default value is 10240. Any integer between 1024 and 100000 is considered valid.'
296+
default_from_api: true
287297
- name: 'recordApplicationTags'
288298
type: Boolean
289299
description: 'Record application tags for an instance. This flag is turned "on" by default.'
290300
include_empty_value_in_cai: true # Default value is false in CAI asset
301+
send_empty_value: true
302+
default_from_api: true
291303
- name: 'queryPlansPerMinute'
292304
type: Integer
293305
description: 'Number of query execution plans captured by Insights per minute for all queries combined. The default value is 5. Any integer between 0 and 200 is considered valid.'
306+
default_from_api: true
294307
- name: 'trackActiveQueries'
295308
type: Boolean
296309
description: 'Track actively running queries. If not set, default value is "off".'
297310
include_empty_value_in_cai: true # Default value is false in CAI asset
311+
send_empty_value: true
312+
default_from_api: true
298313
- name: 'assistiveExperiencesEnabled'
299314
type: Boolean
300315
description: 'Whether assistive experiences are enabled for this AlloyDB instance.'
316+
send_empty_value: true
317+
default_from_api: true
301318
- name: 'readPoolConfig'
302319
type: NestedObject
303320
description: 'Read pool specific config. If the instance type is READ_POOL, this configuration must be provided.'
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
func expandAlloydbInstanceObservabilityConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
2+
if v == nil {
3+
return nil, nil
4+
}
5+
l := v.([]interface{})
6+
if len(l) == 0 || l[0] == nil {
7+
return nil, nil
8+
}
9+
raw := l[0]
10+
original := raw.(map[string]interface{})
11+
transformed := make(map[string]interface{})
12+
13+
transformedEnabled, err := expandAlloydbInstanceObservabilityConfigEnabled(original["enabled"], d, config)
14+
if err != nil {
15+
return nil, err
16+
} else if transformedEnabled != nil {
17+
transformed["enabled"] = transformedEnabled
18+
}
19+
20+
transformedPreserveComments, err := expandAlloydbInstanceObservabilityConfigPreserveComments(original["preserve_comments"], d, config)
21+
if err != nil {
22+
return nil, err
23+
} else if transformedPreserveComments != nil {
24+
transformed["preserveComments"] = transformedPreserveComments
25+
}
26+
27+
transformedTrackWaitEvents, err := expandAlloydbInstanceObservabilityConfigTrackWaitEvents(original["track_wait_events"], d, config)
28+
if err != nil {
29+
return nil, err
30+
} else if transformedTrackWaitEvents != nil {
31+
transformed["trackWaitEvents"] = transformedTrackWaitEvents
32+
}
33+
34+
transformedMaxQueryStringLength, err := expandAlloydbInstanceObservabilityConfigMaxQueryStringLength(original["max_query_string_length"], d, config)
35+
if err != nil {
36+
return nil, err
37+
} else if val := reflect.ValueOf(transformedMaxQueryStringLength); val.IsValid() && !tpgresource.IsEmptyValue(val) {
38+
transformed["maxQueryStringLength"] = transformedMaxQueryStringLength
39+
}
40+
41+
transformedRecordApplicationTags, err := expandAlloydbInstanceObservabilityConfigRecordApplicationTags(original["record_application_tags"], d, config)
42+
if err != nil {
43+
return nil, err
44+
} else if transformedRecordApplicationTags != nil {
45+
transformed["recordApplicationTags"] = transformedRecordApplicationTags
46+
}
47+
48+
transformedQueryPlansPerMinute, err := expandAlloydbInstanceObservabilityConfigQueryPlansPerMinute(original["query_plans_per_minute"], d, config)
49+
if err != nil {
50+
return nil, err
51+
} else if val := reflect.ValueOf(transformedQueryPlansPerMinute); val.IsValid() && !tpgresource.IsEmptyValue(val) {
52+
transformed["queryPlansPerMinute"] = transformedQueryPlansPerMinute
53+
}
54+
55+
transformedTrackActiveQueries, err := expandAlloydbInstanceObservabilityConfigTrackActiveQueries(original["track_active_queries"], d, config)
56+
if err != nil {
57+
return nil, err
58+
} else if transformedTrackActiveQueries != nil {
59+
transformed["trackActiveQueries"] = transformedTrackActiveQueries
60+
}
61+
62+
transformedAssistiveExperiencesEnabled, err := expandAlloydbInstanceObservabilityConfigAssistiveExperiencesEnabled(original["assistive_experiences_enabled"], d, config)
63+
if err != nil {
64+
return nil, err
65+
} else if transformedAssistiveExperiencesEnabled != nil {
66+
transformed["assistiveExperiencesEnabled"] = transformedAssistiveExperiencesEnabled
67+
}
68+
log.Printf("vkanishk: expandAlloydbInstanceObservabilityConfig transformed %v", transformed)
69+
return transformed, nil
70+
}
71+
72+
73+
func expandAlloydbInstanceObservabilityConfigEnabled(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
74+
return v, nil
75+
}
76+
77+
func expandAlloydbInstanceObservabilityConfigPreserveComments(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
78+
return v, nil
79+
}
80+
81+
func expandAlloydbInstanceObservabilityConfigTrackWaitEvents(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
82+
return v, nil
83+
}
84+
85+
func expandAlloydbInstanceObservabilityConfigTrackWaitEventTypes(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
86+
return v, nil
87+
}
88+
89+
func expandAlloydbInstanceObservabilityConfigMaxQueryStringLength(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
90+
return v, nil
91+
}
92+
93+
func expandAlloydbInstanceObservabilityConfigRecordApplicationTags(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
94+
return v, nil
95+
}
96+
97+
func expandAlloydbInstanceObservabilityConfigQueryPlansPerMinute(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
98+
return v, nil
99+
}
100+
101+
func expandAlloydbInstanceObservabilityConfigTrackActiveQueries(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
102+
return v, nil
103+
}
104+
105+
func expandAlloydbInstanceObservabilityConfigAssistiveExperiencesEnabled(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
106+
return v, nil
107+
}

mmv1/third_party/terraform/services/alloydb/resource_alloydb_instance_test.go

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ resource "google_alloydb_instance" "default" {
687687
688688
client_connection_config {
689689
require_connectors = %{require_connectors}
690-
}
690+
}
691691
}
692692
693693
resource "google_alloydb_cluster" "default" {
@@ -724,7 +724,7 @@ resource "google_alloydb_instance" "default" {
724724
ssl_config {
725725
ssl_mode = "%{ssl_mode}"
726726
}
727-
}
727+
}
728728
}
729729
730730
resource "google_alloydb_cluster" "default" {
@@ -858,7 +858,7 @@ resource "google_alloydb_instance" "default" {
858858
enable_public_ip = %{enable_public_ip}
859859
enable_outbound_public_ip = %{enable_outbound_public_ip}
860860
%{authorized_external_networks}
861-
}
861+
}
862862
}
863863
864864
resource "google_alloydb_cluster" "default" {
@@ -898,7 +898,7 @@ resource "google_alloydb_instance" "default" {
898898
authorized_external_networks {
899899
cidr_range = "%{cidr_range}"
900900
}
901-
}
901+
}
902902
}
903903
904904
resource "google_alloydb_cluster" "default" {
@@ -1568,3 +1568,142 @@ data "google_compute_network" "default" {
15681568
}
15691569
`, context)
15701570
}
1571+
1572+
func TestAccAlloydbInstance_ObservabilityConfig_Update(t *testing.T) {
1573+
t.Parallel()
1574+
random_suffix := acctest.RandString(t, 10)
1575+
networkName := acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-1")
1576+
1577+
// 1. Initial State: Everything Enabled
1578+
contextEnableAll := map[string]interface{}{
1579+
"random_suffix": random_suffix,
1580+
"network_name": networkName,
1581+
"enabled": true,
1582+
"preserve_comments": true,
1583+
"track_wait_events": true,
1584+
"max_query_string_length": 1024,
1585+
"record_application_tags": true,
1586+
"query_plans_per_minute": 10,
1587+
"track_active_queries": true,
1588+
"assistive_experiences_enabled": false,
1589+
}
1590+
1591+
contextDisable := map[string]interface{}{
1592+
"random_suffix": random_suffix,
1593+
"network_name": networkName,
1594+
}
1595+
1596+
// 3. Re-Enable Main Toggle, but Disable Sub-features (Test Case 2)
1597+
contextEnabledButSubFeaturesDisabled := map[string]interface{}{
1598+
"random_suffix": random_suffix,
1599+
"network_name": networkName,
1600+
"enabled": true,
1601+
"preserve_comments": false,
1602+
"track_wait_events": false,
1603+
"max_query_string_length": 2048,
1604+
"record_application_tags": false,
1605+
"query_plans_per_minute": 5,
1606+
"track_active_queries": false,
1607+
"assistive_experiences_enabled": false,
1608+
}
1609+
1610+
acctest.VcrTest(t, resource.TestCase{
1611+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1612+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1613+
CheckDestroy: testAccCheckAlloydbInstanceDestroyProducer(t),
1614+
Steps: []resource.TestStep{
1615+
{
1616+
Config: testAccAlloydbInstance_ObservabilityConfig(contextEnableAll),
1617+
Check: resource.ComposeTestCheckFunc(
1618+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.enabled", "true"),
1619+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.max_query_string_length", "1024"),
1620+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.track_wait_events", "true"),
1621+
),
1622+
},
1623+
{
1624+
Config: testAccAlloydbInstance_ObservabilityConfig_Disabled(contextDisable),
1625+
Check: resource.ComposeTestCheckFunc(
1626+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.enabled", "false"),
1627+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.max_query_string_length", "10240"), // Disabled default value
1628+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.query_plans_per_minute", "20"), // default value
1629+
),
1630+
},
1631+
// Step 3: Mark enabled = true and turn all the other booleans to false
1632+
{
1633+
Config: testAccAlloydbInstance_ObservabilityConfig(contextEnabledButSubFeaturesDisabled),
1634+
Check: resource.ComposeTestCheckFunc(
1635+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.enabled", "true"),
1636+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.preserve_comments", "false"),
1637+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.track_wait_events", "false"),
1638+
resource.TestCheckResourceAttr("google_alloydb_instance.default", "observability_config.0.max_query_string_length", "2048"),
1639+
),
1640+
},
1641+
},
1642+
})
1643+
}
1644+
1645+
func testAccAlloydbInstance_ObservabilityConfig_Disabled(context map[string]interface{}) string {
1646+
return acctest.Nprintf(`
1647+
resource "google_alloydb_instance" "default" {
1648+
cluster = google_alloydb_cluster.default.name
1649+
instance_id = "tf-test-alloydb-instance%{random_suffix}"
1650+
instance_type = "PRIMARY"
1651+
machine_config {
1652+
cpu_count = 2
1653+
}
1654+
observability_config {
1655+
enabled = false
1656+
}
1657+
}
1658+
resource "google_alloydb_cluster" "default" {
1659+
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
1660+
location = "us-central1"
1661+
network_config {
1662+
network = data.google_compute_network.default.id
1663+
}
1664+
initial_user {
1665+
password = "tf-test-alloydb-cluster%{random_suffix}"
1666+
}
1667+
deletion_protection = false
1668+
}
1669+
data "google_compute_network" "default" {
1670+
name = "%{network_name}"
1671+
}
1672+
`, context)
1673+
}
1674+
func testAccAlloydbInstance_ObservabilityConfig(context map[string]interface{}) string {
1675+
return acctest.Nprintf(`
1676+
resource "google_alloydb_instance" "default" {
1677+
cluster = google_alloydb_cluster.default.name
1678+
instance_id = "tf-test-alloydb-instance%{random_suffix}"
1679+
instance_type = "PRIMARY"
1680+
machine_config {
1681+
cpu_count = 2
1682+
}
1683+
observability_config {
1684+
enabled = %{enabled}
1685+
preserve_comments = %{preserve_comments}
1686+
track_wait_events = %{track_wait_events}
1687+
max_query_string_length = %{max_query_string_length}
1688+
record_application_tags = %{record_application_tags}
1689+
query_plans_per_minute = %{query_plans_per_minute}
1690+
track_active_queries = %{track_active_queries}
1691+
assistive_experiences_enabled = %{assistive_experiences_enabled}
1692+
}
1693+
}
1694+
resource "google_alloydb_cluster" "default" {
1695+
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
1696+
location = "us-central1"
1697+
network_config {
1698+
network = data.google_compute_network.default.id
1699+
}
1700+
initial_user {
1701+
password = "tf-test-alloydb-cluster%{random_suffix}"
1702+
}
1703+
deletion_protection = false
1704+
}
1705+
data "google_compute_network" "default" {
1706+
name = "%{network_name}"
1707+
}
1708+
`, context)
1709+
}

0 commit comments

Comments
 (0)