providres/docker: cache client
This commit is contained in:
parent
7c253155c1
commit
09333e5e76
|
@ -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
|
||||
}
|
||||
|
|
|
@ -38,5 +38,5 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|||
CertPath: d.Get("cert_path").(string),
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
return config.NewClient()
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue