Skip to content

Commit

Permalink
Merge pull request #38016 from mattburgess/ecs-awssdkv2-migration
Browse files Browse the repository at this point in the history
ecs: Migrate to AWS SDK v2
  • Loading branch information
ewbankkit authored Jul 18, 2024
2 parents 2b5896f + 3c94609 commit 0e58082
Show file tree
Hide file tree
Showing 68 changed files with 2,936 additions and 2,908 deletions.
32 changes: 32 additions & 0 deletions internal/acctest/jsoncmp/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package jsoncmp

// Inspired by https://github.com/hgsgtk/jsoncmp.

import (
"encoding/json"
"fmt"

"github.com/google/go-cmp/cmp"
)

func Diff(x, y string) string {
xform := cmp.Transformer("jsoncmp", func(s string) (m map[string]any) {
if err := json.Unmarshal([]byte(s), &m); err != nil {
panic(fmt.Sprintf("json.Unmarshal(%s): %s", s, err))
}
return m
})
opt := cmp.FilterPath(func(p cmp.Path) bool {
for _, ps := range p {
if tr, ok := ps.(cmp.Transform); ok && tr.Option() == xform {
return false
}
}
return true
}, xform)

return cmp.Diff(x, y, opt)
}
44 changes: 44 additions & 0 deletions internal/acctest/jsoncmp/compare_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package jsoncmp_test

import (
"testing"

"github.com/hashicorp/terraform-provider-aws/internal/acctest/jsoncmp"
)

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

testCases := []struct {
testName string
x, y string
wantDiff bool
}{
{
testName: "no diff",
x: `{"A": "test1", "D": ["test2", "test3"], "C": {"A": true}, "B": 42}`,
y: `{"A":"test1", "B":42, "C":{"A":true}, "D": ["test2", "test3"]}`,
},
{
testName: "has diff",
x: `{"A": "test1", "D": ["test2", "test3"], "C": {"A": true}, "B": 42}`,
y: `{"A":"test1", "B":41, "C":{"A":true}, "D": ["test3"]}`,
wantDiff: true,
},
}

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

output := jsoncmp.Diff(testCase.x, testCase.y)
if got, want := output != "", testCase.wantDiff; got != want {
t.Errorf("Diff(%s, %s) = %t, want %t", testCase.x, testCase.y, got, want)
}
})
}
}
5 changes: 0 additions & 5 deletions internal/conns/awsclient_gen.go

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

4 changes: 2 additions & 2 deletions internal/errs/unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package errs

import (
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
tfawserr_sdkv2 "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-provider-aws/names"
)

const (
Expand Down Expand Up @@ -34,7 +34,7 @@ const (
// Be careful with a return value of `false`, which means either there is NO error
// or there is an error but not one that suggests an unsupported feature in ISO.
func IsUnsupportedOperationInPartitionError(partition string, err error) bool {
if partition == endpoints.AwsPartitionID {
if partition == names.StandardPartitionID {
return false
}

Expand Down
36 changes: 36 additions & 0 deletions internal/json/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package json

import (
"bytes"
"encoding/json"
"io"
"strings"
)

// DecodeFromBytes decodes (unmarshals) the given byte slice, containing valid JSON, into `to`.
func DecodeFromBytes(b []byte, to any) error {
return DecodeFromReader(bytes.NewReader(b), to)
}

// DecodeFromReader decodes (unmarshals) the given io.Reader, pointing to a JSON stream, into `to`.
func DecodeFromReader(r io.Reader, to any) error {
dec := json.NewDecoder(r)

for {
if err := dec.Decode(to); err == io.EOF {
break
} else if err != nil {
return err
}
}

return nil
}

// DecodeFromString decodes (unmarshals) the given string, containing valid JSON, into `to`.
func DecodeFromString(s string, to any) error {
return DecodeFromReader(strings.NewReader(s), to)
}
77 changes: 77 additions & 0 deletions internal/json/decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package json_test

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-provider-aws/internal/json"
)

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

type to struct {
A string
B int
C struct {
A bool
}
D []string
}
var to0, to1, to2, to3 to
to4 := to{
A: "test1",
B: 42,
C: struct {
A bool
}{A: true},
D: []string{"test2", "test3"},
}

testCases := []struct {
testName string
input string
output any
wantOutput any
wantErr bool
}{
{
testName: "empty JSON",
input: `{}`,
output: &to1,
wantOutput: &to0,
},
{
testName: "bad JSON",
input: `{test`,
output: &to2,
wantErr: true,
},
{
testName: "full JSON",
input: `{"A": "test1", "D": ["test2", "test3"], "C": {"A": true}, "B": 42}`,
output: &to3,
wantOutput: &to4,
},
}

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

err := json.DecodeFromString(testCase.input, testCase.output)
if got, want := err != nil, testCase.wantErr; !cmp.Equal(got, want) {
t.Errorf("DecodeFromString(%s) err %t, want %t", testCase.input, got, want)
}
if err == nil {
if diff := cmp.Diff(testCase.output, testCase.wantOutput); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
}
})
}
}
42 changes: 42 additions & 0 deletions internal/json/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package json

