Skip to content

Commit

Permalink
docker: improve validation of runtime constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
ryane committed Nov 10, 2015
1 parent b5ae355 commit 4fc60c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
21 changes: 21 additions & 0 deletions builtin/providers/docker/resource_docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
18 changes: 5 additions & 13 deletions builtin/providers/docker/resource_docker_container_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4fc60c9

Please sign in to comment.