forked from grafana/terraform-provider-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_user_test.go
121 lines (113 loc) · 3.06 KB
/
resource_user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package grafana
import (
"fmt"
"regexp"
"strconv"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
gapi "github.com/grafana/grafana-api-golang-client"
)
func TestAccUser_basic(t *testing.T) {
var user gapi.User
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccUserCheckDestroy(&user),
Steps: []resource.TestStep{
{
Config: testAccUserConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccUserCheckExists("grafana_user.test", &user),
resource.TestCheckResourceAttr(
"grafana_user.test", "email", "terraform-test@localhost",
),
resource.TestCheckResourceAttr(
"grafana_user.test", "name", "Terraform Test",
),
resource.TestCheckResourceAttr(
"grafana_user.test", "login", "tt",
),
resource.TestCheckResourceAttr(
"grafana_user.test", "password", "abc123",
),
resource.TestMatchResourceAttr(
"grafana_user.test", "id", regexp.MustCompile(`\d+`),
),
),
},
{
Config: testAccUserConfig_update,
Check: resource.ComposeTestCheckFunc(
testAccUserCheckExists("grafana_user.test", &user),
resource.TestCheckResourceAttr(
"grafana_user.test", "email", "terraform-test-update@localhost",
),
resource.TestCheckResourceAttr(
"grafana_user.test", "name", "Terraform Test Update",
),
resource.TestCheckResourceAttr(
"grafana_user.test", "login", "ttu",
),
resource.TestCheckResourceAttr(
"grafana_user.test", "password", "zyx987",
),
resource.TestCheckResourceAttr(
"grafana_user.test", "is_admin", "true",
),
),
},
},
})
}
func testAccUserCheckExists(rn string, a *gapi.User) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("resource not found: %s", rn)
}
if rs.Primary.ID == "" {
return fmt.Errorf("resource id not set")
}
tmp, err := strconv.ParseInt(rs.Primary.ID, 10, 64)
id := int64(tmp)
if err != nil {
return fmt.Errorf("resource id is malformed")
}
client := testAccProvider.Meta().(*gapi.Client)
user, err := client.User(id)
if err != nil {
return fmt.Errorf("error getting data source: %s", err)
}
*a = user
return nil
}
}
func testAccUserCheckDestroy(a *gapi.User) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*gapi.Client)
user, err := client.User(a.ID)
if err == nil && user.Email != "" {
return fmt.Errorf("user still exists")
}
return nil
}
}
const testAccUserConfig_basic = `
resource "grafana_user" "test" {
email = "terraform-test@localhost"
name = "Terraform Test"
login = "tt"
password = "abc123"
is_admin = false
}
`
const testAccUserConfig_update = `
resource "grafana_user" "test" {
email = "terraform-test-update@localhost"
name = "Terraform Test Update"
login = "ttu"
password = "zyx987"
is_admin = true
}
`