-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Issue #3984 New Datasource: aws_vpcs #4736
Conversation
$ make testacc TEST=./aws/ TESTARGS='-run=TestAccDataSourceAwsVpcIDs_' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @saravanan30erd 👋 Thanks for submitting this! I left you some initial feedback below. Please take a look and let us know if you have any questions or do not have time to implement the feedback.
aws/data_source_aws_vpc_ids_test.go
Outdated
{ | ||
Config: testAccDataSourceAwsVpcIDsConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
//datasource aws_vpc_ids will list all the vpcs in a region, so it also includes the Default vpc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not look like anything is reading the filter
schema attribute into req.Filter
, which might explain the behavior you're seeing here when you have an invalid filter in the test configuration. It looks like the valid value should be vpc-id
according to the SDK documentation: https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#DescribeVpcsInput
For compatibility in all AWS accounts (which may or may not have other existing VPCs since you can delete the default VPC), I would recommend setting up the _basic
test to not have any ids/filter/tags arguments, create 1 VPC resource, add depends_on
in the data source to that VPC resource, then ensure that the value of the attribute is greater than 0. We cannot count on ids
being "3"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bflad I added the filter to req.Filter now.
Regarding acceptance tests, I am getting the plan empty error now.
testing.go:518: Step 0 error: After applying this step and refreshing, the plan was not empty:
what I am missing here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you turn on the debug logging, you should see the empty plan output. I generally don't show it during PR submission but I find it very helpful to run my acceptance testing like this:
rm -f aws/log.txt; TF_LOG_PATH=log.txt TF_LOG=debug make testacc TEST=./aws TESTARGS='-run=TestAccAWSXXX'
Which will turn on debug logging and write that output to a separate log file (aws/log.txt
) that I can inspect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bflad In acceptance test _basic
, getting the below error when I using depends_on
in data source to VPC resource.
`--- FAIL: TestAccDataSourceAwsVpcs_basic (79.22s)
testing.go:518: Step 0 error: After applying this step and refreshing, the plan was not empty:
DIFF:
CREATE: data.aws_vpcs.all
ids.#: "" => "<computed>"
tags.%: "" => "<computed>"
STATE:
aws_vpc.test-vpc:
ID = vpc-954c44ec
provider = provider.aws
assign_generated_ipv6_cidr_block = false
cidr_block = 10.0.0.0/24
default_network_acl_id = acl-61b72c19
default_route_table_id = rtb-5ff0e927
default_security_group_id = sg-29daa658
dhcp_options_id = dopt-f0038698
enable_classiclink = false
enable_classiclink_dns_support = false
enable_dns_hostnames = false
enable_dns_support = true
instance_tenancy = default
main_route_table_id = rtb-5ff0e927
tags.% = 0
FAIL
FAIL github.com/terraform-providers/terraform-provider-aws/aws 79.264s`
After destroyed the vpc resource and data source and running the plan shows the drift due to depends_on
(to vpc resource) in data source. In aws_vpc resource test data_source_aws_vpc_test.go
, TestAccDataSourceAwsVpc_basic
also fails with same error after adding depends_on
in data source. So I am just checking data source id in _basic
acceptance test here.
aws/data_source_aws_vpc_ids_test.go
Outdated
|
||
func testAccDataSourceAwsVpcIDsConfig() string { | ||
return fmt.Sprintf(` | ||
provider "aws" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declaring the "default" provider region is extraneous in test configurations and discouraged unless its absolutely necessary (e.g. EC2 Classic testing) -- its handled by the AWS_DEFAULT_REGION
environment variable (or defaults to us-west-2
) here:
aws/data_source_aws_vpc_ids_test.go
Outdated
} | ||
|
||
data "aws_vpc_ids" "selected" { | ||
filter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove this in preference of only testing tags
in this configuration 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
aws/data_source_aws_vpc_ids.go
Outdated
} | ||
|
||
d.SetId(time.Now().UTC().String()) | ||
d.Set("ids", vpcs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend adding error checking for setting a non-scalar attribute:
if err := d.Set("ids", vpcs); err != nil {
return fmt.Errorf("error setting ids: %s", err)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, can you please explain about non-scalar attribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically speaking, we should probably perform the error checking on all attributes during d.Set()
, but its quite verbose code-wise and we have found nested and list/set attributes to be the most troublesome when it comes to ensuring that the type from the API matches the type defined in the schema (e.g. incorrectly trying to put []*string
into a TypeList
with TypeString
elements). Generally its worse when there is more attribute nesting happening when you need to be very concerned about lining up []map[string]interface{}
correctly.
When this type mismatch occurs, the behavior of Terraform core is to pass the configuration through into the state as-is from the configuration and not provide any feedback about this behavior unless the error check is in place. In the situation where we don't error check and provide incompatible type(s) into d.Set()
, its not possible to detect drift from the API. It will also show as a plan difference after resource import since the state could not set the real value during the import read.
We have a flag in Terraform core to instead panic()
when it runs into this situation (you can manually enable it today with TF_SCHEMA_PANIC_ON_ERROR=1
environment variable), but I'm not sure we'll ever flip it on given the current state of many of the providers and generally poorer user experience that provides even though it would certainly make these bugs way more obvious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
aws/data_source_aws_vpc_ids.go
Outdated
vpcs := make([]string, 0) | ||
|
||
for _, vpc := range resp.Vpcs { | ||
vpcs = append(vpcs, *vpc.VpcId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent potential panics we might want to use the SDK helper function here: aws.StringValue(vpc.VpcId)
website/docs/d/vpc_ids.html.markdown
Outdated
Provides a list of VPC Ids in a region | ||
--- | ||
|
||
# Data Source: aws_vpc_ids |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This documentation page is missing a sidebar link in website/aws.erb
website/docs/d/vpc_ids.html.markdown
Outdated
} | ||
``` | ||
|
||
The primary use case would be interpolate the vpc_ids output into `count` of an aws_flow_log resource. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: We generally try not to prescribe what might be a "primary" use case -- people use Terraform in all sorts of scenarios. I'd consider saying An example use case
.
aws/data_source_aws_vpc_ids.go
Outdated
return &schema.Resource{ | ||
Read: dataSourceAwsVpcIDsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"filter": dataSourceFiltersSchema(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few items with this attribute:
- It does seem to be read into
req.Filters
so it appears to not currently be doing anything - Its missing in the data source documentation
I would recommend testing this attribute separately from the _basic
test (preferably not being passed any arguments) and the _tags
test (preferably only testing tags filtering).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its my mistake, I added the filter in last minute when I was working on test cases, then I forgot to follow on.
website/docs/d/vpc_ids.html.markdown
Outdated
|
||
# Data Source: aws_vpc_ids | ||
|
||
`aws_vpc_ids` provides a list of vpc ids |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: This sentence seems redundant with the one below. Also, the casing of VPC IDs is different between various sentences in the documentation page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
aws/provider.go
Outdated
@@ -242,6 +242,7 @@ func Provider() terraform.ResourceProvider { | |||
"aws_ssm_parameter": dataSourceAwsSsmParameter(), | |||
"aws_subnet": dataSourceAwsSubnet(), | |||
"aws_subnet_ids": dataSourceAwsSubnetIDs(), | |||
"aws_vpc_ids": dataSourceAwsVpcIDs(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend calling this data source aws_vpcs
-- we may return more than IDs in the future. Unfortunately we have some more explicitly _ids
named ones (like aws_subnet_ids
above) that eventually may need to be deprecated should we need to return information other than IDs.
8a5ae9b
to
20b3347
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates @saravanan30erd -- looks good! Let's get this in 🚀
3 tests passed (all tests)
=== RUN TestAccDataSourceAwsVpcs_basic
--- PASS: TestAccDataSourceAwsVpcs_basic (9.17s)
=== RUN TestAccDataSourceAwsVpcs_tags
--- PASS: TestAccDataSourceAwsVpcs_tags (9.47s)
=== RUN TestAccDataSourceAwsVpcs_filters
--- PASS: TestAccDataSourceAwsVpcs_filters (9.51s)
} | ||
} | ||
|
||
func testAccCheckAwsVpcsDataSourceID(n string) resource.TestCheckFunc { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nitpick: We will have the documentation up very shortly (hashicorp/terraform-website#317 😉 ), but even when we're not checking for resource/datasource API object, we prefer to name these functions testAccCheckXXXExists
for consistency. I'll adjust this on merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure 👍
This has been released in version 1.23.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
New datasource aws_vpc_ids to discover all VPC IDs(Issue #3984 ).