Skip to content

Commit 2afdd5f

Browse files
committed
Fix crash when user data source id or email not found
1 parent 9a9d413 commit 2afdd5f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

sumologic/data_source_sumologic_user.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func dataSourceSumologicUserRead(d *schema.ResourceData, meta interface{}) error
5959
if err != nil {
6060
return fmt.Errorf("user with id %v not found: %v", id, err)
6161
}
62+
if user == nil {
63+
return fmt.Errorf("user with id %v not found", id)
64+
}
6265
} else {
6366
if userEmail, ok := d.GetOk("email"); ok {
6467
email := userEmail.(string)
@@ -102,6 +105,9 @@ func (s *Client) GetUserByEmail(email string) (*User, error) {
102105
if err != nil {
103106
return nil, err
104107
}
108+
if len(response.User) == 0 {
109+
return nil, fmt.Errorf("user with email address '%s' does not exist", email)
110+
}
105111

106112
return &response.User[0], nil
107113
}

sumologic/data_source_sumologic_user_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package sumologic
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
89
)
910

10-
func TestAccDataSourcSumologicUser_basic(t *testing.T) {
11+
func TestAccDataSourceSumologicUser_basic(t *testing.T) {
1112
resource.Test(t, resource.TestCase{
1213
PreCheck: func() { testAccPreCheck(t) },
1314
Providers: testAccProviders,
@@ -34,6 +35,40 @@ func testAccDataSourceUserCheck(email, reference string) resource.TestCheckFunc
3435
)
3536
}
3637

38+
func TestAccDataSourceSumologicUser_user_email_doesnt_exist(t *testing.T) {
39+
userDoestExistConfig := `
40+
data "sumologic_user" "user_email_doesnt_exist" {
41+
42+
}`
43+
resource.Test(t, resource.TestCase{
44+
PreCheck: func() { testAccPreCheck(t) },
45+
Providers: testAccProviders,
46+
Steps: []resource.TestStep{
47+
{
48+
Config: userDoestExistConfig,
49+
ExpectError: regexp.MustCompile("user with email address '[email protected]' does not exist"),
50+
},
51+
},
52+
})
53+
}
54+
55+
func TestAccDataSourceSumologicUser_user_id_doesnt_exist(t *testing.T) {
56+
userDoestExistConfig := `
57+
data "sumologic_user" "user_id_doesnt_exist" {
58+
id = 99999999999999
59+
}`
60+
resource.Test(t, resource.TestCase{
61+
PreCheck: func() { testAccPreCheck(t) },
62+
Providers: testAccProviders,
63+
Steps: []resource.TestStep{
64+
{
65+
Config: userDoestExistConfig,
66+
ExpectError: regexp.MustCompile("user with id 99999999999999 not found"),
67+
},
68+
},
69+
})
70+
}
71+
3772
var testDataSourceAccSumologicUserConfig = fmt.Sprintf(`
3873
resource "sumologic_user" "test_user" {
3974
first_name = "Test"

0 commit comments

Comments
 (0)