|
| 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 | +} |
0 commit comments