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

terraform modules command #35884

Merged
merged 10 commits into from
Oct 28, 2024
Merged

terraform modules command #35884

merged 10 commits into from
Oct 28, 2024

Conversation

sebasslash
Copy link
Contributor

@sebasslash sebasslash commented Oct 21, 2024

This PR implements the terraform modules -json command which provides a full view of all configuration declared modules (local, registry, remote) in a working directory (under .terraform/). To do so, this command will read the internal module manifest and subsequently traverse the module tree to find references for each entry in the manifest. If a reference is found, which are unique per module caller, the entry will be added to the final result. Currently the internal module manifest, which can be found ./.terraform/modules/modules.json, was an implementation detail and not guaranteed to have a fixed public interface. This command creates a public, versioned interface for that manifest.

Currently this command only supports machine-readable output and thus the -json flag will be required. At a later point a human readable format will be supported.

This command also does not display the root module entry from the list of modules as it is redundant information & never directly referenced in Terraform configuration.

Target Release

1.10.0

Draft CHANGELOG entry

NEW FEATURES

  • New Terraform command terraform modules -json: Displays a full list of all installed modules in a working directory, including whether each module is currently referenced by the working directory's Terraform configuration.

@brandonc
Copy link
Contributor

brandonc commented Oct 22, 2024

This command json doesn't quite have the same experience as the other json view commands. We should be very careful with the schema since it effectively becomes public API. To that end, we could define the JSON schema in https://github.com/hashicorp/terraform-json

At a minimum, it would need a schema version string and probably some lower case keys. I'm also unsure of the "dir" attribute and think it should be more (like source refs in diags) or removed altogether.

Copy link
Contributor

@uturunku1 uturunku1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! I have been smoke testing and I really like the behavior when there are no modules, when there is config parsing error, etc.
The output itself, as we have discussed in the project Slack channel, can be improved a little to, for example, not include dir field and include other fields seen in terraform providers schema -json and other json outputs.
I have dropped other comments in the code as well.

internal/command/modules.go Show resolved Hide resolved
internal/command/modules.go Outdated Show resolved Hide resolved
internal/moduleref/resolver.go Outdated Show resolved Hide resolved
internal/moduleref/record.go Outdated Show resolved Hide resolved
// appended to the manifest that records this new information.
func (r *Resolver) findAndTrimReferencedEntries(cfg *configs.Config) {
for entryKey, entry := range r.internalManifest {
for callerKey := range cfg.Module.ModuleCalls {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance we could get to this point and cfg or cfg.Module is nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command will ensure that configuration is loaded & correct before attempting to resolve module references. Each configs.Config object represents the Terraform configuration for a single module. The Module field, holds data about resources, variables, module blocks, defined in that configuration. While configuration can be empty, this field will never be nil.

Copy link
Contributor

@rkoron007 rkoron007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved for unblocking purposes. Added a tiny suggestion for style and some questions to expand the context for users! As always, let me know if anything doesn't feel good on these ✨

---
page_title: 'Command: modules'
description: >-
The terraform modules command prints information about all the declared
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of information?

# Command: modules

The `terraform modules` command provides a holistic view of all Terraform
modules and their resolved versions that have been declared in Terraform
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
modules and their resolved versions that have been declared in Terraform
modules and their resolved versions declared in Terraform

website/docs/cli/commands/modules.mdx Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Show resolved Hide resolved

## Usage

Usage: `terraform modules -json`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you omit the -json flag? Does it error? Or just provide json anyways? This is just me being curious 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will error similar to how terraform providers schema errors when you omit the -json flag. It will return an error stating -json is required with the command help text below it.

The modules command provides a holistic view of all installed modules
for a given root module. It also includes information on whether a
installed module is currently referenced by configuration. As of now,
this command only supports a machine-readable view format.
This commit makes several modifications to the modules command. It
downcases the field names in the resulting JSON string, adds a format
version to the result. The result of this command will now only contain
entries which have been declared in configuration, as opposed to the
internal manifest which shows all installed modules for a working dir.
Copy link
Contributor

@rkoron007 rkoron007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super small nits, but I think this looks great! Thank you for doing all this so quickly! Everything is good to go from my side with these comments ✨

website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
website/docs/cli/commands/modules.mdx Outdated Show resolved Hide resolved
@sebasslash sebasslash merged commit 8144156 into main Oct 28, 2024
6 of 7 checks passed
@sebasslash sebasslash deleted the sebasslash/modules-command branch October 28, 2024 22:10
Copy link
Contributor

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

Copy link
Contributor

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants