Skip to content

Commit ad8b1fd

Browse files
authored
feat: convert datasource to resource (#15)
- Convert datasource to resource - Update examples - Update tests
1 parent abf099b commit ad8b1fd

File tree

8 files changed

+528
-302
lines changed

8 files changed

+528
-302
lines changed

docs/data-sources/cached_image.md renamed to docs/resources/cached_image.md

+5-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
---
22
# generated by https://github.com/hashicorp/terraform-plugin-docs
3-
page_title: "envbuilder_cached_image Data Source - envbuilder"
3+
page_title: "envbuilder_cached_image Resource - envbuilder"
44
subcategory: ""
55
description: |-
6-
The cached image data source can be used to retrieve a cached image produced by envbuilder. Reading from this data source will clone the specified Git repository, read a Devcontainer specification or Dockerfile, and check for its presence in the provided cache repo.
6+
The cached image resource can be used to retrieve a cached image produced by envbuilder. Creating this resource will clone the specified Git repository, read a Devcontainer specification or Dockerfile, and check for its presence in the provided cache repo. If any of the layers of the cached image are missing in the provided cache repo, the image will be considered as missing. A cached image in this state will be recreated until found.
77
---
88

9-
# envbuilder_cached_image (Data Source)
9+
# envbuilder_cached_image (Resource)
1010

11-
The cached image data source can be used to retrieve a cached image produced by envbuilder. Reading from this data source will clone the specified Git repository, read a Devcontainer specification or Dockerfile, and check for its presence in the provided cache repo.
11+
The cached image resource can be used to retrieve a cached image produced by envbuilder. Creating this resource will clone the specified Git repository, read a Devcontainer specification or Dockerfile, and check for its presence in the provided cache repo. If any of the layers of the cached image are missing in the provided cache repo, the image will be considered as missing. A cached image in this state will be recreated until found.
1212

13-
## Example Usage
1413

15-
```terraform
16-
data "envbuilder_cached_image" "example" {
17-
builder_image = "ghcr.io/coder/envbuilder:latest"
18-
git_url = "https://github.com/coder/envbuilder-starter-devcontainer"
19-
cache_repo = "localhost:5000/local/test-cache"
20-
extra_env = {
21-
"ENVBUILDER_VERBOSE" : "true"
22-
}
23-
}
24-
25-
resource "docker_container" "container" {
26-
image = envbuilder_cached_image.example.image
27-
env = data.envbuilder_image.cached.env
28-
}
29-
```
3014

3115
<!-- schema generated by tfplugindocs -->
3216
## Schema
@@ -63,7 +47,7 @@ resource "docker_container" "container" {
6347

6448
### Read-Only
6549

66-
- `env` (List of String) Computed envbuilder configuration to be set for the container.
50+
- `env` (List of String, Sensitive) Computed envbuilder configuration to be set for the container. May contain secrets.
6751
- `exists` (Boolean) Whether the cached image was exists or not for the given config.
6852
- `id` (String) Cached image identifier. This will generally be the image's SHA256 digest.
6953
- `image` (String) Outputs the cached image repo@digest if it exists, and builder image otherwise.

examples/data-sources/envbuilder_cached_image/data-source.tf

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// The below example illustrates the behavior of the envbuilder_cached_image
2+
// resource.
3+
// 1) Run a local registry:
4+
//
5+
// ```shell
6+
// docker run -d -p 5000:5000 --name test-registry registry:2
7+
// ```
8+
//
9+
// 2) Running a `terraform plan` should result in the following outputs:
10+
//
11+
// ```
12+
// + builder_image = "ghcr.io/coder/envbuilder-preview:latest"
13+
// + exists = (known after apply)
14+
// + id = (known after apply)
15+
// + image = (known after apply)
16+
// ```
17+
//
18+
// 3) Running `terraform apply` should result in outputs similar to the below:
19+
//
20+
// ```
21+
// builder_image = "ghcr.io/coder/envbuilder-preview:latest"
22+
// exists = false
23+
// id = "00000000-0000-0000-0000-000000000000"
24+
// image = "ghcr.io/coder/envbuilder-preview:latest"
25+
// ```
26+
//
27+
// 4) Populate the cache by running Envbuilder and pushing the built image to
28+
// the local registry:
29+
//
30+
// ```shell
31+
// docker run -it --rm \
32+
// -e ENVBUILDER_CACHE_REPO=localhost:5000/test \
33+
// -e ENVBUILDER_GIT_URL=https://github.com/coder/envbuilder-starter-devcontainer \
34+
// -e ENVBUILDER_PUSH_IMAGE=true \
35+
// -e ENVBUILDER_INIT_SCRIPT=exit \
36+
// --net=host \
37+
// ghcr.io/coder/envbuilder-preview:latest
38+
// ```
39+
//
40+
// 5) Run `terraform plan` once more. Now, the cached image will be detected:
41+
//
42+
// ```
43+
// Note: Objects have changed outside of Terraform
44+
//
45+
// Terraform detected the following changes made outside of Terraform since the last "terraform apply" which may have affected this plan:
46+
// envbuilder_cached_image.example has been deleted
47+
// - resource "envbuilder_cached_image" "example" {
48+
// - exists = false -> null
49+
// - id = "00000000-0000-0000-0000-000000000000" -> null
50+
// - image = "ghcr.io/coder/envbuilder-preview:latest" -> null
51+
// # (5 unchanged attributes hidden)
52+
// ```
53+
//
54+
// 6) Run `terraform apply` and the newly pushed image will be saved in the Terraform state:
55+
// ```shell
56+
// builder_image = "ghcr.io/coder/envbuilder-preview:latest"
57+
// exists = true
58+
// id = "sha256:xxx..."
59+
// image = "localhost:5000/test@sha256:xxx..."
60+
// ```
61+
62+
terraform {
63+
required_providers {
64+
envbuilder = {
65+
source = "coder/envbuilder"
66+
}
67+
}
68+
}
69+
70+
resource "envbuilder_cached_image" "example" {
71+
builder_image = "ghcr.io/coder/envbuilder-preview:latest"
72+
git_url = "https://github.com/coder/envbuilder-starter-devcontainer"
73+
cache_repo = "localhost:5000/test"
74+
extra_env = {
75+
"ENVBUILDER_VERBOSE" : "true"
76+
}
77+
}
78+
79+
output "builder_image" {
80+
value = envbuilder_cached_image.example.builder_image
81+
}
82+
83+
output "exists" {
84+
value = envbuilder_cached_image.example.exists
85+
}
86+
87+
output "id" {
88+
value = envbuilder_cached_image.example.id
89+
}
90+
91+
output "image" {
92+
value = envbuilder_cached_image.example.image
93+
}

internal/provider/cached_image_data_source_test.go

-128
This file was deleted.

0 commit comments

Comments
 (0)