mirror of
https://github.com/kreuzwerker/terraform-provider-docker.git
synced 2025-12-21 07:09:48 -05:00
* chore: add tfplugindocs tool
* feat: add tfplugin doc dependency and make target
* chore: apply documentation generation
* docs(contributing): update for documentation generation
* fix: adapt website-lint target to new do folder
* docs(network): update ds descriptions
* docs: add template for index.md
* docs: add network resource generation
* chore(ci): updates paths for website checks
* docs: add plugin data source generation
* docs: add import cmd for network resource
* docs: add plugin resource generation
* feat: outlines remaining resources with example and import cmd
* feat: add descriptions to docs
* chore: add DevSkim ignores and fix capitalized errors
* docs: complete ds registry image
* docs: add container resource generation
* docs: add lables description to missing resources
* docs: remove computed:true from network data
so the list is rendered in the description
* Revert "docs: remove computed:true from network data"
This reverts commit dce9b7a5a2.
* docs: add docker image descriptions to generate the docs
* docs: add docker registry image descriptions to generate the docs
* docs: add docker service descriptions to generate the docs
* docs: add docker volume descriptions to generate the docs
* docs(index): clarifies description
so more docker resources are mentioned
* docs(network): fixes required and read-only attributes
so the ds can only be read by-name
* docs(plugin): clarifies the ds docs attributes
* docs: fix typo registry image ds
* docs(config): clarifies attributes and enhances examples
Provide a long example and import command
* fix(config): make data non-sensitive
Because only secrets data is
* docs(containter): clarifies attributes
and enhances examples with import
* docs(config): fix typo
* docs(image): clarifies attributes and remove import
* docs(network): clarifies attributes and adapts import
* docs(plugin): clarifies attributes and import
* docs(registry_image): clarifies attributes and removes import
* chore(secret): remove typo
* docs(service): clarifies attributes and import
* docs(volume): clarifies attributes and import
* fix: correct md linter rules after doc gen
* docs(volume): regenerated
* docs: add config custom template
* docs: add templates for all resources
* docs(config): templates all sections and examples
for better redability and structure
* docs(config): fix md linter
* docs(container): templates all sections and examples
* docs(image): templates all sections and examples
* docs(image): fix import resource by renaming
* docs(network): templates all sections and examples
* docs(service): templates all sections and examples
* docs(volume): templates all sections and examples
* fix(lint): replace website with doc directory
* fix(ci): link check file extension check
* fix: markdown links
* chore: remove old website folder
* chore: fix website-lint terrafmr dir and pattern
* fix: lint fix target website folder
* fix: website links
* docs(provider): update examples
with templates on auth and certs
* docs(provider): add tf-plugin-docs line
* docs(contributing): split doc generation section
* docs: final brush up for readability and structure
* chore(ci): add website-generation job
to see if files changed and it should run locally again
* chore(ci): remove explicit docker setup
from website lint because it's installed by default
169 lines
5 KiB
Go
169 lines
5 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/volume"
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
|
)
|
|
|
|
const (
|
|
volumeReadRefreshTimeout = 30 * time.Second
|
|
volumeReadRefreshWaitBeforeRefreshes = 5 * time.Second
|
|
volumeReadRefreshDelay = 2 * time.Second
|
|
)
|
|
|
|
func resourceDockerVolume() *schema.Resource {
|
|
return &schema.Resource{
|
|
Description: "Creates and destroys a volume in Docker. This can be used alongside [docker_container](container.md) to prepare volumes that can be shared across containers.",
|
|
|
|
CreateContext: resourceDockerVolumeCreate,
|
|
ReadContext: resourceDockerVolumeRead,
|
|
DeleteContext: resourceDockerVolumeDelete,
|
|
Importer: &schema.ResourceImporter{
|
|
StateContext: schema.ImportStatePassthroughContext,
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"name": {
|
|
Type: schema.TypeString,
|
|
Description: "The name of the Docker volume (will be generated if not provided).",
|
|
Optional: true,
|
|
Computed: true,
|
|
ForceNew: true,
|
|
},
|
|
"labels": {
|
|
Type: schema.TypeSet,
|
|
Description: "User-defined key/value metadata",
|
|
Optional: true,
|
|
ForceNew: true,
|
|
Elem: labelSchema,
|
|
},
|
|
"driver": {
|
|
Type: schema.TypeString,
|
|
Description: "Driver type for the volume. Defaults to `local`.",
|
|
Optional: true,
|
|
Computed: true,
|
|
ForceNew: true,
|
|
},
|
|
"driver_opts": {
|
|
Type: schema.TypeMap,
|
|
Description: "Options specific to the driver.",
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
"mountpoint": {
|
|
Type: schema.TypeString,
|
|
Description: "The mountpoint of the volume.",
|
|
Computed: true,
|
|
},
|
|
},
|
|
SchemaVersion: 1,
|
|
StateUpgraders: []schema.StateUpgrader{
|
|
{
|
|
Version: 0,
|
|
Type: resourceDockerVolumeV0().CoreConfigSchema().ImpliedType(),
|
|
Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
|
|
return replaceLabelsMapFieldWithSetField(rawState), nil
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceDockerVolumeCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
|
client := meta.(*ProviderConfig).DockerClient
|
|
|
|
createOpts := volume.VolumeCreateBody{}
|
|
|
|
if v, ok := d.GetOk("name"); ok {
|
|
createOpts.Name = v.(string)
|
|
}
|
|
if v, ok := d.GetOk("labels"); ok {
|
|
createOpts.Labels = labelSetToMap(v.(*schema.Set))
|
|
}
|
|
if v, ok := d.GetOk("driver"); ok {
|
|
createOpts.Driver = v.(string)
|
|
}
|
|
if v, ok := d.GetOk("driver_opts"); ok {
|
|
createOpts.DriverOpts = mapTypeMapValsToString(v.(map[string]interface{}))
|
|
}
|
|
|
|
var err error
|
|
var retVolume types.Volume
|
|
retVolume, err = client.VolumeCreate(ctx, createOpts)
|
|
|
|
if err != nil {
|
|
return diag.Errorf("Unable to create volume: %s", err)
|
|
}
|
|
|
|
d.SetId(retVolume.Name)
|
|
return resourceDockerVolumeRead(ctx, d, meta)
|
|
}
|
|
|
|
func resourceDockerVolumeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
|
client := meta.(*ProviderConfig).DockerClient
|
|
|
|
var err error
|
|
var retVolume types.Volume
|
|
retVolume, err = client.VolumeInspect(ctx, d.Id())
|
|
|
|
if err != nil {
|
|
return diag.Errorf("Unable to inspect volume: %s", err)
|
|
}
|
|
|
|
d.Set("name", retVolume.Name)
|
|
d.Set("labels", mapToLabelSet(retVolume.Labels))
|
|
d.Set("driver", retVolume.Driver)
|
|
d.Set("driver_opts", retVolume.Options)
|
|
d.Set("mountpoint", retVolume.Mountpoint)
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceDockerVolumeDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
|
log.Printf("[INFO] Waiting for volume: '%s' to get removed: max '%v seconds'", d.Id(), volumeReadRefreshTimeout)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
Pending: []string{"in_use"},
|
|
Target: []string{"removed"},
|
|
Refresh: resourceDockerVolumeRemoveRefreshFunc(d.Id(), meta),
|
|
Timeout: volumeReadRefreshTimeout,
|
|
MinTimeout: volumeReadRefreshWaitBeforeRefreshes,
|
|
Delay: volumeReadRefreshDelay,
|
|
}
|
|
|
|
// Wait, catching any errors
|
|
_, err := stateConf.WaitForStateContext(ctx)
|
|
if err != nil {
|
|
return diag.FromErr(err)
|
|
}
|
|
|
|
d.SetId("")
|
|
return nil
|
|
}
|
|
|
|
func resourceDockerVolumeRemoveRefreshFunc(
|
|
volumeID string, meta interface{}) resource.StateRefreshFunc {
|
|
return func() (interface{}, string, error) {
|
|
client := meta.(*ProviderConfig).DockerClient
|
|
forceDelete := true
|
|
|
|
if err := client.VolumeRemove(context.Background(), volumeID, forceDelete); err != nil {
|
|
if strings.Contains(err.Error(), "volume is in use") { // store.IsInUse(err)
|
|
log.Printf("[INFO] Volume with id '%v' is still in use", volumeID)
|
|
return volumeID, "in_use", nil
|
|
}
|
|
log.Printf("[INFO] Removing volume with id '%v' caused an error: %v", volumeID, err)
|
|
return nil, "", err
|
|
}
|
|
log.Printf("[INFO] Removing volume with id '%v' got removed", volumeID)
|
|
return volumeID, "removed", nil
|
|
}
|
|
}
|