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

Added public_dns and private_dns to aws_eip #7349

Merged
merged 1 commit into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion aws/data_source_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -44,11 +45,19 @@ func dataSourceAwsEip() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"private_dns": {
Type: schema.TypeString,
Computed: true,
},
"public_ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"public_dns": {
Type: schema.TypeString,
Computed: true,
},
"public_ipv4_pool": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -86,7 +95,6 @@ func dataSourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
req.Filters = nil
}

log.Printf("[DEBUG] Reading EIP: %s", req)
resp, err := conn.DescribeAddresses(req)
if err != nil {
return fmt.Errorf("error describing EC2 Address: %s", err)
Expand All @@ -112,8 +120,30 @@ func dataSourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
d.Set("instance_id", eip.InstanceId)
d.Set("network_interface_id", eip.NetworkInterfaceId)
d.Set("network_interface_owner_id", eip.NetworkInterfaceOwnerId)

region := *conn.Config.Region
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nit: We tend to access the provider region across the codebase via region := meta.(*AWSClient).region instead of via the client.


d.Set("private_ip", eip.PrivateIpAddress)
if eip.PrivateIpAddress != nil {
dashIP := strings.Replace(*eip.PrivateIpAddress, ".", "-", -1)

if region == "us-east-1" {
d.Set("private_dns", fmt.Sprintf("ip-%s.ec2.internal", dashIP))
} else {
d.Set("private_dns", fmt.Sprintf("ip-%s.%s.compute.internal", dashIP, region))
}
}

d.Set("public_ip", eip.PublicIp)
if eip.PublicIp != nil {
dashIP := strings.Replace(*eip.PublicIp, ".", "-", -1)

if region == "us-east-1" {
d.Set("public_dns", fmt.Sprintf("ec2-%s.compute-1.amazonaws.com", dashIP))
} else {
d.Set("public_dns", fmt.Sprintf("ec2-%s.%s.compute.amazonaws.com", dashIP, region))
}
}
d.Set("public_ipv4_pool", eip.PublicIpv4Pool)
d.Set("tags", tagsToMap(eip.Tags))

Expand Down
32 changes: 32 additions & 0 deletions aws/resource_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,23 @@ func resourceAwsEip() *schema.Resource {
Computed: true,
},

"public_dns": {
Type: schema.TypeString,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: true should be removed here as operators cannot specify this argument.

Computed: true,
},

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

"private_dns": {
Type: schema.TypeString,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: true should be removed here as operators cannot specify this argument.

Computed: true,
},

"associate_with_private_ip": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -220,8 +232,28 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
} else {
d.Set("network_interface", "")
}

region := *ec2conn.Config.Region
d.Set("private_ip", address.PrivateIpAddress)
if address.PrivateIpAddress != nil {
dashIP := strings.Replace(*address.PrivateIpAddress, ".", "-", -1)

if region == "us-east-1" {
d.Set("private_dns", fmt.Sprintf("ip-%s.ec2.internal", dashIP))
} else {
d.Set("private_dns", fmt.Sprintf("ip-%s.%s.compute.internal", dashIP, region))
}
}
d.Set("public_ip", address.PublicIp)
if address.PublicIp != nil {
dashIP := strings.Replace(*address.PublicIp, ".", "-", -1)

if region == "us-east-1" {
d.Set("public_dns", fmt.Sprintf("ec2-%s.compute-1.amazonaws.com", dashIP))
} else {
d.Set("public_dns", fmt.Sprintf("ec2-%s.%s.compute.amazonaws.com", dashIP, region))
}
}
d.Set("public_ipv4_pool", address.PublicIpv4Pool)

// On import (domain never set, which it must've been if we created),
Expand Down
4 changes: 4 additions & 0 deletions website/docs/d/eip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ In addition to all arguments above, the following attributes are exported:
* `network_interface_id` - The ID of the network interface.
* `network_interface_owner_id` - The ID of the AWS account that owns the network interface.
* `private_ip` - The private IP address associated with the Elastic IP address.
* `private_dns` - The Private DNS associated with the Elastic IP address.
* `public_ip` - Public IP address of Elastic IP.
* `public_dns` - Public DNS associated with the Elastic IP address.
* `public_ipv4_pool` - The ID of an address pool.
* `tags` - Key-value map of tags associated with Elastic IP.

~> **Note:** The data source computes the `public_dns` and `private_dns` attributes according to the [VPC DNS Guide](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html#vpc-dns-hostnames) as they are not available with the EC2 API.
4 changes: 4 additions & 0 deletions website/docs/r/eip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ In addition to all arguments above, the following attributes are exported:

* `id` - Contains the EIP allocation ID.
* `private_ip` - Contains the private IP address (if in VPC).
* `private_dns` - The Private DNS associated with the Elastic IP address (if in VPC).
* `associate_with_private_ip` - Contains the user specified private IP address
(if in VPC).
* `public_ip` - Contains the public IP address.
* `public_dns` - Public DNS associated with the Elastic IP address.
* `instance` - Contains the ID of the attached instance.
* `network_interface` - Contains the ID of the attached network interface.
* `public_ipv4_pool` - EC2 IPv4 address pool identifier (if in VPC).

~> **Note:** The resource computes the `public_dns` and `private_dns` attributes according to the [VPC DNS Guide](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html#vpc-dns-hostnames) as they are not available with the EC2 API.

## Timeouts
`aws_eip` provides the following [Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

Expand Down