Skip to content

Commit

Permalink
feat: add sha256_digest attribute to image
Browse files Browse the repository at this point in the history
to be able to remove the latest attribute in the next major
  • Loading branch information
mavogel committed Jun 2, 2021
1 parent 640619f commit 812ae72
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/data-sources/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ data "docker_image" "tag_and_digest" {

### Read-Only

- **latest** (String) The ID of the image in the form of `sha256:<hash>` image digest. Do not confuse it with the default `latest` tag.
- **latest** (String, Deprecated) The ID of the image in the form of `sha256:<hash>` image digest. Do not confuse it with the default `latest` tag.
- **sha256_digest** (String) The image sha256 digest in the form of `sha256:<hash>`.


3 changes: 2 additions & 1 deletion docs/resources/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ resource "docker_image" "zoo" {

### Read-Only

- **latest** (String) The ID of the image in the form of `sha256:<hash>` image digest. Do not confuse it with the default `latest` tag.
- **latest** (String, Deprecated) The ID of the image in the form of `sha256:<hash>` image digest. Do not confuse it with the default `latest` tag.
- **output** (String, Deprecated)
- **sha256_digest** (String) The image sha256 digest in the form of `sha256:<hash>`.

<a id="nestedblock--build"></a>
### Nested Schema for `build`
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/data_source_docker_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ func dataSourceDockerImage() *schema.Resource {
Description: "The name of the Docker image, including any tags or SHA256 repo digests.",
Required: true,
},

"latest": {
Type: schema.TypeString,
Description: "The ID of the image in the form of `sha256:<hash>` image digest. Do not confuse it with the default `latest` tag.",
Computed: true,
Deprecated: "Use `sha256_digest` instead",
},
"sha256_digest": {
Type: schema.TypeString,
Description: "The image sha256 digest in the form of `sha256:<hash>`.",
Computed: true,
},
},
}
Expand All @@ -51,6 +56,7 @@ func dataSourceDockerImageRead(ctx context.Context, d *schema.ResourceData, meta
d.SetId(foundImage.ID)
d.Set("name", imageName)
d.Set("latest", foundImage.ID)
d.Set("sha256_digest", foundImage.ID)

return nil
}
4 changes: 4 additions & 0 deletions internal/provider/data_source_docker_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestAccDockerImageDataSource_withSpecificTag(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.docker_image.foo", "name", imageName),
resource.TestCheckResourceAttr("data.docker_image.foo", "latest", "sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657"),
resource.TestCheckResourceAttr("data.docker_image.foo", "sha256_digest", "sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657"),
),
},
},
Expand All @@ -58,6 +59,7 @@ func TestAccDockerImageDataSource_withDefaultTag(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.docker_image.foo", "name", imageName),
resource.TestMatchResourceAttr("data.docker_image.foo", "latest", imageDigestRegexp),
resource.TestMatchResourceAttr("data.docker_image.foo", "sha256_digest", imageDigestRegexp),
),
},
},
Expand All @@ -83,6 +85,7 @@ func TestAccDockerImageDataSource_withSha256Digest(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.docker_image.foo", "name", imageName),
resource.TestMatchResourceAttr("data.docker_image.foo", "latest", imageDigestRegexp),
resource.TestMatchResourceAttr("data.docker_image.foo", "sha256_digest", imageDigestRegexp),
),
},
},
Expand All @@ -107,6 +110,7 @@ func TestAccDockerImageDataSource_withTagAndSha256Digest(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.docker_image.foo", "name", imageName),
resource.TestMatchResourceAttr("data.docker_image.foo", "latest", imageDigestRegexp),
resource.TestMatchResourceAttr("data.docker_image.foo", "sha256_digest", imageDigestRegexp),
),
},
},
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/resource_docker_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func resourceDockerImage() *schema.Resource {
Type: schema.TypeString,
Description: "The ID of the image in the form of `sha256:<hash>` image digest. Do not confuse it with the default `latest` tag.",
Computed: true,
Deprecated: "Use sha256_digest instead",
},

