Skip to content

Proposal for supporting versions of the tflint-ruleset-azurerm #64

@hattan

Description

@hattan

Hi @wata727 and @bendrucker ! Here is a proposal for adding support for versioning rules and allowing users to specify a rule version specific to the version for AzureRM terraform Provider.

Our team would be happy to help out with development and contribute to the project, I wanted to open the discussion and gather your thoughts.

Summary

This proposal is to add support for versioning and specifically pinning tflint rulesets to Terraform Provider versions and also API versions.

In the case of the TFlint ruleset for AzureRM, this would version the Terraform AzureRM provider against the version of the OpenApi Spec for the ARM Apis.

Currently a terraform resource is mapped to the ARM api version via a mapping file that is manually created.


Approach 1 - Applying version numbers to existing manual mapping structure.

  • tools/apispec-rule-gen/mappings could become tools/apispec-rule-gen/mappings-$PROVIDER_VERSION
  • Schema would reside in schema-$PROVIDER_VERSION
  • The tool will need to generate
    • docs-$PROVIDER_VERSION/rules
    • rules-$PROVIDER_VERSION/apispec
  • Manually created rules could live in rules-$PROVIDER_VERSION/

As part of the CI/CD build, the version could be appended and attached to the final build artifact.


Approach 2 - Generating Mappings by inspection of AzureRM Source.

In this approach, rather than a manual static mapping. An automated process could clone the AzureRM repo and locate api information.

For example:
Assuming we would like to find the ARM api for azurerm_analysis_services_server.

  • git clone git@github.com:terraform-providers/terraform-provider-azurerm.git

  • git fetch && git fetch --tags

  • git checkout v2.27.0

    This can be read automatically from provider.tf via something like:

    providerVersion=$(cat provider.tf | grep version)
    providerVersion="v"$(echo "$providerVersion" | cut -d'"' -f 2)
  • grep -Ril "${resourceName}" . --include=*.go --exclude=*_test.go --exclude=*registration.go Find Go Source file for resource.

  • go list -json -f "{{.ImportPath}} {{.Imports}}" github.com/terraform-providers/terraform-provider-azurerm/$resourceFolder | jq -r '.Imports[] | select(. | contains("azure-sdk-for-go"))'
    popd

A basic implementation of this would be:

#!/usr/bin/env bash

declare version=""
declare resourceName=""
declare DEBUG_FLAG=false

while [[ "$#" -gt 0 ]]
do
  case $1 in
    -r | --resource)
      resourceName=$2
      ;;

    -d | --debug )             
      DEBUG_FLAG=true
      ;; 
  esac
  shift
done

providerVersion=$(cat provider.tf | grep version)
providerVersion="v"$(echo "$providerVersion" | cut -d'"' -f 2)
echo "Found Provider version: $providerVersion"

if [ ! -d ./terraform-provider-azurerm ]; then
  git clone git@github.com:terraform-providers/terraform-provider-azurerm.git
fi

pushd terraform-provider-azurerm
  git fetch && git fetch --tags
  git checkout $version

  resourceFile=$(grep -Ril "${resourceName}" . --include=*.go --exclude=*_test.go --exclude=*registration.go)
  resourceFolder="${resourceFile%/*}/"
  go list -json -f "{{.ImportPath}} {{.Imports}}" github.com/terraform-providers/terraform-provider-azurerm/$resourceFolder | jq -r '.Imports[]  | select(. | contains("azure-sdk-for-go"))'
 popd

Deployment of Rules.

We'd also like to propose a mechanism for easily loading up cloud specific rulesets. In the case of AzureRM, perfhaps adding a tflint command to load pluging rulesets.

tflint -configure-plugin=azurerm

Which would then fetch the pluging from a blob storage account and configure it locally for the end user. As part of the CI/CD process for building the ruleset, the compiled binary could be pushed to the storage account.

Let me know your thoughts and we'd love to put together a quick spike to share with you when ready.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions