Skip to content

Commit 45a3b88

Browse files
committed
misc fixes and update doc
1 parent 3d0d089 commit 45a3b88

File tree

5 files changed

+174
-117
lines changed

5 files changed

+174
-117
lines changed

sumologic/resource_sumologic_permissions.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66

77
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
89
)
910

1011
func resourceSumologicPermissions() *schema.Resource {
@@ -13,6 +14,9 @@ func resourceSumologicPermissions() *schema.Resource {
1314
Read: resourceSumologicPermissionsRead,
1415
Delete: resourceSumologicPermissionsDelete,
1516
Update: resourceSumologicPermissionsUpdate,
17+
Importer: &schema.ResourceImporter{
18+
State: schema.ImportStatePassthrough,
19+
},
1620

1721
Schema: map[string]*schema.Schema{
1822
"content_id": {
@@ -25,7 +29,8 @@ func resourceSumologicPermissions() *schema.Resource {
2529
},
2630
"notification_message": {
2731
Type: schema.TypeString,
28-
Required: true,
32+
Optional: true,
33+
Default: "",
2934
},
3035
"permission": {
3136
Type: schema.TypeSet,
@@ -35,10 +40,13 @@ func resourceSumologicPermissions() *schema.Resource {
3540
"permission_name": {
3641
Type: schema.TypeString,
3742
Required: true,
43+
ValidateFunc: validation.StringInSlice(
44+
[]string{"View", "GrantView", "Edit", "GrantEdit", "Manage", "GrantManage"}, false),
3845
},
3946
"source_type": {
40-
Type: schema.TypeString,
41-
Required: true,
47+
Type: schema.TypeString,
48+
Required: true,
49+
ValidateFunc: validation.StringInSlice([]string{"user", "role", "org"}, false),
4250
},
4351
"source_id": {
4452
Type: schema.TypeString,
@@ -56,9 +64,10 @@ func resourceSumologicPermissionsCreate(d *schema.ResourceData, meta interface{}
5664

5765
if d.Id() == "" {
5866
id, err := c.UpdatePermissions(PermissionsRequest{
59-
PermissionAssignmentype: resourceToPermissionsArray(d.Get("permission").(*schema.Set), d.Get("content_id").(string)),
60-
NotifyRecipients: d.Get("notify_recipient").(bool),
61-
NotificationMessage: d.Get("notification_message").(string),
67+
Permissions: resourceToPermissionsArray(d.Get("permission").(*schema.Set),
68+
d.Get("content_id").(string)),
69+
NotifyRecipients: d.Get("notify_recipient").(bool),
70+
NotificationMessage: d.Get("notification_message").(string),
6271
}, d.Get("content_id").(string))
6372

6473
if err != nil {
@@ -78,12 +87,13 @@ func resourceSumologicPermissionsRead(d *schema.ResourceData, meta interface{})
7887

7988
permissionsResponse, err := c.GetPermissions(id)
8089
if err != nil {
81-
log.Printf("[WARN] Error when get permissions by id: %s, err: %v", id, err)
90+
log.Printf("[WARN] Error getting permissions for content(id=%s), err: %v", id, err)
8291
return err
8392
}
8493

8594
if permissionsResponse == nil {
86-
log.Printf("[WARN] Permission not found, removing from state: %v - %v", id, err)
95+
log.Printf("[WARN] Permissions not found for content(id=%s), removing from state. err: %v",
96+
id, err)
8797
d.SetId("")
8898
return nil
8999
}
@@ -93,7 +103,9 @@ func resourceSumologicPermissionsRead(d *schema.ResourceData, meta interface{})
93103
log.Printf("[WARN] Creator id is empty for this content %v", id)
94104
}
95105

96-
d.Set("permission", permissionsArrayToResource(permissionsResponse.ExplicitPermissions, creatorId))
106+
d.Set("permission",
107+
permissionsArrayToResource(permissionsResponse.ExplicitPermissions, creatorId))
108+
log.Printf("[WARN] Content id %v", d.Get("content_id"))
97109

98110
return nil
99111
}
@@ -184,8 +196,8 @@ func resourceToPermissionRequest(d *schema.ResourceData) (PermissionsRequest, er
184196
}
185197

186198
return PermissionsRequest{
187-
PermissionAssignmentype: resourceToPermissionsArray(d.Get("permission").(*schema.Set), id),
188-
NotifyRecipients: d.Get("notify_recipient").(bool),
189-
NotificationMessage: d.Get("notification_message").(string),
199+
Permissions: resourceToPermissionsArray(d.Get("permission").(*schema.Set), id),
200+
NotifyRecipients: d.Get("notify_recipient").(bool),
201+
NotificationMessage: d.Get("notification_message").(string),
190202
}, nil
191203
}

sumologic/resource_sumologic_permissions_test.go

Lines changed: 89 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ func TestAccPermission_create(t *testing.T) {
1717
CheckDestroy: testAccCheckPermissionDestroy,
1818
Steps: []resource.TestStep{
1919
{
20-
Config: testAccSumologicPermission(otherResource, false, "create", "role", "sumologic_role.permission_test_role"),
20+
Config: testAccSumologicPermission(
21+
otherResource, false, "create", "role", "sumologic_role.permission_test_role"),
2122
Check: resource.ComposeTestCheckFunc(
2223
testAccCheckPermissionExists("sumologic_content_permission.content_permission_test", &response, t),
2324
testAccCheckPermissionAttributes("sumologic_content_permission.content_permission_test"),
24-
resource.TestCheckResourceAttr("sumologic_content_permission.content_permission_test", "notify_recipient", "false"),
25-
resource.TestCheckResourceAttr("sumologic_content_permission.content_permission_test", "notification_message", "create"),
26-
// need to figure out how to test TypeSet
25+
resource.TestCheckResourceAttr(
26+
"sumologic_content_permission.content_permission_test", "notify_recipient", "false"),
27+
resource.TestCheckResourceAttr(
28+
"sumologic_content_permission.content_permission_test", "notification_message", "create"),
29+
// Need to upgrade to plugin SDKv2 to test TypeSet objects
2730
),
2831
},
2932
},
@@ -39,20 +42,26 @@ func TestAccPermission_update(t *testing.T) {
3942
CheckDestroy: testAccCheckPermissionDestroy,
4043
Steps: []resource.TestStep{
4144
{
42-
Config: testAccSumologicPermission(otherResource, false, "create", "role", "sumologic_role.permission_test_role"),
45+
Config: testAccSumologicPermission(
46+
otherResource, false, "create", "role", "sumologic_role.permission_test_role"),
4347
Check: resource.ComposeTestCheckFunc(
4448
testAccCheckPermissionExists("sumologic_content_permission.content_permission_test", &response, t),
4549
testAccCheckPermissionAttributes("sumologic_content_permission.content_permission_test"),
46-
resource.TestCheckResourceAttr("sumologic_content_permission.content_permission_test", "notify_recipient", "false"),
47-
resource.TestCheckResourceAttr("sumologic_content_permission.content_permission_test", "notification_message", "create"),
50+
resource.TestCheckResourceAttr(
51+
"sumologic_content_permission.content_permission_test", "notify_recipient", "false"),
52+
resource.TestCheckResourceAttr(
53+
"sumologic_content_permission.content_permission_test", "notification_message", "create"),
4854
),
4955
}, {
50-
Config: testAccSumologicPermissionUpdate(otherResource, true, "update", "user", "sumologic_user.permission_test_user"),
56+
Config: testAccSumologicPermissionUpdate(
57+
otherResource, true, "update", "user", "sumologic_user.permission_test_user"),
5158
Check: resource.ComposeTestCheckFunc(
5259
testAccCheckPermissionExists("sumologic_content_permission.content_permission_test", &response, t),
5360
testAccCheckPermissionAttributes("sumologic_content_permission.content_permission_test"),
54-
resource.TestCheckResourceAttr("sumologic_content_permission.content_permission_test", "notify_recipient", "true"),
55-
resource.TestCheckResourceAttr("sumologic_content_permission.content_permission_test", "notification_message", "update"),
61+
resource.TestCheckResourceAttr(
62+
"sumologic_content_permission.content_permission_test", "notify_recipient", "true"),
63+
resource.TestCheckResourceAttr(
64+
"sumologic_content_permission.content_permission_test", "notification_message", "update"),
5665
),
5766
},
5867
},
@@ -68,12 +77,15 @@ func TestAccPermission_delete(t *testing.T) {
6877
CheckDestroy: testAccCheckPermissionDestroy,
6978
Steps: []resource.TestStep{
7079
{
71-
Config: testAccSumologicPermission(otherResource, false, "create", "role", "sumologic_role.permission_test_role"),
80+
Config: testAccSumologicPermission(
81+
otherResource, false, "create", "role", "sumologic_role.permission_test_role"),
7282
Check: resource.ComposeTestCheckFunc(
7383
testAccCheckPermissionExists("sumologic_content_permission.content_permission_test", &response, t),
7484
testAccCheckPermissionAttributes("sumologic_content_permission.content_permission_test"),
75-
resource.TestCheckResourceAttr("sumologic_content_permission.content_permission_test", "notify_recipient", "false"),
76-
resource.TestCheckResourceAttr("sumologic_content_permission.content_permission_test", "notification_message", "create"),
85+
resource.TestCheckResourceAttr(
86+
"sumologic_content_permission.content_permission_test", "notify_recipient", "false"),
87+
resource.TestCheckResourceAttr(
88+
"sumologic_content_permission.content_permission_test", "notification_message", "create"),
7789
),
7890
}, {
7991
Config: testAccSumologicPermissionDelete(),
@@ -94,15 +106,15 @@ func testAccCheckPermissionDestroy(s *terraform.State) error {
94106
}
95107

96108
if rs.Primary.ID == "" {
97-
return fmt.Errorf("CSE Chain Rule destruction check: CSE Chain Rule ID is not set")
109+
return fmt.Errorf("Content Permission ID is not set")
98110
}
99111
id := rs.Primary.ID
100112
u, err := client.GetPermissions(id)
101113
if err != nil {
102114
return fmt.Errorf("Encountered an error: " + err.Error())
103115
}
104116
if u != nil {
105-
return fmt.Errorf("content still exists")
117+
return fmt.Errorf("Content and permissions still exists")
106118
}
107119
}
108120
return nil
@@ -141,7 +153,8 @@ func testAccCheckPermissionDelete(name string) resource.TestCheckFunc {
141153
}
142154
}
143155

144-
func testAccCheckPermissionExists(name string, response *PermissionsResponse, t *testing.T) resource.TestCheckFunc {
156+
func testAccCheckPermissionExists(name string, response *PermissionsResponse,
157+
t *testing.T) resource.TestCheckFunc {
145158
return func(s *terraform.State) error {
146159
rs, ok := s.RootModule().Resources[name]
147160
if !ok {
@@ -174,75 +187,76 @@ func testAccCheckPermissionAttributes(name string) resource.TestCheckFunc {
174187
}
175188
}
176189

177-
func testAccSumologicPermission(resource string, notify_recipient bool, notification_message string, source_type string, source_id string) string {
190+
func testAccSumologicPermission(resource string, notify_recipient bool, notification_message string,
191+
source_type string, source_id string) string {
178192
return fmt.Sprintf(`
179-
%s
180-
181-
resource "sumologic_content_permission" "content_permission_test" {
182-
content_id = sumologic_content.permission_test_content.id
183-
notify_recipient = %t
184-
notification_message = "%s"
185-
permission {
186-
permission_name = "View"
187-
source_type = "%s"
188-
source_id = %s.id
189-
}
190-
}
191-
192-
`, resource, notify_recipient, notification_message, source_type, source_id)
193+
%s
194+
195+
resource "sumologic_content_permission" "content_permission_test" {
196+
content_id = sumologic_content.permission_test_content.id
197+
notify_recipient = %t
198+
notification_message = "%s"
199+
permission {
200+
permission_name = "View"
201+
source_type = "%s"
202+
source_id = %s.id
203+
}
204+
}`,
205+
resource, notify_recipient, notification_message, source_type, source_id,
206+
)
193207
}
194208

195-
func testAccSumologicPermissionUpdate(resource string, notify_recipient bool, notification_message string, source_type string, source_id string) string {
209+
func testAccSumologicPermissionUpdate(resource string, notify_recipient bool,
210+
notification_message string, source_type string, source_id string) string {
196211
return fmt.Sprintf(`
197-
%s
198-
199-
resource "sumologic_content_permission" "content_permission_test" {
200-
content_id = sumologic_content.permission_test_content.id
201-
notify_recipient = %t
202-
notification_message = "%s"
203-
permission {
204-
permission_name = "View"
205-
source_type = "role"
206-
source_id = sumologic_role.permission_test_role.id
207-
}
208-
permission {
209-
permission_name = "View"
210-
source_type = "%s"
211-
source_id = %s.id
212-
}
213-
}
214-
215-
`, resource, notify_recipient, notification_message, source_type, source_id)
212+
%s
213+
214+
resource "sumologic_content_permission" "content_permission_test" {
215+
content_id = sumologic_content.permission_test_content.id
216+
notify_recipient = %t
217+
notification_message = "%s"
218+
permission {
219+
permission_name = "View"
220+
source_type = "role"
221+
source_id = sumologic_role.permission_test_role.id
222+
}
223+
permission {
224+
permission_name = "View"
225+
source_type = "%s"
226+
source_id = %s.id
227+
}
228+
}`,
229+
resource, notify_recipient, notification_message, source_type, source_id,
230+
)
216231
}
217232

218233
func testAccSumologicPermissionDelete() string {
219234
return otherResource
220235
}
221236

222237
var otherResource = `
223-
data "sumologic_personal_folder" "personalFolder" {}
224-
225-
resource "sumologic_content" "permission_test_content" {
226-
parent_id = data.sumologic_personal_folder.personalFolder.id
227-
config = jsonencode({
228-
"type": "FolderSyncDefinition",
229-
"name": "test_permission_resource_folder",
230-
"description": "",
231-
"children": []
232-
})
233-
}
238+
data "sumologic_personal_folder" "personalFolder" {}
234239
235-
resource "sumologic_role" "permission_test_role" {
236-
name = "permission_test_role"
237-
description = "Testing content permission resource"
238-
}
240+
resource "sumologic_content" "permission_test_content" {
241+
parent_id = data.sumologic_personal_folder.personalFolder.id
242+
config = jsonencode({
243+
"type": "FolderSyncDefinition",
244+
"name": "test_permission_resource_folder",
245+
"description": "",
246+
"children": []
247+
})
248+
}
239249
240-
resource "sumologic_user" "permission_test_user" {
241-
first_name = "test"
242-
last_name = "permission"
243-
244-
is_active = true
245-
role_ids = [sumologic_role.permission_test_role.id]
246-
transfer_to = ""
247-
}
248-
`
250+
resource "sumologic_role" "permission_test_role" {
251+
name = "permission_test_role"
252+
description = "Testing content permission resource"
253+
}
254+
255+
resource "sumologic_user" "permission_test_user" {
256+
first_name = "test"
257+
last_name = "permission"
258+
259+
is_active = true
260+
role_ids = [sumologic_role.permission_test_role.id]
261+
transfer_to = ""
262+
}`

sumologic/sumologic_client.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ type Client struct {
2929
var ProviderVersion string
3030

3131
var endpoints = map[string]string{
32-
"us1": "https://api.sumologic.com/api/",
33-
"us2": "https://api.us2.sumologic.com/api/",
34-
"fed": "https://api.fed.sumologic.com/api/",
35-
"eu": "https://api.eu.sumologic.com/api/",
36-
"au": "https://api.au.sumologic.com/api/",
37-
"de": "https://api.de.sumologic.com/api/",
38-
"jp": "https://api.jp.sumologic.com/api/",
39-
"ca": "https://api.ca.sumologic.com/api/",
40-
"in": "https://api.in.sumologic.com/api/",
41-
"nite": "https://nite-api.sumologic.net/api/",
42-
"stag": "https://stag-api.sumologic.net/api/",
32+
"us1": "https://api.sumologic.com/api/",
33+
"us2": "https://api.us2.sumologic.com/api/",
34+
"fed": "https://api.fed.sumologic.com/api/",
35+
"eu": "https://api.eu.sumologic.com/api/",
36+
"au": "https://api.au.sumologic.com/api/",
37+
"de": "https://api.de.sumologic.com/api/",
38+
"jp": "https://api.jp.sumologic.com/api/",
39+
"ca": "https://api.ca.sumologic.com/api/",
40+
"in": "https://api.in.sumologic.com/api/",
4341
}
4442

4543
var rateLimiter = time.NewTicker(time.Minute / 240)

sumologic/sumologic_permissions.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ func (s *Client) GetPermissions(id string) (*PermissionsResponse, error) {
2323
return &contentPermssionsResponse, nil
2424
}
2525

26-
func (s *Client) UpdatePermissions(contentPermissionsRequest PermissionsRequest, id string) (string, error) {
26+
func (s *Client) UpdatePermissions(contentPermissionsRequest PermissionsRequest,
27+
id string) (string, error) {
28+
2729
url := fmt.Sprintf("v2/content/%s/permissions/add", id)
2830
_, err := s.Put(url, contentPermissionsRequest)
2931
return id, err
@@ -75,9 +77,9 @@ type PermissionsResponse struct {
7577
}
7678

7779
type PermissionsRequest struct {
78-
PermissionAssignmentype []Permission `json:"contentPermissionAssignments"`
79-
NotifyRecipients bool `json:"notifyRecipients"`
80-
NotificationMessage string `json:"notificationMessage"`
80+
Permissions []Permission `json:"contentPermissionAssignments"`
81+
NotifyRecipients bool `json:"notifyRecipients"`
82+
NotificationMessage string `json:"notificationMessage"`
8183
}
8284

8385
type Permission struct {

0 commit comments

Comments
 (0)