Skip to content

Commit 66d6ec2

Browse files
Remove deleted resources and datasources from Terraform state on Read (all remaining services) (#346)
* Remove deleted resources and datasources from state on Read * Simplify code * Fix function description Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com> * Fix function description Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com> * Fix whitespace --------- Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com>
1 parent 464884c commit 66d6ec2

33 files changed

Lines changed: 262 additions & 40 deletions

File tree

stackit/internal/services/argus/credential/resource.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argus
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"strings"
78

89
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
@@ -14,6 +15,7 @@ import (
1415
"github.com/hashicorp/terraform-plugin-framework/types"
1516
"github.com/hashicorp/terraform-plugin-log/tflog"
1617
"github.com/stackitcloud/stackit-sdk-go/core/config"
18+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1719
"github.com/stackitcloud/stackit-sdk-go/services/argus"
1820
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1921
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
@@ -208,6 +210,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
208210
userName := model.Username.ValueString()
209211
_, err := r.client.GetCredentials(ctx, instanceId, projectId, userName).Execute()
210212
if err != nil {
213+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
214+
if ok && oapiErr.StatusCode == http.StatusNotFound {
215+
resp.State.RemoveResource(ctx)
216+
return
217+
}
211218
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
212219
return
213220
}

stackit/internal/services/argus/instance/datasource.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argus
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67

78
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
89
"github.com/hashicorp/terraform-plugin-framework/datasource"
@@ -11,7 +12,9 @@ import (
1112
"github.com/hashicorp/terraform-plugin-framework/types"
1213
"github.com/hashicorp/terraform-plugin-log/tflog"
1314
"github.com/stackitcloud/stackit-sdk-go/core/config"
15+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1416
"github.com/stackitcloud/stackit-sdk-go/services/argus"
17+
"github.com/stackitcloud/stackit-sdk-go/services/argus/wait"
1518
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1619
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1720
)
@@ -215,11 +218,20 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
215218
}
216219
projectId := model.ProjectId.ValueString()
217220
instanceId := model.InstanceId.ValueString()
218-
instanceResponse, err := d.client.GetInstance(ctx, instanceId, projectId).Execute()
221+
instanceResp, err := d.client.GetInstance(ctx, instanceId, projectId).Execute()
219222
if err != nil {
223+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
224+
if ok && oapiErr.StatusCode == http.StatusNotFound {
225+
resp.State.RemoveResource(ctx)
226+
}
220227
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err))
221228
return
222229
}
230+
if instanceResp != nil && instanceResp.Status != nil && *instanceResp.Status == wait.DeleteSuccess {
231+
resp.State.RemoveResource(ctx)
232+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", "Instance was deleted successfully")
233+
return
234+
}
223235

224236
aclList, err := d.client.ListACL(ctx, instanceId, projectId).Execute()
225237
if err != nil {
@@ -228,7 +240,7 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
228240
}
229241

230242
// Map response body to schema
231-
err = mapFields(ctx, instanceResponse, &model)
243+
err = mapFields(ctx, instanceResp, &model)
232244
if err != nil {
233245
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err))
234246
return

stackit/internal/services/argus/instance/resource.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argus
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"strings"
78

89
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
@@ -18,6 +19,7 @@ import (
1819
"github.com/hashicorp/terraform-plugin-framework/types"
1920
"github.com/hashicorp/terraform-plugin-log/tflog"
2021
"github.com/stackitcloud/stackit-sdk-go/core/config"
22+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2123
"github.com/stackitcloud/stackit-sdk-go/core/utils"
2224
"github.com/stackitcloud/stackit-sdk-go/services/argus"
2325
"github.com/stackitcloud/stackit-sdk-go/services/argus/wait"
@@ -370,9 +372,18 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
370372

371373
instanceResp, err := r.client.GetInstance(ctx, instanceId, projectId).Execute()
372374
if err != nil {
375+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
376+
if ok && oapiErr.StatusCode == http.StatusNotFound {
377+
resp.State.RemoveResource(ctx)
378+
return
379+
}
373380
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err))
374381
return
375382
}
383+
if instanceResp != nil && instanceResp.Status != nil && *instanceResp.Status == wait.DeleteSuccess {
384+
resp.State.RemoveResource(ctx)
385+
return
386+
}
376387

