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

Added error parsing to cause project recreation on 404 #69

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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