Skip to content

Commit

Permalink
azurerm_site_recovery_replication_policy - improve existing check (#…
Browse files Browse the repository at this point in the history
…19391)

* ignore bad request on existing check of `azurerm_site_recovery_replication_policy`

* data source `azurerm_site_recovery_replication_policy` ignore bad request too

* adding depends_on in test config to improve acctest

* revert useless change

* improve the existing check with further check

* add bad request handle method

* update fabric handle bad request

* refactor util func
  • Loading branch information
ziyeqf authored Nov 29, 2022
1 parent 6a17fa1 commit dd7082e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func resourceSiteRecoveryFabricCreate(d *pluginsdk.ResourceData, meta interface{
existing, err := client.Get(ctx, name)
if err != nil {
// NOTE: Bad Request due to https://github.com/Azure/azure-rest-api-specs/issues/12759
if !utils.ResponseWasNotFound(existing.Response) && !utils.ResponseWasBadRequest(existing.Response) {
if !utils.ResponseWasNotFound(existing.Response) && !utils.ResponseWasBadRequestWithServiceCode(existing.Response, err, "SubscriptionIdNotRegisteredWithSrs") {
return fmt.Errorf("checking for presence of existing site recovery fabric %s (vault %s): %+v", name, vaultName, err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func dataSourceSiteRecoveryReplicationPolicyRead(d *pluginsdk.ResourceData, meta

resp, err := client.Get(ctx, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
// NOTE: Bad Request due to https://github.com/Azure/azure-rest-api-specs/issues/12759
if utils.ResponseWasNotFound(resp.Response) || utils.ResponseWasBadRequestWithServiceCode(resp.Response, err, "SubscriptionIdNotRegisteredWithSrs") {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("making Read request on site recovery replication policy %s : %+v", id.String(), err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func resourceSiteRecoveryReplicationPolicyCreate(d *pluginsdk.ResourceData, meta
if d.IsNewResource() {
existing, err := client.Get(ctx, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
// NOTE: Bad Request due to https://github.com/Azure/azure-rest-api-specs/issues/12759
if !utils.ResponseWasNotFound(existing.Response) && !utils.ResponseWasBadRequestWithServiceCode(existing.Response, err, "SubscriptionIdNotRegisteredWithSrs") {
return fmt.Errorf("checking for presence of existing site recovery replication policy %s: %+v", name, err)
}
}
Expand Down
24 changes: 24 additions & 0 deletions utils/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
)

func ResponseWasNotFound(resp autorest.Response) bool {
Expand Down Expand Up @@ -48,3 +49,26 @@ func ResponseWasStatusCode(resp autorest.Response, statusCode int) bool { // nol

return false
}

func ResponseWasBadRequestWithServiceCode(resp autorest.Response, err error, serviceCode string) bool {
e, ok := err.(autorest.DetailedError)
if !ok {
return false
}

r, ok := e.Original.(*azure.RequestError)
if !ok {
return false
}

if r.ServiceError == nil || len(r.ServiceError.Details) == 0 {
return false
}

sc, ok := r.ServiceError.Details[0]["code"]
if !ok {
return false
}

return ResponseWasStatusCode(resp, http.StatusBadRequest) && sc == serviceCode
}

0 comments on commit dd7082e

Please sign in to comment.