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 NatGateway data source #1294

Merged
merged 39 commits into from
Oct 26, 2017
Merged

Add NatGateway data source #1294

merged 39 commits into from
Oct 26, 2017

Conversation

shadycuz
Copy link
Contributor

I never used go and I'm not familiar with the inter workings of terraform but I do need this data source to fix #1293 and #1238. I had issues with getting this to compile so I could not run tests (fat chance they would have passed). Looking for help on how to proceed.

Thanks,
Levi

levi@La-Tower:~/repos/terraform-provider-aws$ make build
==> Checking that code complies with gofmt requirements...
gofmt needs running on the following files:
./aws/data_source_aws_nat_gateway.go
./aws/data_source_aws_nat_gateway_test.go
You can use the command: `make fmt` to reformat code.
GNUmakefile:30: recipe for target 'fmtcheck' failed
make: *** [fmtcheck] Error 1
levi@La-Tower:~/repos/terraform-provider-aws$ make fmt
gofmt -w $(find . -name '*.go' |grep -v vendor)
levi@La-Tower:~/repos/terraform-provider-aws$ make build
==> Checking that code complies with gofmt requirements...
go install
main.go:4:2: cannot find package "github.com/hashicorp/terraform/plugin" in any of:
        /usr/local/go/src/github.com/hashicorp/terraform/plugin (from $GOROOT)
        /home/levi/work/src/github.com/hashicorp/terraform/plugin (from $GOPATH)
main.go:5:2: cannot find package "github.com/terraform-providers/terraform-provider-aws/aws" in any of:
        /usr/local/go/src/github.com/terraform-providers/terraform-provider-aws/aws (from $GOROOT)
        /home/levi/work/src/github.com/terraform-providers/terraform-provider-aws/aws (from $GOPATH)
GNUmakefile:7: recipe for target 'build' failed
make: *** [build] Error 1

I'm guessing I can build that dir tree and clone those repos into it?

@shadycuz
Copy link
Contributor Author

Realized I will need to return these values...

                <item>
                    <networkInterfaceId>eni-00e37850</networkInterfaceId>
                    <publicIp>198.18.125.129</publicIp>
                    <allocationId>eipalloc-37fc1a52</allocationId>
                    <privateIp>10.0.2.147</privateIp>
                </item>

Is it against best practice to use your travis as a debugger?

@shadycuz
Copy link
Contributor Author

This needs thoroughly tested and reviewed but we might have a working PR. Wish I could test on my local machine in Terraform. Instructions on how would be greatly appreciated.

@shadycuz
Copy link
Contributor Author

I will update aws/provider.go and the website documentation tomorrow.

@clintoncwolfe
Copy link

Here's how I am locally testing.

  1. Install go and set GOPATH, per golang.org
  2. Per https://github.com/terraform-providers/terraform-provider-aws#developing-the-provider , I cloned the AWS provider under the GOPATH, from the terraform-providers repo.
  3. In that local repo, I then added your repo as a remote, and pulled your branch.
  4. I set my AWS... environment variables to an empty test account.
  5. I ran the acceptance tests using
make testacc TEST=./aws TESTARGS="-run=TestAccDataSourceAwsNatGateway"

I am running terraform 0.9.latest; but I do also have terraform 0.10.0-dev (which is master). The provider docs say 0.10 is required. That hasn't bit me yet - other things have.

@shadycuz
Copy link
Contributor Author

shadycuz commented Aug 1, 2017 via email

@clintoncwolfe
Copy link

It's close - missing some pieces in the acctest config; a NGW resource requires a subnet, so that means a data lookup for a subnet; which likely means a data lookup for the default VPC... I may look around at other acctests and see how they handled that - not sure we should assume a default VPC is present, or just make what we need. Probably the latter.

Also I've added it to the data source list for the provider, but it's not picking it up. I imagine that is something silly. I'll put another hour or two into it tonight, then open a PR on your repo from my fork.

