Skip to content

Commit

Permalink
Added error parsing to cause project recreation on 404 (#69)
Browse files Browse the repository at this point in the history
* Added error handling to cause project recreation on 404
The `request(...)` function now returns a custom error type `ResourceNotFoundError`

Co-authored-by: atlantisbot <lkysow+atlantis@gmail.com>
Co-authored-by: Matthias Bartelmeß <mba@fourplusone.de>
  • Loading branch information
3 people committed Jul 29, 2022
1 parent d9735a8 commit 24eb995
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
7 changes: 6 additions & 1 deletion jira/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type ProjectRequest struct {
IssueSecurityScheme int `json:"issueSecurityScheme,omitempty" structs:"issueSecurityScheme,omitempty"`
PermissionScheme int `json:"permissionScheme,omitempty" structs:"permissionScheme,omitempty"`
NotificationScheme int `json:"notificationScheme,omitempty" structs:"notificationScheme,omitempty"`
CategoryID string `json:"categoryId,omitempty" structs:"categoryId,omitempty"`
CategoryID string `json:"categoryId,omitempty" structs:"categoryId,omitempty"`
}

type SharedConfigurationProjectResponse struct {
Expand Down Expand Up @@ -231,7 +231,12 @@ func resourceProjectRead(d *schema.ResourceData, m interface{}) error {

urlStr := fmt.Sprintf("%s/%s", projectAPIEndpoint, d.Id())
err := request(config.jiraClient, "GET", urlStr, nil, project)

if err != nil {
if errors.Is(err, ResourceNotFoundError) {
d.SetId("")
return nil
}
return errors.Wrap(err, "Request failed")
}

Expand Down
33 changes: 29 additions & 4 deletions jira/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ func TestAccJiraProject_basic(t *testing.T) {
})
}

func TestAccJiraProject_deleted(t *testing.T) {
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckJiraProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccJiraProjectConfig(rInt),
},
{
PreConfig: func() {
urlStr := fmt.Sprintf("%s/PX%d", projectAPIEndpoint, rInt%100000)
jiraClient := testAccProvider.Meta().(*Config).jiraClient
err := request(jiraClient, "DELETE", urlStr, nil, nil)
if err != nil {
t.Fatal(err)
}
},
Config: testAccJiraProjectConfig(rInt),
},
},
})
}

func TestAccJiraProject_shared(t *testing.T) {
rInt := acctest.RandInt()

Expand Down Expand Up @@ -109,7 +134,7 @@ resource "jira_user" "foo" {
}
resource "jira_project_category" "category" {
name = "Managed"
name = "Managed %d"
description = "Managed Projects"
}
Expand All @@ -120,7 +145,7 @@ resource "jira_project" "foo" {
project_type_key = "business"
project_template_key = "com.atlassian.jira-core-project-templates:jira-core-project-management"
category_id = "${jira_project_category.category.id}"
}`, rInt, rInt, rInt%100000)
}`, rInt, rInt, rInt, rInt%100000)
}

func testAccJiraProjectConfigUpdate(rInt int) string {
Expand All @@ -132,7 +157,7 @@ resource "jira_user" "foo" {
}
resource "jira_project_category" "category" {
name = "Managed"
name = "Managed %d"
description = "Managed Projects"
}
Expand All @@ -143,7 +168,7 @@ resource "jira_project" "foo" {
project_type_key = "software"
project_template_key = "com.atlassian.jira-core-project-templates:jira-core-project-management"
category_id = "${jira_project_category.category.id}"
}`, rInt, rInt, rInt%100000)
}`, rInt, rInt, rInt, rInt%100000)
}

func testAccJiraSharedProjectConfig(rInt int) string {
Expand Down
25 changes: 24 additions & 1 deletion jira/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ func filterPermissionEndpoint(filterID string) string {

}

type resourceNotFoundError struct {
wrapped error
}

func (r *resourceNotFoundError) Error() string {
return "Resource not found"
}

func (r *resourceNotFoundError) Unwrap() error {
return r.wrapped
}

func (r *resourceNotFoundError) Is(e error) bool {
return e == ResourceNotFoundError
}

var (
ResourceNotFoundError = &resourceNotFoundError{} // Constant to use with errors.Is and errors.As
)

func request(client *jira.Client, method string, endpoint string, in interface{}, out interface{}) error {

req, err := client.NewRequest(method, endpoint, in)
Expand All @@ -49,16 +69,19 @@ func request(client *jira.Client, method string, endpoint string, in interface{}

res, err := client.Do(req, out)
if err != nil {

if in != nil && res != nil {
typeName := reflect.TypeOf(in).Name()
body, readErr := ioutil.ReadAll(res.Response.Body)
if readErr != nil {
return errors.Wrapf(readErr, "Creating %s Request failed", typeName)
}
return errors.Wrapf(err, "Creating %s Request failed: %s", typeName, body)
}

if res.StatusCode == 404 {
return &resourceNotFoundError{err}
}

return errors.Wrapf(err, "Creating Request failed")
}

Expand Down

0 comments on commit 24eb995

Please sign in to comment.