Skip to content

Commit b7e11f0

Browse files
authored
Merge pull request #12 from terraform-providers/paddy_auth_backend_import
Update tests, add import for auth backends.
2 parents 444f0d6 + 1d230cb commit b7e11f0

40 files changed

Lines changed: 14119 additions & 59 deletions

vault/import_auth_backend_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package vault
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/acctest"
7+
"github.com/hashicorp/terraform/helper/resource"
8+
)
9+
10+
func TestAccAuthBackend_importBasic(t *testing.T) {
11+
path := "github-" + acctest.RandString(10)
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testProviders,
15+
CheckDestroy: testAccCheckAuthBackendDestroy,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testResourceAuth_initialConfig(path),
19+
Check: testResourceAuth_initialCheck(path),
20+
},
21+
{
22+
ResourceName: "vault_auth_backend.test",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}

vault/resource_auth_backend.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ import (
1212

1313
func authBackendResource() *schema.Resource {
1414
return &schema.Resource{
15+
SchemaVersion: 1,
16+
1517
Create: authBackendWrite,
1618
Delete: authBackendDelete,
1719
Read: authBackendRead,
20+
Importer: &schema.ResourceImporter{
21+
State: schema.ImportStatePassthrough,
22+
},
23+
MigrateState: resourceAuthBackendMigrateState,
1824

1925
Schema: map[string]*schema.Schema{
2026
"type": &schema.Schema{
@@ -37,6 +43,9 @@ func authBackendResource() *schema.Resource {
3743
}
3844
return
3945
},
46+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
47+
return old+"/" == new || new+"/" == old
48+
},
4049
},
4150

4251
"description": &schema.Schema{
@@ -56,37 +65,31 @@ func authBackendWrite(d *schema.ResourceData, meta interface{}) error {
5665
desc := d.Get("description").(string)
5766
path := d.Get("path").(string)
5867

59-
log.Printf("[DEBUG] Writing auth %s to Vault", name)
60-
61-
var err error
62-
6368
if path == "" {
6469
path = name
65-
err = d.Set("path", name)
66-
if err != nil {
67-
return fmt.Errorf("unable to set state: %s", err)
68-
}
6970
}
7071

71-
err = client.Sys().EnableAuth(path, name, desc)
72+
log.Printf("[DEBUG] Writing auth %q to Vault", path)
73+
74+
err := client.Sys().EnableAuth(path, name, desc)
7275

7376
if err != nil {
7477
return fmt.Errorf("error writing to Vault: %s", err)
7578
}
7679

77-
d.SetId(name)
80+
d.SetId(path)
7881

79-
return nil
82+
return authBackendRead(d, meta)
8083
}
8184

8285
func authBackendDelete(d *schema.ResourceData, meta interface{}) error {
8386
client := meta.(*api.Client)
8487

85-
name := d.Id()
88+
path := d.Id()
8689

87-
log.Printf("[DEBUG] Deleting auth %s from Vault", name)
90+
log.Printf("[DEBUG] Deleting auth %s from Vault", path)
8891

89-
err := client.Sys().DisableAuth(name)
92+
err := client.Sys().DisableAuth(path)
9093

9194
if err != nil {
9295
return fmt.Errorf("error disabling auth from Vault: %s", err)
@@ -98,7 +101,7 @@ func authBackendDelete(d *schema.ResourceData, meta interface{}) error {
98101
func authBackendRead(d *schema.ResourceData, meta interface{}) error {
99102
client := meta.(*api.Client)
100103

101-
name := d.Id()
104+
targetPath := d.Id() + "/"
102105

103106
auths, err := client.Sys().ListAuth()
104107

@@ -107,10 +110,10 @@ func authBackendRead(d *schema.ResourceData, meta interface{}) error {
107110
}
108111

109112
for path, auth := range auths {
110-
configuredPath := d.Get("path").(string)
111-
112-
vaultPath := configuredPath + "/"
113-
if auth.Type == name && path == vaultPath {
113+
if path == targetPath {
114+
d.Set("type", auth.Type)
115+
d.Set("path", path)
116+
d.Set("description", auth.Description)
114117
return nil
115118
}
116119
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package vault
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func resourceAuthBackendMigrateState(v int, s *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
12+
if s.Empty() {
13+
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
14+
return s, nil
15+
}
16+
17+
switch v {
18+
case 0:
19+
log.Println("[INFO] Found Vault Auth Backend state v0; migrating to v1")
20+
s, err := migrateAuthBackendStateV0toV1(s)
21+
return s, err
22+
default:
23+
return s, fmt.Errorf("Unexpected schema version: %d", v)
24+
}
25+
}
26+
27+
func migrateAuthBackendStateV0toV1(s *terraform.InstanceState) (*terraform.InstanceState, error) {
28+
log.Printf("[DEBUG] Attributes before migration: %#v", s.Attributes)
29+
30+
s.Attributes["type"] = s.ID
31+
if s.Attributes["path"] == "" {
32+
s.Attributes["path"] = s.Attributes["type"]
33+
}
34+
s.ID = strings.TrimSuffix(s.Attributes["path"], "/")
35+
36+
log.Printf("[DEBUG] Attributes after migration: %#v:", s.Attributes)
37+
return s, nil
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package vault
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/terraform"
7+
)
8+
9+
func TestAuthBackendMigrateState(t *testing.T) {
10+
cases := map[string]struct {
11+
StateVersion int
12+
Attributes map[string]string
13+
Expected map[string]string
14+
ID string
15+
ExpectedID string
16+
}{
17+
"switch ID to path instead of type": {
18+
StateVersion: 0,
19+
Attributes: map[string]string{
20+
"type": "github",
21+
"path": "github-123/",
22+
},
23+
Expected: map[string]string{
24+
"type": "github",
25+
"path": "github-123/",
26+
},
27+
ID: "github",
28+
ExpectedID: "github-123",
29+
},
30+
}
31+
32+
for tn, tc := range cases {
33+
is := &terraform.InstanceState{
34+
ID: tc.ID,
35+
Attributes: tc.Attributes,
36+
}
37+
is, err := resourceAuthBackendMigrateState(
38+
tc.StateVersion, is, nil)
39+
40+
if err != nil {
41+
t.Fatalf("Unexpected error for migration %q: %+v", tn, err)
42+
}
43+
44+
if is.ID != tc.ExpectedID {
45+
t.Fatalf("Expected %q to be %v for %q, got %v", "ID", tc.ExpectedID, tn, is.ID)
46+
}
47+
for k, v := range tc.Expected {
48+
if is.Attributes[k] != v {
49+
t.Fatalf("Expected %q to be %v for %q, got %v", k, v, tn, is.Attributes[k])
50+
}
51+
}
52+
}
53+
}

vault/resource_auth_backend_test.go

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,105 @@ import (
44
"fmt"
55
"testing"
66

7-
r "github.com/hashicorp/terraform/helper/resource"
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
89
"github.com/hashicorp/terraform/terraform"
910
"github.com/hashicorp/vault/api"
1011
)
1112

1213
func TestResourceAuth(t *testing.T) {
13-
r.Test(t, r.TestCase{
14+
path := "github-" + acctest.RandString(10)
15+
resource.Test(t, resource.TestCase{
1416
Providers: testProviders,
1517
PreCheck: func() { testAccPreCheck(t) },
16-
Steps: []r.TestStep{
17-
r.TestStep{
18-
Config: testResourceAuth_initialConfig,
19-
Check: testResourceAuth_initialCheck,
18+
Steps: []resource.TestStep{
19+
resource.TestStep{
20+
Config: testResourceAuth_initialConfig(path),
21+
Check: testResourceAuth_initialCheck(path),
2022
},
21-
r.TestStep{
23+
resource.TestStep{
2224
Config: testResourceAuth_updateConfig,
2325
Check: testResourceAuth_updateCheck,
2426
},
2527
},
2628
})
2729
}
2830

29-
var testResourceAuth_initialConfig = `
31+
func testAccCheckAuthBackendDestroy(s *terraform.State) error {
32+
client := testProvider.Meta().(*api.Client)
33+
34+
auths, err := client.Sys().ListAuth()
35+
if err != nil {
36+
return err
37+
}
38+
for _, rs := range s.RootModule().Resources {
39+
if rs.Type != "vault_auth_backend" {
40+
continue
41+
}
42+
instanceState := rs.Primary
43+
if instanceState == nil {
44+
return fmt.Errorf("resource not found in state")
45+
}
46+
47+
if _, ok := auths[instanceState.ID]; ok {
48+
return fmt.Errorf("Auth backend still exists.")
49+
}
50+
}
51+
return nil
52+
}
3053

54+
func testResourceAuth_initialConfig(path string) string {
55+
return fmt.Sprintf(`
3156
resource "vault_auth_backend" "test" {
3257
type = "github"
58+
path = "%s"
59+
description = "Test auth backend"
60+
}`, path)
3361
}
3462

35-
`
63+
func testResourceAuth_initialCheck(expectedPath string) resource.TestCheckFunc {
64+
return func(s *terraform.State) error {
65+
resourceState := s.Modules[0].Resources["vault_auth_backend.test"]
66+
if resourceState == nil {
67+
return fmt.Errorf("resource not found in state")
68+
}
3669

37-
func testResourceAuth_initialCheck(s *terraform.State) error {
38-
resourceState := s.Modules[0].Resources["vault_auth_backend.test"]
39-
if resourceState == nil {
40-
return fmt.Errorf("resource not found in state")
41-
}
70+
instanceState := resourceState.Primary
71+
if instanceState == nil {
72+
return fmt.Errorf("resource has no primary instance")
73+
}
4274

43-
instanceState := resourceState.Primary
44-
if instanceState == nil {
45-
return fmt.Errorf("resource has no primary instance")
46-
}
75+
path := instanceState.ID
4776

48-
name := instanceState.ID
77+
if path+"/" != instanceState.Attributes["path"] {
78+
return fmt.Errorf("id doesn't match path")
79+
}
4980

50-
if name != instanceState.Attributes["type"] {
51-
return fmt.Errorf("id doesn't match name")
52-
}
81+
if path != expectedPath {
82+
return fmt.Errorf("unexpected auth path %q, expected %q", path, expectedPath)
83+
}
5384

54-
if name != "github" {
55-
return fmt.Errorf("unexpected auth name %s", name)
56-
}
85+
client := testProvider.Meta().(*api.Client)
86+
auths, err := client.Sys().ListAuth()
5787

58-
client := testProvider.Meta().(*api.Client)
59-
auths, err := client.Sys().ListAuth()
88+
if err != nil {
89+
return fmt.Errorf("error reading back auth: %s", err)
90+
}
6091

61-
if err != nil {
62-
return fmt.Errorf("error reading back auth: %s", err)
63-
}
92+
found := false
93+
for serverPath := range auths {
94+
if serverPath == expectedPath+"/" {
95+
found = true
96+
break
97+
}
98+
}
6499

65-
found := false
66-
for _, auth := range auths {
67-
if auth.Type == name {
68-
found = true
69-
break
100+
if !found {
101+
return fmt.Errorf("could not find auth backend %q in %+v", expectedPath, auths)
70102
}
71-
}
72103

73-
if !found {
74-
return fmt.Errorf("could not find auth backend %s in %+v", name, auths)
104+
return nil
75105
}
76-
77-
return nil
78106
}
79107

80108
var testResourceAuth_updateConfig = `

vendor/github.com/hashicorp/terraform/helper/acctest/acctest.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)