From c005859d70b8366218be408aa906ea54267e022f Mon Sep 17 00:00:00 2001 From: Brett Wright Date: Tue, 17 May 2022 11:45:30 +0200 Subject: [PATCH 1/2] now only removes resource from state if sendResquest returns 404 and err --- client/client.go | 2 +- client/project_repositories.go | 4 ++-- client/system.go | 8 ++++---- provider/resource_config_auth.go | 8 +++++--- provider/resource_config_email.go | 7 ++++--- provider/resource_garbage_collection.go | 6 +++--- provider/resource_harbor_project_webhook.go | 6 +++--- provider/resource_labels.go | 6 +++--- provider/resource_project.go | 6 +++--- provider/resource_project_member_group.go | 6 +++--- provider/resource_project_member_user.go | 7 +++---- provider/resource_registry.go | 8 ++++---- provider/resource_replication.go | 7 +++---- 13 files changed, 41 insertions(+), 40 deletions(-) diff --git a/client/client.go b/client/client.go index 2f8139b..070789b 100644 --- a/client/client.go +++ b/client/client.go @@ -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{} diff --git a/client/project_repositories.go b/client/project_repositories.go index ba53de4..ccc027a 100644 --- a/client/project_repositories.go +++ b/client/project_repositories.go @@ -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 } @@ -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 } diff --git a/client/system.go b/client/system.go index 7a1bc21..2a17e7f 100644 --- a/client/system.go +++ b/client/system.go @@ -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 } @@ -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 @@ -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, @@ -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 diff --git a/provider/resource_config_auth.go b/provider/resource_config_auth.go index cc28a82..497010a 100644 --- a/provider/resource_config_auth.go +++ b/provider/resource_config_auth.go @@ -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" @@ -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) diff --git a/provider/resource_config_email.go b/provider/resource_config_email.go index e039865..1784844 100644 --- a/provider/resource_config_email.go +++ b/provider/resource_config_email.go @@ -2,6 +2,7 @@ package provider import ( "encoding/json" + "fmt" "github.com/BESTSELLER/terraform-provider-harbor/client" "github.com/BESTSELLER/terraform-provider-harbor/models" @@ -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 diff --git a/provider/resource_garbage_collection.go b/provider/resource_garbage_collection.go index 4197a03..05d35f8 100644 --- a/provider/resource_garbage_collection.go +++ b/provider/resource_garbage_collection.go @@ -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 diff --git a/provider/resource_harbor_project_webhook.go b/provider/resource_harbor_project_webhook.go index 3c9f116..9c65c7b 100644 --- a/provider/resource_harbor_project_webhook.go +++ b/provider/resource_harbor_project_webhook.go @@ -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 diff --git a/provider/resource_labels.go b/provider/resource_labels.go index 6b90905..75fc9d8 100644 --- a/provider/resource_labels.go +++ b/provider/resource_labels.go @@ -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 diff --git a/provider/resource_project.go b/provider/resource_project.go index 868efa5..1b40d8a 100644 --- a/provider/resource_project.go +++ b/provider/resource_project.go @@ -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 diff --git a/provider/resource_project_member_group.go b/provider/resource_project_member_group.go index edc8f2c..36b8d81 100644 --- a/provider/resource_project_member_group.go +++ b/provider/resource_project_member_group.go @@ -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 diff --git a/provider/resource_project_member_user.go b/provider/resource_project_member_user.go index d06226e..4ab8d50 100644 --- a/provider/resource_project_member_user.go +++ b/provider/resource_project_member_user.go @@ -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 { diff --git a/provider/resource_registry.go b/provider/resource_registry.go index 567e790..5d2c68c 100644 --- a/provider/resource_registry.go +++ b/provider/resource_registry.go @@ -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()) } diff --git a/provider/resource_replication.go b/provider/resource_replication.go index 6c021fe..cdc2ecc 100644 --- a/provider/resource_replication.go +++ b/provider/resource_replication.go @@ -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 { From 60d0c7e96a1c0e57e3e447c47cd664823dcb632d Mon Sep 17 00:00:00 2001 From: Brett Wright Date: Tue, 17 May 2022 11:51:29 +0200 Subject: [PATCH 2/2] fixed test --- provider/provider_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/provider_test.go b/provider/provider_test.go index 496e16a..4ea6511 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -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) }