Skip to content

Commit 9a9d413

Browse files
committed
Fix crash when role data source id or name not found
1 parent 2444695 commit 9a9d413

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

sumologic/data_source_sumologic_role.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func dataSourceSumologicRoleRead(d *schema.ResourceData, meta interface{}) error
5555
if err != nil {
5656
return fmt.Errorf("role with id %v not found: %v", id, err)
5757
}
58+
if role == nil {
59+
return fmt.Errorf("role with id %v not found", id)
60+
}
5861
} else {
5962
if rname, ok := d.GetOk("name"); ok {
6063
name := rname.(string)
@@ -97,6 +100,9 @@ func (s *Client) GetRoleName(name string) (*Role, error) {
97100
if err != nil {
98101
return nil, err
99102
}
103+
if len(response.Roles) == 0 {
104+
return nil, fmt.Errorf("role with name '%s' does not exist", name)
105+
}
100106

101107
return &response.Roles[0], nil
102108
}

sumologic/data_source_sumologic_role_test.go

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

33
import (
4+
"regexp"
45
"testing"
56

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

9-
func TestAccDataSourcSumologicRole_basic(t *testing.T) {
10+
func TestAccDataSourceSumologicRole_basic(t *testing.T) {
1011
resource.Test(t, resource.TestCase{
1112
PreCheck: func() { testAccPreCheck(t) },
1213
Providers: testAccProviders,
@@ -33,6 +34,40 @@ func testAccDataSourceRoleCheck(name, reference string) resource.TestCheckFunc {
3334
)
3435
}
3536

37+
func TestAccDataSourceSumologicRole_role_name_doesnt_exist(t *testing.T) {
38+
roleDoestExistConfig := `
39+
data "sumologic_role" "role_name_doesnt_exist" {
40+
name = "someRoleNameDoesntExist8746"
41+
}`
42+
resource.Test(t, resource.TestCase{
43+
PreCheck: func() { testAccPreCheck(t) },
44+
Providers: testAccProviders,
45+
Steps: []resource.TestStep{
46+
{
47+
Config: roleDoestExistConfig,
48+
ExpectError: regexp.MustCompile("role with name 'someRoleNameDoesntExist8746' does not exist"),
49+
},
50+
},
51+
})
52+
}
53+
54+
func TestAccDataSourceSumologicRole_role_id_doesnt_exist(t *testing.T) {
55+
roleDoestExistConfig := `
56+
data "sumologic_role" "role_id_doesnt_exist" {
57+
id = 99999999999999
58+
}`
59+
resource.Test(t, resource.TestCase{
60+
PreCheck: func() { testAccPreCheck(t) },
61+
Providers: testAccProviders,
62+
Steps: []resource.TestStep{
63+
{
64+
Config: roleDoestExistConfig,
65+
ExpectError: regexp.MustCompile("role with id 99999999999999 not found"),
66+
},
67+
},
68+
})
69+
}
70+
3671
var testDataSourceAccSumologicRoleConfig = `
3772
resource "sumologic_role" "test" {
3873
name = "My_SumoRole"

0 commit comments

Comments
 (0)