Skip to content

Commit f4e1d27

Browse files
committed
fixes #7; if a resource is not found the terraform read signal remove the resource from the state
1 parent 72fd38b commit f4e1d27

File tree

4 files changed

+78
-57
lines changed

4 files changed

+78
-57
lines changed

internal/provider/attribution_group_resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
"strings"
56

67
"fmt"
78
"time"
@@ -163,6 +164,10 @@ func (r *attributionGroupResource) Read(ctx context.Context, req resource.ReadRe
163164
// Get refreshed attributionGroup value from DoiT
164165
attributionGroup, err := r.client.GetAttributionGroup(state.Id.ValueString())
165166
if err != nil {
167+
if strings.Contains(err.Error(), "404") {
168+
resp.State.RemoveResource(ctx)
169+
return
170+
}
166171
resp.Diagnostics.AddError(
167172
"Error Reading Doit Console AttributionGroup",
168173
"Could not read Doit Console AttributionGroup ID "+state.Id.ValueString()+": "+err.Error(),

internal/provider/attribution_resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
"strings"
56

67
"fmt"
78
"log"
@@ -201,6 +202,10 @@ func (r *attributionResource) Read(ctx context.Context, req resource.ReadRequest
201202
// Get refreshed attribution value from DoiT
202203
attribution, err := r.client.GetAttribution(state.Id.ValueString())
203204
if err != nil {
205+
if strings.Contains(err.Error(), "404") {
206+
resp.State.RemoveResource(ctx)
207+
return
208+
}
204209
resp.Diagnostics.AddError(
205210
"Error Reading Doit Console Attribution",
206211
"Could not read Doit Console Attribution ID "+state.Id.ValueString()+": "+err.Error(),

internal/provider/budget_resource.go

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"time"
78

89
"log"
@@ -427,64 +428,65 @@ func (r *budgetResource) Create(ctx context.Context, req resource.CreateRequest,
427428
}
428429

429430
func budgetToBudgetResourceModel(budget *Budget, budgetModel *budgetResourceModel, ctx context.Context) {
430-
budgetModel.Id = types.StringValue(budget.Id)
431-
budgetModel.Alerts = []ExternalBudgetAlertModel{}
432-
for _, alert := range budget.Alerts {
433-
budgetModel.Alerts = append(budgetModel.Alerts, ExternalBudgetAlertModel{
434-
ForecastedDate: types.Int64Value(alert.ForecastedDate),
435-
Percentage: types.Float64Value(alert.Percentage),
436-
Triggered: types.BoolValue(alert.Triggered),
437-
})
438-
}
439-
budgetModel.Amount = types.Float64Value(budget.Amount)
440-
budgetModel.Collaborators = []CollaboratorModel{}
441-
for _, collaborator := range budget.Collaborators {
442-
budgetModel.Collaborators = append(budgetModel.Collaborators, CollaboratorModel{
443-
Email: types.StringValue(collaborator.Email),
444-
Role: types.StringValue(collaborator.Role),
445-
})
446-
}
447-
budgetModel.Currency = types.StringValue(budget.Currency)
448-
budgetModel.Description = types.StringValue(budget.Description)
449-
if budget.EndPeriod > 0 && budget.EndPeriod != 2678400000 {
450-
budgetModel.EndPeriod = types.Int64Value(budget.EndPeriod)
451-
}
452-
budgetModel.GrowthPerPeriod = types.Float64Value(budget.GrowthPerPeriod)
453-
budgetModel.Metric = types.StringValue(budget.Metric)
454-
budgetModel.Type = types.StringValue(budget.Type)
455-
budgetModel.Name = types.StringValue(budget.Name)
456-
if budget.Public != nil {
457-
public := budget.Public
458-
if *public != "" {
459-
budgetModel.Public = types.StringValue(*public)
431+
if budget != nil {
432+
budgetModel.Id = types.StringValue(budget.Id)
433+
budgetModel.Alerts = []ExternalBudgetAlertModel{}
434+
for _, alert := range budget.Alerts {
435+
budgetModel.Alerts = append(budgetModel.Alerts, ExternalBudgetAlertModel{
436+
ForecastedDate: types.Int64Value(alert.ForecastedDate),
437+
Percentage: types.Float64Value(alert.Percentage),
438+
Triggered: types.BoolValue(alert.Triggered),
439+
})
460440
}
461-
}
462-
budgetModel.Recipients = []types.String{}
463-
for _, recipient := range budget.Recipients {
464-
budgetModel.Recipients = append(budgetModel.Recipients, types.StringValue(recipient))
465-
}
466-
if budget.RecipientsSlackChannels != nil {
467-
budgetModel.RecipientsSlackChannels = []SlackChannelModel{}
468-
for _, recipient := range budget.RecipientsSlackChannels {
469-
budgetModel.RecipientsSlackChannels = append(budgetModel.RecipientsSlackChannels, SlackChannelModel{
470-
CustomerId: types.StringValue(recipient.CustomerId),
471-
Id: types.StringValue(recipient.Id),
472-
Name: types.StringValue(recipient.Name),
473-
Shared: types.BoolValue(recipient.Shared),
474-
Type: types.StringValue(recipient.Type),
475-
Workspace: types.StringValue(recipient.Workspace),
441+
budgetModel.Amount = types.Float64Value(budget.Amount)
442+
budgetModel.Collaborators = []CollaboratorModel{}
443+
for _, collaborator := range budget.Collaborators {
444+
budgetModel.Collaborators = append(budgetModel.Collaborators, CollaboratorModel{
445+
Email: types.StringValue(collaborator.Email),
446+
Role: types.StringValue(collaborator.Role),
476447
})
477448
}
449+
budgetModel.Currency = types.StringValue(budget.Currency)
450+
budgetModel.Description = types.StringValue(budget.Description)
451+
if budget.EndPeriod > 0 && budget.EndPeriod != 2678400000 {
452+
budgetModel.EndPeriod = types.Int64Value(budget.EndPeriod)
453+
}
454+
budgetModel.GrowthPerPeriod = types.Float64Value(budget.GrowthPerPeriod)
455+
budgetModel.Metric = types.StringValue(budget.Metric)
456+
budgetModel.Type = types.StringValue(budget.Type)
457+
budgetModel.Name = types.StringValue(budget.Name)
458+
if budget.Public != nil {
459+
public := budget.Public
460+
if *public != "" {
461+
budgetModel.Public = types.StringValue(*public)
462+
}
463+
}
464+
budgetModel.Recipients = []types.String{}
465+
for _, recipient := range budget.Recipients {
466+
budgetModel.Recipients = append(budgetModel.Recipients, types.StringValue(recipient))
467+
}
468+
if budget.RecipientsSlackChannels != nil {
469+
budgetModel.RecipientsSlackChannels = []SlackChannelModel{}
470+
for _, recipient := range budget.RecipientsSlackChannels {
471+
budgetModel.RecipientsSlackChannels = append(budgetModel.RecipientsSlackChannels, SlackChannelModel{
472+
CustomerId: types.StringValue(recipient.CustomerId),
473+
Id: types.StringValue(recipient.Id),
474+
Name: types.StringValue(recipient.Name),
475+
Shared: types.BoolValue(recipient.Shared),
476+
Type: types.StringValue(recipient.Type),
477+
Workspace: types.StringValue(recipient.Workspace),
478+
})
479+
}
480+
}
481+
budgetModel.Scope = []types.String{}
482+
for _, scope := range budget.Scope {
483+
budgetModel.Scope = append(budgetModel.Scope, types.StringValue(scope))
484+
}
485+
budgetModel.StartPeriod = types.Int64Value(budget.StartPeriod)
486+
budgetModel.TimeInterval = types.StringValue(budget.TimeInterval)
487+
budgetModel.Type = types.StringValue(budget.Type)
488+
budgetModel.UsePrevSpend = types.BoolValue(budget.UsePrevSpend)
478489
}
479-
budgetModel.Scope = []types.String{}
480-
for _, scope := range budget.Scope {
481-
budgetModel.Scope = append(budgetModel.Scope, types.StringValue(scope))
482-
}
483-
budgetModel.StartPeriod = types.Int64Value(budget.StartPeriod)
484-
budgetModel.TimeInterval = types.StringValue(budget.TimeInterval)
485-
budgetModel.Type = types.StringValue(budget.Type)
486-
budgetModel.UsePrevSpend = types.BoolValue(budget.UsePrevSpend)
487-
488490
}
489491

490492
// Read refreshes the Terraform state with the latest data.
@@ -503,14 +505,18 @@ func (r *budgetResource) Read(ctx context.Context, req resource.ReadRequest, res
503505
log.Print(state.Id.ValueString())
504506
// Get refreshed budget value from DoiT
505507
budget, err := r.client.GetBudget(state.Id.ValueString())
506-
budgetToBudgetResourceModel(budget, &state, ctx)
507508
if err != nil {
509+
if strings.Contains(err.Error(), "404") {
510+
resp.State.RemoveResource(ctx)
511+
return
512+
}
508513
resp.Diagnostics.AddError(
509-
"Error Reading Doit Console Attribution",
510-
"Could not read Doit Console Attribution ID "+state.Id.ValueString()+": "+err.Error(),
514+
"Error Reading Doit Console Budget",
515+
"Could not read Doit Console Budget ID "+state.Id.ValueString()+": "+err.Error(),
511516
)
512517
return
513518
}
519+
budgetToBudgetResourceModel(budget, &state, ctx)
514520
log.Print("response::::::::::::::::::::::::::)")
515521
log.Print(budget)
516522
// Set refreshed state

internal/provider/report_resource.go

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

89
"log"
@@ -824,6 +825,10 @@ func (r *reportResource) Read(ctx context.Context, req resource.ReadRequest, res
824825
// Get refreshed report value from DoiT
825826
report, err := r.client.GetReport(state.Id.ValueString())
826827
if err != nil {
828+
if strings.Contains(err.Error(), "404") {
829+
resp.State.RemoveResource(ctx)
830+
return
831+
}
827832
resp.Diagnostics.AddError(
828833
"Error Reading Doit Console Attribution",
829834
"Could not read Doit Console Attribution ID "+state.Id.ValueString()+": "+err.Error(),

0 commit comments

Comments
 (0)