Skip to content

Commit 4448e08

Browse files
authored
add data topic output field to cdc_v3 (#448)
1 parent 4f31776 commit 4448e08

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

docs/resources/cdc_v3.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ Required:
128128
- `keyspace` (String)
129129
- `table` (String)
130130

131+
Read-Only:
132+
133+
- `data_topics` (List of String) List of Pulsar topics to which CDC data is published. One data topic per region, in the same order of regions.
134+
131135
## Import
132136

133137
Import is supported using the following syntax:

internal/provider/resource_cdc_v3.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ type CDCResourceModel struct {
4747
}
4848

4949
type KeyspaceTable struct {
50-
Keyspace types.String `tfsdk:"keyspace"`
51-
Table types.String `tfsdk:"table"`
50+
Keyspace types.String `tfsdk:"keyspace"`
51+
Table types.String `tfsdk:"table"`
52+
DataTopics []types.String `tfsdk:"data_topics"`
5253
}
5354

5455
type DatacenterToStreamingMap struct {
@@ -58,6 +59,17 @@ type DatacenterToStreamingMap struct {
5859
StreamingTenant types.String `tfsdk:"streaming_tenant"`
5960
}
6061

62+
// setDataTopics updates the data topics field for each table based on caculateCDCDataTopicName.
63+
func (m *CDCResourceModel) setDataTopics() {
64+
for i := range m.Tables {
65+
m.Tables[i].DataTopics = make([]types.String, len(m.Regions))
66+
for j := range m.Regions {
67+
m.Tables[i].DataTopics[j] = types.StringValue(calculateCDCDataTopicName(m.Regions[j].StreamingTenant.ValueString(),
68+
m.DatabaseID.ValueString(), m.Tables[i].Keyspace.ValueString(), m.Tables[i].Table.ValueString()))
69+
}
70+
}
71+
}
72+
6173
func (r *CDCResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
6274
resp.TypeName = "astra_cdc_v3"
6375
}
@@ -88,6 +100,12 @@ func (r *CDCResource) Schema(_ context.Context, req resource.SchemaRequest, resp
88100
"table": schema.StringAttribute{
89101
Required: true,
90102
},
103+
"data_topics": schema.ListAttribute{
104+
Description: "List of Pulsar topics to which CDC data is published. " +
105+
"One data topic per region, in the same order of regions.",
106+
Computed: true,
107+
ElementType: types.StringType,
108+
},
91109
},
92110
},
93111
},
@@ -144,6 +162,7 @@ func (r *CDCResource) Create(ctx context.Context, req resource.CreateRequest, re
144162
return
145163
}
146164

165+
plan.setDataTopics()
147166
diags = resp.State.Set(ctx, &plan)
148167
resp.Diagnostics.Append(diags...)
149168
}
@@ -286,7 +305,7 @@ func updateStateForCDCReadRequest(tfData *CDCResourceModel, respData *astra.List
286305
})
287306
}
288307
tfData.Regions = regions
289-
308+
tfData.setDataTopics()
290309
}
291310

292311
func createDeleteCDCRequestBody(tfData *CDCResourceModel) astra.DeleteCDCJSONRequestBody {
@@ -302,3 +321,11 @@ func createDeleteCDCRequestBody(tfData *CDCResourceModel) astra.DeleteCDCJSONReq
302321
}
303322
return reqData
304323
}
324+
325+
const AstraCDCPulsarNamespace = "astracdc"
326+
327+
// calculateCDCDataTopicName constructs the expected CDC data topic name based on the database ID and streaming tenant.
328+
// For example 'persistent://terraform-support1/astracdc/data-0d509b0f-d38a-4c8e-9680-fa7c752189b7-ks1.table1'
329+
func calculateCDCDataTopicName(streamingTenant, databaseID, keyspace, tableName string) string {
330+
return fmt.Sprintf("persistent://%s/%s/data-%s-%s.%s", streamingTenant, AstraCDCPulsarNamespace, databaseID, keyspace, tableName)
331+
}

0 commit comments

Comments
 (0)