Skip to content

Commit

Permalink
Merge pull request #218 from BESTSELLER/issue-192
Browse files Browse the repository at this point in the history
now only removes resource from state if sendResquest returns 404 and err
  • Loading branch information
wrighbr authored May 17, 2022
2 parents a023c3b + 60d0c7e commit c26190c
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewClient(url string, username string, password string, insecure bool) *Cli
}

// SendRequest send a http request
func (c *Client) SendRequest(method string, path string, payload interface{}, statusCode int) (value string, respheaders string, returnedStatusCode int, err error) {
func (c *Client) SendRequest(method string, path string, payload interface{}, statusCode int) (value string, respheaders string, respCode int, err error) {
url := c.url + path
client := &http.Client{}

Expand Down
4 changes: 2 additions & 2 deletions client/project_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c *Client) GetProjectRepositories(projectName string) ([]models.Repository
for {
reposPath := fmt.Sprintf("/projects/%s/repositories?page=%d&page_size=100", projectName, page)

resp, _, err := c.SendRequest("GET", reposPath, nil, 200)
resp, _, _, err := c.SendRequest("GET", reposPath, nil, 200)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -58,7 +58,7 @@ func (c *Client) DeleteProjectRepositories(projectName string) error {

repoPath := fmt.Sprintf("/projects/%s/repositories/%s", projectName, repoName)

_, _, err := c.SendRequest("DELETE", repoPath, nil, 200)
_, _, _, err := c.SendRequest("DELETE", repoPath, nil, 200)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions client/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (client *Client) SetSchedule(d *schema.ResourceData, scheduleType string) (

body := GetSystemBoby(d, scheduleType)

resp, _, err := client.SendRequest("GET", path, nil, 200)
resp, _, _, err := client.SendRequest("GET", path, nil, 200)
if err != nil {
return err
}
Expand All @@ -58,7 +58,7 @@ func (client *Client) SetSchedule(d *schema.ResourceData, scheduleType string) (
log.Printf("No Schedule found performing POST request")
}

_, _, err = client.SendRequest(requestType, path, body, httpStatusCode)
_, _, _, err = client.SendRequest(requestType, path, body, httpStatusCode)
if err != nil {
return err

Expand All @@ -68,7 +68,7 @@ func (client *Client) SetSchedule(d *schema.ResourceData, scheduleType string) (

// SetDefaultScanner set the default scanner within harbor
func (client *Client) SetDefaultScanner(scanner string) (err error) {
resp, _, err := client.SendRequest("GET", models.PathScanners, nil, 0)
resp, _, _, err := client.SendRequest("GET", models.PathScanners, nil, 0)

body := models.ScannerBody{
IsDefault: true,
Expand All @@ -83,7 +83,7 @@ func (client *Client) SetDefaultScanner(scanner string) (err error) {
for _, v := range jsonData {

if v.Name == strings.Title(scanner) {
_, _, err = client.SendRequest("PATCH", models.PathScanners+"/"+v.UUID, body, 0)
_, _, _, err = client.SendRequest("PATCH", models.PathScanners+"/"+v.UUID, body, 0)
}
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func testAccCheckResourceExists(resource string) resource.TestCheckFunc {
name := rs.Primary.ID

apiClient := testAccProvider.Meta().(*client.Client)
_, _, err := apiClient.SendRequest("GET", name, nil, 200)
_, _, _, err := apiClient.SendRequest("GET", name, nil, 200)
if err != nil {
return fmt.Errorf("error fetching item with resource %s. %s", resource, err)
}
Expand Down
8 changes: 5 additions & 3 deletions provider/resource_config_auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package provider

import (
"fmt"

"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -166,10 +168,10 @@ func resourceConfigAuthCreate(d *schema.ResourceData, m interface{}) error {
func resourceConfigAuthRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", models.PathConfig, nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", models.PathConfig, nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

err = client.SetAuthValues(d, resp)
Expand Down
7 changes: 4 additions & 3 deletions provider/resource_config_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"encoding/json"
"fmt"

"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
Expand Down Expand Up @@ -67,10 +68,10 @@ func resourceConfigEmailCreate(d *schema.ResourceData, m interface{}) error {
func resourceConfigEmailRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", models.PathConfig, nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", models.PathConfig, nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.ConfigBodyResponse
Expand Down
6 changes: 3 additions & 3 deletions provider/resource_garbage_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func resourceGCCreate(d *schema.ResourceData, m interface{}) error {
func resourceGCRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", models.PathGC, nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", models.PathGC, nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.SystemBody
Expand Down
6 changes: 3 additions & 3 deletions provider/resource_harbor_project_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func resourceProjectWebhookCreate(d *schema.ResourceData, m interface{}) error {
func resourceProjectWebhookRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.ProjectWebhook
Expand Down
6 changes: 3 additions & 3 deletions provider/resource_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ func resourceLabelCreate(d *schema.ResourceData, m interface{}) error {
func resourceLabelRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.Labels
Expand Down
6 changes: 3 additions & 3 deletions provider/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func resourceProjectCreate(d *schema.ResourceData, m interface{}) error {
func resourceProjectRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.ProjectsBodyResponses
Expand Down
6 changes: 3 additions & 3 deletions provider/resource_project_member_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ func resourceMembersGroupCreate(d *schema.ResourceData, m interface{}) error {
func resourceMembersGroupRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.ProjectMembersBodyResponses
Expand Down
7 changes: 3 additions & 4 deletions provider/resource_project_member_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ func resourceMembersUserCreate(d *schema.ResourceData, m interface{}) error {
func resourceMembersUserRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.ProjectMembersBodyResponses
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions provider/resource_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ func resourceRegistryCreate(d *schema.ResourceData, m interface{}) error {
func resourceRegistryRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.RegistryBody
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
if respCode == 404 && err != nil {
d.SetId("")
return fmt.Errorf("Resource not found %s", d.Id())
}
Expand Down
7 changes: 3 additions & 4 deletions provider/resource_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,11 @@ func resourceReplicationCreate(d *schema.ResourceData, m interface{}) error {
func resourceReplicationRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

resp, _, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return nil
return fmt.Errorf("Resource not found %s", d.Id())
}

var jsonData models.RegistryBody
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
Expand Down

0 comments on commit c26190c

Please sign in to comment.