Skip to content

Commit

Permalink
azurerm_synapse_linked_service - report error during create/update (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Jan 5, 2023
1 parent 9be8ea0 commit f1e4cf8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/services/synapse/synapse_linked_service_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package synapse
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"

"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down Expand Up @@ -305,6 +307,12 @@ func resourceSynapseLinkedServiceCreateUpdate(d *pluginsdk.ResourceData, meta in
return fmt.Errorf("waiting on creation for %s: %+v", id, err)
}

// Sometimes this resource fails to create but Azure is returning a 200. We'll check if the last response failed or not before moving on
// todo remove this once https://github.com/hashicorp/go-azure-sdk/pull/122 is merged
if err = checkLinkedServiceResponse(future.Response()); err != nil {
return err
}

d.SetId(id.ID())

return resourceSynapseLinkedServiceRead(d, meta)
Expand Down Expand Up @@ -508,3 +516,37 @@ func flattenSynapseLinkedServiceIntegrationRuntimeV2(input *artifacts.Integratio
func suppressJsonOrderingDifference(_, old, new string, _ *pluginsdk.ResourceData) bool {
return utils.NormalizeJson(old) == utils.NormalizeJson(new)
}

func checkLinkedServiceResponse(response *http.Response) error {
respBody, err := io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("reading status response body: %+v", err)
}
defer response.Body.Close()

body := make(map[string]interface{})
err = json.Unmarshal(respBody, &body)
if err != nil {
return fmt.Errorf("could not parse status response: %+v", err)
}

if statusRaw, ok := body["status"]; ok && statusRaw != nil {
if status, ok := statusRaw.(string); ok {
if status == "Failed" {
if errorRaw, ok := body["error"]; ok && errorRaw != nil {
if responseError, ok := errorRaw.(map[string]interface{}); ok {
if messageRaw, ok := responseError["message"]; ok && messageRaw != nil {
if message, ok := messageRaw.(string); ok {
return fmt.Errorf("creating/updating Linked Service: %s", message)
}
}
}
}
// we are specifically checking for `error` in the payload but if the status is Failed, we should return what we know
return fmt.Errorf("creating/updating Linked Service: %+v", body)
}
}
}

return nil
}

0 comments on commit f1e4cf8

Please sign in to comment.