Skip to content

Commit

Permalink
Added public_dns and private_dns to aws_eip
Browse files Browse the repository at this point in the history
  • Loading branch information
hamstah committed Jan 27, 2019
1 parent 5b8ed28 commit 40cde7e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
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

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,
Computed: true,
},

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

"private_dns": {
Type: schema.TypeString,
Optional: true,
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

0 comments on commit 40cde7e

Please sign in to comment.