@@ -86,14 +86,14 @@ func (r *MSKTopicConfigCommentsRule) validateTopicConfigComments(runner tflint.R
86
86
return nil
87
87
}
88
88
89
- type configTimeValueCommentInfo struct {
89
+ type configValueCommentInfo struct {
90
90
key string
91
91
infiniteValue string
92
92
baseComment string
93
93
issueWhenInvalid bool
94
94
}
95
95
96
- var configTimeValueCommentInfos = []configTimeValueCommentInfo {
96
+ var configTimeValueCommentInfos = []configValueCommentInfo {
97
97
{
98
98
key : retentionTimeAttr ,
99
99
infiniteValue : "-1" ,
@@ -114,6 +114,21 @@ var configTimeValueCommentInfos = []configTimeValueCommentInfo{
114
114
},
115
115
}
116
116
117
+ var configByteValueCommentInfos = []configValueCommentInfo {
118
+ {
119
+ key : "max.message.bytes" ,
120
+ infiniteValue : "" ,
121
+ baseComment : "allow for a batch of records maximum" ,
122
+ issueWhenInvalid : true ,
123
+ },
124
+ {
125
+ key : "retention.bytes" ,
126
+ infiniteValue : "-1" ,
127
+ baseComment : "keep on each partition" ,
128
+ issueWhenInvalid : true ,
129
+ },
130
+ }
131
+
117
132
func (r * MSKTopicConfigCommentsRule ) validateConfigValuesInComments (
118
133
runner tflint.Runner ,
119
134
configKeyToPairMap map [string ]hcl.KeyValuePair ,
@@ -123,16 +138,22 @@ func (r *MSKTopicConfigCommentsRule) validateConfigValuesInComments(
123
138
return err
124
139
}
125
140
}
141
+ for _ , configValueInfo := range configByteValueCommentInfos {
142
+ if err := r .validateByteConfigValue (runner , configKeyToPairMap , configValueInfo ); err != nil {
143
+ return err
144
+ }
145
+ }
126
146
127
147
return nil
128
148
}
129
149
130
150
func (r * MSKTopicConfigCommentsRule ) validateTimeConfigValue (
131
151
runner tflint.Runner ,
132
152
configKeyToPairMap map [string ]hcl.KeyValuePair ,
133
- configValueInfo configTimeValueCommentInfo ,
153
+ configValueInfo configValueCommentInfo ,
134
154
) error {
135
- timePair , hasConfig := configKeyToPairMap [configValueInfo .key ]
155
+ key := configValueInfo .key
156
+ timePair , hasConfig := configKeyToPairMap [key ]
136
157
if ! hasConfig {
137
158
return nil
138
159
}
@@ -145,39 +166,70 @@ func (r *MSKTopicConfigCommentsRule) validateTimeConfigValue(
145
166
return nil
146
167
}
147
168
148
- comment , err := r .getExistingComment (runner , timePair )
169
+ return r .reportHumanReadableComment (runner , timePair , key , msg )
170
+ }
171
+
172
+ func (r * MSKTopicConfigCommentsRule ) validateByteConfigValue (
173
+ runner tflint.Runner ,
174
+ configKeyToPairMap map [string ]hcl.KeyValuePair ,
175
+ configValueInfo configValueCommentInfo ,
176
+ ) error {
177
+ key := configValueInfo .key
178
+ dataPair , hasConfig := configKeyToPairMap [key ]
179
+ if ! hasConfig {
180
+ return nil
181
+ }
182
+
183
+ msg , err := r .buildDataSizeComment (runner , dataPair , configValueInfo )
184
+ if err != nil {
185
+ return err
186
+ }
187
+ if msg == "" {
188
+ return nil
189
+ }
190
+
191
+ return r .reportHumanReadableComment (runner , dataPair , key , msg )
192
+ }
193
+
194
+ func (r * MSKTopicConfigCommentsRule ) reportHumanReadableComment (
195
+ runner tflint.Runner ,
196
+ keyValuePair hcl.KeyValuePair ,
197
+ key string ,
198
+ commentMsg string ,
199
+ ) error {
200
+ comment , err := r .getExistingComment (runner , keyValuePair )
149
201
if err != nil {
150
202
return err
151
203
}
152
204
153
205
if comment == nil {
154
206
err := runner .EmitIssueWithFix (
155
207
r ,
156
- fmt .Sprintf ("%s must have a comment with the human readable value: adding it ..." , configValueInfo . key ),
157
- timePair .Key .Range (),
208
+ fmt .Sprintf ("%s must have a comment with the human readable value: adding it ..." , key ),
209
+ keyValuePair .Key .Range (),
158
210
func (f tflint.Fixer ) error {
159
- return f .InsertTextAfter (timePair .Value .Range (), msg )
211
+ return f .InsertTextAfter (keyValuePair .Value .Range (), commentMsg )
160
212
},
161
213
)
162
214
if err != nil {
163
- return fmt .Errorf ("emitting issue: no comment for time value: %w" , err )
215
+ return fmt .Errorf ("emitting issue: no comment for human readable value: %w" , err )
164
216
}
165
217
return nil
166
218
}
167
219
168
220
commentTxt := strings .TrimSpace (string (comment .Bytes ))
169
- if commentTxt != msg {
221
+ if commentTxt != commentMsg {
170
222
issueMsg := fmt .Sprintf (
171
223
"%s value doesn't correspond to the human readable value in the comment: fixing it ..." ,
172
- configValueInfo . key ,
224
+ key ,
173
225
)
174
226
err := runner .EmitIssueWithFix (r , issueMsg , comment .Range ,
175
227
func (f tflint.Fixer ) error {
176
- return f .ReplaceText (comment .Range , msg + "\n " )
228
+ return f .ReplaceText (comment .Range , commentMsg + "\n " )
177
229
},
178
230
)
179
231
if err != nil {
180
- return fmt .Errorf ("emitting issue: wrong comment for time value: %w" , err )
232
+ return fmt .Errorf ("emitting issue: wrong comment for human readable value: %w" , err )
181
233
}
182
234
}
183
235
return nil
@@ -243,7 +295,7 @@ func isNotComment(token hclsyntax.Token) bool {
243
295
func (r * MSKTopicConfigCommentsRule ) buildDurationComment (
244
296
runner tflint.Runner ,
245
297
timePair hcl.KeyValuePair ,
246
- configValueInfo configTimeValueCommentInfo ,
298
+ configValueInfo configValueCommentInfo ,
247
299
) (string , error ) {
248
300
var timeVal string
249
301
diags := gohcl .DecodeExpression (timePair .Value , nil , & timeVal )
@@ -271,10 +323,73 @@ func (r *MSKTopicConfigCommentsRule) buildDurationComment(
271
323
return "" , nil
272
324
}
273
325
274
- baseComment := configValueInfo .baseComment
326
+ return buildCommentForMillis (timeMillis , configValueInfo .baseComment ), nil
327
+ }
328
+
329
+ func (r * MSKTopicConfigCommentsRule ) buildDataSizeComment (
330
+ runner tflint.Runner ,
331
+ dataPair hcl.KeyValuePair ,
332
+ configValueInfo configValueCommentInfo ,
333
+ ) (string , error ) {
334
+ var dataVal string
335
+ diags := gohcl .DecodeExpression (dataPair .Value , nil , & dataVal )
336
+ if diags .HasErrors () {
337
+ return "" , diags
338
+ }
339
+
340
+ if dataVal == configValueInfo .infiniteValue {
341
+ return fmt .Sprintf ("# %s unlimited data" , configValueInfo .baseComment ), nil
342
+ }
275
343
276
- msg := buildCommentForMillis (timeMillis , baseComment )
277
- return msg , nil
344
+ byteVal , err := strconv .Atoi (dataVal )
345
+ if err != nil {
346
+ if configValueInfo .issueWhenInvalid {
347
+ issueMsg := fmt .Sprintf (
348
+ "%s must have a valid integer value expressed in bytes" ,
349
+ configValueInfo .key ,
350
+ )
351
+ err := runner .EmitIssue (r , issueMsg , dataPair .Value .Range ())
352
+ if err != nil {
353
+ return "" , fmt .Errorf ("emitting issue: invalid data value: %w" , err )
354
+ }
355
+ }
356
+
357
+ return "" , nil
358
+ }
359
+
360
+ return buildCommentForBytes (byteVal , configValueInfo .baseComment ), nil
361
+ }
362
+
363
+ func buildCommentForBytes (bytes int , baseComment string ) string {
364
+ byteUnits , unit := determineByteUnits (bytes )
365
+
366
+ byteUnitsStr := strconv .FormatFloat (byteUnits , 'f' , - 1 , 64 )
367
+ return fmt .Sprintf ("# %s %s%s" , baseComment , byteUnitsStr , unit )
368
+ }
369
+
370
+ const (
371
+ bytesInOneKiB = 1024
372
+ bytesInOneMiB = 1024 * bytesInOneKiB
373
+ bytesInOneGiB = 1024 * bytesInOneMiB
374
+ )
375
+
376
+ func determineByteUnits (bytes int ) (float64 , string ) {
377
+ floatBytes := float64 (bytes )
378
+ gbs := round (floatBytes / bytesInOneGiB )
379
+ if gbs >= 1 {
380
+ return gbs , "GiB"
381
+ }
382
+
383
+ mbs := round (floatBytes / bytesInOneMiB )
384
+ if mbs >= 1 {
385
+ return mbs , "MiB"
386
+ }
387
+
388
+ kbs := round (floatBytes / bytesInOneKiB )
389
+ if kbs >= 1 {
390
+ return kbs , "KiB"
391
+ }
392
+ return floatBytes , "B"
278
393
}
279
394
280
395
func buildCommentForMillis (timeMillis int , baseComment string ) string {
0 commit comments