splunk / terraform-provider-splunk

Terraform Provider for Splunk
Mozilla Public License 2.0
102 stars 75 forks source link

Check response status code for errors #169

Closed sriniketh923 closed 1 year ago

sriniketh923 commented 1 year ago

We don't check the response for errors when making API calls. Currently if the API returns a non 200 or 201 we ignore the error and terraform errors out with Could not find object id=... because it could not find the desired output in the response. This leaves the state file in a bad state to make any further changes. This change will propagate the error properly back to the user and not leave the state file broken.

Before:

2023-06-21T16:34:30.160-0400 [ERROR] vertex "module.lookup_testing_1.splunk_saved_searches.lookup_template" error: Could not find object id=test search
╷
│ Error: Could not find object id=test search
│
│   with module.lookup_testing_1.splunk_saved_searches.lookup_template,
│   on ../../../modules/lookup_template/main.tf line 1, in resource "splunk_saved_searches" "lookup_template":
│    1: resource "splunk_saved_searches" "lookup_template" {
│
╵
2023-06-21T16:34:30.184-0400 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"

After fix:

2023-06-22T09:13:00.109-0400 [ERROR] vertex "module.lookup_testing_1.splunk_saved_searches.lookup_template" error: 400 Bad Request: {"messages":[{"type":"ERROR","text":"windowed real-time per result alerts require field based alert throttling to be enabled"}]}
╷
│ Error: 400 Bad Request: {"messages":[{"type":"ERROR","text":"windowed real-time per result alerts require field based alert throttling to be enabled"}]}
│
│   with module.lookup_testing_1.splunk_saved_searches.lookup_template,
│   on ../../../modules/lookup_template/main.tf line 1, in resource "splunk_saved_searches" "lookup_template":
│    1: resource "splunk_saved_searches" "lookup_template" {
│
╵
2023-06-22T09:13:00.147-0400 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"