Skip to content

Commit

Permalink
Merge pull request #4148 from hashicorp/b-use-container-id
Browse files Browse the repository at this point in the history
provider/docker: locate container via ID not name
  • Loading branch information
jen20 committed Dec 2, 2015
2 parents d6ae527 + 597fafb commit f7f0899
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions builtin/providers/docker/resource_docker_container_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"time"

dc "github.com/fsouza/go-dockerclient"
Expand Down Expand Up @@ -160,7 +159,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dc.Client)

apiContainer, err := fetchDockerContainer(d.Get("name").(string), client)
apiContainer, err := fetchDockerContainer(d.Id(), client)
if err != nil {
return err
}
Expand Down Expand Up @@ -268,28 +267,16 @@ func mapTypeMapValsToString(typeMap map[string]interface{}) map[string]string {
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})

if err != nil {
return nil, fmt.Errorf("Error fetching container information from Docker: %s\n", err)
}

for _, apiContainer := range apiContainers {
// Sometimes the Docker API prefixes container names with /
// 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
}
default:
for _, containerName := range apiContainer.Names {
if strings.TrimLeft(containerName, "/") == name {
return &apiContainer, nil
}
}
if apiContainer.ID == ID {
return &apiContainer, nil
}
}

Expand Down

0 comments on commit f7f0899

Please sign in to comment.