Provider Docker: (#6376)

- Add option keep_locally
- Add unit test
- Add documentation
This commit is contained in:
Xavier Sellier 2016-04-27 12:18:02 -04:00 committed by Paul Stack
parent 813f2ca708
commit e4a1d21a4b
4 changed files with 49 additions and 2 deletions

View File

@ -26,6 +26,11 @@ func resourceDockerImage() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"keep_locally": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
}, },
} }
} }

View File

@ -63,6 +63,11 @@ func searchLocalImages(data Data, imageName string) *dc.APIImages {
func removeImage(d *schema.ResourceData, client *dc.Client) error { func removeImage(d *schema.ResourceData, client *dc.Client) error {
var data Data var data Data
if keepLocally := d.Get("keep_locally").(bool); keepLocally {
return nil
}
if err := fetchLocalImages(&data, client); err != nil { if err := fetchLocalImages(&data, client); err != nil {
return err return err
} }

View File

@ -44,9 +44,36 @@ func TestAccDockerImage_private(t *testing.T) {
}) })
} }
func testAccDockerImageDestroy(s *terraform.State) error { func TestAccDockerImage_destroy(t *testing.T) {
//client := testAccProvider.Meta().(*dc.Client) 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 { for _, rs := range s.RootModule().Resources {
if rs.Type != "docker_image" { if rs.Type != "docker_image" {
continue continue
@ -76,3 +103,10 @@ resource "docker_image" "foobar" {
keep_updated = true keep_updated = true
} }
` `
const testAccDockerImageKeepLocallyConfig = `
resource "docker_image" "foobarzoo" {
name = "crux:3.1"
keep_locally = true
}
`

View File

@ -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 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 image is downloaded with the correct tag, it won't be redownloaded if
there is a newer image. 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 ## Attributes Reference