From 1a040fee859bb4d9ecaf62ab7ae0f4f98614c5a1 Mon Sep 17 00:00:00 2001 From: Ray Tung Date: Thu, 9 Mar 2023 21:19:59 +1100 Subject: [PATCH] fix(index, saved_searches): Refreshing Terraform state if resources are deleted outside of Terraform Instead of erroring out on Terraform plan / apply --- splunk/resource_splunk_indexes.go | 7 +++++++ splunk/resource_splunk_indexes_test.go | 8 ++++++++ splunk/resource_splunk_saved_searches.go | 13 ++++++++++--- splunk/resource_splunk_saved_searches_test.go | 8 ++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/splunk/resource_splunk_indexes.go b/splunk/resource_splunk_indexes.go index 8a8d5880..220a328a 100644 --- a/splunk/resource_splunk_indexes.go +++ b/splunk/resource_splunk_indexes.go @@ -403,6 +403,13 @@ func indexRead(d *schema.ResourceData, meta interface{}) error { } if entry == nil { + if !d.IsNewResource() { + // This probably means someone has deleted the index from Splunk directly. Setting empty id here to + // force Terraform to delete resource from state and thereby recreating it + d.SetId("") + + return nil + } return fmt.Errorf("Unable to find resource: %s", name) } diff --git a/splunk/resource_splunk_indexes_test.go b/splunk/resource_splunk_indexes_test.go index bac9898f..a671c9a1 100644 --- a/splunk/resource_splunk_indexes_test.go +++ b/splunk/resource_splunk_indexes_test.go @@ -38,6 +38,14 @@ func TestAccCreateSplunkIndex(t *testing.T) { ), }, { + // to test re-creation of remotely deleted or missing resources, delete the new index before updating it + PreConfig: func() { + client, _ := newTestClient() + + if _, err := client.DeleteIndexObject("new-index", "nobody", "system"); err != nil { + t.Error("PreConfig deletion of new-index failed") + } + }, Config: updateIndex, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "max_time_unreplicated_no_acks", "301"), diff --git a/splunk/resource_splunk_saved_searches.go b/splunk/resource_splunk_saved_searches.go index 6508fc2f..f573172e 100644 --- a/splunk/resource_splunk_saved_searches.go +++ b/splunk/resource_splunk_saved_searches.go @@ -58,9 +58,9 @@ func savedSearches() *schema.Resource { "4 - Warning", }, "action_snow_event_param_description": { - Type: schema.TypeString, - Optional: true, - Computed: true, + Type: schema.TypeString, + Optional: true, + Computed: true, Description: " A brief description of the event.", }, "action_snow_event_param_ci_identifier": { @@ -1152,6 +1152,13 @@ func savedSearchesRead(d *schema.ResourceData, meta interface{}) error { } if entry == nil { + if !d.IsNewResource() { + // This probably means someone has deleted the saved search from Splunk directly. Setting empty id here to + // force Terraform to delete resource from state and thereby recreating it + d.SetId("") + + return nil + } return fmt.Errorf("Unable to find resource: %v", name) } diff --git a/splunk/resource_splunk_saved_searches_test.go b/splunk/resource_splunk_saved_searches_test.go index 6e3a3d04..36800370 100644 --- a/splunk/resource_splunk_saved_searches_test.go +++ b/splunk/resource_splunk_saved_searches_test.go @@ -296,6 +296,14 @@ func TestAccSplunkSavedSearches(t *testing.T) { ), }, { + // to test re-creation of remotely deleted or missing resources, delete the new saved serarch before updating it + PreConfig: func() { + client, _ := newTestClient() + + if _, err := client.DeleteSavedSearches("Test New Alert", "nobody", "search"); err != nil { + t.Error("PreConfig deletion of Test New Alert failed") + } + }, Config: updatedSavedSearches, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "name", "Test New Alert"),