import (
"bytes"
"encoding/json"
)

// EncodeToBytes JSON encodes (marshals) `from` into a byte slice.
func EncodeToBytes(from any) ([]byte, error) {
to, err := encodeToBuffer(from)

if err != nil {
return nil, err
}

return to.Bytes(), nil
}

// EncodeToString JSON encodes (marshals) `from` into a string.
func EncodeToString(from any) (string, error) {
to, err := encodeToBuffer(from)

if err != nil {
return "", err
}

return to.String(), nil
}

func encodeToBuffer(from any) (*bytes.Buffer, error) {
to := new(bytes.Buffer)
enc := json.NewEncoder(to)

if err := enc.Encode(from); err != nil {
return nil, err
}

return to, nil
}
69 changes: 69 additions & 0 deletions internal/json/encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package json_test

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-provider-aws/internal/acctest/jsoncmp"
"github.com/hashicorp/terraform-provider-aws/internal/json"
)

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

type from struct {
A string
B int
C struct {
A bool
}
D []string
}
var from0 from
from1 := from{
A: "test1",
B: 42,
C: struct {
A bool
}{A: true},
D: []string{"test2", "test3"},
}

testCases := []struct {
testName string
input any
wantOutput string
wantErr bool
}{
{
testName: "empty JSON",
input: &from0,
wantOutput: `{"A": "", "B": 0, "C": {"A": false}, "D": null}`,
},
{
testName: "empty JSON",
input: &from1,
wantOutput: `{"A": "test1", "D": ["test2", "test3"], "C": {"A": true}, "B": 42}`,
},
}

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

output, err := json.EncodeToString(testCase.input)
if got, want := err != nil, testCase.wantErr; !cmp.Equal(got, want) {
t.Errorf("EncodeToString(%v) err %t, want %t", testCase.input, got, want)
}
if err == nil {
if diff := jsoncmp.Diff(output, testCase.wantOutput); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
}
})
}
}
28 changes: 28 additions & 0 deletions internal/json/equal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package json

import (
"reflect"
)

// EqualBytes returns whether the JSON documents in the given byte slices are equal.
func EqualBytes(b1, b2 []byte) bool {
var v1 any
if err := DecodeFromBytes(b1, &v1); err != nil {
return false
}

var v2 any
if err := DecodeFromBytes(b2, &v2); err != nil {
return false
}

return reflect.DeepEqual(v1, v2)
}

// EqualStrings returns whether the JSON documents in the given strings are equal.
func EqualStrings(s1, s2 string) bool {
return EqualBytes([]byte(s1), []byte(s2))
}
Loading

0 comments on commit 0e58082

Please sign in to comment.