Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new data source for docker_containers #644

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/data-sources/containers.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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.


2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- `username` (String) Username for the registry. Defaults to `DOCKER_REGISTRY_USER` env variable if set.
2 changes: 1 addition & 1 deletion docs/resources/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ then the import command is as follows
```shell
#!/bin/bash
terraform import docker_config.foo 08c26c477474478d971139f750984775a7f019dbe8a2e7f09d66a187c009e66d
```
```
2 changes: 1 addition & 1 deletion docs/resources/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,4 @@ then the import command is as follows
```shell
#!/bin/bash
terraform import docker_container.foo 9a550c0f0163d39d77222d3efd58701b625d47676c25c686c95b5b92d1cba6fd
```
```
2 changes: 1 addition & 1 deletion docs/resources/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ then the import command is as follows
```shell
#!/bin/bash
terraform import docker_network.foo 87b57a9b91ecab2db2a6dbf38df74c67d7c7108cbe479d6576574ec2cd8c2d73
```
```
2 changes: 1 addition & 1 deletion docs/resources/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
```
```
2 changes: 1 addition & 1 deletion docs/resources/registry_image.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- `sha256_digest` (String) The sha256 digest of the image.
2 changes: 1 addition & 1 deletion docs/resources/secret.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
```
2 changes: 1 addition & 1 deletion docs/resources/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -782,4 +782,4 @@ then the import command is as follows
```shell
#!/bin/bash
terraform import docker_service.foo 4pcphbxkfn2rffhbhe6czytgi
```
```
2 changes: 1 addition & 1 deletion docs/resources/volume.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ then the import command is as follows
```shell
#!/bin/bash
terraform import docker_volume.foo 524b0457aa2a87dd2b75c74c3e4e53f406974249e63ab3ed9bf21e5644f9dc7d
```
```
2 changes: 1 addition & 1 deletion docs/v2_v3_migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ resource "docker_image" "foo_image" {
resource "docker_registry_image" "foo" {
name = docker_image.foo_image.name
}
```
```
61 changes: 61 additions & 0 deletions internal/provider/data_source_docker_containers.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions internal/provider/data_source_docker_containers_test.go
Original file line number Diff line number Diff line change
@@ -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"),
),
},
},
})
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func New(version string) func() *schema.Provider {
"docker_plugin": dataSourceDockerPlugin(),
"docker_image": dataSourceDockerImage(),
"docker_logs": dataSourceDockerLogs(),
"docker_containers": dataSourceDockerContainers(),
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "docker_containers" "this" {}