From 4fc60c9f89cc4ebad933bbffb0f243eb9213c183 Mon Sep 17 00:00:00 2001 From: ryane Date: Mon, 9 Nov 2015 19:36:23 -0500 Subject: [PATCH] docker: improve validation of runtime constraints --- .../docker/resource_docker_container.go | 21 +++++++++++++++++++ .../docker/resource_docker_container_funcs.go | 18 +++++----------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index 92331fc79..242462e1a 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -182,18 +182,39 @@ func resourceDockerContainer() *schema.Resource { Type: schema.TypeInt, Optional: true, ForceNew: true, + ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { + value := v.(int) + if value < 0 { + es = append(es, fmt.Errorf("%q must be greater than or equal to 0", k)) + } + return + }, }, "memory_swap": &schema.Schema{ Type: schema.TypeInt, Optional: true, ForceNew: true, + ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { + value := v.(int) + if value < -1 { + es = append(es, fmt.Errorf("%q must be greater than or equal to -1", k)) + } + return + }, }, "cpu_shares": &schema.Schema{ Type: schema.TypeInt, Optional: true, ForceNew: true, + ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { + value := v.(int) + if value < 0 { + es = append(es, fmt.Errorf("%q must be greater than or equal to 0", k)) + } + return + }, }, "log_driver": &schema.Schema{ diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index 2b0259bc9..b0c262dfc 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -118,27 +118,19 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err } if v, ok := d.GetOk("memory"); ok { - memory := int64(v.(int)) - if memory > 0 { - hostConfig.Memory = memory * 1024 * 1024 - } + hostConfig.Memory = int64(v.(int)) * 1024 * 1024 } if v, ok := d.GetOk("memory_swap"); ok { swap := int64(v.(int)) - if swap != 0 { - if swap > 0 { // only convert positive #s to bytes - swap = swap * 1024 * 1024 - } - hostConfig.MemorySwap = swap + if swap > 0 { + swap = swap * 1024 * 1024 } + hostConfig.MemorySwap = swap } if v, ok := d.GetOk("cpu_shares"); ok { - shares := int64(v.(int)) - if shares > 0 { - hostConfig.CPUShares = shares - } + hostConfig.CPUShares = int64(v.(int)) } if v, ok := d.GetOk("log_opts"); ok {