Skip to content

Commit ff5412c

Browse files
committed
fix: partition issue solved
Set not Add Decreasing partitions causes recreate
1 parent c07c48b commit ff5412c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

redpanda/resources/topic/resource_topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (t *Topic) Update(ctx context.Context, request resource.UpdateRequest, resp
211211

212212
// we can only increase the partition count
213213
if to.Cmp(from) > 0 {
214-
_, err := t.TopicClient.AddTopicPartitions(ctx, &dataplanev1.AddTopicPartitionsRequest{
214+
_, err := t.TopicClient.SetTopicPartitions(ctx, &dataplanev1.SetTopicPartitionsRequest{
215215
TopicName: plan.Name.ValueString(),
216216
PartitionCount: *utils.NumberToInt32(plan.PartitionCount),
217217
})

redpanda/resources/topic/schema_resource.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package topic
22

33
import (
4+
"context"
5+
46
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
57
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
68
"github.com/hashicorp/terraform-plugin-framework/resource/schema/numberplanmodifier"
@@ -19,11 +21,16 @@ func resourceTopicSchema() schema.Schema {
1921
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
2022
},
2123
"partition_count": schema.NumberAttribute{
22-
Description: "The number of partitions for the topic. This determines how the data is distributed across brokers. Increases are fully supported without data loss, decreases will result in an error and should be accomplished by creating a new topic and migrating the data.",
24+
Description: "The number of partitions for the topic. This determines how the data is distributed across brokers. Increases are fully supported without data loss. Decreases will destroy and recreate the topic if allow_deletion is set to true (defaults to false).",
2325
Optional: true,
2426
Computed: true,
2527
PlanModifiers: []planmodifier.Number{
2628
numberplanmodifier.UseStateForUnknown(),
29+
numberplanmodifier.RequiresReplaceIf(
30+
partitionRequiresReplaceWhenShrinking,
31+
"Decreasing partition count requires recreating the topic",
32+
"Decreasing partition count requires recreating the topic",
33+
),
2734
},
2835
},
2936
"replication_factor": schema.NumberAttribute{
@@ -60,3 +67,14 @@ func resourceTopicSchema() schema.Schema {
6067
},
6168
}
6269
}
70+
71+
func partitionRequiresReplaceWhenShrinking(_ context.Context, req planmodifier.NumberRequest, resp *numberplanmodifier.RequiresReplaceIfFuncResponse) {
72+
if !req.PlanValue.IsNull() && !req.StateValue.IsNull() {
73+
to := req.PlanValue.ValueBigFloat()
74+
from := req.StateValue.ValueBigFloat()
75+
if to.Cmp(from) < 0 {
76+
resp.RequiresReplace = true
77+
resp.Diagnostics.AddWarning("Partition count decrease detected", "Decreasing partition count requires recreating the topic")
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)