From 09333e5e769d62a52bc4097556779871b46c1098 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 28 Mar 2015 18:37:20 -0700 Subject: [PATCH] providres/docker: cache client --- builtin/providers/docker/config.go | 15 ++++------ builtin/providers/docker/provider.go | 2 +- .../docker/resource_docker_container_funcs.go | 27 ++++-------------- .../docker/resource_docker_image_funcs.go | 28 ++++++++----------- 4 files changed, 24 insertions(+), 48 deletions(-) diff --git a/builtin/providers/docker/config.go b/builtin/providers/docker/config.go index ed13314a2..199182744 100644 --- a/builtin/providers/docker/config.go +++ b/builtin/providers/docker/config.go @@ -6,14 +6,11 @@ import ( dc "github.com/fsouza/go-dockerclient" ) +// Config is the structure that stores the configuration to talk to a +// Docker API compatible host. type Config struct { Host string CertPath string - SkipPull bool -} - -type Data struct { - DockerImages map[string]*dc.APIImages } // NewClient() returns a new Docker client. @@ -30,9 +27,7 @@ func (c *Config) NewClient() (*dc.Client, error) { return dc.NewTLSClient(c.Host, cert, key, ca) } -// NewData() returns a new data struct. -func (c *Config) NewData() *Data { - return &Data{ - DockerImages: map[string]*dc.APIImages{}, - } +// Data ia structure for holding data that we fetch from Docker. +type Data struct { + DockerImages map[string]*dc.APIImages } diff --git a/builtin/providers/docker/provider.go b/builtin/providers/docker/provider.go index 77da4bf15..15bc840a4 100644 --- a/builtin/providers/docker/provider.go +++ b/builtin/providers/docker/provider.go @@ -38,5 +38,5 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { CertPath: d.Get("cert_path").(string), } - return &config, nil + return config.NewClient() } diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index d0daa08bb..17a8e4eed 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -11,16 +11,11 @@ import ( ) func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) + var err error + client := meta.(*dc.Client) - client, err := config.NewClient() - if err != nil { - return fmt.Errorf("Unable to connect to Docker: %s", err) - } - - data := config.NewData() - - if err := fetchLocalImages(data, client); err != nil { + var data Data + if err := fetchLocalImages(&data, client); err != nil { return err } @@ -116,12 +111,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err } func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - - client, err := config.NewClient() - if err != nil { - return fmt.Errorf("Unable to connect to Docker: %s", err) - } + client := meta.(*dc.Client) apiContainer, err := fetchDockerContainer(d.Get("name").(string), client) if err != nil { @@ -152,12 +142,7 @@ func resourceDockerContainerUpdate(d *schema.ResourceData, meta interface{}) err } func resourceDockerContainerDelete(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - - client, err := config.NewClient() - if err != nil { - return fmt.Errorf("Unable to connect to Docker: %s", err) - } + client := meta.(*dc.Client) removeOpts := dc.RemoveContainerOptions{ ID: d.Id(), diff --git a/builtin/providers/docker/resource_docker_image_funcs.go b/builtin/providers/docker/resource_docker_image_funcs.go index a34f21994..2c7470db0 100644 --- a/builtin/providers/docker/resource_docker_image_funcs.go +++ b/builtin/providers/docker/resource_docker_image_funcs.go @@ -9,9 +9,8 @@ import ( ) func resourceDockerImageCreate(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - - apiImage, err := findImage(d, config) + client := meta.(*dc.Client) + apiImage, err := findImage(d, client) if err != nil { return fmt.Errorf("Unable to read Docker image into resource: %s", err) } @@ -23,9 +22,8 @@ func resourceDockerImageCreate(d *schema.ResourceData, meta interface{}) error { } func resourceDockerImageRead(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - - apiImage, err := findImage(d, config) + client := meta.(*dc.Client) + apiImage, err := findImage(d, client) if err != nil { return fmt.Errorf("Unable to read Docker image into resource: %s", err) } @@ -53,6 +51,10 @@ func fetchLocalImages(data *Data, client *dc.Client) error { return fmt.Errorf("Unable to list Docker images: %s", err) } + if data.DockerImages == nil { + data.DockerImages = make(map[string]*dc.APIImages) + } + // Docker uses different nomenclatures in different places...sometimes a short // ID, sometimes long, etc. So we store both in the map so we can always find // the same image object. We store the tags, too. @@ -132,15 +134,9 @@ func getImageTag(image string) string { return "" } -func findImage(d *schema.ResourceData, config *Config) (*dc.APIImages, error) { - client, err := config.NewClient() - if err != nil { - return nil, fmt.Errorf("Unable to connect to Docker: %s", err) - } - - data := config.NewData() - - if err := fetchLocalImages(data, client); err != nil { +func findImage(d *schema.ResourceData, client *dc.Client) (*dc.APIImages, error) { + var data Data + if err := fetchLocalImages(&data, client); err != nil { return nil, err } @@ -163,7 +159,7 @@ func findImage(d *schema.ResourceData, config *Config) (*dc.APIImages, error) { foundImage := searchLocal() if d.Get("keep_updated").(bool) || foundImage == nil { - if err := pullImage(data, client, imageName); err != nil { + if err := pullImage(&data, client, imageName); err != nil { return nil, fmt.Errorf("Unable to pull image %s: %s", imageName, err) } }