Skip to content

Commit b2f534a

Browse files
authored
**New Action**: azurerm_managed_redis_databases_flush (#31132)
[FEATURE] **New Action**: `azurerm_managed_redis_databases_flush`
1 parent d5ec14d commit b2f534a

9 files changed

+326
-1
lines changed

internal/provider/services.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ func SupportedFrameworkServices() []sdk.FrameworkServiceRegistration {
350350
keyvault.Registration{},
351351
mssql.Registration{},
352352
network.Registration{},
353+
managedredis.Registration{},
353354
storage.Registration{},
354355
}
355356

internal/services/managedredis/managed_redis_data_source.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type DefaultDatabaseDataSourceModel struct {
4747
EvictionPolicy string `tfschema:"eviction_policy"`
4848
GeoReplicationGroupName string `tfschema:"geo_replication_group_name"`
4949
GeoReplicationLinkedDatabaseIds []string `tfschema:"geo_replication_linked_database_ids"`
50+
ID string `tfschema:"id"`
5051
Module []ModuleModel `tfschema:"module"`
5152
PersistenceAppendOnlyFileBackupFrequency string `tfschema:"persistence_append_only_file_backup_frequency"`
5253
PersistenceRedisDatabaseBackupFrequency string `tfschema:"persistence_redis_database_backup_frequency"`
@@ -92,6 +93,11 @@ func (r ManagedRedisDataSource) Attributes() map[string]*pluginsdk.Schema {
9293
Computed: true,
9394
Elem: &pluginsdk.Resource{
9495
Schema: map[string]*pluginsdk.Schema{
96+
"id": {
97+
Type: pluginsdk.TypeString,
98+
Computed: true,
99+
},
100+
95101
"access_keys_authentication_enabled": {
96102
Type: pluginsdk.TypeBool,
97103
Computed: true,
@@ -268,12 +274,18 @@ func (r ManagedRedisDataSource) Read() sdk.ResourceFunc {
268274

269275
if model := dbResp.Model; model != nil {
270276
if props := model.Properties; props != nil {
277+
databaseId, err := redisenterprise.ParseDatabaseID(pointer.From(model.Id))
278+
if err != nil {
279+
return fmt.Errorf("parsing Managed Redis Database ID %q: %+v", pointer.From(model.Id), err)
280+
}
281+
271282
defaultDb := DefaultDatabaseDataSourceModel{
272283
AccessKeysAuthenticationEnabled: strings.EqualFold(pointer.FromEnum(props.AccessKeysAuthentication), string(databases.AccessKeysAuthenticationEnabled)),
273284
ClientProtocol: pointer.FromEnum(props.ClientProtocol),
274285
ClusteringPolicy: pointer.FromEnum(props.ClusteringPolicy),
275286
EvictionPolicy: pointer.FromEnum(props.EvictionPolicy),
276287
GeoReplicationGroupName: flattenGeoReplicationGroupName(props.GeoReplication),
288+
ID: databaseId.ID(),
277289
Module: flattenModules(props.Modules),
278290
PersistenceAppendOnlyFileBackupFrequency: flattenPersistenceAOF(props.Persistence),
279291
PersistenceRedisDatabaseBackupFrequency: flattenPersistenceRDB(props.Persistence),
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package managedredis
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/hashicorp/go-azure-helpers/framework/typehelpers"
9+
"github.com/hashicorp/go-azure-helpers/lang/pointer"
10+
"github.com/hashicorp/go-azure-sdk/resource-manager/redisenterprise/2025-07-01/redisenterprise"
11+
"github.com/hashicorp/terraform-plugin-framework/action"
12+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
14+
"github.com/hashicorp/terraform-plugin-framework/types"
15+
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
16+
)
17+
18+
type ManagedRedisFlushDatabasesAction struct {
19+
sdk.ActionMetadata
20+
}
21+
22+
var _ sdk.Action = &ManagedRedisFlushDatabasesAction{}
23+
24+
func newManagedRedisFlushDatabasesAction() action.Action {
25+
return &ManagedRedisFlushDatabasesAction{}
26+
}
27+
28+
type ManagedRedisFlushDatabasesActionActionModel struct {
29+
ManagedRedisDatabaseId types.String `tfsdk:"managed_redis_database_id"`
30+
LinkedDatabaseIds []types.String `tfsdk:"linked_database_ids"`
31+
Timeout types.String `tfsdk:"timeout"`
32+
}
33+
34+
func (m *ManagedRedisFlushDatabasesAction) Schema(_ context.Context, _ action.SchemaRequest, response *action.SchemaResponse) {
35+
response.Schema = schema.Schema{
36+
Attributes: map[string]schema.Attribute{
37+
"managed_redis_database_id": schema.StringAttribute{
38+
Required: true,
39+
Description: "The ID of the Managed Redis Database where the keys will be flushed.",
40+
MarkdownDescription: "The ID of the Managed Redis Database where the keys will be flushed.",
41+
Validators: []validator.String{
42+
typehelpers.WrappedStringValidator{
43+
Func: redisenterprise.ValidateRedisEnterpriseID,
44+
},
45+
},
46+
},
47+
48+
"linked_database_ids": schema.ListAttribute{
49+
Optional: true,
50+
Description: "The IDs of any Linked Databases where the keys will be flushed.",
51+
MarkdownDescription: "The IDs of any Linked Databases where the keys will be flushed.",
52+
ElementType: types.StringType,
53+
Validators: []validator.List{
54+
typehelpers.WrappedListValidator{
55+
Func: redisenterprise.ValidateRedisEnterpriseID,
56+
},
57+
},
58+
},
59+
60+
"timeout": schema.StringAttribute{
61+
Optional: true,
62+
Description: "Timeout duration for the Managed Redis Databases Flush action to complete. Defaults to 30m.",
63+
MarkdownDescription: "Timeout duration for the Managed Redis Databases Flush action to complete. Defaults to 30m.",
64+
},
65+
},
66+
}
67+
}
68+
69+
func (m *ManagedRedisFlushDatabasesAction) Metadata(_ context.Context, _ action.MetadataRequest, response *action.MetadataResponse) {
70+
response.TypeName = "azurerm_managed_redis_databases_flush"
71+
}
72+
73+
func (m *ManagedRedisFlushDatabasesAction) Invoke(ctx context.Context, request action.InvokeRequest, response *action.InvokeResponse) {
74+
client := m.Client.ManagedRedis.Client
75+
76+
model := ManagedRedisFlushDatabasesActionActionModel{}
77+
78+
response.Diagnostics.Append(request.Config.Get(ctx, &model)...)
79+
if response.Diagnostics.HasError() {
80+
return
81+
}
82+
83+
timeout := 30 * time.Minute
84+
if t := model.Timeout; !t.IsNull() {
85+
duration, err := time.ParseDuration(t.ValueString())
86+
if err != nil {
87+
sdk.SetResponseErrorDiagnostic(response, "parsing `timeout`", err)
88+
return
89+
}
90+
timeout = duration
91+
}
92+
93+
ctx, cancel := context.WithTimeout(ctx, timeout)
94+
defer cancel()
95+
96+
id, err := redisenterprise.ParseDatabaseID(model.ManagedRedisDatabaseId.ValueString())
97+
if err != nil {
98+
sdk.SetResponseErrorDiagnostic(response, "id parsing error", err)
99+
return
100+
}
101+
102+
response.SendProgress(action.InvokeProgressEvent{
103+
Message: fmt.Sprintf("flushing %s", id),
104+
})
105+
106+
linkedDatabaseIds := make([]string, 0)
107+
for _, linkedDatabaseId := range model.LinkedDatabaseIds {
108+
linkedDatabaseIds = append(linkedDatabaseIds, linkedDatabaseId.ValueString())
109+
}
110+
111+
payload := redisenterprise.FlushParameters{
112+
Ids: pointer.To(linkedDatabaseIds),
113+
}
114+
115+
if err = client.DatabasesFlushThenPoll(ctx, *id, payload); err != nil {
116+
sdk.SetResponseErrorDiagnostic(response, fmt.Sprintf("flushing database for %s", id), err)
117+
return
118+
}
119+
120+
response.SendProgress(action.InvokeProgressEvent{
121+
Message: fmt.Sprintf("flushing completed for %s", id.ID()),
122+
})
123+
}
124+
125+
func (m *ManagedRedisFlushDatabasesAction) Configure(ctx context.Context, request action.ConfigureRequest, response *action.ConfigureResponse) {
126+
m.Defaults(ctx, request, response)
127+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package managedredis_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
10+
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
11+
"github.com/hashicorp/terraform-provider-azurerm/internal/provider/framework"
12+
)
13+
14+
type ManagedRedisFlushDatabasesAction struct{}
15+
16+
func TestAccManagedRedisFlushDatabasesAction_basic(t *testing.T) {
17+
data := acceptance.BuildTestData(t, "azurerm_managed_redis_databases_flush", "test")
18+
a := ManagedRedisFlushDatabasesAction{}
19+
20+
resource.ParallelTest(t, resource.TestCase{
21+
ProtoV5ProviderFactories: framework.ProtoV5ProviderFactoriesInit(context.Background(), "azurerm"),
22+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
23+
tfversion.SkipBelow(tfversion.Version1_14_0),
24+
},
25+
Steps: []resource.TestStep{
26+
{
27+
Config: a.basic(data),
28+
Check: nil, // TODO
29+
},
30+
},
31+
})
32+
}
33+
34+
func TestAccManagedRedisFlushDatabasesAction_complete(t *testing.T) {
35+
data := acceptance.BuildTestData(t, "azurerm_managed_redis_databases_flush", "test")
36+
a := ManagedRedisFlushDatabasesAction{}
37+
38+
resource.ParallelTest(t, resource.TestCase{
39+
ProtoV5ProviderFactories: framework.ProtoV5ProviderFactoriesInit(context.Background(), "azurerm"),
40+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
41+
tfversion.SkipBelow(tfversion.Version1_14_0),
42+
},
43+
Steps: []resource.TestStep{
44+
{
45+
Config: a.complete(data),
46+
Check: nil, // TODO
47+
},
48+
},
49+
})
50+
}
51+
52+
func (r *ManagedRedisFlushDatabasesAction) basic(data acceptance.TestData) string {
53+
return fmt.Sprintf(`
54+
%s
55+
56+
action "azurerm_managed_redis_databases_flush" "test" {
57+
config {
58+
managed_redis_database_id = azurerm_managed_redis.test.default_database[0].id
59+
}
60+
}
61+
`, r.template(data))
62+
}
63+
64+
func (r *ManagedRedisFlushDatabasesAction) complete(data acceptance.TestData) string {
65+
return fmt.Sprintf(`
66+
%s
67+
68+
action "azurerm_managed_redis_databases_flush" "test" {
69+
config {
70+
managed_redis_database_id = azurerm_managed_redis.test.default_database[0].id
71+
linked_database_ids = [azurerm_managed_redis.test.default_database[0].id]
72+
}
73+
}
74+
`, r.template(data))
75+
}
76+
77+
func (r *ManagedRedisFlushDatabasesAction) template(data acceptance.TestData) string {
78+
return fmt.Sprintf(`
79+
%s
80+
81+
resource "terraform_data" "trigger" {
82+
input = azurerm_managed_redis.test.id
83+
lifecycle {
84+
action_trigger {
85+
events = [after_create]
86+
actions = [action.azurerm_managed_redis_databases_flush.test]
87+
}
88+
}
89+
}
90+
`, ManagedRedisResource{}.update(data))
91+
}

internal/services/managedredis/managed_redis_resource.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type DefaultDatabaseModel struct {
7575
PersistenceAppendOnlyFileBackupFrequency string `tfschema:"persistence_append_only_file_backup_frequency"`
7676
PersistenceRedisDatabaseBackupFrequency string `tfschema:"persistence_redis_database_backup_frequency"`
7777

78+
ID string `tfschema:"id"`
7879
Port int64 `tfschema:"port"`
7980
PrimaryAccessKey string `tfschema:"primary_access_key"`
8081
SecondaryAccessKey string `tfschema:"secondary_access_key"`
@@ -135,6 +136,11 @@ func (r ManagedRedisResource) Arguments() map[string]*pluginsdk.Schema {
135136
MaxItems: 1,
136137
Elem: &pluginsdk.Resource{
137138
Schema: map[string]*pluginsdk.Schema{
139+
"id": {
140+
Type: pluginsdk.TypeString,
141+
Computed: true,
142+
},
143+
138144
"access_keys_authentication_enabled": {
139145
Type: pluginsdk.TypeBool,
140146
Optional: true,
@@ -410,12 +416,18 @@ func (r ManagedRedisResource) Read() sdk.ResourceFunc {
410416

411417
if model := dbResp.Model; model != nil {
412418
if props := model.Properties; props != nil {
419+
databaseId, err := redisenterprise.ParseDatabaseID(pointer.From(model.Id))
420+
if err != nil {
421+
return fmt.Errorf("parsing Managed Redis Database ID %q: %+v", pointer.From(model.Id), err)
422+
}
423+
413424
defaultDb := DefaultDatabaseModel{
414425
AccessKeysAuthenticationEnabled: strings.EqualFold(pointer.FromEnum(props.AccessKeysAuthentication), string(databases.AccessKeysAuthenticationEnabled)),
415426
ClientProtocol: pointer.FromEnum(props.ClientProtocol),
416427
ClusteringPolicy: pointer.FromEnum(props.ClusteringPolicy),
417428
EvictionPolicy: pointer.FromEnum(props.EvictionPolicy),
418429
GeoReplicationGroupName: flattenGeoReplicationGroupName(props.GeoReplication),
430+
ID: databaseId.ID(),
419431
Module: flattenModules(props.Modules),
420432
PersistenceAppendOnlyFileBackupFrequency: flattenPersistenceAOF(props.Persistence),
421433
PersistenceRedisDatabaseBackupFrequency: flattenPersistenceRDB(props.Persistence),

internal/services/managedredis/registration.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
package managedredis
55

66
import (
7+
"github.com/hashicorp/terraform-plugin-framework/action"
8+
"github.com/hashicorp/terraform-plugin-framework/ephemeral"
79
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
810
)
911

10-
var _ sdk.TypedServiceRegistrationWithAGitHubLabel = Registration{}
12+
var (
13+
_ sdk.FrameworkServiceRegistration = Registration{}
14+
_ sdk.TypedServiceRegistrationWithAGitHubLabel = Registration{}
15+
)
1116

1217
type Registration struct{}
1318

@@ -35,3 +40,25 @@ func (r Registration) Resources() []sdk.Resource {
3540
ManagedRedisResource{},
3641
}
3742
}
43+
44+
func (r Registration) Actions() []func() action.Action {
45+
return []func() action.Action{
46+
newManagedRedisFlushDatabasesAction,
47+
}
48+
}
49+
50+
func (r Registration) FrameworkResources() []sdk.FrameworkWrappedResource {
51+
return []sdk.FrameworkWrappedResource{}
52+
}
53+
54+
func (r Registration) FrameworkDataSources() []sdk.FrameworkWrappedDataSource {
55+
return []sdk.FrameworkWrappedDataSource{}
56+
}
57+
58+
func (r Registration) EphemeralResources() []func() ephemeral.EphemeralResource {
59+
return []func() ephemeral.EphemeralResource{}
60+
}
61+
62+
func (r Registration) ListResources() []sdk.FrameworkListWrappedResource {
63+
return []sdk.FrameworkListWrappedResource{}
64+
}

0 commit comments

Comments
 (0)