hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
42.76k stars 9.56k forks source link

`terraform providers schema -json` version 0 #32127

Open drandell opened 2 years ago

drandell commented 2 years ago

Terraform Version

Terraform v1.3.3
on windows_amd64
+ provider registry.terraform.io/hashicorp/google v4.42.0

Terraform Configuration Files

terraform {
  required_version = ">= 1.0.0, < 2.0.0"

  required_providers {
    google = {
      source  = "hashicorp/google"
      version = ">= 4.0.0, < 5.0.0"
    }
  }
}

Debug Output

{"format_version":"1.0","provider_schemas":{"registry.terraform.io/hashicorp/google":{"provider":{"version":0,"block":{"attributes":{"

Expected Behavior

version should be "4.41.0"

Actual Behavior

Schema.json contains all of the resource information but critically, I can't tell what version of the provider the schema was generated from and the version field is just set to 0

Steps to Reproduce

terraform init terraform providers schema -json > schema.json

Additional Context

No response

References

No response

apparentlymart commented 2 years ago

Hi @drandell,

The version number you've found here is the version number of the schema for that particular configuration block, rather than the version of the provider. Provider schema elements have separate version numbers from the providers themselves because this allows the schema for each resource type and for the provider configuration itself to evolve independently. Therefore the schema JSON you've included here is working as designed.

The provider's own version number isn't included in its schema because a provider doesn't typically know its own version number: that's an artifact of its packaging and metadata in the registry rather than of the plugin executable itself.

Can you say a little more about what you intend to do with this information? It may be possible to get the information you need in a different way. Otherwise, we can turn this into a feature request representing whatever use-case you have. Thanks!

drandell commented 2 years ago

Hi, @apparentlymart thanks for replying so quickly :) My plan is to query the provider schema to determine the available resources, I'm working on an application for managed terraform via GUI. As a bit of added context, I thought knowing what provider version was used to determine the available resources would be helpful for point-in-time comparisons later on i.e. I've queried and found XY resources but later on found XYZ because XY was ran with provider version 40 but now it's 41.

I had thought of querying the .terraform.lock.hcl file as that contains the version information but it doesn't feel quite right.

apparentlymart commented 2 years ago

Hi @drandell! Thanks for the additional context.

I think the best machine-readable integration point for that at the moment would be to run terraform version -json and refer to the provider_selections property, which is a mapping from provider address to version number. This command will return a JSON equivalent of the human-oriented information you can see from terraform version.

This command is actually just directly interpreting the content of .terraform.lock.hcl and returning it, because that is Terraform's source of record about which version of each provider is selected. That means you will need to have an up-to-date lock file before running it, which you can achieve by running terraform init. I assume you must already be doing this anyway because it would not be possible to request the schemas for the providers if you hadn't already installed them.