Skip to content

Commit

Permalink
fixes if replication/groups/labels are in tf state but deleted outsid…
Browse files Browse the repository at this point in the history
…e terraform (#411)

fixes #407

Signed-off-by: flbla <[email protected]>
  • Loading branch information
flbla authored Feb 7, 2024
1 parent dc03210 commit 10e5b08
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
14 changes: 9 additions & 5 deletions provider/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions provider/resource_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions provider/resource_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 10e5b08

Please sign in to comment.