Merge pull request #5801 from rhyas/docker_image_destroy

Fix Image Destroy bug. #3609 #3771
This commit is contained in:
Paul Stack 2016-04-03 19:17:52 -05:00
commit fa8ac5a1dc
2 changed files with 69 additions and 17 deletions

View File

@ -41,10 +41,49 @@ func resourceDockerImageUpdate(d *schema.ResourceData, meta interface{}) error {
} }
func resourceDockerImageDelete(d *schema.ResourceData, meta interface{}) error { func resourceDockerImageDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dc.Client)
err := removeImage(d, client)
if err != nil {
return fmt.Errorf("Unable to remove Docker image: %s", err)
}
d.SetId("") d.SetId("")
return nil return nil
} }
func searchLocalImages(data Data, imageName string) *dc.APIImages {
if apiImage, ok := data.DockerImages[imageName]; ok {
return apiImage
}
if apiImage, ok := data.DockerImages[imageName+":latest"]; ok {
imageName = imageName + ":latest"
return apiImage
}
return nil
}
func removeImage(d *schema.ResourceData, client *dc.Client) error {
var data Data
if err := fetchLocalImages(&data, client); err != nil {
return err
}
imageName := d.Get("name").(string)
if imageName == "" {
return fmt.Errorf("Empty image name is not allowed")
}
foundImage := searchLocalImages(data, imageName)
if foundImage != nil {
err := client.RemoveImage(foundImage.ID)
if err != nil {
return err
}
}
return nil
}
func fetchLocalImages(data *Data, client *dc.Client) error { func fetchLocalImages(data *Data, client *dc.Client) error {
images, err := client.ListImages(dc.ListImagesOptions{All: false}) images, err := client.ListImages(dc.ListImagesOptions{All: false})
if err != nil { if err != nil {
@ -146,18 +185,7 @@ func findImage(d *schema.ResourceData, client *dc.Client) (*dc.APIImages, error)
return nil, fmt.Errorf("Empty image name is not allowed") return nil, fmt.Errorf("Empty image name is not allowed")
} }
searchLocal := func() *dc.APIImages { foundImage := searchLocalImages(data, imageName)
if apiImage, ok := data.DockerImages[imageName]; ok {
return apiImage
}
if apiImage, ok := data.DockerImages[imageName+":latest"]; ok {
imageName = imageName + ":latest"
return apiImage
}
return nil
}
foundImage := searchLocal()
if d.Get("keep_updated").(bool) || foundImage == nil { if d.Get("keep_updated").(bool) || foundImage == nil {
if err := pullImage(&data, client, imageName); err != nil { if err := pullImage(&data, client, imageName); err != nil {
@ -165,7 +193,7 @@ func findImage(d *schema.ResourceData, client *dc.Client) (*dc.APIImages, error)
} }
} }
foundImage = searchLocal() foundImage = searchLocalImages(data, imageName)
if foundImage != nil { if foundImage != nil {
return foundImage, nil return foundImage, nil
} }

View File

@ -1,10 +1,13 @@
package docker package docker
import ( import (
"fmt"
"regexp" "regexp"
"testing" "testing"
dc "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
) )
var contentDigestRegexp = regexp.MustCompile(`\A[A-Za-z0-9_\+\.-]+:[A-Fa-f0-9]+\z`) var contentDigestRegexp = regexp.MustCompile(`\A[A-Za-z0-9_\+\.-]+:[A-Fa-f0-9]+\z`)
@ -13,6 +16,7 @@ func TestAccDockerImage_basic(t *testing.T) {
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccDockerImageDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccDockerImageConfig, Config: testAccDockerImageConfig,
@ -28,6 +32,7 @@ func TestAccDockerImage_private(t *testing.T) {
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccDockerImageDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAddDockerPrivateImageConfig, Config: testAddDockerPrivateImageConfig,
@ -39,6 +44,25 @@ func TestAccDockerImage_private(t *testing.T) {
}) })
} }
func testAccDockerImageDestroy(s *terraform.State) error {
//client := testAccProvider.Meta().(*dc.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "docker_image" {
continue
}
client := testAccProvider.Meta().(*dc.Client)
_, err := client.InspectImage(rs.Primary.Attributes["latest"])
if err == nil {
return fmt.Errorf("Image still exists")
} else if err != dc.ErrNoSuchImage {
return err
}
}
return nil
}
const testAccDockerImageConfig = ` const testAccDockerImageConfig = `
resource "docker_image" "foo" { resource "docker_image" "foo" {
name = "alpine:3.1" name = "alpine:3.1"