add label support to docker container resource

This commit is contained in:
ryane 2015-11-03 15:20:58 -05:00
parent 6842c32d03
commit 4531866d8d
4 changed files with 28 additions and 0 deletions

View File

@ -172,6 +172,12 @@ func resourceDockerContainer() *schema.Resource {
ForceNew: true,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
"memory": &schema.Schema{
Type: schema.TypeInt,
Optional: true,

View File

@ -82,6 +82,10 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
createOpts.Config.Volumes = volumes
}
if v, ok := d.GetOk("labels"); ok {
createOpts.Config.Labels = mapLabels(v.(map[string]interface{}))
}
var retContainer *dc.Container
if retContainer, err = client.CreateContainer(createOpts); err != nil {
return fmt.Errorf("Unable to create container: %s", err)
@ -255,6 +259,14 @@ func stringSetToStringSlice(stringSet *schema.Set) []string {
return ret
}
func mapLabels(labels map[string]interface{}) map[string]string {
mapped := make(map[string]string, len(labels))
for k, v := range labels {
mapped[k] = v.(string)
}
return mapped
}
func fetchDockerContainer(name string, client *dc.Client) (*dc.APIContainers, error) {
apiContainers, err := client.ListContainers(dc.ListContainersOptions{All: true})

View File

@ -55,6 +55,11 @@ func TestAccDockerContainer_customized(t *testing.T) {
if c.HostConfig.CPUShares != 512 {
return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares)
}
if c.Config.Labels["env"] != "prod" || c.Config.Labels["role"] != "test" {
return fmt.Errorf("Container does not have the correct labels")
}
return nil
}
@ -129,5 +134,9 @@ resource "docker_container" "foo" {
memory = 128
memory_swap = 128
cpu_shares = 512
labels {
env = "prod"
role = "test"
}
}
`

View File

@ -44,6 +44,7 @@ The following arguments are supported:
`["/usr/bin/myprogram"]`.
* `dns` - (Optional, set of strings) Set of DNS servers.
* `env` - (Optional, set of strings) Environmental variables to set.
* `labels` - (Optional) Key/value pairs to set as labels on the container.
* `links` - (Optional, set of strings) Set of links for link based
connectivity between containers that are running on the same host.
* `hostname` - (Optional, string) Hostname of the container.