Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/docker: add option to keep local image on destroy #6376

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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