Skip to content

Commit 2397aa0

Browse files
committed
fetch users via GetOrgUsersForCurrentOrg which returns email addresses
1 parent a79d4b5 commit 2397aa0

File tree

2 files changed

+53
-132
lines changed

2 files changed

+53
-132
lines changed

internal/resources/grafana/data_source_organization_user.go

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -45,70 +45,47 @@ func dataSourceOrganizationUserRead(ctx context.Context, d *schema.ResourceData,
4545
client, orgID := OAPIClientFromNewOrgResource(meta, d)
4646

4747
var resp interface {
48-
GetPayload() []*models.UserLookupDTO
48+
GetPayload() []*models.OrgUserDTO
4949
}
5050

51-
email := d.Get("email").(string)
52-
login := d.Get("login").(string)
53-
54-
if email == "" && login == "" {
55-
return diag.Errorf("must specify one of email or login")
51+
matchBy := matchByEmail
52+
emailOrLogin := d.Get("email").(string)
53+
if emailOrLogin == "" {
54+
emailOrLogin = d.Get("login").(string)
55+
matchBy = matchByLogin
5656
}
57-
58-
// Use email if provided, otherwise use login
59-
query := email
60-
if query == "" {
61-
query = login
57+
if emailOrLogin == "" {
58+
return diag.Errorf("must specify one of email or login")
6259
}
6360

64-
params := org.NewGetOrgUsersForCurrentOrgLookupParams().WithQuery(&query)
65-
resp, err := client.Org.GetOrgUsersForCurrentOrgLookup(params)
61+
params := org.NewGetOrgUsersForCurrentOrgParams().WithQuery(&emailOrLogin)
62+
resp, err := client.Org.GetOrgUsersForCurrentOrg(params)
6663
if err != nil {
6764
return diag.FromErr(err)
6865
}
6966

70-
users := resp.GetPayload()
71-
72-
// If no users found, return error
73-
if len(users) == 0 {
74-
return diag.Errorf("organization user not found with query: %q", query)
67+
if len(resp.GetPayload()) == 0 {
68+
return diag.Errorf("organization user not found with query: %q", emailOrLogin)
7569
}
7670

77-
// If exactly one user found, use it
78-
if len(users) == 1 {
79-
user := users[0]
80-
d.Set("user_id", user.UserID)
81-
d.Set("login", user.Login)
82-
d.SetId(MakeOrgResourceID(orgID, user.UserID))
83-
return nil
71+
for _, user := range resp.GetPayload() {
72+
if matchBy(user, emailOrLogin) {
73+
d.Set("user_id", user.UserID)
74+
d.Set("login", user.Login)
75+
d.Set("email", user.Email)
76+
d.SetId(MakeOrgResourceID(orgID, user.UserID))
77+
return nil
78+
}
8479
}
8580

86-
// Multiple users found - try to find exact match
87-
var exactMatch *models.UserLookupDTO
81+
return diag.Errorf("ambiguous query when reading organization user, multiple users returned by query: %q", emailOrLogin)
8882

89-
if login != "" {
90-
// Look for exact login match
91-
for _, user := range users {
92-
if user.Login == login {
93-
if exactMatch != nil {
94-
// Multiple exact matches found (shouldn't happen with login)
95-
return diag.Errorf("ambiguous query when reading organization user, multiple users with exact login match: %q", login)
96-
}
97-
exactMatch = user
98-
}
99-
}
100-
} else if email != "" {
101-
// For email queries, we can't do exact matching since UserLookupDTO doesn't have Email field, return error
102-
return diag.Errorf("ambiguous query when reading organization user, multiple users returned by query: %q", query)
103-
}
83+
}
10484

105-
if exactMatch != nil {
106-
d.Set("user_id", exactMatch.UserID)
107-
d.Set("login", exactMatch.Login)
108-
d.SetId(MakeOrgResourceID(orgID, exactMatch.UserID))
109-
return nil
110-
}
85+
func matchByEmail(user *models.OrgUserDTO, email string) bool {
86+
return user.Email == email
87+
}
11188

112-
// No exact match found, return error
113-
return diag.Errorf("ambiguous query when reading organization user, multiple users returned by query: %q", query)
89+
func matchByLogin(user *models.OrgUserDTO, login string) bool {
90+
return user.Login == login
11491
}

internal/resources/grafana/data_source_organization_user_test.go

Lines changed: 26 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -39,110 +39,54 @@ func TestAccDatasourceOrganizationUser_basic(t *testing.T) {
3939
})
4040
}
4141

42-
func TestAccDatasourceOrganizationUser_exactMatch(t *testing.T) {
42+
func TestAccDatasourceOrganizationUser_disambiguation(t *testing.T) {
4343
testutils.CheckOSSTestsEnabled(t)
4444

4545
var user1, user2 models.UserProfileDTO
4646
checks := []resource.TestCheckFunc{
47-
userCheckExists.exists("grafana_user.test1", &user1),
48-
userCheckExists.exists("grafana_user.test2", &user2),
49-
// Test that exact login match works when multiple users are returned
50-
resource.TestCheckResourceAttr(
51-
"data.grafana_organization_user.exact_match", "login", "test-exact-match",
52-
),
53-
resource.TestMatchResourceAttr(
54-
"data.grafana_organization_user.exact_match", "user_id", common.IDRegexp,
55-
),
47+
userCheckExists.exists("grafana_user.user1", &user1),
48+
userCheckExists.exists("grafana_user.user2", &user2),
49+
resource.TestCheckResourceAttr("data.grafana_organization_user.from_email", "login", "login1"),
50+
resource.TestCheckResourceAttr("data.grafana_organization_user.from_email", "email", "[email protected]"),
51+
resource.TestCheckResourceAttr("data.grafana_organization_user.from_login", "login", "log"),
52+
resource.TestCheckResourceAttr("data.grafana_organization_user.from_login", "email", "[email protected]~"),
5653
}
5754

5855
resource.ParallelTest(t, resource.TestCase{
5956
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
60-
CheckDestroy: userCheckExists.destroyed(&user1, nil),
57+
CheckDestroy: resource.ComposeTestCheckFunc(
58+
userCheckExists.destroyed(&user1, nil),
59+
userCheckExists.destroyed(&user2, nil),
60+
),
6161
Steps: []resource.TestStep{
6262
{
63-
Config: testAccDatasourceOrganizationUserExactMatch,
63+
Config: testAccDatasourceOrganizationUserDisambiguation,
6464
Check: resource.ComposeTestCheckFunc(checks...),
6565
},
6666
},
6767
})
6868
}
6969

70-
// TestDataSourceOrganizationUserExactMatchLogic tests the exact matching logic without requiring a Grafana instance
71-
func TestDataSourceOrganizationUserExactMatchLogic(t *testing.T) {
72-
// Test case 2: Multiple users returned, exact login match exists
73-
usersMultiple := []*models.UserLookupDTO{
74-
{
75-
UserID: 1,
76-
Login: "test-exact-match",
77-
},
78-
{
79-
UserID: 2,
80-
Login: "test-exact-match-other",
81-
},
82-
}
83-
84-
// Test case 3: Multiple users returned, no exact login match
85-
usersNoExact := []*models.UserLookupDTO{
86-
{
87-
UserID: 1,
88-
Login: "test-exact-match-other1",
89-
},
90-
{
91-
UserID: 2,
92-
Login: "test-exact-match-other2",
93-
},
94-
}
95-
96-
// Test that we can identify exact matches
97-
var exactMatch *models.UserLookupDTO
98-
login := "test-exact-match"
99-
100-
for _, user := range usersMultiple {
101-
if user.Login == login {
102-
if exactMatch != nil {
103-
t.Fatal("Multiple exact matches found when there should only be one")
104-
}
105-
exactMatch = user
106-
}
107-
}
108-
109-
if exactMatch == nil {
110-
t.Fatal("Expected to find exact match but didn't")
111-
}
112-
113-
if exactMatch.UserID != 1 {
114-
t.Fatalf("Expected UserID 1, got %d", exactMatch.UserID)
115-
}
116-
117-
// Test that we don't find exact matches when they don't exist
118-
exactMatch = nil
119-
for _, user := range usersNoExact {
120-
if user.Login == login {
121-
exactMatch = user
122-
}
123-
}
124-
125-
if exactMatch != nil {
126-
t.Fatal("Expected no exact match but found one")
127-
}
70+
var testAccDatasourceOrganizationUserDisambiguation = `
71+
resource "grafana_user" "user1" {
72+
73+
name = "Test User 1"
74+
login = "login1"
75+
password = "my-password"
12876
}
12977
130-
const testAccDatasourceOrganizationUserExactMatch = `
131-
resource "grafana_user" "test1" {
132-
133-
name = "Test Exact Match 1"
134-
login = "test-exact-match"
78+
resource "grafana_user" "user2" {
79+
email = "[email protected]~"
80+
name = "Test User 1a"
81+
login = "log"
13582
password = "my-password"
13683
}
13784
138-
resource "grafana_user" "test2" {
139-
140-
name = "Test Exact Match 2"
141-
login = "test-exact-match-other"
142-
password = "my-password"
85+
data "grafana_organization_user" "from_email" {
86+
email = grafana_user.user1.email
14387
}
14488
145-
data "grafana_organization_user" "exact_match" {
146-
login = grafana_user.test1.login
89+
data "grafana_organization_user" "from_login" {
90+
login = grafana_user.user2.login
14791
}
14892
`

0 commit comments

Comments
 (0)