From c84d111440bb5f6b966c20748692d70fefbc4022 Mon Sep 17 00:00:00 2001 From: KetchupBomb Date: Sat, 24 Aug 2024 23:04:02 +0000 Subject: [PATCH 1/2] fix: fix auto-generated documentation formatting --- docs/index.md | 2 +- docs/resources/config.md | 2 +- docs/resources/container.md | 2 +- docs/resources/network.md | 2 +- docs/resources/plugin.md | 2 +- docs/resources/registry_image.md | 2 +- docs/resources/secret.md | 2 +- docs/resources/service.md | 2 +- docs/resources/volume.md | 2 +- docs/v2_v3_migration.md | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/index.md b/docs/index.md index bef2113ae..79a50855b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -187,4 +187,4 @@ Optional: - `config_file` (String) Path to docker json file for registry auth. Defaults to `~/.docker/config.json`. If `DOCKER_CONFIG` is set, the value of `DOCKER_CONFIG` is used as the path. `config_file` has predencen over all other options. - `config_file_content` (String) Plain content of the docker json file for registry auth. `config_file_content` has precedence over username/password. - `password` (String, Sensitive) Password for the registry. Defaults to `DOCKER_REGISTRY_PASS` env variable if set. -- `username` (String) Username for the registry. Defaults to `DOCKER_REGISTRY_USER` env variable if set. \ No newline at end of file +- `username` (String) Username for the registry. Defaults to `DOCKER_REGISTRY_USER` env variable if set. diff --git a/docs/resources/config.md b/docs/resources/config.md index 074554d6c..03a520eed 100644 --- a/docs/resources/config.md +++ b/docs/resources/config.md @@ -133,4 +133,4 @@ then the import command is as follows ```shell #!/bin/bash terraform import docker_config.foo 08c26c477474478d971139f750984775a7f019dbe8a2e7f09d66a187c009e66d -``` \ No newline at end of file +``` diff --git a/docs/resources/container.md b/docs/resources/container.md index e293ffcd7..d7c21ceb0 100644 --- a/docs/resources/container.md +++ b/docs/resources/container.md @@ -332,4 +332,4 @@ then the import command is as follows ```shell #!/bin/bash terraform import docker_container.foo 9a550c0f0163d39d77222d3efd58701b625d47676c25c686c95b5b92d1cba6fd -``` \ No newline at end of file +``` diff --git a/docs/resources/network.md b/docs/resources/network.md index acf11fdb0..26a5bf440 100644 --- a/docs/resources/network.md +++ b/docs/resources/network.md @@ -96,4 +96,4 @@ then the import command is as follows ```shell #!/bin/bash terraform import docker_network.foo 87b57a9b91ecab2db2a6dbf38df74c67d7c7108cbe479d6576574ec2cd8c2d73 -``` \ No newline at end of file +``` diff --git a/docs/resources/plugin.md b/docs/resources/plugin.md index 42f328e82..be77b489d 100644 --- a/docs/resources/plugin.md +++ b/docs/resources/plugin.md @@ -69,4 +69,4 @@ Import is supported using the following syntax: ```shell #!/bin/bash terraform import docker_plugin.sample-volume-plugin "$(docker plugin inspect -f {{.ID}} tiborvass/sample-volume-plugin:latest)" -``` \ No newline at end of file +``` diff --git a/docs/resources/registry_image.md b/docs/resources/registry_image.md index dc30fec4e..39fd06214 100644 --- a/docs/resources/registry_image.md +++ b/docs/resources/registry_image.md @@ -44,4 +44,4 @@ resource "docker_image" "image" { ### Read-Only - `id` (String) The ID of this resource. -- `sha256_digest` (String) The sha256 digest of the image. \ No newline at end of file +- `sha256_digest` (String) The sha256 digest of the image. diff --git a/docs/resources/secret.md b/docs/resources/secret.md index 93ff1d43a..a5105550e 100644 --- a/docs/resources/secret.md +++ b/docs/resources/secret.md @@ -89,4 +89,4 @@ Import is supported using the following syntax: #!/bin/bash # Docker secret cannot be imported as the secret data, once set, is never exposed again. -``` \ No newline at end of file +``` diff --git a/docs/resources/service.md b/docs/resources/service.md index 496c93bbe..8b786ebb3 100644 --- a/docs/resources/service.md +++ b/docs/resources/service.md @@ -782,4 +782,4 @@ then the import command is as follows ```shell #!/bin/bash terraform import docker_service.foo 4pcphbxkfn2rffhbhe6czytgi -``` \ No newline at end of file +``` diff --git a/docs/resources/volume.md b/docs/resources/volume.md index 44dcb4279..6a9a4d9d4 100644 --- a/docs/resources/volume.md +++ b/docs/resources/volume.md @@ -74,4 +74,4 @@ then the import command is as follows ```shell #!/bin/bash terraform import docker_volume.foo 524b0457aa2a87dd2b75c74c3e4e53f406974249e63ab3ed9bf21e5644f9dc7d -``` \ No newline at end of file +``` diff --git a/docs/v2_v3_migration.md b/docs/v2_v3_migration.md index 437debad3..dbbfa106c 100644 --- a/docs/v2_v3_migration.md +++ b/docs/v2_v3_migration.md @@ -45,4 +45,4 @@ resource "docker_image" "foo_image" { resource "docker_registry_image" "foo" { name = docker_image.foo_image.name } -``` \ No newline at end of file +``` From b78dee91ed67d79a49bd97bd593636e29a6a8f89 Mon Sep 17 00:00:00 2001 From: KetchupBomb Date: Sat, 24 Aug 2024 22:45:13 +0000 Subject: [PATCH 2/2] feat: add new data source for docker_containers --- docs/data-sources/containers.md | 23 +++++++ .../provider/data_source_docker_containers.go | 61 +++++++++++++++++++ .../data_source_docker_containers_test.go | 22 +++++++ internal/provider/provider.go | 1 + .../testAccDockerContainersDataSourceBasic.tf | 1 + 5 files changed, 108 insertions(+) create mode 100644 docs/data-sources/containers.md create mode 100644 internal/provider/data_source_docker_containers.go create mode 100644 internal/provider/data_source_docker_containers_test.go create mode 100644 testdata/data-sources/docker_containers/testAccDockerContainersDataSourceBasic.tf diff --git a/docs/data-sources/containers.md b/docs/data-sources/containers.md new file mode 100644 index 000000000..89f6166ec --- /dev/null +++ b/docs/data-sources/containers.md @@ -0,0 +1,23 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "docker_containers Data Source - terraform-provider-docker" +subcategory: "" +description: |- + docker_containers provides details about existing containers. +--- + +# docker_containers (Data Source) + +`docker_containers` provides details about existing containers. + + + + +## Schema + +### Read-Only + +- `containers` (List of String) The list of JSON-encoded containers currently running on the docker host. +- `id` (String) The ID of this resource. + + diff --git a/internal/provider/data_source_docker_containers.go b/internal/provider/data_source_docker_containers.go new file mode 100644 index 000000000..37f851ae6 --- /dev/null +++ b/internal/provider/data_source_docker_containers.go @@ -0,0 +1,61 @@ +package provider + +import ( + "context" + "crypto/md5" + "encoding/hex" + "encoding/json" + "sort" + "strings" + + "github.com/docker/docker/api/types" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceDockerContainers() *schema.Resource { + return &schema.Resource{ + Description: "`docker_containers` provides details about existing containers.", + + ReadContext: dataSourceDockerContainersRead, + + Schema: map[string]*schema.Schema{ + "containers": { + Type: schema.TypeList, + Description: "The list of JSON-encoded containers currently running on the docker host.", + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceDockerContainersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + client := meta.(*ProviderConfig).DockerClient + + containers, err := client.ContainerList(ctx, types.ContainerListOptions{All: true}) + if err != nil { + return diag.Errorf("Could not list docker containers: %v", err) + } + + var jsons []string + for _, container := range containers { + json, err := json.Marshal(container) + if err != nil { + return diag.Errorf("Could not marshal JSON from container ID %s: %v", container.ID, err) + } + jsons = append(jsons, string(json)) + } + + // Sort the JSON strings to ensure consistent order for hash calculation + sort.Strings(jsons) + if err := d.Set("containers", jsons); err != nil { + return diag.Errorf("Failed to set containers: %v", err) + } + + md5sum := md5.Sum([]byte(strings.Join(jsons, ""))) + digest := hex.EncodeToString(md5sum[:]) + d.SetId(digest) + + return nil +} diff --git a/internal/provider/data_source_docker_containers_test.go b/internal/provider/data_source_docker_containers_test.go new file mode 100644 index 000000000..cc11a3867 --- /dev/null +++ b/internal/provider/data_source_docker_containers_test.go @@ -0,0 +1,22 @@ +package provider + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDockerContainersDataSource_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: loadTestConfiguration(t, DATA_SOURCE, "docker_containers", "testAccDockerContainersDataSourceBasic"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.docker_containers.this", "containers"), + ), + }, + }, + }) +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index b52deabbd..d7312556c 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -161,6 +161,7 @@ func New(version string) func() *schema.Provider { "docker_plugin": dataSourceDockerPlugin(), "docker_image": dataSourceDockerImage(), "docker_logs": dataSourceDockerLogs(), + "docker_containers": dataSourceDockerContainers(), }, } diff --git a/testdata/data-sources/docker_containers/testAccDockerContainersDataSourceBasic.tf b/testdata/data-sources/docker_containers/testAccDockerContainersDataSourceBasic.tf new file mode 100644 index 000000000..ae6c1ece4 --- /dev/null +++ b/testdata/data-sources/docker_containers/testAccDockerContainersDataSourceBasic.tf @@ -0,0 +1 @@ +data "docker_containers" "this" {}