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

support for data resources? #111

Closed
vrbcntrl opened this issue Jun 28, 2019 · 17 comments
Closed

support for data resources? #111

vrbcntrl opened this issue Jun 28, 2019 · 17 comments
Assignees

Comments

@vrbcntrl
Copy link
Contributor

Hi,

May I know if we have support for terraform data sources?
for example:
data "aws_autoscaling_group" "foo" { name = "foo" .......... }
is there a way to read the name or any attribute inside data block? Thanks in advance!

@eerkunt
Copy link
Member

eerkunt commented Jun 28, 2019

Technically possible. Marking as enhancement. Will have a look this weekend, hopefully.

@eerkunt eerkunt self-assigned this Jun 28, 2019
@vrbcntrl
Copy link
Contributor Author

thank you!

@eerkunt
Copy link
Member

eerkunt commented Jun 28, 2019

Could you please try your cases with 1.0.11 ?

Also please let me know about the use cases, I couldn't find much by myself really.

@eerkunt eerkunt added the waiting for confirmation Workaround/Fix applied, waiting for confirmation label Jun 28, 2019
@vrbcntrl
Copy link
Contributor Author

Thanks for the quick turnaround.
I took the aws_avaialability_zones data source example from terraform site and ran terraform plan it and generated the plan.out and plan.json, however the data.example.feature failed with Error:

Scenario: Subnet Count SKIPPING: Can not find aws_availability_zones defined in target terraform plan. Given I have aws_availability_zones defined When it contains zone_ids And I count them Then I expect the result is greater than 2

and when I see the plan.json, it actually has the data block as shown below

{ "address": "data.aws_availability_zones.available", "mode": "data", "type": "aws_availability_zones", "name": "available", "provider_config_key": "aws", "expressions": { "state": { "constant_value": "available" } }, "schema_version": 0 }

however for some reason, TC could not recognize that data block.
I have attached my test terraform file, plan.json and results screen shot for your reference.
Please let me know if am doing anything wrong here... thanks!
111.zip

@vrbcntrl
Copy link
Contributor Author

I think I found the reason why TC is not able to recognize the data block in the plan json

The hierarchy of resources for standard resource is like this, i.e. planned_values >root_module>resources as shown in below screen shot

image

However, for data resources, the hierarchy is little different, it is configuration>root_module>resources as shown in below screen shot
image

so, I changed terraform.py , line#84 inside def _parse_resources(self) function with the below code
# Resources ( exists in Plan ) for findings in seek_key_in_dict(self.raw.get('configuration', {}).get('root_module', {}), 'resources'):

then my BDD is able to recognize the data resource, however my when and Then steps were failing, so I had to change the def it_condition_contain_something(_step_obj, something): in steps.py

image

after the above changes, my BDD has passed 2 steps but I think we still need to make changes to steps.py for handling data resources as they are different from standard resources.
image

below is my data resource from plan json

image

@eerkunt
Copy link
Member

eerkunt commented Jul 1, 2019

Smashing issue report :D

Will have a look after I fix another bug that I am in the middle of it.

@eerkunt
Copy link
Member

eerkunt commented Jul 1, 2019

Could you please have a try with 1.0.15 version ?

@eerkunt eerkunt added the waiting for confirmation Workaround/Fix applied, waiting for confirmation label Jul 1, 2019
@vrbcntrl
Copy link
Contributor Author

vrbcntrl commented Jul 1, 2019

Awesome...Thanks for the quick turnaround.
However, we have some good and bad news..
Good news is, the Data Resources tests are working as expected
Bad news is, the standard resources tests are failing with errors.. the same testes were executed successfully before 1.0.15. I ran a test with a standard resources use case and it failed with errors
Example :
image

my tf has the below configuration for the 2 encryption properties

at_rest_encryption_enabled = true
transit_encryption_enabled = false

And the test gets executed successfully when I have both the above properties set to true, however when any of the properties set to false, the tool fails to read and throws AttributeError

image

Terraform configuration

image

@eerkunt eerkunt added bug and removed waiting for confirmation Workaround/Fix applied, waiting for confirmation labels Jul 2, 2019
@eerkunt
Copy link
Member

eerkunt commented Jul 2, 2019

#90

@eerkunt
Copy link
Member

eerkunt commented Jul 2, 2019

That's why we need #74 :(

Fixing this problem.

@eerkunt eerkunt mentioned this issue Jul 2, 2019
@eerkunt
Copy link
Member

eerkunt commented Jul 2, 2019

Can you please try with 1.0.16 ?

@vrbcntrl
Copy link
Contributor Author

vrbcntrl commented Jul 2, 2019

Thank you!
Its working as expected now, however the I think the Failure message is little bit confusing, pls see below

Examples:
| encryption | value |
| at_rest_encryption_enabled | true |
| transit_encryption_enabled | true |
Failure: transit_encryption_enabled property in False named aws_elasticache_replication_group resource does not match with true regex. It is set to False.

@eerkunt
Copy link
Member

eerkunt commented Jul 3, 2019

Thanks for testing. Error message will be fixed in 1.0.17 along with another fix.

@eerkunt eerkunt closed this as completed Jul 3, 2019
@andrewjkeith
Copy link

andrewjkeith commented Oct 3, 2019

So I am trying to test a data source but it keeps saying it can't find it. I am trying to test the filter conditions on the aws_ami data source.

data "aws_ami" "amazon_ami" {
  most_recent = true
  owners = ["xxxxx"]
  filter {
    name   = "name"
    values = ["test-name"]
  }
}

And my scenario is :
Scenario: AMI enforcement on EC2 instances
  Given I have aws_ami defined
  When it contains a filter
  Then it must contain name
  And its value must be name


I see the appropriate JSON like above in the plan ouput, but it still says it cant find `aws_ami`

@eerkunt
Copy link
Member

eerkunt commented Oct 4, 2019

hi @andrewjkeith,

It is better to re-open the issue or create a new issue in these situations :)

It couldn't find, because you are looking for a resource. https://terraform-compliance.com/pages/bdd-references/given.html#given-i-have-name-defined

Can you try with ;

Scenario: AMI enforcement on EC2 instances
  Given I have aws_ami data configured
  When it contains a filter
  Then it must contain name
  And its value must be name

@eerkunt eerkunt reopened this Oct 4, 2019
@andrewjkeith
Copy link

Sorry, didnt realize I could re-open issues.

But yes, thank you, that worked!

It ultimately ended up being (without the 'a' in the when):

Scenario: AMI enforcement on EC2 instances
  Given I have aws_ami data configured
  When it contains filter
  Then it must contain name
  And its value must be name

@eerkunt
Copy link
Member

eerkunt commented Oct 4, 2019

Glad that it worked! 🎉

Closing the issue, please don't hesitate to re-open this or open a new issue! :)

@eerkunt eerkunt closed this as completed Oct 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants