Skip to content

Commit

Permalink
feat: supports Docker plugin (#35)
Browse files Browse the repository at this point in the history
Closes #24
  • Loading branch information
suzuki-shunsuke authored Jan 8, 2021
1 parent 0ef85ad commit c927a8c
Show file tree
Hide file tree
Showing 13 changed files with 1,047 additions and 16 deletions.
80 changes: 80 additions & 0 deletions docker/data_source_docker_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package docker

import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceDockerPlugin() *schema.Resource {
return &schema.Resource{
Read: dataSourceDockerPluginRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"alias": {
Type: schema.TypeString,
Optional: true,
Description: "Docker Plugin alias",
},

"plugin_reference": {
Type: schema.TypeString,
Description: "Docker Plugin Reference",
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"grant_all_permissions": {
Type: schema.TypeBool,
Computed: true,
Description: "If true, grant all permissions necessary to run the plugin",
},
"env": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

var errDataSourceKeyIsMissing = errors.New("One of id or alias must be assigned")

func getDataSourcePluginKey(d *schema.ResourceData) (string, error) {
id, idOK := d.GetOk("id")
alias, aliasOK := d.GetOk("alias")
if idOK {
if aliasOK {
return "", errDataSourceKeyIsMissing
}
return id.(string), nil
}
if aliasOK {
return alias.(string), nil
}
return "", errDataSourceKeyIsMissing
}

func dataSourceDockerPluginRead(d *schema.ResourceData, meta interface{}) error {
key, err := getDataSourcePluginKey(d)
if err != nil {
return err
}
client := meta.(*ProviderConfig).DockerClient
ctx := context.Background()
plugin, _, err := client.PluginInspectWithRaw(ctx, key)
if err != nil {
return fmt.Errorf("inspect a Docker plugin "+key+": %w", err)
}

setDockerPlugin(d, plugin)
return nil
}
39 changes: 39 additions & 0 deletions docker/data_source_docker_plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package docker

import (
"os/exec"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDockerPluginDataSource_basic(t *testing.T) {
pluginName := "tiborvass/sample-volume-plugin"
// This fails if the plugin is already installed.
if err := exec.Command("docker", "plugin", "install", pluginName).Run(); err != nil {
t.Fatal(err)
}
defer func() {
if err := exec.Command("docker", "plugin", "rm", "-f", pluginName).Run(); err != nil {
t.Logf("failed to remove the Docker plugin %s: %v", pluginName, err)
}
}()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDockerPluginDataSourceTest,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.docker_plugin.test", "plugin_reference", "docker.io/tiborvass/sample-volume-plugin:latest"),
),
},
},
})
}

const testAccDockerPluginDataSourceTest = `
data "docker_plugin" "test" {
alias = "tiborvass/sample-volume-plugin:latest"
}
`
2 changes: 2 additions & 0 deletions docker/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ func Provider() terraform.ResourceProvider {
"docker_config": resourceDockerConfig(),
"docker_secret": resourceDockerSecret(),
"docker_service": resourceDockerService(),
"docker_plugin": resourceDockerPlugin(),
},

DataSourcesMap: map[string]*schema.Resource{
"docker_registry_image": dataSourceDockerRegistryImage(),
"docker_network": dataSourceDockerNetwork(),
"docker_plugin": dataSourceDockerPlugin(),
},

ConfigureFunc: providerConfigure,
Expand Down
2 changes: 1 addition & 1 deletion docker/resource_docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func resourceDockerContainer() *schema.Resource {
},
"driver_name": {
Type: schema.TypeString,
Description: "Name of the driver to use to create the volume.",
Description: "Name of the driver to use to create the volume",
Optional: true,
},
"driver_options": {
Expand Down
2 changes: 1 addition & 1 deletion docker/resource_docker_container_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func resourceDockerContainerV1() *schema.Resource {
},
"driver_name": {
Type: schema.TypeString,
Description: "Name of the driver to use to create the volume.",
Description: "Name of the driver to use to create the volume",
Optional: true,
},
"driver_options": {
Expand Down
95 changes: 95 additions & 0 deletions docker/resource_docker_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package docker

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceDockerPlugin() *schema.Resource {
return &schema.Resource{
Create: resourceDockerPluginCreate,
Read: resourceDockerPluginRead,
Update: resourceDockerPluginUpdate,
Delete: resourceDockerPluginDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Docker Plugin name",
DiffSuppressFunc: diffSuppressFuncPluginName,
ValidateFunc: validateFuncPluginName,
},
"alias": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: "Docker Plugin alias",
DiffSuppressFunc: func(k, oldV, newV string, d *schema.ResourceData) bool {
return complementTag(oldV) == complementTag(newV)
},
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"grant_all_permissions": {
Type: schema.TypeBool,
Optional: true,
Description: "If true, grant all permissions necessary to run the plugin",
ConflictsWith: []string{"grant_permissions"},
},
"grant_permissions": {
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"grant_all_permissions"},
Set: dockerPluginGrantPermissionsSetFunc,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"env": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"plugin_reference": {
Type: schema.TypeString,
Description: "Docker Plugin Reference",
Computed: true,
},

"force_destroy": {
Type: schema.TypeBool,
Optional: true,
},
"enable_timeout": {
Type: schema.TypeInt,
Optional: true,
Description: "HTTP client timeout to enable the plugin",
},
"force_disable": {
Type: schema.TypeBool,
Optional: true,
Description: "If true, then the plugin is disabled forcibly when the plugin is disabled",
},
},
}
}
Loading

0 comments on commit c927a8c

Please sign in to comment.