Skip to content

Commit

Permalink
Merge pull request #38804 from hashicorp/b-aws_ecs_task_definition-co…
Browse files Browse the repository at this point in the history
…ntainer_definitions-first-char

r/aws_ecs_task_definition: Restore `container_definitions` serialization fidelity
  • Loading branch information
ewbankkit authored Aug 9, 2024
2 parents 91a1565 + 2ed43f5 commit 02c06ae
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 150 deletions.
3 changes: 3 additions & 0 deletions .changelog/38804.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ecs_task_definition: Prevent lowercasing of the first character of JSON keys in `container_definitions.dockerLabels`
```
51 changes: 0 additions & 51 deletions internal/json/transform.go

This file was deleted.

68 changes: 0 additions & 68 deletions internal/json/transform_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
package ecs

import (
"fmt"
"sort"
_ "unsafe" // Required for go:linkname

"github.com/aws/aws-sdk-go-v2/aws"
_ "github.com/aws/aws-sdk-go-v2/service/ecs" // Required for go:linkname
awstypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
smithyjson "github.com/aws/smithy-go/encoding/json"
tfjson "github.com/hashicorp/terraform-provider-aws/internal/json"
itypes "github.com/hashicorp/terraform-provider-aws/internal/types"
)

func containerDefinitionsAreEquivalent(def1, def2 string, isAWSVPC bool) (bool, error) {
Expand Down Expand Up @@ -143,3 +148,36 @@ func (cd containerDefinitions) orderContainers() {
return aws.ToString(cd[i].Name) < aws.ToString(cd[j].Name)
})
}

// Dirty hack to avoid any backwards compatibility issues with the AWS SDK for Go v2 migration.
// Reach down into the SDK and use the same serialization function that the SDK uses.
//
//go:linkname serializeContainerDefinitions github.com/aws/aws-sdk-go-v2/service/ecs.awsAwsjson11_serializeDocumentContainerDefinitions
func serializeContainerDefinitions(v []awstypes.ContainerDefinition, value smithyjson.Value) error

func flattenContainerDefinitions(apiObjects []awstypes.ContainerDefinition) (string, error) {
jsonEncoder := smithyjson.NewEncoder()
err := serializeContainerDefinitions(apiObjects, jsonEncoder.Value)

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

return jsonEncoder.String(), nil
}

func expandContainerDefinitions(tfString string) ([]awstypes.ContainerDefinition, error) {
var apiObjects []awstypes.ContainerDefinition

if err := tfjson.DecodeFromString(tfString, &apiObjects); err != nil {
return nil, err
}

for i, apiObject := range apiObjects {
if itypes.IsZero(&apiObject) {
return nil, fmt.Errorf("invalid container definition supplied at index (%d)", i)
}
}

return apiObjects, nil
}
31 changes: 0 additions & 31 deletions internal/service/ecs/task_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tfjson "github.com/hashicorp/terraform-provider-aws/internal/json"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
itypes "github.com/hashicorp/terraform-provider-aws/internal/types"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -1211,35 +1209,6 @@ func flattenFSxWinVolumeAuthorizationConfig(config *awstypes.FSxWindowsFileServe
return items
}

func flattenContainerDefinitions(apiObjects []awstypes.ContainerDefinition) (string, error) {
json, err := tfjson.EncodeToBytes(apiObjects)
if err != nil {
return "", err
}

// Remove empty fields and convert first character of keys to lowercase.
json = tfjson.RemoveEmptyFields(json)
json = tfjson.KeyFirstLower(json)

return string(json), nil
}

func expandContainerDefinitions(tfString string) ([]awstypes.ContainerDefinition, error) {
var apiObjects []awstypes.ContainerDefinition

if err := tfjson.DecodeFromString(tfString, &apiObjects); err != nil {
return nil, err
}

for i, apiObject := range apiObjects {
if itypes.IsZero(&apiObject) {
return nil, fmt.Errorf("invalid container definition supplied at index (%d)", i)
}
}

return apiObjects, nil
}

func expandTaskDefinitionEphemeralStorage(config []interface{}) *awstypes.EphemeralStorage {
configMap := config[0].(map[string]interface{})

Expand Down
Loading

0 comments on commit 02c06ae

Please sign in to comment.