Skip to content

Commit

Permalink
Merge pull request #35991 from kathmbeck/f-cross-account-attachment
Browse files Browse the repository at this point in the history
[New Resource] : `aws_globalaccelerator_cross_account_attachment`
  • Loading branch information
ewbankkit authored Apr 23, 2024
2 parents bd0c9f2 + 3577e15 commit 0a344a8
Show file tree
Hide file tree
Showing 42 changed files with 1,599 additions and 827 deletions.
11 changes: 11 additions & 0 deletions .changelog/35991.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:new-resource
aws_globalaccelerator_cross_account_attachment
```

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

```release-note:enhancement
data-source/aws_eip: Add `arn` attribute
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/fis v1.24.2
github.com/aws/aws-sdk-go-v2/service/fms v1.31.4
github.com/aws/aws-sdk-go-v2/service/glacier v1.22.4
github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.1
github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.0
github.com/aws/aws-sdk-go-v2/service/healthlake v1.24.0
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ github.com/aws/aws-sdk-go-v2/service/fms v1.31.4 h1:gY+Dp2QdphY6m5IVkETmsNauYztd
github.com/aws/aws-sdk-go-v2/service/fms v1.31.4/go.mod h1:X4DjA4sm8cobhR9DtHn947+dLYxU1oWq3zwRZUmFSLo=
github.com/aws/aws-sdk-go-v2/service/glacier v1.22.4 h1:y0/RN8LwIbyDTPe/dnDBdsCw89ko8ZNFPW4vStye4aE=
github.com/aws/aws-sdk-go-v2/service/glacier v1.22.4/go.mod h1:8ofkOuh1SZLKR5EdfxPhQ1UgaQuCBAZzUwbeIBmeKIM=
github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.1 h1:E48tPAIKptyIb8OFOAsZ3xSzjwou8A63f40ao1H3tVU=
github.com/aws/aws-sdk-go-v2/service/globalaccelerator v1.23.1/go.mod h1:6morRSCgJD400qAu5DCEtvoaAC1owS5t6oq8ddLLwxw=
github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.0 h1:joAdQdtfg8Yy/e5Pq5qwAe0hjH3+EJUzd1jPrlXE3SA=
github.com/aws/aws-sdk-go-v2/service/groundstation v1.27.0/go.mod h1:yGNTqdu48YxjsCyLpalmwHCQF52GGERK7+J3nTcvJ3A=
github.com/aws/aws-sdk-go-v2/service/healthlake v1.24.0 h1:HRcwttYPNlE5mv6uDlcsk9m4oV3fxV8+a+V4U3jIMd4=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 23 additions & 18 deletions internal/flex/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package flex

import (
"fmt"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -413,24 +414,6 @@ func ResourceIdPartCount(id string) int {
return len(idParts)
}

type Set[T comparable] []T

// Difference find the elements in two sets that are not similar.
func (s Set[T]) Difference(ns Set[T]) Set[T] {
m := make(map[T]struct{})
for _, v := range ns {
m[v] = struct{}{}
}

var result []T
for _, v := range s {
if _, ok := m[v]; !ok {
result = append(result, v)
}
}
return result
}

// DiffStringMaps returns the set of keys and values that must be created, the set of keys
// and values that must be destroyed, and the set of keys and values that are unchanged.
func DiffStringMaps(oldMap, newMap map[string]interface{}) (map[string]*string, map[string]*string, map[string]*string) {
Expand Down Expand Up @@ -478,3 +461,25 @@ func DiffStringValueMaps(oldMap, newMap map[string]interface{}) (map[string]stri

return add, remove, unchanged
}

func DiffSlices[E any](old []E, new []E, eq func(E, E) bool) ([]E, []E, []E) {
// First, we're creating everything we have.
add := new

// Build the slices of what to remove and what is unchanged.
remove := make([]E, 0)
unchanged := make([]E, 0)
for _, e := range old {
eq := func(v E) bool { return eq(v, e) }
if !slices.ContainsFunc(new, eq) {
// Delete it!
remove = append(remove, e)
} else {
unchanged = append(unchanged, e)
// Already present, so remove from new.
add = slices.DeleteFunc(add, eq)
}
}

return add, remove, unchanged
}
59 changes: 59 additions & 0 deletions internal/flex/flex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,62 @@ func TestDiffStringValueMaps(t *testing.T) {
}
}
}

