From 5a5fbeb9daa87b8221f058c7019fe21ae4e6f524 Mon Sep 17 00:00:00 2001 From: Alexander Albrecht Date: Mon, 31 Oct 2022 15:27:26 +0100 Subject: [PATCH] fix branch restriction error on 404 response --- bitbucket/resource_branch_restriction.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/bitbucket/resource_branch_restriction.go b/bitbucket/resource_branch_restriction.go index 547dc9c3..8d9d2083 100644 --- a/bitbucket/resource_branch_restriction.go +++ b/bitbucket/resource_branch_restriction.go @@ -212,16 +212,16 @@ func resourceBranchRestrictionsRead(ctx context.Context, d *schema.ResourceData, brRes, res, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsIdGet(c.AuthContext, url.PathEscape(d.Id()), d.Get("repository").(string), d.Get("owner").(string)) - if err != nil { - return diag.FromErr(err) - } - - if res.StatusCode == http.StatusNotFound { + if res != nil && res.StatusCode == http.StatusNotFound { log.Printf("[WARN] Branch Restrictions (%s) not found, removing from state", d.Id()) d.SetId("") return nil } + if err != nil { + return diag.FromErr(err) + } + d.SetId(string(fmt.Sprintf("%v", brRes.Id))) d.Set("kind", brRes.Kind) d.Set("pattern", brRes.Pattern) @@ -254,9 +254,14 @@ func resourceBranchRestrictionsDelete(ctx context.Context, d *schema.ResourceDat c := m.(Clients).genClient brApi := c.ApiClient.BranchRestrictionsApi - _, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsIdDelete(c.AuthContext, url.PathEscape(d.Id()), + res, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsIdDelete(c.AuthContext, url.PathEscape(d.Id()), d.Get("repository").(string), d.Get("owner").(string)) + if res != nil && res.StatusCode == http.StatusNotFound { + log.Printf("[WARN] Branch Restrictions (%s) not found, removing from state", d.Id()) + return nil + } + if err != nil { return diag.FromErr(err) }