diff --git a/vault/import_mount_test.go b/vault/import_mount_test.go new file mode 100644 index 000000000..c7b284963 --- /dev/null +++ b/vault/import_mount_test.go @@ -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, + }, + }, + }) +} diff --git a/vault/resource_mount.go b/vault/resource_mount.go index 8a0ec240f..56bfa423a 100644 --- a/vault/resource_mount.go +++ b/vault/resource_mount.go @@ -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 { @@ -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": { @@ -137,13 +142,26 @@ 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) + // path can have a trailing slash, but doesn't need to have one + // this standardises on having a trailing slash, which is how the + // API always responds. + mount, ok := mounts[strings.Trim(path, "/")+"/"] + 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 } diff --git a/vault/resource_mount_test.go b/vault/resource_mount_test.go index 904150261..388cc50aa 100644 --- a/vault/resource_mount_test.go +++ b/vault/resource_mount_test.go @@ -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" { @@ -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, @@ -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 = `