Skip to content

Commit

Permalink
Project tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Mar 8, 2020
1 parent 29b8f69 commit 725f7c4
Show file tree
Hide file tree
Showing 8 changed files with 457 additions and 71 deletions.
373 changes: 373 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ build:

install: build
mkdir -p ~/.tflint.d/plugins
mv ./tflint-ruleset-template ~/.tflint.d/plugins
mv ./tflint-ruleset-google ~/.tflint.d/plugins
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# TFLint Ruleset Template
# WORKING IN PROGRESS AND MAINTAINER WANTED!

This is a template repository for building a custom ruleset. You can create a plugin repository from "Use this template".
This project was started to support terraform-provider-google resources in TFLint. Everything is working in progress and not available as a plugin. If you are interested in this project, consider the following:

- Open an issue for the rule you want
- Provider-specific knowledge is very important. Sharing the problems you faced will make it clear what we need to develop.
- Write a rule and open a pull request
- If you are already familiar with tflint rulesets, Writing rules to solve open issues helps all users of TFLint. If you are interested but are not familiar, please let us know. Support you as much as possible.
- Make automatic rule generation system
- In reality, it is impossible to manage rules for so many resources by hand. tflint has a mechanism to automatically generate AWS validation rules from aws-sdk (See [here](https://github.com/terraform-linters/tflint/blob/master/docs/DEVELOPING.md#sdk-based)). Building such a system can bring a lot of value at low cost.

See [terraform-lintes/tflint#606](https://github.com/terraform-linters/tflint/issues/606) for more details.

# TFLint Ruleset for terraform-provider-google

TFLint ruleset plugin for Terraform Google Cloud Platform provider

## Requirements

Expand All @@ -9,10 +22,10 @@ This is a template repository for building a custom ruleset. You can create a pl

## Installation

Download the plugin and place it in `~/.tflint.d/plugins/tflint-ruleset-template` (or `./.tflint.d/plugins/tflint-ruleset-template`). When using the plugin, configure as follows in `.tflint.hcl`:
Download the plugin and place it in `~/.tflint.d/plugins/tflint-ruleset-google` (or `./.tflint.d/plugins/tflint-ruleset-google`). When using the plugin, configure as follows in `.tflint.hcl`:

```hcl
plugin "template" {
plugin "google" {
enabled = true
}
```
Expand All @@ -21,7 +34,7 @@ plugin "template" {

|Name|Description|Severity|Enabled|Link|
| --- | --- | --- | --- | --- |
|aws_instance_example_type|Show instance type|ERROR|||
|google_compute_instance_example_type|Show machine type|ERROR|||

## Building the plugin

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/terraform-linters/tflint-ruleset-template
module github.com/terraform-linters/tflint-ruleset-google

go 1.13

Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main

import (
"github.com/terraform-linters/tflint-ruleset-template/rules"
"github.com/terraform-linters/tflint-plugin-sdk/plugin"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-google/rules"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
RuleSet: tflint.RuleSet{
Name: "template",
Name: "google",
Version: "0.1.0",
Rules: []tflint.Rule{
rules.NewAwsInstanceExampleTypeRule(),
rules.NewGoogleComputeInstanceExampleTypeRule(),
},
},
})
Expand Down
53 changes: 0 additions & 53 deletions rules/aws_instance_example_type.go

This file was deleted.

53 changes: 53 additions & 0 deletions rules/google_compute_instance_example_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package rules

import (
"fmt"

hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)

// GoogleComputeInstanceExampleTypeRule checks whether ...
type GoogleComputeInstanceExampleTypeRule struct{}

// NewGoogleComputeInstanceExampleTypeRule returns a new rule
func NewGoogleComputeInstanceExampleTypeRule() *GoogleComputeInstanceExampleTypeRule {
return &GoogleComputeInstanceExampleTypeRule{}
}

// Name returns the rule name
func (r *GoogleComputeInstanceExampleTypeRule) Name() string {
return "google_compute_instance_example_type"
}

// Enabled returns whether the rule is enabled by default
func (r *GoogleComputeInstanceExampleTypeRule) Enabled() bool {
return true
}

// Severity returns the rule severity
func (r *GoogleComputeInstanceExampleTypeRule) Severity() string {
return tflint.ERROR
}

// Link returns the rule reference link
func (r *GoogleComputeInstanceExampleTypeRule) Link() string {
return ""
}

// Check checks whether ...
func (r *GoogleComputeInstanceExampleTypeRule) Check(runner tflint.Runner) error {
return runner.WalkResourceAttributes("google_compute_instance", "machine_type", func(attribute *hcl.Attribute) error {
var machineType string
err := runner.EvaluateExpr(attribute.Expr, &machineType)

return runner.EnsureNoError(err, func() error {
return runner.EmitIssue(
r,
fmt.Sprintf("machine type is %s", machineType),
attribute.Expr.Range(),
tflint.Metadata{Expr: attribute.Expr},
)
})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/terraform-linters/tflint-plugin-sdk/helper"
)

func Test_AwsInstanceExampleType(t *testing.T) {
func Test_GoogleComputeInstanceExampleType(t *testing.T) {
cases := []struct {
Name string
Content string
Expand All @@ -16,24 +16,24 @@ func Test_AwsInstanceExampleType(t *testing.T) {
{
Name: "issue found",
Content: `
resource "aws_instance" "web" {
instance_type = "t2.micro"
resource "google_compute_instance" "vm_instance" {
machine_type = "f1-micro"
}`,
Expected: helper.Issues{
{
Rule: NewAwsInstanceExampleTypeRule(),
Message: "instance type is t2.micro",
Rule: NewGoogleComputeInstanceExampleTypeRule(),
Message: "machine type is f1-micro",
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 3, Column: 21},
End: hcl.Pos{Line: 3, Column: 31},
Start: hcl.Pos{Line: 3, Column: 20},
End: hcl.Pos{Line: 3, Column: 30},
},
},
},
},
}

rule := NewAwsInstanceExampleTypeRule()
rule := NewGoogleComputeInstanceExampleTypeRule()

for _, tc := range cases {
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
Expand Down

0 comments on commit 725f7c4

Please sign in to comment.