Skip to content

Commit 327dc8e

Browse files
committed
Better error handling & escape URLs
1 parent e97a8bd commit 327dc8e

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

sumologic/resource_sumologic_permissions.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ func resourceSumologicPermissionsRead(d *schema.ResourceData, meta interface{})
9898
return nil
9999
}
100100

101-
creatorId, _ := getCreatorId(id, meta)
102-
if creatorId == "" {
103-
log.Printf("[WARN] Creator id is empty for this content %v", id)
101+
creatorId, err := getCreatorId(id, meta)
102+
if err != nil {
103+
log.Printf("[ERROR] Cannot find owner of content %s", id)
104+
return err
104105
}
105106

106107
d.Set("permission",
@@ -137,21 +138,16 @@ func getCreatorId(contentId string, meta interface{}) (string, error) {
137138
c := meta.(*Client)
138139
path, err := c.GetContentPath(contentId)
139140
if err != nil {
140-
log.Printf("[WARN] Cannot get path for content %v - %v", contentId, err)
141+
log.Printf("[ERROR] Cannot get path for content %s - %v", contentId, err)
141142
return "", err
142143
}
143-
if path == "" {
144-
log.Printf("[WARN] Path is empty %v", contentId)
145-
return "", nil
146-
}
144+
147145
creatorId, err := c.GetCreatorId(path)
148146
if err != nil {
149-
log.Printf("[WARN] Cannot get content by path %v - %v", contentId, err)
147+
log.Printf("[ERROR] Cannot find owner of content %s - %v", contentId, err)
150148
return "", err
151149
}
152-
if creatorId == "" {
153-
log.Printf("[WARN] Creator ID is empty %v", contentId)
154-
}
150+
log.Printf("[DEBUG] content=%s, path='%s', owner=%s", contentId, path, creatorId)
155151
return creatorId, nil
156152
}
157153

sumologic/resource_sumologic_permissions_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func TestAccPermission_delete(t *testing.T) {
9898
}
9999

100100
func testAccCheckPermissionDestroy(s *terraform.State) error {
101-
// keep it here at this moment
102101
client := testAccProvider.Meta().(*Client)
103102
for _, rs := range s.RootModule().Resources {
104103
if rs.Type != "sumologic_content_permission" {
@@ -140,9 +139,9 @@ func testAccCheckPermissionDelete(name string) resource.TestCheckFunc {
140139
if s == nil {
141140
return fmt.Errorf("All permissions have been deleted")
142141
}
143-
creatorId, _ := getCreatorId(contentId, meta)
144-
if creatorId == "" {
145-
return fmt.Errorf("Empty creator's id")
142+
creatorId, err := getCreatorId(contentId, meta)
143+
if err != nil {
144+
return err
146145
}
147146
for _, permission := range permissions.ExplicitPermissions {
148147
if creatorId != permission.SourceId || permission.SourceType != "user" {

sumologic/sumologic_permissions.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sumologic
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/url"
67
)
78

89
func (s *Client) GetPermissions(id string) (*PermissionsResponse, error) {
@@ -44,31 +45,32 @@ func (s *Client) GetContentPath(id string) (string, error) {
4445
return "", err
4546
}
4647
if data == nil {
47-
return "", nil
48+
return "", fmt.Errorf("Cannot find path of content='%s'", id)
4849
}
49-
m := make(map[string]interface{})
50-
err = json.Unmarshal(data, &m)
50+
rsp := make(map[string]interface{})
51+
err = json.Unmarshal(data, &rsp)
5152
if err != nil {
5253
return "", err
5354
}
54-
return m["path"].(string), nil
55+
return rsp["path"].(string), nil
5556
}
5657

5758
func (s *Client) GetCreatorId(path string) (string, error) {
58-
url := fmt.Sprintf("v2/content/path?path=%s", path)
59+
url := fmt.Sprintf("v2/content/path?path=%s", url.QueryEscape(path))
5960
data, _, err := s.Get(url)
6061
if err != nil {
6162
return "", err
6263
}
6364
if data == nil {
64-
return "", nil
65+
return "", fmt.Errorf("Cannot find content by path='%s'", path)
6566
}
66-
m := make(map[string]interface{})
67-
err = json.Unmarshal(data, &m)
67+
68+
rsp := make(map[string]interface{})
69+
err = json.Unmarshal(data, &rsp)
6870
if err != nil {
6971
return "", err
7072
}
71-
return m["createdBy"].(string), nil
73+
return rsp["createdBy"].(string), nil
7274
}
7375

7476
type PermissionsResponse struct {

website/docs/r/content_permission_source.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ org. You can read more [here](https://help.sumologic.com/Manage/Content_Sharing/
1212
There are three permission levels `View`, `Edit` and `Manage`. You can read more about different
1313
levels [here](https://help.sumologic.com/Manage/Content_Sharing/Share-Content#available-permission-levels).
1414

15-
-> When you add a new permission to a content, all the lower level permissions are added by default.
15+
~> When you add a new permission to a content, all the lower level permissions are added by default.
1616
For example, giving a user "Manage" permission on a content, implicitly gives them "Edit" and "View"
1717
permissions on the content. Due to this behavior, when you add a higher level permission, you must
1818
also add all the lower level permissions. For example, when you give a user "Edit" permission via

0 commit comments

Comments
 (0)