Skip to content
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

Add Rule for Deprecated partition_key_path in azurerm_cosmosdb_sql_container #356

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ These are the rules that warn against invalid values generated from [azure-rest-
|[azurerm_cosmosdb_mongo_collection_invalid_resource_group_name](rules/azurerm_cosmosdb_mongo_collection_invalid_resource_group_name.md)|✔|
|[azurerm_cosmosdb_mongo_database_invalid_account_name](rules/azurerm_cosmosdb_mongo_database_invalid_account_name.md)|✔|
|[azurerm_cosmosdb_mongo_database_invalid_resource_group_name](rules/azurerm_cosmosdb_mongo_database_invalid_resource_group_name.md)|✔|
|[azurerm_cosmosdb_sql_container_deprecated_partition_key_path](rules/azurerm_cosmosdb_sql_container_deprecated_partition_key_path.md)|✔|
|[azurerm_cosmosdb_sql_container_invalid_account_name](rules/azurerm_cosmosdb_sql_container_invalid_account_name.md)|✔|
|[azurerm_cosmosdb_sql_container_invalid_resource_group_name](rules/azurerm_cosmosdb_sql_container_invalid_resource_group_name.md)|✔|
|[azurerm_cosmosdb_sql_database_invalid_account_name](rules/azurerm_cosmosdb_sql_database_invalid_account_name.md)|✔|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# azurerm_cosmosdb_sql_container_deprecated_partition_key_path

### Description

This rule checks for the use of the deprecated `partition_key_path` attribute in `azurerm_cosmosdb_sql_container` resources. The `partition_key_path` attribute is deprecated and should be replaced with the `partition_key_paths` attribute, which supports a list of partition key paths. This update ensures compatibility with newer versions of the AzureRM provider and supports Cosmos DB's enhanced partitioning capabilities.

---

## Example

```hcl
resource "azurerm_cosmosdb_sql_container" "example" {
partition_key_path = "/deprecated_key"
}
```

### Output when inspected by TFLint:

```bash
$ tflint
resource.tf:3:24 - Warning: `partition_key_path` is deprecated and should be replaced with `partition_key_paths`.
```

---

## Why

Using deprecated attributes like `partition_key_path` can lead to future compatibility issues with the AzureRM provider as it progresses to newer versions. The `partition_key_paths` attribute offers more flexibility by supporting multiple partition key paths, which is crucial for modern Cosmos DB designs that rely on enhanced partitioning strategies.

---

## How to Fix

Replace the deprecated `partition_key_path` attribute with the `partition_key_paths` attribute. Update your resource configuration as follows:

```hcl
resource "azurerm_cosmosdb_sql_container" "example" {
partition_key_paths = ["/valid_key"]
}
```

This ensures your Terraform code is compliant with the latest version of the AzureRM provider and avoids potential errors when upgrading or using newer features of Cosmos DB.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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"
)

// NewAzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule creates a new rule to check for the deprecated attribute partition_key_path.
func NewAzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule() *AzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule {
return &AzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule{
resourceType: "azurerm_cosmosdb_sql_container",
attributeName: "partition_key_path",
}
}

// AzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule defines a rule that checks for the deprecated partition_key_path in azurerm_cosmosdb_sql_container.
type AzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule struct {
tflint.DefaultRule

resourceType string
attributeName string
}

// Name returns the name of the rule.
func (r *AzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule) Name() string {
return "azurerm_cosmosdb_sql_container_deprecated_partition_key_path"
}

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

// Severity returns the severity level of the rule.
func (r *AzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule) Severity() tflint.Severity {
return tflint.WARNING
}

// Link returns the documentation link for the rule.
func (r *AzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule) Link() string {
return project.ReferenceLink(r.Name())
}

// Check examines azurerm_cosmosdb_sql_container resources and emits a warning if the deprecated partition_key_path is used.
func (r *AzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule) Check(runner tflint.Runner) error {
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{
{Name: r.attributeName},
},
}, nil)
if err != nil {
return err
}

for _, resource := range resources.Blocks {
fmt.Printf("Processing resource: %+v\n", resource)
attribute, exists := resource.Body.Attributes[r.attributeName]
if !exists {
continue
}

fmt.Printf("Found deprecated attribute: %+v\n", attribute)

runner.EmitIssue(
r,
"`partition_key_path` is deprecated and should be replaced with `partition_key_paths`.",
attribute.Expr.Range(),
)
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package rules

import (
// to print actual issues for debugging
"testing"

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

func Test_AzurermCosmosdbSqlContainerDeprecatedPartitionKeyPath(t *testing.T) {
cases := []struct {
Name string
Content string
Expected helper.Issues
}{
{
Name: "Deprecated partition_key_path",
Content: `
resource "azurerm_cosmosdb_sql_container" "example" {
partition_key_path = "/deprecated_key"
}`,
Expected: helper.Issues{
{
Rule: NewAzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule(),
Message: "`partition_key_path` is deprecated and should be replaced with `partition_key_paths`.",
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 3, Column: 24},
End: hcl.Pos{Line: 3, Column: 41},
},
},
},
},
{
Name: "Valid partition_key_paths",
Content: `
resource "azurerm_cosmosdb_sql_container" "example" {
partition_key_paths = ["/valid_key"]
}`,
Expected: helper.Issues{}, // No issues since the correct attribute is used
},
}

rule := NewAzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule()

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)
}
}
1 change: 1 addition & 0 deletions rules/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ var Rules = append([]tflint.Rule{
NewAzurermWindowsVirtualMachineInvalidSizeRule(),
NewAzurermWindowsVirtualMachineScaleSetInvalidSkuRule(),
NewAzurermResourceMissingTagsRule(),
NewAzurermCosmosdbSQLContainerDeprecatedPartitionKeyPathRule(),
}, apispec.Rules...)
Loading