Skip to content

Commit 6be657a

Browse files
Added support persistence_config field to google_redis_instance resource. (#6528) (#12569)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 3b0ae51 commit 6be657a

File tree

4 files changed

+282
-14
lines changed

4 files changed

+282
-14
lines changed

.changelog/6528.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```release-note: enhancement
2+
redis: Added `persistence_config` field to the `google_redis_instance` resource.
3+
4+
```

google/resource_redis_instance.go

+168-7
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,67 @@ resolution and up to nine fractional digits.`,
298298
},
299299
},
300300
},
301+
"persistence_config": {
302+
Type: schema.TypeList,
303+
Computed: true,
304+
Optional: true,
305+
Description: `Maintenance policy for an instance.`,
306+
MaxItems: 1,
307+
Elem: &schema.Resource{
308+
Schema: map[string]*schema.Schema{
309+
"persistence_mode": {
310+
Type: schema.TypeString,
311+
Computed: true,
312+
Optional: true,
313+
ValidateFunc: validateEnum([]string{"DISABLED", "RDB"}),
314+
Description: `Optional. Controls whether Persistence features are enabled. If not provided, the existing value will be used.
315+
316+
- DISABLED: Persistence is disabled for the instance, and any existing snapshots are deleted.
317+
- RDB: RDB based Persistence is enabled. Possible values: ["DISABLED", "RDB"]`,
318+
},
319+
"rdb_snapshot_period": {
320+
Type: schema.TypeString,
321+
Required: true,
322+
ValidateFunc: validateEnum([]string{"ONE_HOUR", "SIX_HOURS", "TWELVE_HOURS", "TWENTY_FOUR_HOURS"}),
323+
Description: `Optional. Available snapshot periods for scheduling.
324+
325+
- ONE_HOUR: Snapshot every 1 hour.
326+
- SIX_HOURS: Snapshot every 6 hours.
327+
- TWELVE_HOURS: Snapshot every 12 hours.
328+
- TWENTY_FOUR_HOURS: Snapshot every 24 horus. Possible values: ["ONE_HOUR", "SIX_HOURS", "TWELVE_HOURS", "TWENTY_FOUR_HOURS"]`,
329+
},
330+
"rdb_snapshot_start_time": {
331+
Type: schema.TypeString,
332+
Computed: true,
333+
Optional: true,
334+
Description: `Optional. Date and time that the first snapshot was/will be attempted,
335+
and to which future snapshots will be aligned. If not provided,
336+
the current time will be used.
337+
A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution
338+
and up to nine fractional digits.
339+
Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".`,
340+
},
341+
"rdb_next_snapshot_time": {
342+
Type: schema.TypeString,
343+
Computed: true,
344+
Description: `Output only. The next time that a snapshot attempt is scheduled to occur.
345+
A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up
346+
to nine fractional digits.
347+
Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".`,
348+
},
349+
},
350+
},
351+
},
301352
"read_replicas_mode": {
302353
Type: schema.TypeString,
303354
Computed: true,
304355
Optional: true,
305356
ValidateFunc: validateEnum([]string{"READ_REPLICAS_DISABLED", "READ_REPLICAS_ENABLED", ""}),
306357
Description: `Optional. Read replica mode. Can only be specified when trying to create the instance.
307358
If not set, Memorystore Redis backend will default to READ_REPLICAS_DISABLED.
308-
- READ_REPLICAS_DISABLED: If disabled, read endpoint will not be provided and the
359+
- READ_REPLICAS_DISABLED: If disabled, read endpoint will not be provided and the
309360
instance cannot scale up or down the number of replicas.
310-
- READ_REPLICAS_ENABLED: If enabled, read endpoint will be provided and the instance
361+
- READ_REPLICAS_ENABLED: If enabled, read endpoint will be provided and the instance
311362
can scale up and down the number of replicas. Possible values: ["READ_REPLICAS_DISABLED", "READ_REPLICAS_ENABLED"]`,
312363
},
313364
"redis_configs": {
@@ -323,7 +374,7 @@ https://cloud.google.com/memorystore/docs/redis/reference/rest/v1/projects.locat
323374
Computed: true,
324375
Optional: true,
325376
Description: `The version of Redis software. If not provided, latest supported
326-
version will be used. Please check the API documentation linked
377+
version will be used. Please check the API documentation linked
327378
at the top for the latest valid values.`,
328379
},
329380
"region": {
@@ -337,9 +388,9 @@ at the top for the latest valid values.`,
337388
Type: schema.TypeInt,
338389
Computed: true,
339390
Optional: true,
340-
Description: `Optional. The number of replica nodes. The valid range for the Standard Tier with
391+
Description: `Optional. The number of replica nodes. The valid range for the Standard Tier with
341392
read replicas enabled is [1-5] and defaults to 2. If read replicas are not enabled
342-
for a Standard Tier instance, the only valid value is 1 and the default is 1.
393+
for a Standard Tier instance, the only valid value is 1 and the default is 1.
343394
The valid value for basic tier is 0 and the default is also 0.`,
344395
},
345396
"reserved_ip_range": {
@@ -360,7 +411,7 @@ network.`,
360411
DiffSuppressFunc: secondaryIpDiffSuppress,
361412
Description: `Optional. Additional IP range for node placement. Required when enabling read replicas on
362413
an existing instance. For DIRECT_PEERING mode value must be a CIDR range of size /28, or
363-
"auto". For PRIVATE_SERVICE_ACCESS mode value must be the name of an allocated address
414+
"auto". For PRIVATE_SERVICE_ACCESS mode value must be the name of an allocated address
364415
range associated with the private service access connection, or "auto".`,
365416
},
366417
"tier": {
@@ -447,7 +498,7 @@ will exhibit some lag behind the primary. Write requests must target 'host'.`,
447498
"read_endpoint_port": {
448499
Type: schema.TypeInt,
449500
Computed: true,
450-
Description: `Output only. The port number of the exposed readonly redis endpoint. Standard tier only.
501+
Description: `Output only. The port number of the exposed readonly redis endpoint. Standard tier only.
451502
Write requests should target 'port'.`,
452503
},
453504
"server_ca_certs": {
@@ -563,6 +614,12 @@ func resourceRedisInstanceCreate(d *schema.ResourceData, meta interface{}) error
563614
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
564615
obj["name"] = nameProp
565616
}
617+
persistenceConfigProp, err := expandRedisInstancePersistenceConfig(d.Get("persistence_config"), d, config)
618+
if err != nil {
619+
return err
620+
} else if v, ok := d.GetOkExists("persistence_config"); !isEmptyValue(reflect.ValueOf(persistenceConfigProp)) && (ok || !reflect.DeepEqual(v, persistenceConfigProp)) {
621+
obj["persistenceConfig"] = persistenceConfigProp
622+
}
566623
maintenancePolicyProp, err := expandRedisInstanceMaintenancePolicy(d.Get("maintenance_policy"), d, config)
567624
if err != nil {
568625
return err
@@ -792,6 +849,9 @@ func resourceRedisInstanceRead(d *schema.ResourceData, meta interface{}) error {
792849
if err := d.Set("name", flattenRedisInstanceName(res["name"], d, config)); err != nil {
793850
return fmt.Errorf("Error reading Instance: %s", err)
794851
}
852+
if err := d.Set("persistence_config", flattenRedisInstancePersistenceConfig(res["persistenceConfig"], d, config)); err != nil {
853+
return fmt.Errorf("Error reading Instance: %s", err)
854+
}
795855
if err := d.Set("maintenance_policy", flattenRedisInstanceMaintenancePolicy(res["maintenancePolicy"], d, config)); err != nil {
796856
return fmt.Errorf("Error reading Instance: %s", err)
797857
}
@@ -887,6 +947,12 @@ func resourceRedisInstanceUpdate(d *schema.ResourceData, meta interface{}) error
887947
} else if v, ok := d.GetOkExists("redis_configs"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, redisConfigsProp)) {
888948
obj["redisConfigs"] = redisConfigsProp
889949
}
950+
persistenceConfigProp, err := expandRedisInstancePersistenceConfig(d.Get("persistence_config"), d, config)
951+
if err != nil {
952+
return err
953+
} else if v, ok := d.GetOkExists("persistence_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, persistenceConfigProp)) {
954+
obj["persistenceConfig"] = persistenceConfigProp
955+
}
890956
maintenancePolicyProp, err := expandRedisInstanceMaintenancePolicy(d.Get("maintenance_policy"), d, config)
891957
if err != nil {
892958
return err
@@ -953,6 +1019,10 @@ func resourceRedisInstanceUpdate(d *schema.ResourceData, meta interface{}) error
9531019
updateMask = append(updateMask, "redisConfigs")
9541020
}
9551021

1022+
if d.HasChange("persistence_config") {
1023+
updateMask = append(updateMask, "persistenceConfig")
1024+
}
1025+
9561026
if d.HasChange("maintenance_policy") {
9571027
updateMask = append(updateMask, "maintenancePolicy")
9581028
}
@@ -1165,6 +1235,41 @@ func flattenRedisInstanceName(v interface{}, d *schema.ResourceData, config *Con
11651235
return NameFromSelfLinkStateFunc(v)
11661236
}
11671237

1238+
func flattenRedisInstancePersistenceConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1239+
if v == nil {
1240+
return nil
1241+
}
1242+
original := v.(map[string]interface{})
1243+
if len(original) == 0 {
1244+
return nil
1245+
}
1246+
transformed := make(map[string]interface{})
1247+
transformed["persistence_mode"] =
1248+
flattenRedisInstancePersistenceConfigPersistenceMode(original["persistenceMode"], d, config)
1249+
transformed["rdb_snapshot_period"] =
1250+
flattenRedisInstancePersistenceConfigRdbSnapshotPeriod(original["rdbSnapshotPeriod"], d, config)
1251+
transformed["rdb_next_snapshot_time"] =
1252+
flattenRedisInstancePersistenceConfigRdbNextSnapshotTime(original["rdbNextSnapshotTime"], d, config)
1253+
transformed["rdb_snapshot_start_time"] =
1254+
flattenRedisInstancePersistenceConfigRdbSnapshotStartTime(original["rdbSnapshotStartTime"], d, config)
1255+
return []interface{}{transformed}
1256+
}
1257+
func flattenRedisInstancePersistenceConfigPersistenceMode(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1258+
return v
1259+
}
1260+
1261+
func flattenRedisInstancePersistenceConfigRdbSnapshotPeriod(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1262+
return v
1263+
}
1264+
1265+
func flattenRedisInstancePersistenceConfigRdbNextSnapshotTime(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1266+
return v
1267+
}
1268+
1269+
func flattenRedisInstancePersistenceConfigRdbSnapshotStartTime(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1270+
return v
1271+
}
1272+
11681273
func flattenRedisInstanceMaintenancePolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
11691274
if v == nil {
11701275
return nil
@@ -1564,6 +1669,62 @@ func expandRedisInstanceName(v interface{}, d TerraformResourceData, config *Con
15641669
return replaceVars(d, config, "projects/{{project}}/locations/{{region}}/instances/{{name}}")
15651670
}
15661671

1672+
func expandRedisInstancePersistenceConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1673+
l := v.([]interface{})
1674+
if len(l) == 0 || l[0] == nil {
1675+
return nil, nil
1676+
}
1677+
raw := l[0]
1678+
original := raw.(map[string]interface{})
1679+
transformed := make(map[string]interface{})
1680+
1681+
transformedPersistenceMode, err := expandRedisInstancePersistenceConfigPersistenceMode(original["persistence_mode"], d, config)
1682+
if err != nil {
1683+
return nil, err
1684+
} else if val := reflect.ValueOf(transformedPersistenceMode); val.IsValid() && !isEmptyValue(val) {
1685+
transformed["persistenceMode"] = transformedPersistenceMode
1686+
}
1687+
1688+
transformedRdbSnapshotPeriod, err := expandRedisInstancePersistenceConfigRdbSnapshotPeriod(original["rdb_snapshot_period"], d, config)
1689+
if err != nil {
1690+
return nil, err
1691+
} else if val := reflect.ValueOf(transformedRdbSnapshotPeriod); val.IsValid() && !isEmptyValue(val) {
1692+
transformed["rdbSnapshotPeriod"] = transformedRdbSnapshotPeriod
1693+
}
1694+
1695+
transformedRdbNextSnapshotTime, err := expandRedisInstancePersistenceConfigRdbNextSnapshotTime(original["rdb_next_snapshot_time"], d, config)
1696+
if err != nil {
1697+
return nil, err
1698+
} else if val := reflect.ValueOf(transformedRdbNextSnapshotTime); val.IsValid() && !isEmptyValue(val) {
1699+
transformed["rdbNextSnapshotTime"] = transformedRdbNextSnapshotTime
1700+
}
1701+
1702+
transformedRdbSnapshotStartTime, err := expandRedisInstancePersistenceConfigRdbSnapshotStartTime(original["rdb_snapshot_start_time"], d, config)
1703+
if err != nil {
1704+
return nil, err
1705+
} else if val := reflect.ValueOf(transformedRdbSnapshotStartTime); val.IsValid() && !isEmptyValue(val) {
1706+
transformed["rdbSnapshotStartTime"] = transformedRdbSnapshotStartTime
1707+
}
1708+
1709+
return transformed, nil
1710+
}
1711+
1712+
func expandRedisInstancePersistenceConfigPersistenceMode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1713+
return v, nil
1714+
}
1715+
1716+
func expandRedisInstancePersistenceConfigRdbSnapshotPeriod(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1717+
return v, nil
1718+
}
1719+
1720+
func expandRedisInstancePersistenceConfigRdbNextSnapshotTime(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1721+
return v, nil
1722+
}
1723+
1724+
func expandRedisInstancePersistenceConfigRdbSnapshotStartTime(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1725+
return v, nil
1726+
}
1727+
15671728
func expandRedisInstanceMaintenancePolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
15681729
l := v.([]interface{})
15691730
if len(l) == 0 || l[0] == nil {

google/resource_redis_instance_generated_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,49 @@ data "google_compute_network" "redis-network" {
131131
`, context)
132132
}
133133

