Skip to content

Commit 119d1cf

Browse files
committed
Reorganize ECS Params Reader code
1 parent d8fc41c commit 119d1cf

File tree

1 file changed

+79
-64
lines changed

1 file changed

+79
-64
lines changed

ecs-cli/modules/utils/compose/ecs_params_reader.go

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ import (
3030
"gopkg.in/yaml.v2"
3131
)
3232

33+
///////////////////////////////////
34+
///// ECS Params Schema types /////
35+
///////////////////////////////////
36+
3337
// ECSParams contains the information parsed from the ecs-params.yml file
3438
type ECSParams struct {
3539
Version string
@@ -109,6 +113,11 @@ const (
109113
Disabled AssignPublicIp = "DISABLED"
110114
)
111115

116+
/////////////////////////////
117+
///// Parsing Functions /////
118+
/////////////////////////////
119+
120+
// Having a custom Unmarshaller on ContainerDef allows us to set custom defaults on the Container Definition
112121
func (cd *ContainerDef) UnmarshalYAML(unmarshal func(interface{}) error) error {
113122
type rawContainerDef ContainerDef
114123
raw := rawContainerDef{Essential: true} // If essential is not specified, we want it to be true
@@ -120,70 +129,6 @@ func (cd *ContainerDef) UnmarshalYAML(unmarshal func(interface{}) error) error {
120129
return nil
121130
}
122131

123-
func (h *HealthCheck) ConvertToECSHealthCheck() (*ecs.HealthCheck, error) {
124-
ecsHealthCheck := &ecs.HealthCheck{}
125-
if len(h.Command) > 0 && len(h.Test) > 0 {
126-
return nil, fmt.Errorf("healthcheck.test and healthcheck.command can not both be specified")
127-
}
128-
129-
if len(h.Command) > 0 {
130-
ecsHealthCheck.Command = aws.StringSlice(getHealthCheckCommand(h.Command))
131-
}
132-
133-
if len(h.Test) > 0 {
134-
ecsHealthCheck.Command = aws.StringSlice(getHealthCheckCommand(h.Test))
135-
}
136-
137-
if h.Retries != 0 {
138-
ecsHealthCheck.Retries = &h.Retries
139-
}
140-
141-
timeout, err := parseHealthCheckTime(h.Timeout)
142-
if err != nil {
143-
return ecsHealthCheck, err
144-
}
145-
ecsHealthCheck.Timeout = timeout
146-
147-
startPeriod, err := parseHealthCheckTime(h.StartPeriod)
148-
if err != nil {
149-
return ecsHealthCheck, err
150-
}
151-
ecsHealthCheck.StartPeriod = startPeriod
152-
153-
interval, err := parseHealthCheckTime(h.Interval)
154-
if err != nil {
155-
return ecsHealthCheck, err
156-
}
157-
ecsHealthCheck.Interval = interval
158-
159-
return ecsHealthCheck, nil
160-
}
161-
162-
// parses the command/test field for healthcheck
163-
func getHealthCheckCommand(command []string) []string {
164-
if len(command) == 1 {
165-
// command/test was specified as a single string which wraps it in /bin/sh (CMD-SHELL)
166-
command = append([]string{"CMD-SHELL"}, command...)
167-
}
168-
return command
169-
}
170-
171-
// parses a health check time string which could be a duration or an integer
172-
func parseHealthCheckTime(field string) (*int64, error) {
173-
if field != "" {
174-
duration, err := time.ParseDuration(field)
175-
if err == nil {
176-
return adapter.ConvertToTimeInSeconds(&duration), nil
177-
} else if val, err := strconv.ParseInt(field, 10, 64); err == nil {
178-
return &val, nil
179-
} else {
180-
return nil, fmt.Errorf("Could not parse %s either as an integer or a duration (ex: 1m30s)", field)
181-
}
182-
}
183-
184-
return nil, nil
185-
}
186-
187132
// ReadECSParams parses the ecs-params.yml file and puts it into an ECSParams struct.
188133
func ReadECSParams(filename string) (*ECSParams, error) {
189134
if filename == "" {
@@ -211,6 +156,10 @@ func ReadECSParams(filename string) (*ECSParams, error) {
211156
return ecsParams, nil
212157
}
213158

159+
/////////////////////
160+
//// Converters ////
161+
////////////////////
162+
214163
// ConvertToECSNetworkConfiguration extracts out the NetworkConfiguration from
215164
// the ECSParams into a format that is compatible with ECSClient calls.
216165
func ConvertToECSNetworkConfiguration(ecsParams *ECSParams) (*ecs.NetworkConfiguration, error) {
@@ -261,3 +210,69 @@ func ConvertToECSNetworkConfiguration(ecsParams *ECSParams) (*ecs.NetworkConfigu
261210

262211
return ecsNetworkConfig, nil
263212
}
213+
214+
// ConvertToECSHealthCheck extracts out the HealthCheck from the ECSParams into
215+
// a format that is compatible with ECSClient calls.
216+
func (h *HealthCheck) ConvertToECSHealthCheck() (*ecs.HealthCheck, error) {
217+
ecsHealthCheck := &ecs.HealthCheck{}
218+
if len(h.Command) > 0 && len(h.Test) > 0 {
219+
return nil, fmt.Errorf("healthcheck.test and healthcheck.command can not both be specified")
220+
}
221+
222+
if len(h.Command) > 0 {
223+
ecsHealthCheck.Command = aws.StringSlice(getHealthCheckCommand(h.Command))
224+
}
225+
226+
if len(h.Test) > 0 {
227+
ecsHealthCheck.Command = aws.StringSlice(getHealthCheckCommand(h.Test))
228+
}
229+
230+
if h.Retries != 0 {
231+
ecsHealthCheck.Retries = &h.Retries
232+
}
233+
234+
timeout, err := parseHealthCheckTime(h.Timeout)
235+
if err != nil {
236+
return ecsHealthCheck, err
237+
}
238+
ecsHealthCheck.Timeout = timeout
239+
240+
startPeriod, err := parseHealthCheckTime(h.StartPeriod)
241+
if err != nil {
242+
return ecsHealthCheck, err
243+
}
244+
ecsHealthCheck.StartPeriod = startPeriod
245+
246+
interval, err := parseHealthCheckTime(h.Interval)
247+
if err != nil {
248+
return ecsHealthCheck, err
249+
}
250+
ecsHealthCheck.Interval = interval
251+
252+
return ecsHealthCheck, nil
253+
}
254+
255+
// parses the command/test field for healthcheck
256+
func getHealthCheckCommand(command []string) []string {
257+
if len(command) == 1 {
258+
// command/test was specified as a single string which wraps it in /bin/sh (CMD-SHELL)
259+
command = append([]string{"CMD-SHELL"}, command...)
260+
}
261+
return command
262+
}
263+
264+
// parses a health check time string which could be a duration or an integer
265+
func parseHealthCheckTime(field string) (*int64, error) {
266+
if field != "" {
267+
duration, err := time.ParseDuration(field)
268+
if err == nil {
269+
return adapter.ConvertToTimeInSeconds(&duration), nil
270+
} else if val, err := strconv.ParseInt(field, 10, 64); err == nil {
271+
return &val, nil
272+
} else {
273+
return nil, fmt.Errorf("Could not parse %s either as an integer or a duration (ex: 1m30s)", field)
274+
}
275+
}
276+
277+
return nil, nil
278+
}

0 commit comments

Comments
 (0)