generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds a rule to check that the name of a windows virtual machine is valid #332
Merged
wata727
merged 3 commits into
terraform-linters:master
from
JannoTjarks:feature/invalid-name-windows-server
May 12, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
98dcace
Adds a rule to check that the name of a windows virtual machine is valid
JannoTjarks 12e7899
Adds azurerm_windows_virtual_machine_invalid_name to doc_README.md.tmpl
JannoTjarks 413c83e
fix(azurerm_windows_virtual_machine_invalid_name): corrects regex ins…
JannoTjarks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
docs/rules/azurerm_windows_virtual_machine_invalid_name.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# azurerm_windows_virtual_machine_invalid_name | ||
|
||
Warns about values that appear to be invalid based on [Naming rules and restrictions for Azure resources](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules#microsoftcompute). | ||
|
||
In this rule, the string must match the regular expression `^[a-zA-Z0-9]{0,1}[a-zA-Z0-9]{0,13}[a-zA-Z0-9]$`. | ||
|
||
## Example | ||
|
||
```hcl | ||
resource "azurerm_windows_virtual_machine" "foo" { | ||
name = ... // invalid value | ||
} | ||
``` | ||
|
||
``` | ||
$ tflint | ||
1 issue(s) found: | ||
|
||
Error: "..." does not match valid pattern ^[a-zA-Z0-9]{0,1}[a-zA-Z0-9]{0,13}[a-zA-Z0-9]$ (azurerm_windows_virtual_machine_invalid_name) | ||
|
||
on template.tf line 2: | ||
2: name = ... // invalid value | ||
|
||
``` | ||
|
||
## Why | ||
|
||
Requests containing invalid values will return an error when calling the API by `terraform apply`. | ||
|
||
## How To Fix | ||
|
||
Replace the warned value with a valid value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package rules | ||
|
||
import ( | ||
"fmt" | ||
"github.com/terraform-linters/tflint-plugin-sdk/hclext" | ||
"github.com/terraform-linters/tflint-plugin-sdk/tflint" | ||
"github.com/terraform-linters/tflint-ruleset-azurerm/project" | ||
"regexp" | ||
) | ||
|
||
// AzurermWindowsVirtualMachineInvalidNameRule checks the hostname is valid | ||
type AzurermWindowsVirtualMachineInvalidNameRule struct { | ||
tflint.DefaultRule | ||
|
||
pattern *regexp.Regexp | ||
resourceType string | ||
attributeName string | ||
attributeComputerName string | ||
} | ||
|
||
// NewAzurermWindowsVirtualMachineInvalidNameRule returns new rule with default attributes | ||
func NewAzurermWindowsVirtualMachineInvalidNameRule() *AzurermWindowsVirtualMachineInvalidNameRule { | ||
return &AzurermWindowsVirtualMachineInvalidNameRule{ | ||
resourceType: "azurerm_windows_virtual_machine", | ||
attributeName: "name", | ||
attributeComputerName: "computer_name", | ||
pattern: regexp.MustCompile(`^[a-zA-Z0-9]{0,1}[a-zA-Z0-9-]{0,13}[a-zA-Z0-9]$`), | ||
} | ||
} | ||
|
||
// Name returns the rule name | ||
func (r *AzurermWindowsVirtualMachineInvalidNameRule) Name() string { | ||
return "azurerm_windows_virtual_machine_invalid_name" | ||
} | ||
|
||
// Enabled returns whether the rule is enabled by default | ||
func (r *AzurermWindowsVirtualMachineInvalidNameRule) Enabled() bool { | ||
return true | ||
} | ||
|
||
// Severity returns the rule severity | ||
func (r *AzurermWindowsVirtualMachineInvalidNameRule) Severity() tflint.Severity { | ||
return tflint.ERROR | ||
} | ||
|
||
// Link returns the rule reference link | ||
func (r *AzurermWindowsVirtualMachineInvalidNameRule) Link() string { | ||
return project.ReferenceLink(r.Name()) | ||
} | ||
|
||
// Check checks the name is valid | ||
func (r *AzurermWindowsVirtualMachineInvalidNameRule) Check(runner tflint.Runner) error { | ||
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ | ||
Attributes: []hclext.AttributeSchema{ | ||
{Name: r.attributeName}, | ||
{Name: r.attributeComputerName}, | ||
}, | ||
}, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, resource := range resources.Blocks { | ||
_, exists := resource.Body.Attributes[r.attributeComputerName] | ||
if exists { | ||
continue | ||
} | ||
|
||
primaryAttribute, exists := resource.Body.Attributes[r.attributeName] | ||
if !exists { | ||
continue | ||
} | ||
|
||
err := runner.EvaluateExpr(primaryAttribute.Expr, func(val string) error { | ||
if !r.pattern.MatchString(val) { | ||
runner.EmitIssue( | ||
r, | ||
fmt.Sprintf(`"%s" does not match valid pattern %s`, val, `^[a-zA-Z0-9]{0,1}[a-zA-Z0-9]{0,13}[a-zA-Z0-9]$`), | ||
JannoTjarks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
primaryAttribute.Expr.Range(), | ||
) | ||
} | ||
return nil | ||
}, nil) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
82 changes: 82 additions & 0 deletions
82
rules/azurerm_windows_virtual_machine_invalid_name_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package rules | ||
|
||
import ( | ||
"testing" | ||
|
||
hcl "github.com/hashicorp/hcl/v2" | ||
"github.com/terraform-linters/tflint-plugin-sdk/helper" | ||
) | ||
|
||
func Test_AzurermWindowsVirtualMachineInvalidName(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Content string | ||
Expected helper.Issues | ||
}{ | ||
{ | ||
Name: "InvalidName - Ends with hyphen", | ||
Content: ` | ||
resource "azurerm_windows_virtual_machine" "vm" { | ||
name = "dummy-" | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewAzurermWindowsVirtualMachineInvalidNameRule(), | ||
Message: `"dummy-" does not match valid pattern ^[a-zA-Z0-9]{0,1}[a-zA-Z0-9]{0,13}[a-zA-Z0-9]$`, | ||
Range: hcl.Range{ | ||
Filename: "resource.tf", | ||
Start: hcl.Pos{Line: 3, Column: 10}, | ||
End: hcl.Pos{Line: 3, Column: 18}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "InvalidName - too long", | ||
Content: ` | ||
resource "azurerm_windows_virtual_machine" "vm" { | ||
name = "dummyhostname123" | ||
}`, | ||
Expected: helper.Issues{ | ||
{ | ||
Rule: NewAzurermWindowsVirtualMachineInvalidNameRule(), | ||
Message: `"dummyhostname123" does not match valid pattern ^[a-zA-Z0-9]{0,1}[a-zA-Z0-9]{0,13}[a-zA-Z0-9]$`, | ||
Range: hcl.Range{ | ||
Filename: "resource.tf", | ||
Start: hcl.Pos{Line: 3, Column: 10}, | ||
End: hcl.Pos{Line: 3, Column: 28}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "ValidName - Name is valid", | ||
Content: ` | ||
resource "azurerm_windows_virtual_machine" "vm" { | ||
name = "dummyhostname1" | ||
}`, | ||
Expected: helper.Issues{}, | ||
}, | ||
{ | ||
Name: "ValidName - Hostname is specified", | ||
Content: ` | ||
resource "azurerm_windows_virtual_machine" "vm" { | ||
name = "dummy-" | ||
computer_name = "dummyhostname1" | ||
}`, | ||
Expected: helper.Issues{}, | ||
}, | ||
} | ||
|
||
rule := NewAzurermWindowsVirtualMachineInvalidNameRule() | ||
|
||
for _, tc := range cases { | ||
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) | ||
|
||
if err := rule.Check(runner); err != nil { | ||
t.Fatalf("Unexpected error occurred: %s", err) | ||
} | ||
|
||
helper.AssertIssues(t, tc.Expected, runner.Issues) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are probably referring to the automatically generated rule docs, but we recommend that you write an actual value (e.g.
dummy-
) if possible. Because it's easier for users to understand.It would also be a good idea to replace "How to Fix" with the actual value.