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

Add StateList #206

Closed
wants to merge 4 commits into from
Closed

Add StateList #206

wants to merge 4 commits into from

Conversation

micahkemp
Copy link

This PR adds the StateList command.

@@ -4,6 +4,16 @@ import (
"encoding/json"
)

// AddressOption represents an address.
Copy link
Author

Choose a reason for hiding this comment

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

There may be opinions regarding how this is named, or even implemented. terraform state show is documented as taking zero or more addresses as options. As such I added an AddressOption, which ends up getting applied (for StateList) as positional arguments after all the other options.

I'm more than happy to change how this is implemented if there are other suggestions.

Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

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

Thank you for this PR as well.

I have the same question for this command as in your other PR:
Could you share your use case for this command in the context of automation?

Keep in mind that this command (unlike most others we integrated) does not provide machine-readable (JSON) output and AFAIK therefore doesn't provide any compatibility guarantees for the output format.

If we have a use case though we could propose enhancement of Terraform CLI so that it does offer JSON output - this would then make the integration much easier here.

@radeksimko radeksimko added the enhancement New feature or request label Aug 10, 2021
@micahkemp micahkemp mentioned this pull request Aug 10, 2021
@micahkemp
Copy link
Author

Use case described here on #205.

I'll also mention that as these two PRs are related in terms of how I would use them, and because they're both being submitted at the same time, I'm more than happy to combine them into one. I kept them split to hopefully make them simpler to review and also to allow you flexibility in reviewing them as separate concepts.

@micahkemp
Copy link
Author

micahkemp commented Aug 11, 2021

As discussed in the comments of #205, I think it makes sense for this to wait until state list supports -json.

@radeksimko
Copy link
Member

Yep, I will be doing some exploratory work which will likely result adding JSON output for more commands and I will certainly use your use case in the process to advocate for state list -json specifically.

I will let you know here once any relevant PR is ready upstream.

Thanks.

@radeksimko radeksimko self-assigned this Nov 11, 2021
@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

@kmoe kmoe added this to the v1.0.0 milestone Jun 24, 2022
@radeksimko
Copy link
Member

As mentioned in #205 (comment) the use case can also be satisfied with existing functionality. See below.

$ terraform state list
random_pet.adam
random_pet.basil
random_pet.claire

which is equivalent to

$ terraform show -json | jq -r '.values.root_module.resources[].address'
random_pet.adam
random_pet.basil
random_pet.claire

and similarly to

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/exec"

	"github.com/hashicorp/terraform-exec/tfexec"
)

func main() {
	workDir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	execPath, err := exec.LookPath("terraform")
	if err != nil {
		log.Fatal(err)
	}

	tf, err := tfexec.NewTerraform(workDir, execPath)
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.Background()
	state, err := tf.Show(ctx)
	if err != nil {
		log.Fatal(err)
	}

	for _, resource := range state.Values.RootModule.Resources {
		fmt.Println(resource.Address)
	}
}

I hope that helps.

@radeksimko radeksimko closed this Jul 1, 2022
@skyf0cker skyf0cker mentioned this pull request Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants