nbering / terraform-inventory

An Ansible dynamic inventory script to pair with nbering/terraform-provider-ansible.
MIT License
175 stars 51 forks source link

Fix issue relating to terraform v0.12.0 release #16

Closed designermonkey closed 3 years ago

designermonkey commented 5 years ago

Fixes #15

nbering commented 5 years ago

I'll look at this in the evening after my work day is complete. I would just like to run the tests and make sure this doesn't break anything.

nbering commented 5 years ago

It seems this breaks the tests for the test cases I've recorded for Terraform 0.12.x, based on the pre-release versions.

~/git/nbering/terraform-inventory [issue-15-patch L|✔]
23:34 $ bin/debug /Users/nicholasbering/git/nbering/terraform-inventory/sample_data/0.12.x/count-advanced
Traceback (most recent call last):
  File "/Users/nicholasbering/git/nbering/terraform-inventory/terraform.py", line 393, in _main
    inventory.add_resource(resource)
  File "/Users/nicholasbering/git/nbering/terraform-inventory/terraform.py", line 235, in add_resource
    self.add_host_resource(resource)
  File "/Users/nicholasbering/git/nbering/terraform-inventory/terraform.py", line 199, in add_host_resource
    hostname = resource.read_attr("inventory_hostname")
  File "/Users/nicholasbering/git/nbering/terraform-inventory/terraform.py", line 178, in read_attr
    return self._raw_attributes().get(key, None)
  File "/Users/nicholasbering/git/nbering/terraform-inventory/terraform.py", line 183, in _raw_attributes
    return self.source_json["attributes_flat"]
KeyError: 'attributes_flat'

To run the tests yourself, use the scripts under bin/run-tests, which will run all of the tests. A pass is a return code 0 with no output. A fail will say TEST FAILED: /path/to/test-case.

The tests simply diff the script output to a known good result and fail if it's not a match. It uses jq to sort the keys in the result so that the output doesn't have to be finicky, and the script sorts lists of hosts so that there shouldn't be any variability in results for the same state input.

Use bin/debug /path/to/test-case to get a side-by-side diff for the case you're having trouble with. The test doesn't run Terraform, it's using a shim to fake terraform and just spit out the recorded state file when it's called with the right arguments.

If you add or update a test case and want to record a new expects.json with the results to be compared, there's a helper at bin/update-expects which runs all the scenarios and records their results as a the new expected result.

nbering commented 5 years ago

I think I'd like to continue to support the beta statefile format, at least until I know that they aren't going to flip back again. I would probably approach this by renaming the test case folder for 0.12.x to 0.12.0-beta, copy them to a new 0.12.x, and init the new test case "projects" with 0.12.1 to get a representative sample across recent versions.

I'd be happy to help out solving this, but I usually work on this project over the weekend, so it'll be a few days before I can look at it.

jmMeessen commented 4 years ago

Hello,

As time has passed and that Terraform 0.12 got some more stability, what is the status of this PR?

/- Jmm

nbering commented 4 years ago

@jmMeessen Great question. I'll try and carve out some time to check on the stability of this project against more recent versions. As it stands, this PR is stalled as basic testing failed and the PR author never responded to my comments about that.

patsevanton commented 3 years ago

Hello, Thanks for terraform-inventory.

Terraform 0.13 released. May be merge PR?

nbering commented 3 years ago

@patsevanton Thanks for bringing this again PR to my attention. I think I'm just going to close this, as the issue it's solving (#15) is also closed. I was never able to replicate the original issue. If anyone still has this problem... I could use some help figuring out what the difference in structure of the state file is... and why yours is different from the baselines I've seen elsewhere.