Skip to content

Commit

Permalink
Support plugin names with slashes, extend testing coverage to updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhjp committed Mar 7, 2024
1 parent 1cc711a commit 8bdd30f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
9 changes: 5 additions & 4 deletions vault/resource_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@ func pluginRead(ctx context.Context, d *schema.ResourceData, meta interface{}) d

var typ, name, version string
parts := strings.Split(d.Id(), "/")
switch len(parts) {
lenParts := len(parts)
switch lenParts {
case 0, 1:
return diag.Errorf("invalid ID %q, must be of form <type>/<name> or <type>/<name>/<semantic-version>", d.Id())
case 2:
typ, name = parts[0], parts[1]
case 3:
typ, name, version = parts[0], parts[1], parts[2]
default:
return diag.Errorf("invalid ID %q, must be of form <type>/<name> or <type>/<name>/<semantic-version>", d.Id())
typ, name, version = parts[0], strings.Join(parts[1:lenParts-1], "/"), parts[lenParts-1]
}

if diagErr := versionedPluginsSupported(meta, version); diagErr != nil {
Expand Down
28 changes: 24 additions & 4 deletions vault/resource_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ func TestPlugin(t *testing.T) {
version = "v1.0.0"
args = `["--foo"]`
env = `["FOO=BAR"]`

argsUpdated = `["--bar"]`
envUpdated = `["FOO=BAZ"]`
)

destName := acctest.RandomWithPrefix("tf-plugin")
destName := acctest.RandomWithPrefix("tf/plugin")

resourceName := "vault_plugin.test"
sha256 := strings.Repeat("01234567", 8)
sha256Updated := strings.Repeat("12345678", 8)
cmd := os.Getenv(envPluginCommand)

m := testProvider.Meta().(*provider.ProviderMeta)
versionsSupported := m.GetVaultVersion().GreaterThanOrEqual(provider.VaultVersion112)
versionsSupported := true
if testProvider != nil {
m := testProvider.Meta().(*provider.ProviderMeta)
versionsSupported = m.GetVaultVersion().GreaterThanOrEqual(provider.VaultVersion112)
}

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand All @@ -58,14 +65,27 @@ func TestPlugin(t *testing.T) {
testValidateList(resourceName, fieldEnv, []string{"FOO=BAR"}),
),
},
{
Config: testPluginConfig(typ, destName, version, sha256Updated, cmd, argsUpdated, envUpdated, versionsSupported),
Check: resource.ComposeTestCheckFunc(

resource.TestCheckResourceAttr(resourceName, consts.FieldType, typ),
resource.TestCheckResourceAttr(resourceName, consts.FieldName, destName),
testCheckVersionAttr(resourceName, version, versionsSupported),
resource.TestCheckResourceAttr(resourceName, fieldSHA256, sha256Updated),
resource.TestCheckResourceAttr(resourceName, fieldCommand, cmd),
testValidateList(resourceName, fieldArgs, []string{"--bar"}),
testValidateList(resourceName, fieldEnv, []string{"FOO=BAZ"}),
),
},
testutil.GetImportTestStep(resourceName, false, nil, "env"),
},
})
}

func testPluginConfig(pluginType, name, version, sha256, command, args, env string, versionsSupported bool) string {
if !versionsSupported {
fmt.Sprintf(`
return fmt.Sprintf(`
resource "vault_plugin" "test" {
type = "%s"
name = "%s"
Expand Down

0 comments on commit 8bdd30f

Please sign in to comment.