|
| 1 | +package ephemeral |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/terraform-linters/tflint-plugin-sdk/hclext" |
| 7 | + "github.com/terraform-linters/tflint-plugin-sdk/tflint" |
| 8 | + "github.com/terraform-linters/tflint-ruleset-aws/project" |
| 9 | +) |
| 10 | + |
| 11 | +// TODO: Write the rule's description here |
| 12 | +// AwsEphemeralResourcesRule checks ... |
| 13 | +type AwsEphemeralResourcesRule struct { |
| 14 | + tflint.DefaultRule |
| 15 | + |
| 16 | + replacingEphemeralResources []string |
| 17 | +} |
| 18 | + |
| 19 | +// NewAwsEphemeralResourcesRule returns new rule with default attributes |
| 20 | +func NewAwsEphemeralResourcesRule() *AwsEphemeralResourcesRule { |
| 21 | + return &AwsEphemeralResourcesRule{ |
| 22 | + replacingEphemeralResources: replacingEphemeralResources, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Name returns the rule name |
| 27 | +func (r *AwsEphemeralResourcesRule) Name() string { |
| 28 | + return "aws_ephemeral_resources" |
| 29 | +} |
| 30 | + |
| 31 | +// Enabled returns whether the rule is enabled by default |
| 32 | +func (r *AwsEphemeralResourcesRule) Enabled() bool { |
| 33 | + return false |
| 34 | +} |
| 35 | + |
| 36 | +// Severity returns the rule severity |
| 37 | +func (r *AwsEphemeralResourcesRule) Severity() tflint.Severity { |
| 38 | + return tflint.WARNING |
| 39 | +} |
| 40 | + |
| 41 | +// Link returns the rule reference link |
| 42 | +func (r *AwsEphemeralResourcesRule) Link() string { |
| 43 | + return project.ReferenceLink(r.Name()) |
| 44 | +} |
| 45 | + |
| 46 | +// Check checks if there is an ephemeral resource which can replace an data source |
| 47 | +func (r *AwsEphemeralResourcesRule) Check(runner tflint.Runner) error { |
| 48 | + for _, resourceType := range r.replacingEphemeralResources { |
| 49 | + resources, err := GetDataSourceContent(runner, resourceType, &hclext.BodySchema{}, nil) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + for _, resource := range resources.Blocks { |
| 55 | + if err := runner.EmitIssueWithFix( |
| 56 | + r, |
| 57 | + fmt.Sprintf("\"%s\" is a non-ephemeral data source, which means that all (sensitive) attributes are stored in state. Please use ephemeral resource \"%s\" instead.", resourceType, resourceType), |
| 58 | + resource.TypeRange, |
| 59 | + func(f tflint.Fixer) error { |
| 60 | + return f.ReplaceText(resource.TypeRange, "ephemeral") |
| 61 | + }, |
| 62 | + ); err != nil { |
| 63 | + return fmt.Errorf("failed to call EmitIssueWithFix(): %w", err) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +func GetDataSourceContent(r tflint.Runner, name string, schema *hclext.BodySchema, opts *tflint.GetModuleContentOption) (*hclext.BodyContent, error) { |
| 72 | + body, err := r.GetModuleContent(&hclext.BodySchema{ |
| 73 | + Blocks: []hclext.BlockSchema{ |
| 74 | + {Type: "data", LabelNames: []string{"type", "name"}, Body: schema}, |
| 75 | + }, |
| 76 | + }, opts) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + content := &hclext.BodyContent{Blocks: []*hclext.Block{}} |
| 82 | + for _, resource := range body.Blocks { |
| 83 | + if resource.Labels[0] != name { |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + content.Blocks = append(content.Blocks, resource) |
| 88 | + } |
| 89 | + |
| 90 | + return content, nil |
| 91 | +} |
0 commit comments