-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
1,047 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.