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

Make mounts importable #16

Merged
merged 4 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
27 changes: 27 additions & 0 deletions vault/import_mount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package vault

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccMount_importBasic(t *testing.T) {
path := "test-" + acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testProviders,
Steps: []resource.TestStep{
{
Config: testResourceMount_initialConfig(path),
Check: testResourceMount_initialCheck(path),
},
{
ResourceName: "vault_mount.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
23 changes: 19 additions & 4 deletions vault/resource_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package vault

import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/vault/api"
"log"
)

func mountResource() *schema.Resource {
Expand All @@ -13,6 +15,9 @@ func mountResource() *schema.Resource {
Update: mountUpdate,
Delete: mountDelete,
Read: mountRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"path": {
Expand Down Expand Up @@ -137,13 +142,23 @@ func mountRead(d *schema.ResourceData, meta interface{}) error {

log.Printf("[DEBUG] Reading mount %s from Vault", path)

mount, err := client.Sys().MountConfig(path)
mounts, err := client.Sys().ListMounts()
if err != nil {
return fmt.Errorf("error reading from Vault: %s", err)
}

d.Set("default_lease_ttl_seconds", mount.DefaultLeaseTTL)
d.Set("max_lease_ttl_seconds", mount.MaxLeaseTTL)
mount, ok := mounts[strings.Trim(path, "/")+"/"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, sometimes path has a trailing /, but sometimes it doesn't? So here we ensure it has a single /? Is that what's happening? Please add some comments here for future readers 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with comment :D

if !ok {
log.Printf("[WARN] Mount %q not found, removing from state.", path)
d.SetId("")
return nil
}

d.Set("path", path)
d.Set("type", mount.Type)
d.Set("description", mount.Description)
d.Set("default_lease_ttl_seconds", mount.Config.DefaultLeaseTTL)
d.Set("max_lease_ttl_seconds", mount.Config.MaxLeaseTTL)

return nil
}
92 changes: 48 additions & 44 deletions vault/resource_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"fmt"
"testing"

r "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/vault/api"
)

func TestZeroTTLDoesNotCauseUpdate(t *testing.T) {
r.Test(t, r.TestCase{
resource.Test(t, resource.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []r.TestStep{
Steps: []resource.TestStep{
{
Config: `
resource "vault_mount" "zero_ttl" {
Expand All @@ -36,13 +37,14 @@ func TestZeroTTLDoesNotCauseUpdate(t *testing.T) {
}

func TestResourceMount(t *testing.T) {
r.Test(t, r.TestCase{
path := "example-" + acctest.RandString(10)
resource.Test(t, resource.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []r.TestStep{
Steps: []resource.TestStep{
{
Config: testResourceMount_initialConfig,
Check: testResourceMount_initialCheck,
Config: testResourceMount_initialConfig(path),
Check: testResourceMount_initialCheck(path),
},
{
Config: testResourceMount_updateConfig,
Expand All @@ -52,61 +54,63 @@ func TestResourceMount(t *testing.T) {
})
}

var testResourceMount_initialConfig = `

func testResourceMount_initialConfig(path string) string {
return fmt.Sprintf(`
resource "vault_mount" "test" {
path = "example"
path = "%s"
type = "generic"
description = "Example mount for testing"
default_lease_ttl_seconds = 3600
max_lease_ttl_seconds = 36000
}
`, path)
}

`
func testResourceMount_initialCheck(expectedPath string) resource.TestCheckFunc {
return func(s *terraform.State) error {
resourceState := s.Modules[0].Resources["vault_mount.test"]
if resourceState == nil {
return fmt.Errorf("resource not found in state")
}

func testResourceMount_initialCheck(s *terraform.State) error {
resourceState := s.Modules[0].Resources["vault_mount.test"]
if resourceState == nil {
return fmt.Errorf("resource not found in state")
}
instanceState := resourceState.Primary
if instanceState == nil {
return fmt.Errorf("resource has no primary instance")
}

instanceState := resourceState.Primary
if instanceState == nil {
return fmt.Errorf("resource has no primary instance")
}
path := instanceState.ID

path := instanceState.ID
if path != instanceState.Attributes["path"] {
return fmt.Errorf("id %q doesn't match path %q", path, instanceState.Attributes["path"])
}

if path != instanceState.Attributes["path"] {
return fmt.Errorf("id doesn't match path")
}
if path != expectedPath {
return fmt.Errorf("unexpected path %q, expected %q", path, expectedPath)
}

if path != "example" {
return fmt.Errorf("unexpected path value")
}
mount, err := findMount(path)
if err != nil {
return fmt.Errorf("error reading back mount %q: %s", path, err)
}

mount, err := findMount(path)
if err != nil {
return fmt.Errorf("error reading back mount: %s", err)
}
if wanted := "Example mount for testing"; mount.Description != wanted {
return fmt.Errorf("description is %v; wanted %v", mount.Description, wanted)
}

if wanted := "Example mount for testing"; mount.Description != wanted {
return fmt.Errorf("description is %v; wanted %v", mount.Description, wanted)
}
if wanted := "generic"; mount.Type != wanted {
return fmt.Errorf("type is %v; wanted %v", mount.Description, wanted)
}

if wanted := "generic"; mount.Type != wanted {
return fmt.Errorf("type is %v; wanted %v", mount.Description, wanted)
}
if wanted := 3600; mount.Config.DefaultLeaseTTL != wanted {
return fmt.Errorf("default lease ttl is %v; wanted %v", mount.Description, wanted)
}

if wanted := 3600; mount.Config.DefaultLeaseTTL != wanted {
return fmt.Errorf("default lease ttl is %v; wanted %v", mount.Description, wanted)
}
if wanted := 36000; mount.Config.MaxLeaseTTL != wanted {
return fmt.Errorf("max lease ttl is %v; wanted %v", mount.Description, wanted)
}

if wanted := 36000; mount.Config.MaxLeaseTTL != wanted {
return fmt.Errorf("max lease ttl is %v; wanted %v", mount.Description, wanted)
return nil
}

return nil
}

var testResourceMount_updateConfig = `
Expand Down