134+
func TestAccRedisInstance_redisInstanceFullWithPersistenceConfigExample(t *testing.T) {
135+
t.Parallel()
136+
137+
context := map[string]interface{}{
138+
"network_name": BootstrapSharedTestNetwork(t, "redis-mrr"),
139+
"random_suffix": randString(t, 10),
140+
}
141+
142+
vcrTest(t, resource.TestCase{
143+
PreCheck: func() { testAccPreCheck(t) },
144+
Providers: testAccProviders,
145+
CheckDestroy: testAccCheckRedisInstanceDestroyProducer(t),
146+
Steps: []resource.TestStep{
147+
{
148+
Config: testAccRedisInstance_redisInstanceFullWithPersistenceConfigExample(context),
149+
},
150+
{
151+
ResourceName: "google_redis_instance.cache-persis",
152+
ImportState: true,
153+
ImportStateVerify: true,
154+
ImportStateVerifyIgnore: []string{"region"},
155+
},
156+
},
157+
})
158+
}
159+
160+
func testAccRedisInstance_redisInstanceFullWithPersistenceConfigExample(context map[string]interface{}) string {
161+
return Nprintf(`
162+
resource "google_redis_instance" "cache-persis" {
163+
name = "tf-test-ha-memory-cache-persis%{random_suffix}"
164+
tier = "STANDARD_HA"
165+
memory_size_gb = 1
166+
location_id = "us-central1-a"
167+
alternative_location_id = "us-central1-f"
168+
169+
persistence_config {
170+
persistence_mode = "RDB"
171+
rdb_snapshot_period = "TWELVE_HOURS"
172+
}
173+
}
174+
`, context)
175+
}
176+
134177
func TestAccRedisInstance_redisInstancePrivateServiceExample(t *testing.T) {
135178
skipIfVcr(t)
136179
t.Parallel()

0 commit comments

Comments
 (0)