@@ -39,18 +39,18 @@ func (r *CDCResource) Configure(_ context.Context, req resource.ConfigureRequest
39
39
r .clients = req .ProviderData .(* astraClients2 )
40
40
}
41
41
42
- // CDCResourceModel represents the resource data model
42
+ // CDCResourceModel represents data used to configure CDC
43
43
type CDCResourceModel struct {
44
44
DatabaseID types.String `tfsdk:"database_id"`
45
45
DatabaseName types.String `tfsdk:"database_name"`
46
46
Tables []KeyspaceTable `tfsdk:"tables"`
47
47
Regions []DatacenterToStreamingMap `tfsdk:"regions"`
48
+ DataTopics types.Map `tfsdk:"data_topics"`
48
49
}
49
50
50
51
type KeyspaceTable struct {
51
- Keyspace types.String `tfsdk:"keyspace"`
52
- Table types.String `tfsdk:"table"`
53
- DataTopics types.List `tfsdk:"data_topics"`
52
+ Keyspace types.String `tfsdk:"keyspace"`
53
+ Table types.String `tfsdk:"table"`
54
54
}
55
55
56
56
type DatacenterToStreamingMap struct {
@@ -60,20 +60,6 @@ type DatacenterToStreamingMap struct {
60
60
StreamingTenant types.String `tfsdk:"streaming_tenant"`
61
61
}
62
62
63
- // setDataTopics updates the data topics field for each table based on caculateCDCDataTopicName.
64
- func (m * CDCResourceModel ) setDataTopics () {
65
-
66
- for i := range m .Tables {
67
- dataTopics := []attr.Value {}
68
-
69
- for _ , region := range m .Regions {
70
- topicFQDN := calculateCDCDataTopicName (region .StreamingTenant .ValueString (), m .DatabaseID .ValueString (), m .Tables [i ].Keyspace .ValueString (), m .Tables [i ].Table .ValueString ())
71
- dataTopics = append (dataTopics , types .StringValue (topicFQDN ))
72
- }
73
- m .Tables [i ].DataTopics , _ = types .ListValue (types .StringType , dataTopics )
74
- }
75
- }
76
-
77
63
func (r * CDCResource ) Metadata (_ context.Context , req resource.MetadataRequest , resp * resource.MetadataResponse ) {
78
64
resp .TypeName = "astra_cdc_v3"
79
65
}
@@ -93,7 +79,7 @@ func (r *CDCResource) Schema(_ context.Context, req resource.SchemaRequest, resp
93
79
Description : "Astra database name." ,
94
80
Required : true ,
95
81
},
96
- "tables" : schema.ListNestedAttribute {
82
+ "tables" : schema.SetNestedAttribute {
97
83
Description : "List of tables to enable CDC. Must include at least 1." ,
98
84
Required : true ,
99
85
NestedObject : schema.NestedAttributeObject {
@@ -104,17 +90,11 @@ func (r *CDCResource) Schema(_ context.Context, req resource.SchemaRequest, resp
104
90
"table" : schema.StringAttribute {
105
91
Required : true ,
106
92
},
107
- "data_topics" : schema.ListAttribute {
108
- Description : "List of Pulsar topics to which CDC data is published. " +
109
- "One data topic per region, in the same order of regions." ,
110
- Computed : true ,
111
- ElementType : types .StringType ,
112
- },
113
93
},
114
94
},
115
95
},
116
96
117
- "regions" : schema.ListNestedAttribute {
97
+ "regions" : schema.SetNestedAttribute {
118
98
Description : "Mapping between datacenter regions and streaming tenants." ,
119
99
Required : true ,
120
100
NestedObject : schema.NestedAttributeObject {
@@ -138,6 +118,14 @@ func (r *CDCResource) Schema(_ context.Context, req resource.SchemaRequest, resp
138
118
},
139
119
},
140
120
},
121
+ "data_topics" : schema.MapAttribute {
122
+ Description : "Map of CDC data topics for each table in each region. " +
123
+ "Key is the region in the format `<region>`, " ,
124
+ Computed : true ,
125
+ ElementType : types.MapType {
126
+ ElemType : types .StringType ,
127
+ },
128
+ },
141
129
},
142
130
}
143
131
}
@@ -167,6 +155,7 @@ func (r *CDCResource) Create(ctx context.Context, req resource.CreateRequest, re
167
155
}
168
156
169
157
plan .setDataTopics ()
158
+
170
159
diags = resp .State .Set (ctx , & plan )
171
160
resp .Diagnostics .Append (diags ... )
172
161
}
@@ -194,7 +183,9 @@ func (r *CDCResource) Read(ctx context.Context, req resource.ReadRequest, resp *
194
183
return
195
184
}
196
185
197
- updateStateForCDCReadRequest (& state , cdcResponse .JSON200 )
186
+ copyResponseDataToResourceState (& state , cdcResponse .JSON200 )
187
+ state .setDataTopics ()
188
+
198
189
diags = resp .State .Set (ctx , & state )
199
190
resp .Diagnostics .Append (diags ... )
200
191
}
@@ -287,7 +278,8 @@ func createEnableCDCRequestBody(tfData *CDCResourceModel) astra.EnableCDCJSONReq
287
278
return reqData
288
279
}
289
280
290
- func updateStateForCDCReadRequest (tfData * CDCResourceModel , respData * astra.ListCDCResponse ) {
281
+ // copyResponseDataToResourceState copies the data from the REST endpoing response to the Terraform resource state model.
282
+ func copyResponseDataToResourceState (tfData * CDCResourceModel , respData * astra.ListCDCResponse ) {
291
283
tfData .DatabaseID = types .StringValue (respData .DatabaseID )
292
284
tfData .DatabaseName = types .StringValue (respData .DatabaseName )
293
285
var tables []KeyspaceTable
@@ -309,7 +301,6 @@ func updateStateForCDCReadRequest(tfData *CDCResourceModel, respData *astra.List
309
301
})
310
302
}
311
303
tfData .Regions = regions
312
- tfData .setDataTopics ()
313
304
}
314
305
315
306
func createDeleteCDCRequestBody (tfData * CDCResourceModel ) astra.DeleteCDCJSONRequestBody {
@@ -333,3 +324,26 @@ const AstraCDCPulsarNamespace = "astracdc"
333
324
func calculateCDCDataTopicName (streamingTenant , databaseID , keyspace , tableName string ) string {
334
325
return fmt .Sprintf ("persistent://%s/%s/data-%s-%s.%s" , streamingTenant , AstraCDCPulsarNamespace , databaseID , keyspace , tableName )
335
326
}
327
+
328
+ // getDataTopicsList uses the region and table config to create the two dimensional (region and table) map of data topics.
329
+ func (m * CDCResourceModel ) setDataTopics () {
330
+
331
+ dataTopicsMap := map [string ]attr.Value {}
332
+
333
+ for _ , region := range m .Regions {
334
+ regionName := region .Region .ValueString ()
335
+ regionDataTopics := make (map [string ]attr.Value )
336
+
337
+ for _ , table := range m .Tables {
338
+ keyspaceTable := fmt .Sprintf ("%s.%s" , table .Keyspace .ValueString (), table .Table .ValueString ())
339
+ topicFQDN := calculateCDCDataTopicName (region .StreamingTenant .ValueString (), m .DatabaseID .ValueString (), table .Keyspace .ValueString (), table .Table .ValueString ())
340
+ regionDataTopics [keyspaceTable ] = types .StringValue (topicFQDN )
341
+ }
342
+ dataTopicsMap [regionName ] = types .MapValueMust (
343
+ types .StringType ,
344
+ regionDataTopics ,
345
+ )
346
+ }
347
+
348
+ m .DataTopics = types .MapValueMust (types.MapType {ElemType : types .StringType }, dataTopicsMap )
349
+ }
0 commit comments