"sha256_digest": {
Type: schema.TypeString,
Description: "The image sha256 digest in the form of `sha256:<hash>`.",
Computed: true,
},

"keep_locally": {
Expand Down
1 change: 1 addition & 0 deletions internal/provider/resource_docker_image_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func resourceDockerImageRead(ctx context.Context, d *schema.ResourceData, meta i
// TODO mavogel: remove the appended name from the ID
d.SetId(foundImage.ID + d.Get("name").(string))
d.Set("latest", foundImage.ID)
d.Set("sha256_digest", foundImage.ID)
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions internal/provider/resource_docker_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestAccDockerImage_private(t *testing.T) {
Config: loadTestConfiguration(t, RESOURCE, "docker_image", "testAddDockerPrivateImageConfig"),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foobar", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.foobar", "sha256_digest", contentDigestRegexp),
testAccImageCreated("docker_image.foobar", &i),
testCheckImageInspect,
),
Expand Down Expand Up @@ -114,6 +115,7 @@ func TestAccDockerImage_destroy(t *testing.T) {
Config: loadTestConfiguration(t, RESOURCE, "docker_image", "testAccDockerImageKeepLocallyConfig"),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foobarzoo", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.foobarzoo", "sha256_digest", contentDigestRegexp),
),
},
},
Expand All @@ -130,6 +132,7 @@ func TestAccDockerImage_data(t *testing.T) {
Config: loadTestConfiguration(t, RESOURCE, "docker_image", "testAccDockerImageFromDataConfig"),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foobarbaz", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.foobarbaz", "sha256_digest", contentDigestRegexp),
),
},
},
Expand All @@ -146,6 +149,7 @@ func TestAccDockerImage_data_pull_trigger(t *testing.T) {
Config: loadTestConfiguration(t, RESOURCE, "docker_image", "testAccDockerImageFromDataConfigWithPullTrigger"),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foobarbazoo", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.foobarbazoo", "sha256_digest", contentDigestRegexp),
),
},
},
Expand All @@ -166,6 +170,7 @@ func TestAccDockerImage_data_private(t *testing.T) {
Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_image", "testAccDockerImageFromDataPrivateConfig"), registry, image),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foo_private", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.foo_private", "sha256_digest", contentDigestRegexp),
),
},
},
Expand All @@ -191,6 +196,7 @@ func TestAccDockerImage_data_private_config_file(t *testing.T) {
Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_image", "testAccDockerImageFromDataPrivateConfigFile"), registry, dockerConfig, image),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foo_private", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.foo_private", "sha256_digest", contentDigestRegexp),
),
},
},
Expand All @@ -216,6 +222,7 @@ func TestAccDockerImage_data_private_config_file_content(t *testing.T) {
Config: fmt.Sprintf(loadTestConfiguration(t, RESOURCE, "docker_image", "testAccDockerImageFromDataPrivateConfigFileContent"), registry, dockerConfig, image),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foo_private", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.foo_private", "sha256_digest", contentDigestRegexp),
),
},
},
Expand All @@ -238,6 +245,7 @@ func TestAccDockerImage_sha265(t *testing.T) {
Config: loadTestConfiguration(t, RESOURCE, "docker_image", "testAddDockerImageWithSHA256RepoDigest"),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foobar", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.foobar", "sha256_digest", contentDigestRegexp),
),
},
},
Expand Down Expand Up @@ -272,6 +280,7 @@ func TestAccDockerImage_tag_sha265(t *testing.T) {
Config: loadTestConfiguration(t, RESOURCE, "docker_image", "testAccDockerImageWithTagAndSHA256RepoDigest"),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.nginx", "latest", contentDigestRegexp),
resource.TestMatchResourceAttr("docker_image.nginx", "sha256_digest", contentDigestRegexp),
),
},
},
Expand Down

0 comments on commit 812ae72

Please sign in to comment.