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 Docker plugin #35

Merged
merged 36 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6c0434e
feat: support Docker plugin
suzuki-shunsuke Dec 22, 2020
2238594
feat: add destroy_option
suzuki-shunsuke Dec 29, 2020
c0fc9db
feat: enhance docker plugin
suzuki-shunsuke Dec 29, 2020
ceae7b1
fix: make docker_plugin.alias force new
suzuki-shunsuke Dec 29, 2020
1c1ba51
fix: make docker_plugin.env computed
suzuki-shunsuke Dec 29, 2020
963699b
fix: remove the plugin from state when it failed to inspect the plugin
suzuki-shunsuke Dec 29, 2020
bbd50ed
test: add test of docker_plugin
suzuki-shunsuke Dec 29, 2020
260a2ef
docs: fix a lint error
suzuki-shunsuke Dec 29, 2020
1f7e286
test: add tests of docker_plugin
suzuki-shunsuke Dec 29, 2020
731342a
docs: format document
suzuki-shunsuke Dec 29, 2020
593269b
style: remove TODO comment
suzuki-shunsuke Dec 29, 2020
84dd122
fix: rename env to args
suzuki-shunsuke Dec 29, 2020
3b7ab72
feat: add debug logs
suzuki-shunsuke Dec 29, 2020
a9662ad
docs: update plugin document
suzuki-shunsuke Dec 29, 2020
6522a35
fix: enable plugin after disable it temporarily with defer
suzuki-shunsuke Dec 29, 2020
7a79c28
docs: fix forcely to forcibly
suzuki-shunsuke Dec 29, 2020
908fd7c
feat: add a data source docker_plugin
suzuki-shunsuke Dec 29, 2020
9df6394
docs: add document of data.docker_plugin
suzuki-shunsuke Dec 29, 2020
e52b264
fix: raise an error if both id and alias are specified.
suzuki-shunsuke Dec 29, 2020
5f925d6
fix: replace docker_plugin.disabled to enabled
suzuki-shunsuke Dec 29, 2020
54629cc
test: add test of data.docker_plugin
suzuki-shunsuke Dec 30, 2020
afcb76c
test: test docker_plugin.grant_all_permissions
suzuki-shunsuke Dec 30, 2020
3ec71c5
refactor: remove skipArgs to make code clear
suzuki-shunsuke Dec 30, 2020
c8b9c7f
fix: make attributes `Computed: true`
suzuki-shunsuke Dec 30, 2020
967f2ba
fix: replace args to env
suzuki-shunsuke Dec 30, 2020
03b2187
feat: add an attribute "docker_plugin.grant_permissions"
suzuki-shunsuke Dec 30, 2020
735df8a
docs: add document of plugin.grant_permissions
suzuki-shunsuke Dec 31, 2020
0d31498
test: test docker_plugin.grant_permissions
suzuki-shunsuke Dec 31, 2020
bc2a157
test: test getDockerPluginEnv
suzuki-shunsuke Dec 31, 2020
3a8e2b0
test: test getDockerPluginGrantPermissions
suzuki-shunsuke Dec 31, 2020
abec1d5
fix: remove docker_plugin.disable_when_set
suzuki-shunsuke Jan 4, 2021
17d8fc6
refactor: refactor resourceDockerPluginUpdate
suzuki-shunsuke Jan 5, 2021
e3384e9
style: remove "." in the description
suzuki-shunsuke Jan 8, 2021
7a7aee3
fix: implement DiffSuppressFunc of docker_plugin.alias
suzuki-shunsuke Jan 8, 2021
b82956b
feat: add a field docker_plugin.name
suzuki-shunsuke Jan 8, 2021
e0ce386
test: test normalizePluginName and complementTag
suzuki-shunsuke Jan 8, 2021
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
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": {
suzuki-shunsuke marked this conversation as resolved.
Show resolved Hide resolved
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")
mavogel marked this conversation as resolved.
Show resolved Hide resolved

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)
},
},
suzuki-shunsuke marked this conversation as resolved.
Show resolved Hide resolved
"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,
suzuki-shunsuke marked this conversation as resolved.
Show resolved Hide resolved
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": {
suzuki-shunsuke marked this conversation as resolved.
Show resolved Hide resolved
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",
},
},
mavogel marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading