Merge pull request #4148 from hashicorp/b-use-container-id

provider/docker: locate container via ID not name
This commit is contained in:
James Nugent 2015-12-02 17:39:41 -05:00
commit f7f089940b
1 changed files with 4 additions and 17 deletions

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
dc "github.com/fsouza/go-dockerclient" dc "github.com/fsouza/go-dockerclient"
@ -160,7 +159,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error { func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dc.Client) client := meta.(*dc.Client)
apiContainer, err := fetchDockerContainer(d.Get("name").(string), client) apiContainer, err := fetchDockerContainer(d.Id(), client)
if err != nil { if err != nil {
return err return err
} }
@ -268,7 +267,7 @@ func mapTypeMapValsToString(typeMap map[string]interface{}) map[string]string {
return mapped return mapped
} }
func fetchDockerContainer(name string, client *dc.Client) (*dc.APIContainers, error) { func fetchDockerContainer(ID string, client *dc.Client) (*dc.APIContainers, error) {
apiContainers, err := client.ListContainers(dc.ListContainersOptions{All: true}) apiContainers, err := client.ListContainers(dc.ListContainersOptions{All: true})
if err != nil { if err != nil {
@ -276,21 +275,9 @@ func fetchDockerContainer(name string, client *dc.Client) (*dc.APIContainers, er
} }
for _, apiContainer := range apiContainers { for _, apiContainer := range apiContainers {
// Sometimes the Docker API prefixes container names with / if apiContainer.ID == ID {
// like it does in these commands. But if there's no
// set name, it just uses the ID without a /...ugh.
switch len(apiContainer.Names) {
case 0:
if apiContainer.ID == name {
return &apiContainer, nil return &apiContainer, nil
} }
default:
for _, containerName := range apiContainer.Names {
if strings.TrimLeft(containerName, "/") == name {
return &apiContainer, nil
}
}
}
} }
return nil, nil return nil, nil