Skip to content

Commit

Permalink
Provider Docker: (#6376)
Browse files Browse the repository at this point in the history
- Add option keep_locally
- Add unit test
- Add documentation
  • Loading branch information
Xavier Sellier authored and stack72 committed Apr 27, 2016
1 parent 813f2ca commit e4a1d21
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions builtin/providers/docker/resource_docker_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func resourceDockerImage() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"keep_locally": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
},
}
}
5 changes: 5 additions & 0 deletions builtin/providers/docker/resource_docker_image_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func searchLocalImages(data Data, imageName string) *dc.APIImages {

func removeImage(d *schema.ResourceData, client *dc.Client) error {
var data Data

if keepLocally := d.Get("keep_locally").(bool); keepLocally {
return nil
}

if err := fetchLocalImages(&data, client); err != nil {
return err
}
Expand Down
38 changes: 36 additions & 2 deletions builtin/providers/docker/resource_docker_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,36 @@ func TestAccDockerImage_private(t *testing.T) {
})
}

func testAccDockerImageDestroy(s *terraform.State) error {
//client := testAccProvider.Meta().(*dc.Client)
func TestAccDockerImage_destroy(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(s *terraform.State) error {
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 err
}
}
return nil
},
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDockerImageKeepLocallyConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foobarzoo", "latest", contentDigestRegexp),
),
},
},
})
}

func testAccDockerImageDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "docker_image" {
continue
Expand Down Expand Up @@ -76,3 +103,10 @@ resource "docker_image" "foobar" {
keep_updated = true
}
`

const testAccDockerImageKeepLocallyConfig = `
resource "docker_image" "foobarzoo" {
name = "crux:3.1"
keep_locally = true
}
`
3 changes: 3 additions & 0 deletions website/source/docs/providers/docker/r/image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ The following arguments are supported:
always be updated on the host to the latest. If this is false, as long as an
image is downloaded with the correct tag, it won't be redownloaded if
there is a newer image.
* `keep_locally` - (Optional, boolean) If true, then the Docker image won't be
deleted on destroy operation. If this is false, it will delete the image from
the docker local storage on destroy operation.

## Attributes Reference

Expand Down

0 comments on commit e4a1d21

Please sign in to comment.