func TestDiffSlices(t *testing.T) {
t.Parallel()

type x struct {
A string
B int
}

cases := []struct {
Old, New []x
Create, Remove, Unchanged []x
}{
// Add
{
Old: []x{{A: "foo", B: 1}},
New: []x{{A: "foo", B: 1}, {A: "bar", B: 2}},
Create: []x{{A: "bar", B: 2}},
Remove: []x{},
Unchanged: []x{{A: "foo", B: 1}},
},
// Modify
{
Old: []x{{A: "foo", B: 1}},
New: []x{{A: "foo", B: 2}},
Create: []x{{A: "foo", B: 2}},
Remove: []x{{A: "foo", B: 1}},
Unchanged: []x{},
},
// Overlap
{
Old: []x{{A: "foo", B: 1}, {A: "bar", B: 2}},
New: []x{{A: "foo", B: 3}, {A: "bar", B: 2}},
Create: []x{{A: "foo", B: 3}},
Remove: []x{{A: "foo", B: 1}},
Unchanged: []x{{A: "bar", B: 2}},
},
// Remove
{
Old: []x{{A: "foo", B: 1}, {A: "bar", B: 2}},
New: []x{{A: "foo", B: 1}},
Create: []x{},
Remove: []x{{A: "bar", B: 2}},
Unchanged: []x{{A: "foo", B: 1}},
},
}
for _, tc := range cases {
c, r, u := DiffSlices(tc.Old, tc.New, func(x1, x2 x) bool { return x1.A == x2.A && x1.B == x2.B })
if diff := cmp.Diff(c, tc.Create); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
if diff := cmp.Diff(r, tc.Remove); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
if diff := cmp.Diff(u, tc.Unchanged); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
}
}
3 changes: 2 additions & 1 deletion internal/framework/flex/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
itypes "github.com/hashicorp/terraform-provider-aws/internal/types"
)

func ExpandFrameworkStringSet(ctx context.Context, v basetypes.SetValuable) []*string {
Expand All @@ -20,7 +21,7 @@ func ExpandFrameworkStringSet(ctx context.Context, v basetypes.SetValuable) []*s
return output
}

func ExpandFrameworkStringValueSet(ctx context.Context, v basetypes.SetValuable) Set[string] {
func ExpandFrameworkStringValueSet(ctx context.Context, v basetypes.SetValuable) itypes.Set[string] {
var output []string

must(Expand(ctx, v, &output))
Expand Down
3 changes: 2 additions & 1 deletion internal/framework/flex/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
itypes "github.com/hashicorp/terraform-provider-aws/internal/types"
)

func TestExpandFrameworkStringSet(t *testing.T) {
Expand Down Expand Up @@ -68,7 +69,7 @@ func TestExpandFrameworkStringValueSet(t *testing.T) {

type testCase struct {
input types.Set
expected flex.Set[string]
expected itypes.Set[string]
}
tests := map[string]testCase{
"null": {
Expand Down
19 changes: 18 additions & 1 deletion internal/service/ec2/ec2_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
Expand Down Expand Up @@ -57,6 +58,10 @@ func resourceEIP() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"associate_with_private_ip": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -226,7 +231,9 @@ func resourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interface
}

address := outputRaw.(*types.Address)
d.Set("allocation_id", address.AllocationId)
allocationID := aws.ToString(address.AllocationId)
d.Set("allocation_id", allocationID)
d.Set("arn", eipARN(meta.(*conns.AWSClient), allocationID))
d.Set("association_id", address.AssociationId)
d.Set("carrier_ip", address.CarrierIp)
d.Set("customer_owned_ip", address.CustomerOwnedIp)
Expand Down Expand Up @@ -405,3 +412,13 @@ func disassociateEIP(ctx context.Context, conn *ec2.Client, associationID string

return nil
}

func eipARN(c *conns.AWSClient, allocationID string) string {
return arn.ARN{
Partition: c.Partition,
Service: names.EC2,
Region: c.Region,
AccountID: c.AccountID,
Resource: "elastic-ip/" + allocationID,
}.String()
}
9 changes: 8 additions & 1 deletion internal/service/ec2/ec2_eip_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func dataSourceEIP() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"association_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -132,7 +136,9 @@ func dataSourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interfa
}

if eip.Domain == types.DomainTypeVpc {
d.SetId(aws.ToString(eip.AllocationId))
allocationID := aws.ToString(eip.AllocationId)
d.SetId(allocationID)
d.Set("arn", eipARN(meta.(*conns.AWSClient), allocationID))

addressAttr, err := findEIPDomainNameAttributeByAllocationID(ctx, conn, d.Id())

Expand All @@ -146,6 +152,7 @@ func dataSourceEIPRead(ctx context.Context, d *schema.ResourceData, meta interfa
}
} else {
d.SetId(aws.ToString(eip.PublicIp))
d.Set("arn", nil)
d.Set("ptr_record", nil)
}
d.Set("association_id", eip.AssociationId)
Expand Down
1 change: 1 addition & 0 deletions internal/service/ec2/ec2_eip_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestAccEC2EIPDataSource_filter(t *testing.T) {
{
Config: testAccEIPDataSourceConfig_filter(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_dns", resourceName, "public_dns"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_ip", resourceName, "public_ip"),
Expand Down
1 change: 1 addition & 0 deletions internal/service/ec2/ec2_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestAccEC2EIP_basic(t *testing.T) {
Config: testAccEIPConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckEIPExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "domain", "vpc"),
resource.TestCheckResourceAttr(resourceName, "ptr_record", ""),
resource.TestCheckResourceAttrSet(resourceName, "public_ip"),
Expand Down
Loading

0 comments on commit 0a344a8

Please sign in to comment.