mirror of
https://github.com/kreuzwerker/terraform-provider-docker.git
synced 2026-01-28 17:37:34 -05:00
add label support to docker container resource
This commit is contained in:
parent
38eec5fbc7
commit
8f9b151811
3 changed files with 27 additions and 0 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
`
|
||||
|
|
|
|||
Loading…
Reference in a new issue