[clinton@Carol terraform-provider-aws]$ make testacc TEST=./aws TESTARGS="-run=TestAccDataSourceAwsNatGateway"
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccDataSourceAwsNatGateway -timeout 120m
=== RUN   TestAccDataSourceAwsNatGateway
--- FAIL: TestAccDataSourceAwsNatGateway (1.36s)
	testing.go:428: Step 0 error: Configuration is invalid.

		Warnings: []string(nil)

		Errors: []string{"aws_nat_gateway.test: \"allocation_id\": required field is not set", "aws_nat_gateway.test: \"subnet_id\": required field is not set", "aws_nat_gateway.test: : invalid or unknown key: tags", "data.aws_nat_gateway.test_by_id: Provider doesn't support data source: aws_nat_gateway", "data.aws_nat_gateway.test_by_tags: Provider doesn't support data source: aws_nat_gateway"}
FAIL
exit status 1
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	1.388s
make: *** [testacc] Error 1

@shadycuz
Copy link
Contributor Author

shadycuz commented Aug 1, 2017 via email

@shadycuz
Copy link
Contributor Author

@Ninir @radeksimko When will this be reviewed by a team member and merged?

@ewbankkit
Copy link
Contributor

You can now add "tags": tagsSchemaComputed() to the schema following merge of #1625.

@ewbankkit
Copy link
Contributor

Adding back

			"network_interface_id": {
				Type:     schema.TypeString,
				Computed: true,
			},

would help with #1238 I think.

Copy link
Contributor

@Ninir Ninir left a comment

Choose a reason for hiding this comment

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

Hey @shadycuz

This seems very good to me! 👍

$ make testacc TEST=./aws TESTARGS='-run=TestAccDataSourceAwsNatGateway'     
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccDataSourceAwsNatGateway -timeout 120m
=== RUN   TestAccDataSourceAwsNatGateway
--- PASS: TestAccDataSourceAwsNatGateway (212.44s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	212.472s

Just left a few cosmetic changes, and a request to add tags handling.

Thanks a lot for the work! 👍

Optional: true,
Computed: true,
},
"filter": ec2CustomFiltersSchema(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add the tags part, following @ewbankkit 's comment?

func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

log.Printf("[DEBUG] Reading NAT Gateways.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move this above line 85 and update it using something like:

log.Printf("[DEBUG] Reading NAT Gateway: %s", req)

This will print every parameters, for a better debugging :)

@@ -140,6 +140,9 @@
<li<%= sidebar_current("docs-aws-datasource-kms-secret") %>>
<a href="/docs/providers/aws/d/kms_secret.html">aws_kms_secret</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-nat-gateway") %>>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove the extra plus signs on the lines below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They have been removed

Provides details about a specific Nat Gateway
---

# aws\_nat\_gateway
Copy link
Contributor

Choose a reason for hiding this comment

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

Antislashes are not needed anymore in titles, as per one of @sethvargo 's contributions :)

data "aws_nat_gateway" "default" {

subnet_id = "${var.subnet_id}"

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove additional spaces also? will make it easier to read

Nat Gateway in the current region. The given filters must match exactly one
Nat Gateway whose data will be exported as attributes.

* `nat_gateway_id` - (Optional) The id of the specific Nat Gateway to retrieve.
Copy link
Contributor

Choose a reason for hiding this comment

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

You can name it id indeed

* `subnet_id` - (Optional) The id of subnet that the Nat Gateway resides in.

* `vpc_id` - (Optional) The id of the VPC that the Nat Gateway resides in.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove additional spaces between list items? thanks 😄

@Ninir Ninir added the waiting-response Maintainers are waiting on response from community or contributor. label Sep 28, 2017
@shadycuz
Copy link
Contributor Author

shadycuz commented Oct 4, 2017

@Ninir Waiting for time ;) maybe this weekend

@Ninir
Copy link
Contributor

Ninir commented Oct 18, 2017

Hey @shadycuz

Do you think you can find time for that, or do you mind someone to take over this PR?

@shadycuz
Copy link
Contributor Author

@Ninir I'll start pushing updates tonight

@shadycuz
Copy link
Contributor Author

@Ninir Updated

@shadycuz
Copy link
Contributor Author

@ewbankkit Can you explain your comment? #1294 (comment)
you linked back to my own issue ;) which is already fixed by this PR.

@ewbankkit
Copy link
Contributor

