From 10e5b08601ab71301c27e06d1a1353f130ea8cfc Mon Sep 17 00:00:00 2001 From: Florian Blampey Date: Wed, 7 Feb 2024 18:01:09 +0100 Subject: [PATCH] fixes if replication/groups/labels are in tf state but deleted outside terraform (#411) fixes #407 Signed-off-by: flbla --- provider/resource_group.go | 14 +++++++++----- provider/resource_labels.go | 8 +++++--- provider/resource_replication.go | 9 ++++++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/provider/resource_group.go b/provider/resource_group.go index 109a127b..793862bb 100644 --- a/provider/resource_group.go +++ b/provider/resource_group.go @@ -57,10 +57,14 @@ func resourceGroupCreate(d *schema.ResourceData, m interface{}) error { func resourceGroupRead(d *schema.ResourceData, m interface{}) error { apiClient := m.(*client.Client) - resp, _, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200) - if err != nil { - return err + resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200) + 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.GroupBody err = json.Unmarshal([]byte(resp), &jsonData) if err != nil { @@ -80,8 +84,8 @@ func resourceGroupUpdate(d *schema.ResourceData, m interface{}) error { func resourceGroupDelete(d *schema.ResourceData, m interface{}) error { apiClient := m.(*client.Client) - _, _, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) - if err != nil { + _, _, 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 diff --git a/provider/resource_labels.go b/provider/resource_labels.go index 2ff568fa..464071c3 100644 --- a/provider/resource_labels.go +++ b/provider/resource_labels.go @@ -65,7 +65,9 @@ func resourceLabelRead(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.Labels @@ -97,8 +99,8 @@ func resourceLabelUpdate(d *schema.ResourceData, m interface{}) error { func resourceLabelDelete(d *schema.ResourceData, m interface{}) error { apiClient := m.(*client.Client) - _, _, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) - if err != nil { + _, _, 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 diff --git a/provider/resource_replication.go b/provider/resource_replication.go index 173b1082..25fea2e4 100644 --- a/provider/resource_replication.go +++ b/provider/resource_replication.go @@ -157,8 +157,11 @@ func resourceReplicationRead(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.RegistryBody err = json.Unmarshal([]byte(resp), &jsonData) if err != nil { @@ -232,8 +235,8 @@ func resourceReplicationUpdate(d *schema.ResourceData, m interface{}) error { func resourceReplicationDelete(d *schema.ResourceData, m interface{}) error { apiClient := m.(*client.Client) - _, _, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) - if err != nil { + _, _, 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