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