@shadycuz I was commenting that the ENI ID/allocation ID of the NAT gateway should be outputs of the data source as that should address the linked-to issue.
I see that you have

  • allocated_eip_id
  • allocated_eni_id
  • allocated_private_ip
  • allocated_public_ip

as attributes but they're not declared in the data source's schema.Resource yet.
Also, do you think these attribute names should match the corresponding attribute names from the aws_nat_gateway resource?

  • allocation_id
  • network_interface_id
  • private_ip
  • public_ip

@shadycuz
Copy link
Contributor Author

@ewbankkit for the attributes I export them because if you find a match they are available, however when searching for a nat gateway you are only allowed to use certain attributes. http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-nat-gateways.html

I had thought about cheating, grabbing all natgateways in the aws account and then looping through them to find those attributes but after discussing it with someone else we decided to leave as is for now.

Yes I think the names should match what is in the resources, I will fix any that are different.

@shadycuz
Copy link
Contributor Author

@ewbankkit updated attribute names to match the resource.

@Ninir Ready =)

@Ninir Ninir removed the waiting-response Maintainers are waiting on response from community or contributor. label Oct 26, 2017
Copy link
Contributor

@Ninir Ninir left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the work @shadycuz! :)

$ make testacc TEST=./aws TESTARGS='-run=TestAccDataSourceAwsNatGateway'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccDataSourceAwsNatGateway -timeout 120m
=== RUN   TestAccDataSourceAwsNatGateway
--- PASS: TestAccDataSourceAwsNatGateway (216.63s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	216.664s

@Ninir Ninir merged commit ab9e3e6 into hashicorp:master Oct 26, 2017
@shadycuz
Copy link
Contributor Author

shadycuz commented Oct 26, 2017

Thanks @Ninir , looking forward to seeing it in a release!

Thanks @clintoncwolfe


for _, address := range ngw.NatGatewayAddresses {
if *address.AllocationId != "" {
d.Set("allocation_id", address.AllocationId)
Copy link
Contributor

Choose a reason for hiding this comment

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

@Ninir I tried using this data source and found these attributes are not being included in the output. Running terraform with DEBUG logging I see this warning:

2017/11/07 13:04:19 [WARN] Output interpolation "private_ip" failed: Resource 'data.aws_nat_gateway.default' does not have attribute 'private_ip' for variable 'data.aws_nat_gateway.default.private_ip'

I do see the values in the AWS response logged by Terraform:

2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4: <DescribeNatGatewaysResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:     <requestId>1aa2f2a0-c316-4b83-808b-7402d5cfaa32</requestId>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:     <natGatewaySet>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:         <item>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:             <createTime>2017-11-03T23:34:36.000Z</createTime>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:             <natGatewayAddressSet>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:                 <item>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:                     <allocationId>eipalloc-xxxxxx</allocationId>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:                     <networkInterfaceId>eni-xxxxxx</networkInterfaceId>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:                     <privateIp>172.20.8.29</privateIp>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:                     <publicIp>34.237.xyz.xyz</publicIp>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:                 </item>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:             </natGatewayAddressSet>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:             <natGatewayId>nat-xxxxxxxx</natGatewayId>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:             <state>available</state>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:             <subnetId>subnet-xxxxxxxx</subnetId>
2017-11-07T13:04:17.147-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:             <vpcId>vpc-xxxxxxxx</vpcId>
2017-11-07T13:04:17.148-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:         </item>
2017-11-07T13:04:17.148-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4:     </natGatewaySet>
2017-11-07T13:04:17.148-0800 [DEBUG] plugin.terraform-provider-aws_v1.2.0_x4: </DescribeNatGatewaysResponse>

Does the list of output-able values have to be included in the Schema definition? https://github.com/terraform-providers/terraform-provider-aws/pull/1294/files#diff-5264f6e572425a4527111c5172455cf6R16

Copy link
Contributor

Choose a reason for hiding this comment

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

Opened #2209

@ghost
Copy link

ghost commented Apr 10, 2020

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!

@ghost ghost locked and limited conversation to collaborators Apr 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. new-data-source Introduces a new data source.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wanted - aws_nat_gateway datasource
6 participants