Skip to content

Commit

Permalink
fixes #7; if a resource is not found the terraform read signal remove…
Browse files Browse the repository at this point in the history
… the resource from the state
  • Loading branch information
dianibar committed Feb 27, 2024
1 parent 72fd38b commit f4e1d27
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 57 deletions.
5 changes: 5 additions & 0 deletions internal/provider/attribution_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"strings"

"fmt"
"time"
Expand Down Expand Up @@ -163,6 +164,10 @@ func (r *attributionGroupResource) Read(ctx context.Context, req resource.ReadRe
// Get refreshed attributionGroup value from DoiT
attributionGroup, err := r.client.GetAttributionGroup(state.Id.ValueString())
if err != nil {
if strings.Contains(err.Error(), "404") {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Error Reading Doit Console AttributionGroup",
"Could not read Doit Console AttributionGroup ID "+state.Id.ValueString()+": "+err.Error(),
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/attribution_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"strings"

"fmt"
"log"
Expand Down Expand Up @@ -201,6 +202,10 @@ func (r *attributionResource) Read(ctx context.Context, req resource.ReadRequest
// Get refreshed attribution value from DoiT
attribution, err := r.client.GetAttribution(state.Id.ValueString())
if err != nil {
if strings.Contains(err.Error(), "404") {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Error Reading Doit Console Attribution",
"Could not read Doit Console Attribution ID "+state.Id.ValueString()+": "+err.Error(),
Expand Down
120 changes: 63 additions & 57 deletions internal/provider/budget_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"strings"
"time"

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

func budgetToBudgetResourceModel(budget *Budget, budgetModel *budgetResourceModel, ctx context.Context) {
budgetModel.Id = types.StringValue(budget.Id)
budgetModel.Alerts = []ExternalBudgetAlertModel{}
for _, alert := range budget.Alerts {
budgetModel.Alerts = append(budgetModel.Alerts, ExternalBudgetAlertModel{
ForecastedDate: types.Int64Value(alert.ForecastedDate),
Percentage: types.Float64Value(alert.Percentage),
Triggered: types.BoolValue(alert.Triggered),
})
}
budgetModel.Amount = types.Float64Value(budget.Amount)
budgetModel.Collaborators = []CollaboratorModel{}
for _, collaborator := range budget.Collaborators {
budgetModel.Collaborators = append(budgetModel.Collaborators, CollaboratorModel{
Email: types.StringValue(collaborator.Email),
Role: types.StringValue(collaborator.Role),
})
}
budgetModel.Currency = types.StringValue(budget.Currency)
budgetModel.Description = types.StringValue(budget.Description)
if budget.EndPeriod > 0 && budget.EndPeriod != 2678400000 {
budgetModel.EndPeriod = types.Int64Value(budget.EndPeriod)
}
budgetModel.GrowthPerPeriod = types.Float64Value(budget.GrowthPerPeriod)
budgetModel.Metric = types.StringValue(budget.Metric)
budgetModel.Type = types.StringValue(budget.Type)
budgetModel.Name = types.StringValue(budget.Name)
if budget.Public != nil {
public := budget.Public
if *public != "" {
budgetModel.Public = types.StringValue(*public)
if budget != nil {
budgetModel.Id = types.StringValue(budget.Id)
budgetModel.Alerts = []ExternalBudgetAlertModel{}
for _, alert := range budget.Alerts {
budgetModel.Alerts = append(budgetModel.Alerts, ExternalBudgetAlertModel{
ForecastedDate: types.Int64Value(alert.ForecastedDate),
Percentage: types.Float64Value(alert.Percentage),
Triggered: types.BoolValue(alert.Triggered),
})
}
}
budgetModel.Recipients = []types.String{}
for _, recipient := range budget.Recipients {
budgetModel.Recipients = append(budgetModel.Recipients, types.StringValue(recipient))
}
if budget.RecipientsSlackChannels != nil {
budgetModel.RecipientsSlackChannels = []SlackChannelModel{}
for _, recipient := range budget.RecipientsSlackChannels {
budgetModel.RecipientsSlackChannels = append(budgetModel.RecipientsSlackChannels, SlackChannelModel{
CustomerId: types.StringValue(recipient.CustomerId),
Id: types.StringValue(recipient.Id),
Name: types.StringValue(recipient.Name),
Shared: types.BoolValue(recipient.Shared),
Type: types.StringValue(recipient.Type),
Workspace: types.StringValue(recipient.Workspace),
budgetModel.Amount = types.Float64Value(budget.Amount)
budgetModel.Collaborators = []CollaboratorModel{}
for _, collaborator := range budget.Collaborators {
budgetModel.Collaborators = append(budgetModel.Collaborators, CollaboratorModel{
Email: types.StringValue(collaborator.Email),
Role: types.StringValue(collaborator.Role),
})
}
budgetModel.Currency = types.StringValue(budget.Currency)
budgetModel.Description = types.StringValue(budget.Description)
if budget.EndPeriod > 0 && budget.EndPeriod != 2678400000 {
budgetModel.EndPeriod = types.Int64Value(budget.EndPeriod)
}
budgetModel.GrowthPerPeriod = types.Float64Value(budget.GrowthPerPeriod)
budgetModel.Metric = types.StringValue(budget.Metric)
budgetModel.Type = types.StringValue(budget.Type)
budgetModel.Name = types.StringValue(budget.Name)
if budget.Public != nil {
public := budget.Public
if *public != "" {
budgetModel.Public = types.StringValue(*public)
}
}
budgetModel.Recipients = []types.String{}
for _, recipient := range budget.Recipients {
budgetModel.Recipients = append(budgetModel.Recipients, types.StringValue(recipient))
}
if budget.RecipientsSlackChannels != nil {
budgetModel.RecipientsSlackChannels = []SlackChannelModel{}
for _, recipient := range budget.RecipientsSlackChannels {
budgetModel.RecipientsSlackChannels = append(budgetModel.RecipientsSlackChannels, SlackChannelModel{
CustomerId: types.StringValue(recipient.CustomerId),
Id: types.StringValue(recipient.Id),
Name: types.StringValue(recipient.Name),
Shared: types.BoolValue(recipient.Shared),
Type: types.StringValue(recipient.Type),
Workspace: types.StringValue(recipient.Workspace),
})
}
}
budgetModel.Scope = []types.String{}
for _, scope := range budget.Scope {
budgetModel.Scope = append(budgetModel.Scope, types.StringValue(scope))
}
budgetModel.StartPeriod = types.Int64Value(budget.StartPeriod)
budgetModel.TimeInterval = types.StringValue(budget.TimeInterval)
budgetModel.Type = types.StringValue(budget.Type)
budgetModel.UsePrevSpend = types.BoolValue(budget.UsePrevSpend)
}
budgetModel.Scope = []types.String{}
for _, scope := range budget.Scope {
budgetModel.Scope = append(budgetModel.Scope, types.StringValue(scope))
}
budgetModel.StartPeriod = types.Int64Value(budget.StartPeriod)
budgetModel.TimeInterval = types.StringValue(budget.TimeInterval)
budgetModel.Type = types.StringValue(budget.Type)
budgetModel.UsePrevSpend = types.BoolValue(budget.UsePrevSpend)

}

// Read refreshes the Terraform state with the latest data.
Expand All @@ -503,14 +505,18 @@ func (r *budgetResource) Read(ctx context.Context, req resource.ReadRequest, res
log.Print(state.Id.ValueString())
// Get refreshed budget value from DoiT
budget, err := r.client.GetBudget(state.Id.ValueString())
budgetToBudgetResourceModel(budget, &state, ctx)
if err != nil {
if strings.Contains(err.Error(), "404") {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Error Reading Doit Console Attribution",
"Could not read Doit Console Attribution ID "+state.Id.ValueString()+": "+err.Error(),
"Error Reading Doit Console Budget",
"Could not read Doit Console Budget ID "+state.Id.ValueString()+": "+err.Error(),
)
return
}
budgetToBudgetResourceModel(budget, &state, ctx)
log.Print("response::::::::::::::::::::::::::)")
log.Print(budget)
// Set refreshed state
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/report_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"strings"
"time"

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

0 comments on commit f4e1d27

Please sign in to comment.