diff --git a/operator/es_client.go b/operator/es_client.go index 36f4d69..ef83bf5 100644 --- a/operator/es_client.go +++ b/operator/es_client.go @@ -436,6 +436,11 @@ func (c *ESClient) UpdateIndexSettings(indices []ESIndex) error { } if resp.StatusCode() != http.StatusOK { + // if the index doesn't exist ES would return a 404 + if resp.StatusCode() == http.StatusNotFound { + log.Warnf("Index '%s' not found, assuming it has been deleted.", index.Index) + return nil + } return fmt.Errorf("code status %d - %s", resp.StatusCode(), resp.Body()) } } diff --git a/operator/es_client_test.go b/operator/es_client_test.go index 0bc7e7a..615a8ca 100644 --- a/operator/es_client_test.go +++ b/operator/es_client_test.go @@ -159,7 +159,33 @@ func TestUpdateIndexSettings(t *testing.T) { err := systemUnderTest.UpdateIndexSettings(indices) assert.NoError(t, err) +} + +func TestUpdateIndexSettingsIgnoresUnknownIndex(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder("GET", "http://elasticsearch:9200/_cluster/health", + httpmock.NewStringResponder(200, `{"status":"green"}`)) + httpmock.RegisterResponder("PUT", "http://elasticsearch:9200/myindex/_settings", + httpmock.NewStringResponder(404, `{}`)) + url, _ := url.Parse("http://elasticsearch:9200") + systemUnderTest := &ESClient{ + Endpoint: url, + } + + indices := make([]ESIndex, 0, 1) + indices = append(indices, ESIndex{ + Primaries: 1, + Replicas: 1, + Index: "myindex", + }) + err := systemUnderTest.UpdateIndexSettings(indices) + info := httpmock.GetCallCountInfo() + + assert.NoError(t, err) + require.EqualValues(t, 1, info["PUT http://elasticsearch:9200/myindex/_settings"]) } func TestCreateIndex(t *testing.T) {