Skip to content

Commit

Permalink
Merge pull request #36963 from hashicorp/f-aws_eip.ptr_record
Browse files Browse the repository at this point in the history
New resource: `aws_eip_domain_name`
  • Loading branch information
ewbankkit authored Apr 18, 2024
2 parents 70718c6 + cab40fc commit a187beb
Show file tree
Hide file tree
Showing 30 changed files with 1,088 additions and 266 deletions.
2 changes: 1 addition & 1 deletion .changelog/36767.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:enhancement
resource/openzfs_file_system: Add `endpoint_ip_address` attribute
resource/aws_fsx_openzfs_file_system: Add `endpoint_ip_address` attribute
```
11 changes: 11 additions & 0 deletions .changelog/36963.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:enhancement
data-source/aws_eip: Add `ptr_record` attribute
```

```release-note:enhancement
resource/aws_eip: Add `ptr_record` attribute
```

```release-note:new-resource
aws_eip_domain_name
```
35 changes: 35 additions & 0 deletions internal/conns/awsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"maps"
"net/http"
"os"
"strings"
"sync"

aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -250,6 +251,40 @@ func (c *AWSClient) ReverseDNSPrefix(ctx context.Context) string {
return names.ReverseDNS(c.DNSSuffix(ctx))
}

// EC2RegionalPrivateDNSSuffix returns the EC2 private DNS suffix for the configured AWS Region.
func (c *AWSClient) EC2RegionalPrivateDNSSuffix(context.Context) string {
region := c.Region
if region == names.USEast1RegionID {
return "ec2.internal"
}

return fmt.Sprintf("%s.compute.internal", region)
}

// EC2RegionalPublicDNSSuffix returns the EC2 public DNS suffix for the configured AWS Region.
func (c *AWSClient) EC2RegionalPublicDNSSuffix(context.Context) string {
region := c.Region
if region == names.USEast1RegionID {
return "compute-1"
}

return fmt.Sprintf("%s.compute", region)
}

// EC2PrivateDNSNameForIP returns a EC2 private DNS name in the configured AWS Region.
func (c *AWSClient) EC2PrivateDNSNameForIP(ctx context.Context, ip string) string {
return fmt.Sprintf("ip-%s.%s", convertIPToDashIP(ip), c.EC2RegionalPrivateDNSSuffix(ctx))
}

// EC2PublicDNSNameForIP returns a EC2 public DNS name in the configured AWS Region.
func (c *AWSClient) EC2PublicDNSNameForIP(ctx context.Context, ip string) string {
return c.PartitionHostname(ctx, fmt.Sprintf("ec2-%s.%s", convertIPToDashIP(ip), c.EC2RegionalPublicDNSSuffix(ctx)))
}

func convertIPToDashIP(ip string) string {
return strings.Replace(ip, ".", "-", -1)
}

// apiClientConfig returns the AWS API client configuration parameters for the specified service.
func (c *AWSClient) apiClientConfig(ctx context.Context, servicePackageName string) map[string]any {
m := map[string]any{
Expand Down
88 changes: 88 additions & 0 deletions internal/conns/awsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,91 @@ func TestAWSClientRegionalHostname(t *testing.T) { // nosemgrep:ci.aws-in-func-n
})
}
}

func TestAWSClientEC2PrivateDNSNameForIP(t *testing.T) { // nosemgrep:ci.aws-in-func-name
t.Parallel()

ctx := context.TODO()
testCases := []struct {
Name string
AWSClient *AWSClient
IP string
Expected string
}{
{
Name: "us-west-2",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
Region: "us-west-2", //lintignore:AWSAT003
},
IP: "10.20.30.40",
Expected: "ip-10-20-30-40.us-west-2.compute.internal", //lintignore:AWSAT003
},
{
Name: "us-east-1",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
Region: "us-east-1", //lintignore:AWSAT003
},
IP: "10.20.30.40",
Expected: "ip-10-20-30-40.ec2.internal",
},
}

for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Name, func(t *testing.T) {
t.Parallel()

got := testCase.AWSClient.EC2PrivateDNSNameForIP(ctx, testCase.IP)

if got != testCase.Expected {
t.Errorf("got %s, expected %s", got, testCase.Expected)
}
})
}
}

func TestAWSClientEC2PublicDNSNameForIP(t *testing.T) { // nosemgrep:ci.aws-in-func-name
t.Parallel()

ctx := context.TODO()
testCases := []struct {
Name string
AWSClient *AWSClient
IP string
Expected string
}{
{
Name: "us-west-2",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
Region: "us-west-2", //lintignore:AWSAT003
},
IP: "10.20.30.40",
Expected: "ec2-10-20-30-40.us-west-2.compute.amazonaws.com", //lintignore:AWSAT003
},
{
Name: "us-east-1",
AWSClient: &AWSClient{
dnsSuffix: "amazonaws.com",
Region: "us-east-1", //lintignore:AWSAT003
},
IP: "10.20.30.40",
Expected: "ec2-10-20-30-40.compute-1.amazonaws.com",
},
}

for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Name, func(t *testing.T) {
t.Parallel()

got := testCase.AWSClient.EC2PublicDNSNameForIP(ctx, testCase.IP)

if got != testCase.Expected {
t.Errorf("got %s, expected %s", got, testCase.Expected)
}
})
}
}
16 changes: 16 additions & 0 deletions internal/sdkv2/suppress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package sdkv2

import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// SuppressEquivalentStringCaseInsensitive provides custom difference suppression
// for strings that are equal under case-insensitivity.
func SuppressEquivalentStringCaseInsensitive(k, old, new string, d *schema.ResourceData) bool {
return strings.EqualFold(old, new)
}
5 changes: 5 additions & 0 deletions internal/service/ec2/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ const (
CustomerGatewayStatePending = "pending"
)

// See https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-address-attribute.html#examples.
const (
PTRUpdateStatusPending = "PENDING"
)

const (
managedPrefixListAddressFamilyIPv4 = "IPv4"
managedPrefixListAddressFamilyIPv6 = "IPv6"
Expand Down
Loading

0 comments on commit a187beb

Please sign in to comment.