docker: improve validation of runtime constraints

This commit is contained in:
ryane 2015-11-09 19:36:23 -05:00
parent b5ae355a99
commit 4fc60c9f89
2 changed files with 26 additions and 13 deletions

View File

@ -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{

View File

@ -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 {