Skip to content

Commit ab979ba

Browse files
authored
Merge pull request #58 from SumoLogic/content-resource-update
update content resource to allow updates using overwrite flag
2 parents 5501c3e + b021f8d commit ab979ba

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
## 2.2.0 (Unreleased)
1+
## 2.1.3 (Unreleased)
2+
ENHANCEMENTS:
3+
* Allow updates to content resources so that dashboard links do not exprie. This creates a known bug - do not update the name of a resource.
4+
5+
## 2.1.2 (July 24, 2020)
6+
ENHANCEMENTS:
7+
* Now parrt of the Terraform Registry - compatible with Terraform 0.13
8+
29
## 2.1.1 (July 17, 2020)
310

411
DOCS:

sumologic/resource_sumologic_content.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func resourceSumologicContent() *schema.Resource {
1414
return &schema.Resource{
1515
Create: resourceSumologicContentCreate,
1616
Read: resourceSumologicContentRead,
17+
Update: resourceSumologicContentUpdate,
1718
Delete: resourceSumologicContentDelete,
1819

1920
Schema: map[string]*schema.Schema{
@@ -26,13 +27,13 @@ func resourceSumologicContent() *schema.Resource {
2627
Type: schema.TypeString,
2728
ValidateFunc: validation.StringIsJSON,
2829
Required: true,
29-
ForceNew: true,
3030
DiffSuppressFunc: structure.SuppressJsonDiff,
3131
},
3232
},
3333
Timeouts: &schema.ResourceTimeout{
3434
Read: schema.DefaultTimeout(1 * time.Minute),
3535
Create: schema.DefaultTimeout(10 * time.Minute),
36+
Update: schema.DefaultTimeout(10 * time.Minute),
3637
Delete: schema.DefaultTimeout(1 * time.Minute),
3738
},
3839
}
@@ -94,9 +95,9 @@ func resourceSumologicContentCreate(d *schema.ResourceData, meta interface{}) er
9495
log.Printf("Config: %s", content.Config)
9596

9697
//Call create content with our newly populated struct
97-
id, err := c.CreateContent(*content, d.Timeout(schema.TimeoutCreate))
98+
id, err := c.CreateOrUpdateContent(*content, d.Timeout(schema.TimeoutCreate), false)
9899

99-
//Error during CreateContent
100+
//Error during CreateOrUpdateContent
100101
if err != nil {
101102
return err
102103
}
@@ -114,6 +115,33 @@ func resourceSumologicContentCreate(d *schema.ResourceData, meta interface{}) er
114115
return resourceSumologicContentRead(d, meta)
115116
}
116117

