Skip to content

Commit

Permalink
fix: rename env to args
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Dec 29, 2020
1 parent 2384e4e commit 26624c6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docker/resource_docker_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func resourceDockerPlugin() *schema.Resource {
Optional: true,
Description: "If true, grant all permissions necessary to run the plugin",
},
"env": {
"args": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Expand Down
33 changes: 19 additions & 14 deletions docker/resource_docker_plugin_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func getDockerPluginEnv(src interface{}) []string {
func getDockerPluginArgs(src interface{}) []string {
if src == nil {
return nil
}
b := src.(*schema.Set)
env := make([]string, b.Len())
args := make([]string, b.Len())
for i, a := range b.List() {
env[i] = a.(string)
args[i] = a.(string)
}
return env
return args
}

func resourceDockerPluginCreate(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -32,7 +32,7 @@ func resourceDockerPluginCreate(d *schema.ResourceData, meta interface{}) error
RemoteRef: pluginRef,
AcceptAllPermissions: d.Get("grant_all_permissions").(bool),
Disabled: d.Get("disabled").(bool),
Args: getDockerPluginEnv(d.Get("env")),
Args: getDockerPluginArgs(d.Get("args")),
})
if err != nil {
return fmt.Errorf("install a Docker plugin "+pluginRef+": %w", err)
Expand All @@ -55,7 +55,12 @@ func setDockerPlugin(d *schema.ResourceData, plugin *types.Plugin) {
d.Set("plugin_reference", plugin.PluginReference)
d.Set("alias", plugin.Name)
d.Set("disabled", !plugin.Enabled)
d.Set("env", plugin.Settings.Env)
// TODO support other settings
// https://docs.docker.com/engine/reference/commandline/plugin_set/#extended-description
// source of mounts .Settings.Mounts
// path of devices .Settings.Devices
// args .Settings.Args
d.Set("args", plugin.Settings.Env)
}

func resourceDockerPluginRead(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -72,12 +77,12 @@ func resourceDockerPluginRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func setPluginEnv(ctx context.Context, d *schema.ResourceData, client *client.Client) error {
if !d.HasChange("env") {
func setPluginArgs(ctx context.Context, d *schema.ResourceData, client *client.Client) error {
if !d.HasChange("args") {
return nil
}
pluginID := d.Id()
if err := client.PluginSet(ctx, pluginID, getDockerPluginEnv(d.Get("env"))); err != nil {
if err := client.PluginSet(ctx, pluginID, getDockerPluginArgs(d.Get("args"))); err != nil {
return fmt.Errorf("modifiy settings for the Docker plugin "+pluginID+": %w", err)
}
return nil
Expand All @@ -87,7 +92,7 @@ func resourceDockerPluginUpdate(d *schema.ResourceData, meta interface{}) error
client := meta.(*ProviderConfig).DockerClient
ctx := context.Background()
pluginID := d.Id()
skipEnv := false
skipArgs := false
if d.HasChange("disabled") {
if d.Get("disabled").(bool) {
if err := client.PluginDisable(ctx, pluginID, types.PluginDisableOptions{
Expand All @@ -96,18 +101,18 @@ func resourceDockerPluginUpdate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("disable the Docker plugin "+pluginID+": %w", err)
}
} else {
if err := setPluginEnv(ctx, d, client); err != nil {
if err := setPluginArgs(ctx, d, client); err != nil {
return err
}
skipEnv = true
skipArgs = true
if err := client.PluginEnable(ctx, pluginID, types.PluginEnableOptions{
Timeout: d.Get("enable_timeout").(int),
}); err != nil {
return fmt.Errorf("enable the Docker plugin "+pluginID+": %w", err)
}
}
}
if !skipEnv {
if !skipArgs {
plugin, _, err := client.PluginInspectWithRaw(ctx, pluginID)
if err != nil {
return fmt.Errorf("inspect a Docker plugin "+pluginID+": %w", err)
Expand All @@ -122,7 +127,7 @@ func resourceDockerPluginUpdate(d *schema.ResourceData, meta interface{}) error
}
f = true
}
if err := setPluginEnv(ctx, d, client); err != nil {
if err := setPluginArgs(ctx, d, client); err != nil {
return err
}
if f {
Expand Down
4 changes: 2 additions & 2 deletions docker/resource_docker_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ resource "docker_plugin" "test" {
disable_when_set = true
force_destroy = true
enable_timeout = 60
env = [
args = [
"DEBUG=1"
]
}`
Expand All @@ -101,7 +101,7 @@ resource "docker_plugin" "test" {
force_destroy = true
force_disable = true
enable_timeout = 60
env = [
args = [
"DEBUG=1"
]
}`
4 changes: 2 additions & 2 deletions website/docs/r/plugin.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resource "docker_plugin" "sample-volume-plugin" {
force_destroy = true
enable_timeout = 60
force_disable = true
env = [
args = [
"DEBUG=1"
]
}
Expand All @@ -45,7 +45,7 @@ The following arguments are supported:
* `grant_all_permissions` - (Optional, boolean) If true, grant all permissions necessary to run the plugin.
* `disable_when_set` - (Optional, boolean) If true, the plugin becomes disabled temporarily when the plugin setting is updated.
* `force_destroy` - (Optional, boolean) If true, the plugin becomes disabled temporarily when the plugin setting is updated.
* `env` - (Optional, set of string)
* `args` - (Optional, set of string)
* `enable_timeout` - (Optional, int) HTTP client timeout to enable the plugin.
* `force_disable` - (Optional, boolean) If true, then the plugin is disabled forcely when the plugin is disabled.

Expand Down

0 comments on commit 26624c6

Please sign in to comment.