377388
aclList, err := r.client.ListACL(ctx, instanceId, projectId).Execute()
378389
if err != nil {

stackit/internal/services/argus/scrapeconfig/datasource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argus
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67

78
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
89
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
@@ -14,6 +15,7 @@ import (
1415
"github.com/hashicorp/terraform-plugin-framework/types"
1516
"github.com/hashicorp/terraform-plugin-log/tflog"
1617
"github.com/stackitcloud/stackit-sdk-go/core/config"
18+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1719
"github.com/stackitcloud/stackit-sdk-go/services/argus"
1820
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1921
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
@@ -212,6 +214,10 @@ func (d *scrapeConfigDataSource) Read(ctx context.Context, req datasource.ReadRe
212214

213215
scResp, err := d.client.GetScrapeConfig(ctx, instanceId, scName, projectId).Execute()
214216
if err != nil {
217+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
218+
if ok && oapiErr.StatusCode == http.StatusNotFound {
219+
resp.State.RemoveResource(ctx)
220+
}
215221
core.LogAndAddError(ctx, &resp.Diagnostics, "Unable to read scrape config", err.Error())
216222
return
217223
}

stackit/internal/services/argus/scrapeconfig/resource.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argus
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"strings"
78
"time"
89

@@ -24,6 +25,7 @@ import (
2425
"github.com/hashicorp/terraform-plugin-framework/types"
2526
"github.com/hashicorp/terraform-plugin-log/tflog"
2627
"github.com/stackitcloud/stackit-sdk-go/core/config"
28+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2729
"github.com/stackitcloud/stackit-sdk-go/core/utils"
2830
"github.com/stackitcloud/stackit-sdk-go/services/argus"
2931
"github.com/stackitcloud/stackit-sdk-go/services/argus/wait"
@@ -351,6 +353,11 @@ func (r *scrapeConfigResource) Read(ctx context.Context, req resource.ReadReques
351353

352354
scResp, err := r.client.GetScrapeConfig(ctx, instanceId, scName, projectId).Execute()
353355
if err != nil {
356+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
357+
if ok && oapiErr.StatusCode == http.StatusNotFound {
358+
resp.State.RemoveResource(ctx)
359+
return
360+
}
354361
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading scrape config", fmt.Sprintf("Calling API: %v", err))
355362
return
356363
}

stackit/internal/services/dns/recordset/datasource.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-log/tflog"
1212
"github.com/stackitcloud/stackit-sdk-go/core/config"
1313
"github.com/stackitcloud/stackit-sdk-go/services/dns"
14+
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
1415
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1516
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1617
)
@@ -158,13 +159,18 @@ func (d *recordSetDataSource) Read(ctx context.Context, req datasource.ReadReque
158159
ctx = tflog.SetField(ctx, "project_id", projectId)
159160
ctx = tflog.SetField(ctx, "zone_id", zoneId)
160161
ctx = tflog.SetField(ctx, "record_set_id", recordSetId)
161-
zoneResp, err := d.client.GetRecordSet(ctx, projectId, zoneId, recordSetId).Execute()
162+
recordSetResp, err := d.client.GetRecordSet(ctx, projectId, zoneId, recordSetId).Execute()
162163
if err != nil {
163164
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading record set", fmt.Sprintf("Calling API: %v", err))
164165
return
165166
}
167+
if recordSetResp != nil && recordSetResp.Rrset.State != nil && *recordSetResp.Rrset.State == wait.DeleteSuccess {
168+
resp.State.RemoveResource(ctx)
169+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading record set", "Record set was deleted successfully")
170+
return
171+
}
166172

167-
err = mapFields(ctx, zoneResp, &model)
173+
err = mapFields(ctx, recordSetResp, &model)
168174
if err != nil {
169175
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading record set", fmt.Sprintf("Processing API payload: %v", err))
170176
return

stackit/internal/services/dns/recordset/resource.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ func (r *recordSetResource) Read(ctx context.Context, req resource.ReadRequest,
282282
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading record set", fmt.Sprintf("Calling API: %v", err))
283283
return
284284
}
285+
if recordSetResp != nil && recordSetResp.Rrset.State != nil && *recordSetResp.Rrset.State == wait.DeleteSuccess {
286+
resp.State.RemoveResource(ctx)
287+
return
288+
}
285289

286290
// Map response body to schema
287291
err = mapFields(ctx, recordSetResp, &model)

stackit/internal/services/dns/zone/datasource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-log/tflog"
1212
"github.com/stackitcloud/stackit-sdk-go/core/config"
1313
"github.com/stackitcloud/stackit-sdk-go/services/dns"
14+
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
1415
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1516
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1617
)
@@ -193,6 +194,11 @@ func (d *zoneDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
193194
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading zone", fmt.Sprintf("Calling API: %v", err))
194195
return
195196
}
197+
if zoneResp != nil && zoneResp.Zone.State != nil && *zoneResp.Zone.State == wait.DeleteSuccess {
198+
resp.State.RemoveResource(ctx)
199+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading zone", "Zone was deleted successfully")
200+
return
201+
}
196202

197203
err = mapFields(ctx, zoneResp, &model)
198204
if err != nil {

stackit/internal/services/dns/zone/resource.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ func (r *zoneResource) Read(ctx context.Context, req resource.ReadRequest, resp
361361
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading zone", fmt.Sprintf("Calling API: %v", err))
362362
return
363363
}
364+
if zoneResp != nil && zoneResp.Zone.State != nil && *zoneResp.Zone.State == wait.DeleteSuccess {
365+
resp.State.RemoveResource(ctx)
366+
return
367+
}
364368

365369
// Map response body to schema
366370
err = mapFields(ctx, zoneResp, &model)

stackit/internal/services/mongodbflex/instance/datasource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mongodbflex
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67

78
"github.com/hashicorp/terraform-plugin-framework/datasource"
89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -14,6 +15,7 @@ import (
1415
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1516
"github.com/hashicorp/terraform-plugin-framework/types"
1617
"github.com/stackitcloud/stackit-sdk-go/core/config"
18+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1719
"github.com/stackitcloud/stackit-sdk-go/services/mongodbflex"
1820
)
1921

@@ -184,6 +186,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
184186
ctx = tflog.SetField(ctx, "instance_id", instanceId)
185187
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute()
186188
if err != nil {
189+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
190+
if ok && oapiErr.StatusCode == http.StatusNotFound {
191+
resp.State.RemoveResource(ctx)
192+
}
187193
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err))
188194
return
189195
}

0 commit comments

Comments
 (0)