provider/docker: Add support for a list of pull_triggers within the docker_image resource. (#10845)

This commit is contained in:
hmcgonig 2017-01-03 11:10:39 -05:00 committed by Paul Stack
parent c7f6b37826
commit 78e7d20bcb
5 changed files with 50 additions and 16 deletions

View File

@ -31,6 +31,16 @@ func resourceDockerImage() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
ConflictsWith: []string{"pull_triggers"},
Deprecated: "Use field pull_triggers instead",
},
"pull_triggers": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
}, },
}, },
} }

View File

@ -188,15 +188,11 @@ 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")
} }
foundImage := searchLocalImages(data, imageName)
if 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) return nil, fmt.Errorf("Unable to pull image %s: %s", imageName, err)
} }
}
foundImage = searchLocalImages(data, imageName) foundImage := searchLocalImages(data, imageName)
if foundImage != nil { if foundImage != nil {
return foundImage, nil return foundImage, nil
} }

View File

@ -89,6 +89,22 @@ func TestAccDockerImage_data(t *testing.T) {
}) })
} }
func TestAccDockerImage_data_pull_trigger(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDockerImageFromDataConfigWithPullTrigger,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foobarbazoo", "latest", contentDigestRegexp),
),
},
},
})
}
func testAccDockerImageDestroy(s *terraform.State) error { 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" {
@ -131,6 +147,16 @@ data "docker_registry_image" "foobarbaz" {
} }
resource "docker_image" "foobarbaz" { resource "docker_image" "foobarbaz" {
name = "${data.docker_registry_image.foobarbaz.name}" name = "${data.docker_registry_image.foobarbaz.name}"
pull_trigger = "${data.docker_registry_image.foobarbaz.sha256_digest}" pull_triggers = ["${data.docker_registry_image.foobarbaz.sha256_digest}"]
}
`
const testAccDockerImageFromDataConfigWithPullTrigger = `
data "docker_registry_image" "foobarbazoo" {
name = "alpine:3.1"
}
resource "docker_image" "foobarbazoo" {
name = "${data.docker_registry_image.foobarbazoo.name}"
pull_trigger = "${data.docker_registry_image.foobarbazoo.sha256_digest}"
} }
` `

View File

@ -23,7 +23,7 @@ data "docker_registry_image" "ubuntu" {
resource "docker_image" "ubuntu" { resource "docker_image" "ubuntu" {
name = "${data.docker_registry_image.ubuntu.name}" name = "${data.docker_registry_image.ubuntu.name}"
pull_trigger = "${data.docker_registry_image.ubuntu.sha256_digest}" pull_triggers = ["${data.docker_registry_image.ubuntu.sha256_digest}"]
} }
``` ```

View File

@ -14,7 +14,7 @@ Pulls a Docker image to a given Docker host from a Docker Registry.
This resource will *not* pull new layers of the image automatically unless used in This resource will *not* pull new layers of the image automatically unless used in
conjunction with [`docker_registry_image`](/docs/providers/docker/d/registry_image.html) conjunction with [`docker_registry_image`](/docs/providers/docker/d/registry_image.html)
data source to update the `pull_trigger` field. data source to update the `pull_triggers` field.
## Example Usage ## Example Usage
@ -36,7 +36,7 @@ data "docker_registry_image" "ubuntu" {
resource "docker_image" "ubuntu" { resource "docker_image" "ubuntu" {
name = "${data.docker_registry_image.ubuntu.name}" name = "${data.docker_registry_image.ubuntu.name}"
pull_trigger = "${data.docker_registry_image.ubuntu.sha256_digest}" pull_triggers = ["${data.docker_registry_image.ubuntu.sha256_digest}"]
} }
``` ```
@ -48,10 +48,12 @@ The following arguments are supported:
* `keep_locally` - (Optional, boolean) If true, then the Docker image won't be * `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 deleted on destroy operation. If this is false, it will delete the image from
the docker local storage on destroy operation. the docker local storage on destroy operation.
* `pull_trigger` - (Optional, string) Used to store the image digest from the * `pull_triggers` - (Optional, list of strings) List of values which cause an
registry and will cause an image pull when changed. Needed when using image pull when changed. This is used to store the image digest from the
the `docker_registry_image` [data source](/docs/providers/docker/d/registry_image.html) registry when using the `docker_registry_image` [data source](/docs/providers/docker/d/registry_image.html)
to trigger an update of the image. to trigger an image update.
* `pull_trigger` - **Deprecated**, use `pull_triggers` instead.
## Attributes Reference ## Attributes Reference