-
Notifications
You must be signed in to change notification settings - Fork 14
/
fromproto.go
321 lines (278 loc) · 8.8 KB
/
fromproto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package fromproto
import (
"errors"
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/proto"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/msgpack"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// BodySchema converts proto.BodySchema to hclext.BodySchema
func BodySchema(body *proto.BodySchema) *hclext.BodySchema {
if body == nil {
return nil
}
attributes := make([]hclext.AttributeSchema, len(body.Attributes))
for idx, attr := range body.Attributes {
attributes[idx] = hclext.AttributeSchema{Name: attr.Name, Required: attr.Required}
}
blocks := make([]hclext.BlockSchema, len(body.Blocks))
for idx, block := range body.Blocks {
blocks[idx] = hclext.BlockSchema{
Type: block.Type,
LabelNames: block.LabelNames,
Body: BodySchema(block.Body),
}
}
return &hclext.BodySchema{
Mode: SchemaMode(body.Mode),
Attributes: attributes,
Blocks: blocks,
}
}
// SchemaMode converts proto.SchemaMode to hclext.SchemaMode
func SchemaMode(mode proto.SchemaMode) hclext.SchemaMode {
switch mode {
case proto.SchemaMode_SCHEMA_MODE_UNSPECIFIED:
return hclext.SchemaDefaultMode
case proto.SchemaMode_SCHEMA_MODE_DEFAULT:
return hclext.SchemaDefaultMode
case proto.SchemaMode_SCHEMA_MODE_JUST_ATTRIBUTES:
return hclext.SchemaJustAttributesMode
default:
panic(fmt.Sprintf("invalid SchemaMode: %s", mode))
}
}
// BodyContent converts proto.BodyContent to hclext.BodyContent
func BodyContent(body *proto.BodyContent) (*hclext.BodyContent, hcl.Diagnostics) {
if body == nil {
return nil, nil
}
diags := hcl.Diagnostics{}
attributes := hclext.Attributes{}
for key, attr := range body.Attributes {
var expr hcl.Expression
var exprDiags hcl.Diagnostics
if attr.Expression != nil {
expr, exprDiags = Expression(attr.Expression)
} else {
expr, exprDiags = hclext.ParseExpression(attr.Expr, attr.ExprRange.Filename, Pos(attr.ExprRange.Start))
}
diags = diags.Extend(exprDiags)
attributes[key] = &hclext.Attribute{
Name: attr.Name,
Expr: expr,
Range: Range(attr.Range),
NameRange: Range(attr.NameRange),
}
}
blocks := make(hclext.Blocks, len(body.Blocks))
for idx, block := range body.Blocks {
blockBody, contentDiags := BodyContent(block.Body)
diags = diags.Extend(contentDiags)
labelRanges := make([]hcl.Range, len(block.LabelRanges))
for idx, labelRange := range block.LabelRanges {
labelRanges[idx] = Range(labelRange)
}
blocks[idx] = &hclext.Block{
Type: block.Type,
Labels: block.Labels,
Body: blockBody,
DefRange: Range(block.DefRange),
TypeRange: Range(block.TypeRange),
LabelRanges: labelRanges,
}
}
return &hclext.BodyContent{
Attributes: attributes,
Blocks: blocks,
}, diags
}
// RuleObject is an intermediate representation that satisfies the Rule interface.
type RuleObject struct {
tflint.DefaultRule
Data struct {
Name string
Enabled bool
Severity tflint.Severity
Link string
}
}
// Name returns the rule name
func (r *RuleObject) Name() string { return r.Data.Name }
// Enabled returns whether the rule is enabled
func (r *RuleObject) Enabled() bool { return r.Data.Enabled }
// Severity returns the severify of the rule
func (r *RuleObject) Severity() tflint.Severity { return r.Data.Severity }
// Link returns the link of the rule documentation if exists
func (r *RuleObject) Link() string { return r.Data.Link }
// Check does nothing. This is just a method to satisfy the interface
func (r *RuleObject) Check(tflint.Runner) error { return nil }
// Rule converts proto.EmitIssue_Rule to RuleObject
func Rule(rule *proto.EmitIssue_Rule) *RuleObject {
if rule == nil {
return nil
}
return &RuleObject{
Data: struct {
Name string
Enabled bool
Severity tflint.Severity
Link string
}{
Name: rule.Name,
Enabled: rule.Enabled,
Severity: Severity(rule.Severity),
Link: rule.Link,
},
}
}
// Expression converts proto.Expression to hcl.Expression
func Expression(expr *proto.Expression) (hcl.Expression, hcl.Diagnostics) {
parsed, diags := hclext.ParseExpression(expr.Bytes, expr.Range.Filename, Pos(expr.Range.Start))
if diags.HasErrors() {
return nil, diags
}
if expr.Value != nil {
val, err := msgpack.Unmarshal(expr.Value, cty.DynamicPseudoType)
if err != nil {
panic(fmt.Errorf("cannot unmarshal the bound expr: %w", err))
}
parsed = hclext.BindValue(val, parsed)
}
return parsed, diags
}
// Severity converts proto.EmitIssue_Severity to severity
func Severity(severity proto.EmitIssue_Severity) tflint.Severity {
switch severity {
case proto.EmitIssue_SEVERITY_ERROR:
return tflint.ERROR
case proto.EmitIssue_SEVERITY_WARNING:
return tflint.WARNING
case proto.EmitIssue_SEVERITY_NOTICE:
return tflint.NOTICE
}
return tflint.ERROR
}
// Range converts proto.Range to hcl.Range
func Range(rng *proto.Range) hcl.Range {
if rng == nil {
return hcl.Range{}
}
return hcl.Range{
Filename: rng.Filename,
Start: Pos(rng.Start),
End: Pos(rng.End),
}
}
// Pos converts proto.Range_Pos to hcl.Pos
func Pos(pos *proto.Range_Pos) hcl.Pos {
if pos == nil {
return hcl.Pos{}
}
return hcl.Pos{
Line: int(pos.Line),
Column: int(pos.Column),
Byte: int(pos.Byte),
}
}
// Config converts proto.ApplyGlobalConfig_Config to tflint.Config
func Config(config *proto.ApplyGlobalConfig_Config) *tflint.Config {
if config == nil {
return &tflint.Config{Rules: make(map[string]*tflint.RuleConfig)}
}
rules := map[string]*tflint.RuleConfig{}
for name, rule := range config.Rules {
rules[name] = &tflint.RuleConfig{Name: rule.Name, Enabled: rule.Enabled}
}
return &tflint.Config{
Rules: rules,
DisabledByDefault: config.DisabledByDefault,
Only: config.Only,
}
}
// GetModuleContentOption converts proto.GetModuleContent_Option to tflint.GetModuleContentOption
func GetModuleContentOption(opts *proto.GetModuleContent_Option) tflint.GetModuleContentOption {
if opts == nil {
return tflint.GetModuleContentOption{}
}
return tflint.GetModuleContentOption{
ModuleCtx: ModuleCtxType(opts.ModuleCtx),
IncludeNotCreated: opts.IncludeNotCreated,
ExpandMode: ExpandMode(opts.ExpandMode),
Hint: GetModuleContentHint(opts.Hint),
}
}
// ModuleCtxType converts proto.ModuleCtxType to tflint.ModuleCtxType
func ModuleCtxType(ty proto.ModuleCtxType) tflint.ModuleCtxType {
switch ty {
case proto.ModuleCtxType_MODULE_CTX_TYPE_UNSPECIFIED:
return tflint.SelfModuleCtxType
case proto.ModuleCtxType_MODULE_CTX_TYPE_SELF:
return tflint.SelfModuleCtxType
case proto.ModuleCtxType_MODULE_CTX_TYPE_ROOT:
return tflint.RootModuleCtxType
default:
panic(fmt.Sprintf("invalid ModuleCtxType: %s", ty))
}
}
// ExpandMode converts proto.GetModuleContent_ExpandMode to tflint.ExpandMode
func ExpandMode(mode proto.GetModuleContent_ExpandMode) tflint.ExpandMode {
switch mode {
case proto.GetModuleContent_EXPAND_MODE_UNSPECIFIED:
return tflint.ExpandModeExpand
case proto.GetModuleContent_EXPAND_MODE_EXPAND:
return tflint.ExpandModeExpand
case proto.GetModuleContent_EXPAND_MODE_NONE:
return tflint.ExpandModeNone
default:
panic(fmt.Sprintf("invalid ExpandMode: %s", mode))
}
}
// GetModuleContentHint converts proto.GetModuleContent_Hint to tflint.GetModuleContentHint
func GetModuleContentHint(hint *proto.GetModuleContent_Hint) tflint.GetModuleContentHint {
if hint == nil {
return tflint.GetModuleContentHint{}
}
return tflint.GetModuleContentHint{
ResourceType: hint.ResourceType,
}
}
// Error converts gRPC error status to wrapped error
func Error(err error) error {
if err == nil {
return nil
}
st, ok := status.FromError(err)
if !ok {
return err
}
// Unimplemented is an unexpected error, so return as-is.
if st.Code() == codes.Unimplemented {
return err
}
// If the error status has no details, return an error from the gRPC error status.
// Remove the status code because some statuses are expected and should not be shown to users.
if len(st.Details()) == 0 {
return errors.New(st.Message())
}
// It is not supposed to have multiple details. The detail have an error code and will be wrapped as an error.
switch t := st.Details()[0].(type) {
case *proto.ErrorDetail:
switch t.Code {
case proto.ErrorCode_ERROR_CODE_UNKNOWN_VALUE:
return fmt.Errorf("%s%w", st.Message(), tflint.ErrUnknownValue)
case proto.ErrorCode_ERROR_CODE_NULL_VALUE:
return fmt.Errorf("%s%w", st.Message(), tflint.ErrNullValue)
case proto.ErrorCode_ERROR_CODE_UNEVALUABLE:
return fmt.Errorf("%s%w", st.Message(), tflint.ErrUnevaluable)
case proto.ErrorCode_ERROR_CODE_SENSITIVE:
return fmt.Errorf("%s%w", st.Message(), tflint.ErrSensitive)
}
}
return err
}