Saturday, August 27, 2022

Terraform quick view of resource schema

 I was getting tired of having to go to Hashicorp's website to view provider arguments lists, because SURELY the arguments list is in the code somewhere handy. As it turns out, it is.

I made a quick bash function to export the schema for a given resource provider

tfschema () {
    if [ $# -ne 2 ]
    then
        echo -e "This function requires exactly two arguments, in order: providername resourcename "
        echo "Try: tfschema  registry.terraform.io/hashicorp/vsphere vsphere_virtual_machine"
        echo "It is required to have already run terraform init with HCL for the provider used in the first argument."
        echo "Try again. You have 1 life remaining. Exiting..."
        return
    fi
    if ! which terraform >/dev/null 2>&1
    then
        echo "terraform not found. Exiting..."
        return
    fi
    
    terraform providers schema --json|jq '.provider_schemas["'"$1"'"].resource_schemas["'"$2"'"].block| .attributes, .block_types|values'
}
Quick Screenshot: