From dd7082ea01e219dab4b888cb54103978701ed68d Mon Sep 17 00:00:00 2001 From: ziyeqf <51212351+ziyeqf@users.noreply.github.com> Date: Tue, 29 Nov 2022 16:38:47 +0800 Subject: [PATCH] `azurerm_site_recovery_replication_policy` - improve existing check (#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 --- .../site_recovery_fabric_resource.go | 2 +- ...recovery_replication_policy_data_source.go | 3 ++- ...te_recovery_replication_policy_resource.go | 3 ++- utils/response.go | 24 +++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/internal/services/recoveryservices/site_recovery_fabric_resource.go b/internal/services/recoveryservices/site_recovery_fabric_resource.go index d93b29af073f..278a94c5334d 100644 --- a/internal/services/recoveryservices/site_recovery_fabric_resource.go +++ b/internal/services/recoveryservices/site_recovery_fabric_resource.go @@ -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) } } diff --git a/internal/services/recoveryservices/site_recovery_replication_policy_data_source.go b/internal/services/recoveryservices/site_recovery_replication_policy_data_source.go index 554178eacdc0..cd0217396fe7 100644 --- a/internal/services/recoveryservices/site_recovery_replication_policy_data_source.go +++ b/internal/services/recoveryservices/site_recovery_replication_policy_data_source.go @@ -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) diff --git a/internal/services/recoveryservices/site_recovery_replication_policy_resource.go b/internal/services/recoveryservices/site_recovery_replication_policy_resource.go index 2f427f7a9cc5..016464b08249 100644 --- a/internal/services/recoveryservices/site_recovery_replication_policy_resource.go +++ b/internal/services/recoveryservices/site_recovery_replication_policy_resource.go @@ -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) } } diff --git a/utils/response.go b/utils/response.go index de2074d8082e..8be0dedc5470 100644 --- a/utils/response.go +++ b/utils/response.go @@ -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 { @@ -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 +}