provider/aws: Validate aws_ecs_task_definition.container_definitions (#12161)

This commit is contained in:
Masayuki Morita 2017-04-02 01:31:32 +09:00 committed by Radek Simko
parent 26c5445477
commit 4b9ec9c920
2 changed files with 58 additions and 0 deletions

View File

@ -45,6 +45,7 @@ func resourceAwsEcsTaskDefinition() *schema.Resource {
hash := sha1.Sum([]byte(v.(string)))
return hex.EncodeToString(hash[:])
},
ValidateFunc: validateAwsEcsTaskDefinitionContainerDefinitions,
},
"task_role_arn": {
@ -121,6 +122,15 @@ func validateAwsEcsTaskDefinitionNetworkMode(v interface{}, k string) (ws []stri
return
}
func validateAwsEcsTaskDefinitionContainerDefinitions(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, err := expandEcsContainerDefinitions(value)
if err != nil {
errors = append(errors, fmt.Errorf("ECS Task Definition container_definitions is invalid: %s", err))
}
return
}
func resourceAwsEcsTaskDefinitionCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecsconn

View File

@ -203,6 +203,28 @@ func TestValidateAwsEcsTaskDefinitionNetworkMode(t *testing.T) {
}
}
func TestValidateAwsEcsTaskDefinitionContainerDefinitions(t *testing.T) {
validDefinitions := []string{
testValidateAwsEcsTaskDefinitionValidContainerDefinitions,
}
for _, v := range validDefinitions {
_, errors := validateAwsEcsTaskDefinitionContainerDefinitions(v, "container_definitions")
if len(errors) != 0 {
t.Fatalf("%q should be a valid AWS ECS Task Definition Container Definitions: %q", v, errors)
}
}
invalidDefinitions := []string{
testValidateAwsEcsTaskDefinitionInvalidCommandContainerDefinitions,
}
for _, v := range invalidDefinitions {
_, errors := validateAwsEcsTaskDefinitionContainerDefinitions(v, "container_definitions")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid AWS ECS Task Definition Container Definitions", v)
}
}
}
func testAccCheckAWSEcsTaskDefinitionDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ecsconn
@ -666,3 +688,29 @@ TASK_DEFINITION
}
}
`
var testValidateAwsEcsTaskDefinitionValidContainerDefinitions = `
[
{
"name": "sleep",
"image": "busybox",
"cpu": 10,
"command": ["sleep","360"],
"memory": 10,
"essential": true
}
]
`
var testValidateAwsEcsTaskDefinitionInvalidCommandContainerDefinitions = `
[
{
"name": "sleep",
"image": "busybox",
"cpu": 10,
"command": "sleep 360",
"memory": 10,
"essential": true
}
]
`