Categorygithub.com/IBM-Cloud/terraform-config-inspect
modulepackage
1.0.0-beta4
Repository: https://github.com/ibm-cloud/terraform-config-inspect.git
Documentation: pkg.go.dev

# README

terraform-config-inspect

This repository contains a helper library for extracting high-level metadata about Terraform modules from their source code and also provider schema metadata for respective variables defined for IBM-Cloud resources. It processes only a subset of the information Terraform itself would process, and in return it's able to be broadly compatible with modules written for many different versions of Terraform (>=1.0).

Background

This tool has been enhanced to do the following:

  • augment the variable metadata (in the Terraform module or template), with the metadata of the correspondng variables, extracted from the Terraform provider.
  • override the variable metadata with the user-defined metadata of the corresponding variables in the imported modules

Inputs

This tool uses the following inputs (optional)

  • Terraform Provider metadata (eg. IBM Cloud Provider metadata) in json format.
  • Terraform Module metadata in json format

Note:

Variable metadata extracted by this tool

NameTypeDescription
namestringVariable name
typestringData type of the variable
descriptionstringDescription of the variable
defaultboolDefault value of the variable
requiredboolWhether the variable is required
sensitiveboolWhether the variable contains credentials, secrets, or other sensitive values
sourcestringSource identifier of the module in the form <resource/data_source/module_name>.<resource/data_source/module_identifier>
posobject{filename:"path/to/file/name",line:line number}position of the variable in the template
aliaseslist(string)The list of aliases for the variable name
cloud_data_typestringThe type of IBM Cloud data. Allowable values: Region, ResourceInstance, CRN, Tags, ResourceGroup
link_statusstringThe status of the link
immutableboolIf true, altering the value of the variable destroys and re-creates a resource (ForceNew behavior)
hiddenboolIf true, the variable is not displayed in the UI or CLI.
optionslist(string)Allowable values for the variable
min_valuestringMinimum value of a number variable. Validation is defined in the provider
max_valuestringMaximum value of number variable. Validation is defined in the provider
min_value_lengthintThe minimum length of a string variable. Validation is defined in the provider.
max_value_lengthintThe maximum length of a string variable. Validation is defined in the provider.
matchesstringThe regular expression (regex) for the variable value.
optionalboolWhether the variable is optional (Optional behavior)
computedboolWhether the variable is computed or derived (Computed behavior)
elemprovider schema structChild arguments of complex variable types
max_itemsintMaximum number of items for a list or set variable. Validation is defined in the provider schema.
min_itemsintMinimum number of items for a list or set variable. Validation is defined in the provider schema.
deprecatedboolWhether the variable is deprecated in the provider schema.
cloud_data_rangestringThe range of IBM Cloud data for the CloudDataType. For the ResourceInstance data type, the format is ["service:", ":"].

Install

The releases for terraform-config-inspect can be found here - https://github.com/hashicorp/terraform-config-inspect/releases

You can install terraform-config-inspect using the following commands:

$ go get github.com/ibm-cloud/terraform-config-inspect

You can also build and install the latest version of terraform-config-inspect :

  1. Clone Repo
    git clone [email protected]:IBM-Cloud/terraform-config-inspect.git
    
  2. Run go build on this repo.

    It generates binary in current working directory.

    Add this binary to GOPATH to access from any location.


Usage

The primary way to use this terraform-config-inspect CLI tool is as follows:

Usage 1: Print output in console

$ terraform-config-inspect path/to/module
Console output
```markdown
# Module `path/to/module`

Provider Requirements:
* **null:** (any version)

## Input Variables
* `a` (default `"a default"`)
* `b` (required): The b variable

## Output Values
* `a`
* `b`: I am B

## Managed Resources
* `null_resource.a` from `null`
* `null_resource.b` from `null`
```

Usage 2: Print JSON output in console

$ terraform-config-inspect path/to/module --json
JSON output
```json
{
  "path": "path/to/module",
  "variables": {
    "A": {
      "name": "A",
      "default": "A default",
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 1
      }
    },
    "B": {
      "name": "B",
      "description": "The B variable",
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 5
      }
    }
  },
  "outputs": {
    "A": {
      "name": "A",
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 9
      }
    },
    "B": {
      "name": "B",
      "description": "I am B",
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 13
      }
    }
  },
  "required_providers": {
    "null": []
  },
  "managed_resources": {
    "null_resource.A": {
      "mode": "managed",
      "type": "null_resource",
      "name": "A",
      "provider": {
        "name": "null"
      },
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 18
      }
    },
    "null_resource.B": {
      "mode": "managed",
      "type": "null_resource",
      "name": "B",
      "provider": {
        "name": "null"
      },
      "pos": {
        "filename": "path/to/module/basics.tf",
        "line": 19
      }
    }
  },
  "data_resources": {},
  "module_calls": {}
}
```

Usage 3: Annotate with provider metadata

$ terraform-config-inspect path/to/module --json --metadata path/to/provider-metadata-file

Use the --metadata flag to specify the location of the IBM Cloud provider metadata json file.

NOTE: If you have any module reference in your input template, Run terraform init on your template before using this CLI

Usage 4: Output variable metadata

$ terraform-config-inspect path/to/module --json --filter-variables 

Use the --filter-variables flag include variables in the output metadata file

  • This tool doesn't extract provider metadata of other cloud providers like AWS, Azure, GCP etc. while it doesn't fail when these providers are used. It gives high level template metadata as usual for non IBM-Cloud providers.
$ terraform-config-inspect --json path/to/module --metadata path/to/provider-metadata-file --filter-variables
Output variable metadata in JSON format
```json
{
    "A": {
      "name": "A",
      "default": "A default",
      "min_length": 1,
      "max_length": 63,
      "matches": "^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$",
      "optional": true,
      "computed": true
    },
    "B": {
      "name": "B",
      "description": "The B variable",
      "default": "bx2.4x16",
      "required": true,
      "immutable": true
    }
}
```

Next steps

  1. Extract template level variable validation.
  2. Extract locals block metadata.

# Packages

Package tfconfig is a helper library that does careful, shallow parsing of Terraform modules to provide access to high-level metadata while remaining broadly compatible with configurations targeting various different Terraform versions.