Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
Signed-off-by: flbla <[email protected]>
  • Loading branch information
flbla committed Sep 29, 2023
1 parent 7717006 commit 3f05046
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
10 changes: 6 additions & 4 deletions provider/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func resourceProjectCreate(d *schema.ResourceData, m interface{}) error {
return err
}

id, err := client.GetID(headers)
id, _ := client.GetID(headers)
d.SetId(id)
return resourceProjectRead(d, m)
}
Expand All @@ -101,12 +101,14 @@ func resourceProjectRead(d *schema.ResourceData, m interface{}) error {
if respCode == 404 && err != nil {
d.SetId("")
return nil
} else if err != nil {
return fmt.Errorf("resource not found %s", d.Id())
}

var jsonData models.ProjectsBodyResponses
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
d.Set("name", jsonData.Name)
return fmt.Errorf("resource not found %s", d.Id())
}
autoScan := jsonData.Metadata.AutoScan
var vuln bool
Expand Down Expand Up @@ -185,12 +187,12 @@ func resourceProjectDelete(d *schema.ResourceData, m interface{}) error {
projectName := d.Get("name").(string)
repos, _ := apiClient.GetProjectRepositories(projectName)
if len(repos) != 0 {
return fmt.Errorf("Project %s is not empty, please set force_delete to TRUE to clean all repositories", projectName)
return fmt.Errorf("project %s is not empty, please set force_delete to TRUE to clean all repositories", projectName)
}
}

_, _, respCode, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if respCode == 404 && err != nil { // We can't delete something that doesn't exist. Hence the 404-check
if respCode != 404 && err != nil { // We can't delete something that doesn't exist. Hence the 404-check
return err
}
return nil
Expand Down
21 changes: 10 additions & 11 deletions provider/resource_project_member_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,19 @@ func resourceMembersGroupRead(d *schema.ResourceData, m interface{}) error {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return fmt.Errorf("Resource not found %s", d.Id())
return nil
} else if err != nil {
return fmt.Errorf("resource not found %s", d.Id())
}

var jsonData models.ProjectMembersBodyResponses
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
d.Set("group_name", jsonData.EntityName)
}
if err == nil {
d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
d.Set("project_id", checkProjectid(strconv.Itoa(jsonData.ProjectID)))
d.Set("group_name", jsonData.EntityName)
return fmt.Errorf("resource not found %s", d.Id())
}
d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
d.Set("project_id", checkProjectid(strconv.Itoa(jsonData.ProjectID)))
d.Set("group_name", jsonData.EntityName)
return nil
}

Expand All @@ -133,9 +132,9 @@ func resourceMembersGroupUpdate(d *schema.ResourceData, m interface{}) error {
func resourceMembersGroupDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

_, _, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if err != nil {
fmt.Println(err)
_, _, respCode, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if respCode != 404 && err != nil { // We can't delete something that doesn't exist. Hence the 404-check
return err
}
return nil
}
23 changes: 12 additions & 11 deletions provider/resource_project_member_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,20 @@ func resourceMembersUserRead(d *schema.ResourceData, m interface{}) error {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return fmt.Errorf("Resource not found %s", d.Id())
return nil
} else if err != nil {
return fmt.Errorf("resource not found %s", d.Id())
}

var jsonData models.ProjectMembersBodyResponses
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
d.Set("user_name", jsonData.EntityName)
}
if err == nil {
d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
d.Set("project_id", checkProjectid(strconv.Itoa(jsonData.ProjectID)))
d.Set("user_name", jsonData.EntityName)
return fmt.Errorf("resource not found %s", d.Id())
}
d.Set("role", client.RoleTypeNumber(jsonData.RoleID))
d.Set("project_id", checkProjectid(strconv.Itoa(jsonData.ProjectID)))
d.Set("user_name", jsonData.EntityName)

return nil
}

Expand All @@ -111,9 +112,9 @@ func resourceMembersUserUpdate(d *schema.ResourceData, m interface{}) error {
func resourceMembersUserDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

_, _, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if err != nil {
fmt.Println(err)
_, _, respCode, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if respCode != 404 && err != nil { // We can't delete something that doesn't exist. Hence the 404-check
return err
}
return nil
}

0 comments on commit 3f05046

Please sign in to comment.