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: Support to docker config labels #543

Open
wants to merge 1 commit 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
14 changes: 13 additions & 1 deletion docs/resources/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,22 @@ resource "docker_service" "service" {
- `data` (String) Base64-url-safe-encoded config data
- `name` (String) User-defined name of the config

### Optional

- `labels` (Block Set) User-defined key/value metadata (see [below for nested schema](#nestedblock--labels))

### Read-Only

- `id` (String) The ID of this resource.

<a id="nestedblock--labels"></a>
### Nested Schema for `labels`

Required:

- `label` (String) Name of the label
- `value` (String) Value of the label

## Import

Import is supported using the following syntax by providing the `id`:
Expand Down Expand Up @@ -133,4 +145,4 @@ then the import command is as follows
```shell
#!/bin/bash
terraform import docker_config.foo 08c26c477474478d971139f750984775a7f019dbe8a2e7f09d66a187c009e66d
```
```
13 changes: 13 additions & 0 deletions internal/provider/resource_docker_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func resourceDockerConfig() *schema.Resource {
ForceNew: true,
ValidateDiagFunc: validateStringIsBase64Encoded(),
},

"labels": {
Type: schema.TypeSet,
Description: "User-defined key/value metadata",
Optional: true,
ForceNew: true,
Elem: labelSchema,
},
},
}
}
Expand All @@ -52,6 +60,10 @@ func resourceDockerConfigCreate(ctx context.Context, d *schema.ResourceData, met
Data: data,
}

if v, ok := d.GetOk("labels"); ok {
configSpec.Annotations.Labels = labelSetToMap(v.(*schema.Set))
}

config, err := client.ConfigCreate(ctx, configSpec)
if err != nil {
return diag.FromErr(err)
Expand All @@ -76,6 +88,7 @@ func resourceDockerConfigRead(ctx context.Context, d *schema.ResourceData, meta
d.SetId(config.ID)
d.Set("name", config.Spec.Name)
d.Set("data", base64.StdEncoding.EncodeToString(config.Spec.Data))
d.Set("labels", mapToLabelSet(config.Spec.Labels))
return nil
}

Expand Down
35 changes: 35 additions & 0 deletions internal/provider/resource_docker_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,38 @@ func testAccServiceConfigCreated(resourceName string, config *swarm.Config) reso

}
}

func TestAccDockerConfig_labels(t *testing.T) {
ctx := context.Background()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: func(state *terraform.State) error {
return testCheckDockerSecretDestroy(ctx, state)
},
Steps: []resource.TestStep{
{
Config: `
resource "docker_config" "foo" {
name = "foo-config"
data = "Ymxhc2RzYmxhYmxhMTI0ZHNkd2VzZA=="
labels {
label = "test1"
value = "foo"
}
labels {
label = "test2"
value = "bar"
}
}
`,
Check: testCheckLabelMap("docker_config.foo", "labels",
map[string]string{
"test1": "foo",
"test2": "bar",
},
),
},
},
})
}