118+
func resourceSumologicContentUpdate(d *schema.ResourceData, meta interface{}) error {
119+
log.Println("====Begin Content Update====")
120+
c := meta.(*Client)
121+
122+
//Load all data from the schema into a Content Struct
123+
content := resourceToContent(d)
124+
125+
//Call create content with overwrite set to true
126+
id, err := c.CreateOrUpdateContent(*content, d.Timeout(schema.TimeoutUpdate), true)
127+
128+
//Error during CreateOrUpdateContent
129+
if err != nil {
130+
return err
131+
}
132+
133+
log.Println("Saving Id to state...")
134+
d.SetId(id)
135+
log.Printf("ContentId: %s", id)
136+
log.Printf("ContentType: %s", content.Type)
137+
138+
log.Println("====End Content Update====")
139+
140+
//After updating an object, we read it to make sure the state is properly saved
141+
return resourceSumologicContentRead(d, meta)
142+
143+
}
144+
117145
func resourceToContent(d *schema.ResourceData) *Content {
118146
log.Println("Loading data from schema to Content struct...")
119147
var content Content

sumologic/resource_sumologic_content_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ func testAccCheckContentDestroy(content Content) resource.TestCheckFunc {
9898

9999
var updateConfigJson = `{
100100
"type": "SavedSearchWithScheduleSyncDefinition",
101-
"name": "test-333",
101+
"name": "test-121",
102102
"search": {
103103
"queryText": "\"warn\"",
104104
"defaultTimeRange": "-15m",
105105
"byReceiptTime": false,
106106
"viewName": "",
107107
"viewStartTime": "1970-01-01T00:00:00Z",
108108
"queryParameters": [],
109-
"parsingMode": "Manual"
109+
"parsingMode": "AutoParse"
110110
},
111111
"searchSchedule": {
112112
"cronExpression": "0 0 * * * ? *",
@@ -134,7 +134,7 @@ var updateConfigJson = `{
134134
"muteErrorEmails": false,
135135
"parameters": []
136136
},
137-
"description": "Runs every hour with timerange of 15m and sends email notifications"
137+
"description": "Runs every hour with timerange of 15m and sends email notifications updated"
138138
}`
139139

140140
var configJson = `{
@@ -147,7 +147,7 @@ var configJson = `{
147147
"viewName": "",
148148
"viewStartTime": "1970-01-01T00:00:00Z",
149149
"queryParameters": [],
150-
"parsingMode": "Manual"
150+
"parsingMode": "AutoParse"
151151
},
152152
"searchSchedule": {
153153
"cronExpression": "0 0 * * * ? *",

sumologic/sumologic_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func createNewRequest(method, url string, body io.Reader, accessID string, acces
4242
return nil, err
4343
}
4444
req.Header.Add("Content-Type", "application/json")
45-
req.Header.Add("User-Agent", "SumoLogicTerraformProvider/2.0.2")
45+
req.Header.Add("User-Agent", "SumoLogicTerraformProvider/2.1.3")
4646
req.SetBasicAuth(accessID, accessKey)
4747
return req, nil
4848
}

sumologic/sumologic_content.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"log"
7+
"strconv"
78
"strings"
89
"time"
910

@@ -115,11 +116,11 @@ func (s *Client) DeleteContent(id string, timeout time.Duration) error {
115116
return err
116117
}
117118

118-
//CREATE
119-
func (s *Client) CreateContent(content Content, timeout time.Duration) (string, error) {
120-
log.Println("####Begin CreateContent####")
119+
//CREATE or UPDATE
120+
func (s *Client) CreateOrUpdateContent(content Content, timeout time.Duration, overwrite bool) (string, error) {
121+
log.Println("####Begin CreateOrUpdateContent####")
121122

122-
url := fmt.Sprintf("v2/content/folders/%s/import", content.ParentId)
123+
url := fmt.Sprintf("v2/content/folders/%s/import?overwrite=%s", content.ParentId, strconv.FormatBool(overwrite))
123124
log.Printf("Create content url: %s", url)
124125

125126
//Initiate content creation job

website/docs/r/content.html.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ resource "sumologic_content" "test" {
6262
The following arguments are supported:
6363

6464
- `parent_id` - (Required) The identifier of the folder to import into. Identifiers from the Library in the Sumo user interface are provided in decimal format which is incompatible with Terraform. The identifier needs to be in hexadecimal format.
65-
- `config` - (Required) JSON block for the content to import.
65+
- `config` - (Required) JSON block for the content to import. NOTE: Updating the name will create a new object and leave a untracked content item (delete the existing content item and create a new content item if you want to update the name).
6666

6767
### Timeouts
6868

6969
`sumologic_content` provides the following [Timeouts](/docs/configuration/resources.html#timeouts) configuration options:
7070

7171
- `read` - (Default `1 minute`) Used for waiting for the import job to be successful
7272
- `create` - (Default `10 minutes`) Used for waiting for the import job to be successful
73+
- `update` - (Default `10 minutes`) Used for waiting for the import job to be successful
7374
- `delete` - (Default `1 minute`) Used for waiting for the deletion job to be successful
7475

7576
## Attributes reference

0 commit comments

Comments
 (0)