@@ -3,6 +3,7 @@ package provider
33import (
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
429430func 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
0 commit comments