-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
ecs_properties.go
172 lines (141 loc) · 4.49 KB
/
ecs_properties.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package batch
import (
"sort"
_ "unsafe" // Required for go:linkname
"github.com/aws/aws-sdk-go-v2/aws"
_ "github.com/aws/aws-sdk-go-v2/service/batch" // Required for go:linkname
awstypes "github.com/aws/aws-sdk-go-v2/service/batch/types"
smithyjson "github.com/aws/smithy-go/encoding/json"
tfjson "github.com/hashicorp/terraform-provider-aws/internal/json"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
)
type ecsProperties awstypes.EcsProperties
func (ep *ecsProperties) reduce() {
ep.orderContainers()
ep.orderEnvironmentVariables()
ep.orderSecrets()
// Set all empty slices to nil.
// Deal with special fields which have defaults.
for i, taskProps := range ep.TaskProperties {
for j, container := range taskProps.Containers {
if container.Essential == nil {
container.Essential = aws.Bool(true)
}
if len(container.Command) == 0 {
container.Command = nil
}
if len(container.DependsOn) == 0 {
container.DependsOn = nil
}
if len(container.Environment) == 0 {
container.Environment = nil
}
if container.LogConfiguration != nil && len(container.LogConfiguration.SecretOptions) == 0 {
container.LogConfiguration.SecretOptions = nil
}
if len(container.MountPoints) == 0 {
container.MountPoints = nil
}
if len(container.Secrets) == 0 {
container.Secrets = nil
}
if len(container.Ulimits) == 0 {
container.Ulimits = nil
}
taskProps.Containers[j] = container
}
if taskProps.PlatformVersion == nil {
taskProps.PlatformVersion = aws.String(fargatePlatformVersionLatest)
}
if len(taskProps.Volumes) == 0 {
taskProps.Volumes = nil
}
ep.TaskProperties[i] = taskProps
}
}
func (ep *ecsProperties) orderContainers() {
for i, taskProps := range ep.TaskProperties {
sort.Slice(taskProps.Containers, func(i, j int) bool {
return aws.ToString(taskProps.Containers[i].Name) < aws.ToString(taskProps.Containers[j].Name)
})
ep.TaskProperties[i].Containers = taskProps.Containers
}
}
func (ep *ecsProperties) orderEnvironmentVariables() {
for i, taskProps := range ep.TaskProperties {
for j, container := range taskProps.Containers {
// Remove environment variables with empty values.
container.Environment = tfslices.Filter(container.Environment, func(kvp awstypes.KeyValuePair) bool {
return aws.ToString(kvp.Value) != ""
})
sort.Slice(container.Environment, func(i, j int) bool {
return aws.ToString(container.Environment[i].Name) < aws.ToString(container.Environment[j].Name)
})
ep.TaskProperties[i].Containers[j].Environment = container.Environment
}
}
}
func (ep *ecsProperties) orderSecrets() {
for i, taskProps := range ep.TaskProperties {
for j, container := range taskProps.Containers {
sort.Slice(container.Secrets, func(i, j int) bool {
return aws.ToString(container.Secrets[i].Name) < aws.ToString(container.Secrets[j].Name)
})
ep.TaskProperties[i].Containers[j].Secrets = container.Secrets
}
}
}
func equivalentECSPropertiesJSON(str1, str2 string) (bool, error) {
if str1 == "" {
str1 = "{}"
}
if str2 == "" {
str2 = "{}"
}
var ep1 ecsProperties
err := tfjson.DecodeFromString(str1, &ep1)
if err != nil {
return false, err
}
ep1.reduce()
b1, err := tfjson.EncodeToBytes(ep1)
if err != nil {
return false, err
}
var ep2 ecsProperties
err = tfjson.DecodeFromString(str2, &ep2)
if err != nil {
return false, err
}
ep2.reduce()
b2, err := tfjson.EncodeToBytes(ep2)
if err != nil {
return false, err
}
return tfjson.EqualBytes(b1, b2), nil
}
func expandECSProperties(tfString string) (*awstypes.EcsProperties, error) {
apiObject := &awstypes.EcsProperties{}
if err := tfjson.DecodeFromString(tfString, apiObject); err != nil {
return nil, err
}
return apiObject, nil
}
// 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 serializeECSPProperties github.com/aws/aws-sdk-go-v2/service/batch.awsRestjson1_serializeDocumentEcsProperties
func serializeECSPProperties(v *awstypes.EcsProperties, value smithyjson.Value) error
func flattenECSProperties(apiObject *awstypes.EcsProperties) (string, error) {
if apiObject == nil {
return "", nil
}
jsonEncoder := smithyjson.NewEncoder()
err := serializeECSPProperties(apiObject, jsonEncoder.Value)
if err != nil {
return "", err
}
return jsonEncoder.String(), nil
}