boltops-tools / rspec-terraspace

Terraspace RSpec support
Apache License 2.0
2 stars 2 forks source link

`state_resource` helper can't return specific resources #13

Open alexjfisher opened 1 year ago

alexjfisher commented 1 year ago

This helper only seems to be usable in a very limited number of cases.

For example, if called with the name of a module as shown in the example here, it'll return the first resource in that module only.

I've spent some time trying to work on an improved version, but have got a bit stuck.

The code currently relies on terraform state pull and redirects the output into a file that is then parsed. This state doesn't contain the resource addresses and this makes it harder to extract specific resources.

If we used terraform show -json, the output will contain resource addresses that the helper could use instead. The output format is documented here.

And this is where I'm getting stuck. run("show #{@mod.name} -json > #{state_path}") doesn't behave the same as run("state pull #{@mod.name} > #{state_path}") The output doesn't get redirected to the file and gets displayed in the terminal only (leaving an empty file).

I'm assuming this is something to do with how terraspace show and terraspace state are implemented differently here?? (state has its own subclass)

Unfortunately, terraform show doesn't have any -out option, so redirecting the output (or consuming it directly) is the only option. I think it is the correct interface to terraform though. From the docs terraform state show docs...

The output of terraform state show is intended for human consumption, not programmatic consumption. To extract state data for use in other software, use terraform show -json and decode the result using the documented structure.

If I could get this command to 'work' and capture the output via terraspace then I'm happy to work on updating and testing a new (or updated) state_resource helper.