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 policies importable. #15

Merged
merged 5 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all 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_policy_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 TestAccPolicy_importBasic(t *testing.T) {
name := "test-" + acctest.RandString(10)
Copy link
Contributor

Choose a reason for hiding this comment

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

There's also RandomWithPrefix, if integers are valid in the name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL! Thanks :)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testProviders,
Steps: []resource.TestStep{
{
Config: testResourcePolicy_initialConfig(name),
Check: testResourcePolicy_initialCheck(name),
},
{
ResourceName: "vault_policy.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
6 changes: 5 additions & 1 deletion vault/resource_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func policyResource() *schema.Resource {
Update: policyWrite,
Delete: policyDelete,
Read: policyRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Expand Down Expand Up @@ -47,7 +50,7 @@ func policyWrite(d *schema.ResourceData, meta interface{}) error {

d.SetId(name)

return nil
return policyRead(d, meta)
}

func policyDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -77,6 +80,7 @@ func policyRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("policy", policy)
d.Set("name", name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we set this in the CREATE method, policyWrite? Right now, policyWrite does not call policyRead, so the name attribute won't be set until a refresh operation happens.

Normally we call create and it returns read. If that's not necessary here, I recommend we set name in policyWrite then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, I missed that wasn't happening here. Updated to now call policyRead from policyWrite.


return nil
}
76 changes: 40 additions & 36 deletions vault/resource_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,77 @@ 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 TestResourcePolicy(t *testing.T) {
r.Test(t, r.TestCase{
name := acctest.RandomWithPrefix("test-")
resource.Test(t, resource.TestCase{
Providers: testProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []r.TestStep{
r.TestStep{
Config: testResourcePolicy_initialConfig,
Check: testResourcePolicy_initialCheck,
Steps: []resource.TestStep{
resource.TestStep{
Config: testResourcePolicy_initialConfig(name),
Check: testResourcePolicy_initialCheck(name),
},
r.TestStep{
resource.TestStep{
Config: testResourcePolicy_updateConfig,
Check: testResourcePolicy_updateCheck,
},
},
})
}

var testResourcePolicy_initialConfig = `

func testResourcePolicy_initialConfig(name string) string {
return fmt.Sprintf(`
resource "vault_policy" "test" {
name = "dev-team"
name = "%s"
policy = <<EOT
path "secret/*" {
policy = "read"
}
EOT
}
`, name)
}

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

func testResourcePolicy_initialCheck(s *terraform.State) error {
resourceState := s.Modules[0].Resources["vault_policy.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")
}
name := instanceState.ID

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

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

if name != "dev-team" {
return fmt.Errorf("unexpected policy name")
}
client := testProvider.Meta().(*api.Client)
policy, err := client.Sys().GetPolicy(name)
if err != nil {
return fmt.Errorf("error reading back policy: %s", err)
}

client := testProvider.Meta().(*api.Client)
policy, err := client.Sys().GetPolicy(name)
if err != nil {
return fmt.Errorf("error reading back policy: %s", err)
}
if got, want := policy, "path \"secret/*\" {\n\tpolicy = \"read\"\n}\n"; got != want {
return fmt.Errorf("policy data is %q; want %q", got, want)
}

if got, want := policy, "path \"secret/*\" {\n\tpolicy = \"read\"\n}\n"; got != want {
return fmt.Errorf("policy data is %q; want %q", got, want)
return nil
}

return nil
}

var testResourcePolicy_updateConfig = `
Expand Down