Skip to content

Commit 154000f

Browse files
Luke Petersoncehoffman
authored andcommitted
Fix broken user datasource test
1 parent 25ebbc3 commit 154000f

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

gitlab/data_source_gitlab_user.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ package gitlab
22

33
import (
44
"fmt"
5-
"strconv"
5+
"log"
66
"strings"
77

88
"github.com/hashicorp/terraform/helper/schema"
99
gitlab "github.com/xanzy/go-gitlab"
1010
)
1111

12-
// Search by email required
1312
func dataSourceGitlabUser() *schema.Resource {
1413
return &schema.Resource{
1514
Read: dataSourceGitlabUserRead,
1615
Schema: map[string]*schema.Schema{
17-
//Search option
1816
"email": {
1917
Type: schema.TypeString,
2018
Required: true,
@@ -23,34 +21,34 @@ func dataSourceGitlabUser() *schema.Resource {
2321
}
2422
}
2523

26-
// Performs the lookup
2724
func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
2825
client := meta.(*gitlab.Client)
29-
// Create the query, grab the email for the query and set it for use
30-
var query *gitlab.ListUsersOptions
31-
email := strings.ToLower(d.Get("email").(string))
32-
*query.Search = email
33-
// Query to find the email. Returns a list
26+
27+
log.Printf("[INFO] Reading Gitlab user")
28+
29+
searchEmail := strings.ToLower(d.Get("email").(string))
30+
query := &gitlab.ListUsersOptions{
31+
Search: &searchEmail,
32+
}
3433
users, _, err := client.Users.ListUsers(query)
3534
if err != nil {
3635
return err
3736
}
3837

39-
// Create a user to save userdata to
40-
var user *gitlab.User
41-
// Grab User data out of list
42-
for _, a := range users {
43-
if a.Email == email {
44-
user = a
38+
var found *gitlab.User
39+
40+
for _, user := range users {
41+
if strings.ToLower(user.Email) == searchEmail {
42+
found = user
4543
break
4644
}
4745
}
48-
if user == nil {
49-
return fmt.Errorf("The email '%s' does not match any user email", email)
46+
if found == nil {
47+
return fmt.Errorf("The email '%s' does not match any user email", searchEmail)
5048
}
51-
d.SetId(strconv.Itoa(user.ID))
52-
d.Set("name", user.Name)
53-
d.Set("username", user.Username)
54-
d.Set("email", user.Email)
49+
d.SetId(fmt.Sprintf("%d", found.ID))
50+
d.Set("name", found.Name)
51+
d.Set("username", found.Username)
52+
d.Set("email", found.Email)
5553
return nil
5654
}

gitlab/data_source_gitlab_user_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ func TestAccDataGitlabUser_basic(t *testing.T) {
2929
func testAccDataSourceGitlabUser(src, n string) resource.TestCheckFunc {
3030
return func(s *terraform.State) error {
3131

32-
srcR := s.RootModule().Resources[src]
33-
srcA := srcR.Primary.Attributes
32+
user := s.RootModule().Resources[src]
33+
userResource := user.Primary.Attributes
3434

35-
r := s.RootModule().Resources[n]
36-
a := r.Primary.Attributes
35+
search := s.RootModule().Resources[n]
36+
searchResource := search.Primary.Attributes
3737

38-
if a["id"] == "" {
39-
return fmt.Errorf("Expected to get a user email from Gitlab")
38+
if searchResource["email"] == "" {
39+
return fmt.Errorf("Expected to get user email from Gitlab")
4040
}
4141

42-
testAtts := []string{"id", "email"}
42+
testAttributes := []string{"email"}
4343

44-
for _, att := range testAtts {
45-
if a[att] != srcA[att] {
46-
return fmt.Errorf("Expected the user %s to be: %s, but got: %s", att, srcA[att], a[att])
44+
for _, attribute := range testAttributes {
45+
if searchResource[attribute] != userResource[attribute] {
46+
return fmt.Errorf("Expected the user %s to be: %s, but got: %s", attribute, userResource[attribute], searchResource[attribute])
4747
}
4848
}
4949
return nil
@@ -52,12 +52,15 @@ func testAccDataSourceGitlabUser(src, n string) resource.TestCheckFunc {
5252

5353
func testAccDataGitlabUserConfig(userEmail string) string {
5454
return fmt.Sprintf(`
55-
resource "gitlab_user" "foo"{
56-
email = "%s"
55+
resource "gitlab_user" "foo" {
56+
name = "foo %s"
57+
username = "listest%s"
58+
password = "test%stt"
59+
email = "listest%[email protected]"
5760
}
5861
5962
data "gitlab_user" "foo" {
60-
name = "${gitlab_user.foo.email}"
63+
email = "${gitlab_user.foo.email}"
6164
}
62-
`, userEmail)
65+
`, userEmail, userEmail, userEmail, userEmail)
6366
}

0 commit comments

Comments
 (0)