Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_synapse_linked_service - report error during create/update #19849

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/Azure/azure-rest-api-specs/issues/22035 is fixed
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}