Skip to content

Commit 55e0768

Browse files
Merge pull request #781 from SumoLogic/rs-sv-timezone
Feat: Add timezone support for scheduled view
2 parents 3a2e44a + e67f324 commit 55e0768

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
ENHANCEMENTS:
44
* Added IntervalTimeType Field to configure time types for Log_Search resource
55

6+
ENHANCEMENTS:
7+
* Add timezone support for scheduled_view
8+
69
BUG FIXES:
710
* Corrected capabilities documentation for role_v2
811

sumologic/resource_sumologic_scheduled_view.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func resourceSumologicScheduledView() *schema.Resource {
6666
Type: schema.TypeBool,
6767
Optional: true,
6868
},
69+
"time_zone": {
70+
Type: schema.TypeString,
71+
Optional: true,
72+
Computed: true,
73+
Description: "Time zone for ingesting data in scheduled view. Follow the format in the [IANA Time Zone Database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List).",
74+
},
6975
},
7076
}
7177
}
@@ -74,6 +80,12 @@ func resourceSumologicScheduledViewCreate(d *schema.ResourceData, meta interface
7480
c := meta.(*Client)
7581
if d.Id() == "" {
7682
sview := resourceToScheduledView(d)
83+
84+
// add timeZone if specified
85+
if v, ok := d.GetOk("time_zone"); ok {
86+
sview.TimeZone = v.(string)
87+
}
88+
7789
createdSview, err := c.CreateScheduledView(sview)
7890

7991
if err != nil {
@@ -112,6 +124,7 @@ func resourceSumologicScheduledViewRead(d *schema.ResourceData, meta interface{}
112124
d.Set("retention_period", sview.RetentionPeriod)
113125
d.Set("data_forwarding_id", sview.DataForwardingId)
114126
d.Set("parsing_mode", sview.ParsingMode)
127+
d.Set("time_zone", sview.TimeZone)
115128

116129
return nil
117130
}
@@ -122,6 +135,10 @@ func resourceSumologicScheduledViewDelete(d *schema.ResourceData, meta interface
122135
func resourceSumologicScheduledViewUpdate(d *schema.ResourceData, meta interface{}) error {
123136
sview := resourceToScheduledView(d)
124137

138+
if d.HasChange("time_zone") {
139+
sview.TimeZone = d.Get("time_zone").(string)
140+
}
141+
125142
c := meta.(*Client)
126143
err := c.UpdateScheduledView(sview)
127144

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package sumologic
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
)
12+
13+
// TestAccSumologicScheduledView_basic tests the creation and basic reading of a scheduled view.
14+
func TestAccSumologicScheduledView_basic(t *testing.T) {
15+
resourceName := "sumologic_scheduled_view.test_scheduled_view"
16+
name := "tf_test_scheduled_view_" + acctest.RandString(4)
17+
nameNoTimeZone := "tf_test_scheduled_view_no_tz" + acctest.RandString(4)
18+
initialQuery := "_sourceCategory=terraform/test/scheduledview/basic | count by _source"
19+
initialTimeZone := "America/Los_Angeles"
20+
initialRetention := 30 // days
21+
22+
resource.Test(t, resource.TestCase{
23+
PreCheck: func() { testAccPreCheck(t) },
24+
Providers: testAccProviders,
25+
CheckDestroy: testAccCheckSumologicScheduledViewDestroy,
26+
Steps: []resource.TestStep{
27+
// Test 1: Create a scheduled view with all initial fields, including time_zone.
28+
{
29+
Config: testAccSumologicScheduledViewConfig_Create(
30+
name,
31+
initialQuery,
32+
initialTimeZone,
33+
initialRetention,
34+
),
35+
Check: resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttr(resourceName, "index_name", name),
37+
resource.TestCheckResourceAttr(resourceName, "query", initialQuery),
38+
resource.TestCheckResourceAttr(resourceName, "time_zone", initialTimeZone),
39+
resource.TestCheckResourceAttr(resourceName, "retention_period", fmt.Sprintf("%d", initialRetention)),
40+
resource.TestCheckResourceAttrSet(resourceName, "id"),
41+
),
42+
},
43+
// Test 2: Update only time_zone and retention_period.
44+
// Other fields (name, query, index_alias) should remain unchanged.
45+
{
46+
Config: testAccSumologicScheduledViewConfig_Update(
47+
name, // Keep original name
48+
initialQuery, // Keep original query
49+
"Europe/Berlin", // NEW time_zone
50+
60, // NEW retention_period
51+
),
52+
Check: resource.ComposeTestCheckFunc(
53+
resource.TestCheckResourceAttr(resourceName, "index_name", name),
54+
resource.TestCheckResourceAttr(resourceName, "query", initialQuery), // Verify query is unchanged
55+
resource.TestCheckResourceAttr(resourceName, "time_zone", "Europe/Berlin"),
56+
resource.TestCheckResourceAttr(resourceName, "retention_period", "60"),
57+
),
58+
},
59+
// Test 3: Create a scheduled view WITHOUT time_zone initially (check default behavior)
60+
{
61+
Config: testAccSumologicScheduledViewConfig_CreateNoTimeZone(
62+
nameNoTimeZone,
63+
initialQuery,
64+
initialRetention,
65+
),
66+
Check: resource.ComposeTestCheckFunc(
67+
resource.TestCheckResourceAttr(resourceName, "index_name", nameNoTimeZone),
68+
resource.TestCheckResourceAttr(resourceName, "query", initialQuery),
69+
resource.TestCheckResourceAttr(resourceName, "retention_period", fmt.Sprintf("%d", initialRetention)),
70+
// defaults to "UTC" if time_zone not provided
71+
resource.TestCheckResourceAttr(resourceName, "time_zone", "UTC"),
72+
),
73+
},
74+
// Test 4: For the no-time-zone scheduled view, now set the time_zone and update retention.
75+
{
76+
Config: testAccSumologicScheduledViewConfig_Update(
77+
nameNoTimeZone, // Keep original name
78+
initialQuery, // Keep original query
79+
"Asia/Kolkata", // NEW time_zone
80+
90, // NEW retention_period
81+
),
82+
Check: resource.ComposeTestCheckFunc(
83+
resource.TestCheckResourceAttr(resourceName, "index_name", nameNoTimeZone),
84+
resource.TestCheckResourceAttr(resourceName, "query", initialQuery), // Verify query is unchanged
85+
resource.TestCheckResourceAttr(resourceName, "time_zone", "Asia/Kolkata"),
86+
resource.TestCheckResourceAttr(resourceName, "retention_period", "90"),
87+
),
88+
},
89+
},
90+
})
91+
}
92+
93+
// testAccCheckSumologicScheduledViewDestroy verifies that the scheduled view is deleted upon destroy.
94+
func testAccCheckSumologicScheduledViewDestroy(s *terraform.State) error {
95+
client := testAccProvider.Meta().(*Client)
96+
97+
for _, rs := range s.RootModule().Resources {
98+
if rs.Type != "sumologic_scheduled_view" {
99+
continue
100+
}
101+
fmt.Printf("DEBUG: Checking for id: %s", rs.Primary.ID)
102+
sv, err := client.GetScheduledView(rs.Primary.ID)
103+
if sv != nil {
104+
return fmt.Errorf("scheduled view (%s) still exists", rs.Primary.ID)
105+
}
106+
107+
if err != nil && !regexp.MustCompile(`not found|404|view:scheduled_view_not_found`).MatchString(err.Error()) {
108+
return fmt.Errorf("error checking scheduled view %s: %s", rs.Primary.ID, err)
109+
}
110+
}
111+
112+
return nil
113+
}
114+
115+
// testAccSumologicScheduledViewConfig_Create generates the HCL for initial creation.
116+
func testAccSumologicScheduledViewConfig_Create(name, query, timeZone string, retention int) string {
117+
return fmt.Sprintf(`
118+
resource "sumologic_scheduled_view" "test_scheduled_view" {
119+
index_name = "%s"
120+
query = "%s"
121+
start_time = "2024-01-01T00:00:00Z"
122+
retention_period = %d
123+
time_zone = "%s"
124+
}
125+
`, name, query, retention, timeZone)
126+
}
127+
128+
// testAccSumologicScheduledViewConfig_CreateNoTimeZone generates HCL for creation without time_zone.
129+
func testAccSumologicScheduledViewConfig_CreateNoTimeZone(name, query string, retention int) string {
130+
return fmt.Sprintf(`
131+
resource "sumologic_scheduled_view" "test_scheduled_view" {
132+
index_name = "%s"
133+
query = "%s"
134+
start_time = "2024-01-01T00:00:00Z"
135+
retention_period = %d
136+
# time_zone intentionally omitted to test default behavior
137+
}
138+
`, name, query, retention)
139+
}
140+
141+
// testAccSumologicScheduledViewConfig_Update generates HCL for updating specific fields.
142+
// It assumes that 'name', 'query', and 'indexAlias' are part of the resource config
143+
// but are NOT changed in the update scenario (they are just passed through from initial state).
144+
func testAccSumologicScheduledViewConfig_Update(name, query, newTimeZone string, newRetention int) string {
145+
return fmt.Sprintf(`
146+
resource "sumologic_scheduled_view" "test_scheduled_view" {
147+
index_name = "%s"
148+
query = "%s"
149+
start_time = "2024-01-01T00:00:00Z" # Keep start_time consistent
150+
retention_period = %d # Updated retention_period
151+
reduce_retention_period_immediately = false
152+
time_zone = "%s" # Updated time_zone
153+
}
154+
`, name, query, newRetention, newTimeZone)
155+
}

sumologic/sumologic_scheduled_view.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ type ScheduledView struct {
6666
DataForwardingId string `json:"dataForwardingId"`
6767
ParsingMode string `json:"parsingMode"`
6868
ReduceRetentionPeriodImmediately bool `json:"reduceRetentionPeriodImmediately"`
69+
TimeZone string `json:"timeZone,omitempty"`
6970
}

website/docs/r/scheduled_view.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ QUERY
2525
prevent_destroy = true
2626
ignore_changes = [index_id]
2727
}
28+
time_zone = "America/Los_Angeles"
2829
}
2930
```
3031

@@ -41,6 +42,7 @@ The following arguments are supported:
4142
- `data_forwarding_id` - (Optional) An optional ID of a data forwarding configuration to be used by the scheduled view.
4243
- `parsing_mode` - (Optional, Forces new resource) Default to `Manual`. Define the parsing mode to scan the JSON format log messages. Possible values are: `AutoParse` - In AutoParse mode, the system automatically figures out fields to parse based on the search query. `Manual` - While in the Manual mode, no fields are parsed out automatically. For more information see Dynamic Parsing.
4344
- `reduce_retention_period_immediately` - (Optional) This is required on update if the newly specified retention period is less than the existing retention period. In such a situation, a value of true says that data between the existing retention period and the new retention period should be deleted immediately; if false, such data will be deleted after seven days. This property is optional and ignored if the specified retentionPeriod is greater than or equal to the current retention period.
45+
- `time_zone` - (Optional) Time zone for ingesting data in scheduled view. Follow the format in the [IANA Time Zone Database][3].
4446

4547
The following attributes are exported:
4648

@@ -56,3 +58,4 @@ terraform import sumologic_scheduled_view.failed_connections 1234567890
5658

5759
[1]: https://help.sumologic.com/Manage/Scheduled-Views
5860
[2]: https://api.sumologic.com/docs/#operation/listScheduledViews
61+
[3]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List

0 commit comments

Comments
 (0)