Skip to content

Commit d5e7421

Browse files
committed
fixes #6 by removing the arguments forecasted_date and triggered from the alert, fixes #4 by adding a budget example"
1 parent f4e1d27 commit d5e7421

File tree

5 files changed

+78
-154
lines changed

5 files changed

+78
-154
lines changed

docs/resources/budget.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,43 @@ description: |-
1010

1111

1212

13-
13+
## Example Usage
14+
15+
```terraform
16+
resource "doit_budget" "my_budget" {
17+
name = "test budget terraform"
18+
description = "hellogo test2"
19+
alerts = [
20+
{
21+
percentage = 50
22+
},
23+
{
24+
percentage = 85,
25+
},
26+
{
27+
percentage = 100,
28+
}
29+
]
30+
recipients = [
31+
32+
]
33+
collaborators = [
34+
{
35+
"email" : "[email protected]",
36+
"role" : "owner"
37+
},
38+
]
39+
scope = [
40+
"Evct3J0DYcyXIVuAXORd"
41+
]
42+
amount = 200
43+
currency = "AUD"
44+
growth_per_period = 10
45+
time_interval = "month"
46+
type = "recurring"
47+
use_prev_spend = false
48+
}
49+
```
1450

1551
<!-- schema generated by tfplugindocs -->
1652
## Schema
@@ -57,9 +93,7 @@ Required:
5793

5894
Optional:
5995

60-
- `forecasted_date` (Number)
6196
- `percentage` (Number)
62-
- `triggered` (Boolean)
6397

6498

6599
<a id="nestedatt--recipients_slack_channels"></a>

examples/main.tf

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ resource "doit_budget" "my_budget" {
1515
alerts = [
1616
{
1717
percentage = 50
18-
triggered = false
1918
},
2019
{
21-
"percentage" = 85,
22-
"triggered" = false
20+
percentage = 85,
2321
},
2422
{
25-
"percentage" = 100,
26-
"triggered" = false
23+
percentage = 100,
2724
}
2825
]
2926
recipients = [
@@ -41,8 +38,9 @@ resource "doit_budget" "my_budget" {
4138
amount = 200
4239
currency = "AUD"
4340
growth_per_period = 10
44-
time_interval = "month"
45-
type = "recurring"
41+
time_interval = "month"
42+
type = "recurring"
43+
use_prev_spend = false
4644
}
4745

4846
resource "doit_attribution" "attri" {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
resource "doit_budget" "my_budget" {
2+
name = "test budget terraform"
3+
description = "hellogo test2"
4+
alerts = [
5+
{
6+
percentage = 50
7+
},
8+
{
9+
percentage = 85,
10+
},
11+
{
12+
percentage = 100,
13+
}
14+
]
15+
recipients = [
16+
17+
]
18+
collaborators = [
19+
{
20+
"email" : "[email protected]",
21+
"role" : "owner"
22+
},
23+
]
24+
scope = [
25+
"Evct3J0DYcyXIVuAXORd"
26+
]
27+
amount = 200
28+
currency = "AUD"
29+
growth_per_period = 10
30+
time_interval = "month"
31+
type = "recurring"
32+
use_prev_spend = false
33+
}

internal/provider/budget_resource.go

Lines changed: 2 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ type budgetResourceModel struct {
8080
}
8181

8282
type ExternalBudgetAlertModel struct {
83-
ForecastedDate types.Int64 `tfsdk:"forecasted_date"`
8483
Percentage types.Float64 `tfsdk:"percentage"`
85-
Triggered types.Bool `tfsdk:"triggered"`
8684
}
8785

8886
type CollaboratorModel struct {
@@ -136,21 +134,11 @@ func (r *budgetResource) Schema(_ context.Context, _ resource.SchemaRequest, res
136134
Optional: true,
137135
NestedObject: schema.NestedAttributeObject{
138136
Attributes: map[string]schema.Attribute{
139-
"forecasted_date": schema.Int64Attribute{
140-
Optional: true,
141-
Computed: true,
142-
Default: int64default.StaticInt64(0),
143-
},
144137
"percentage": schema.Float64Attribute{
145138
Optional: true,
146139
Computed: true,
147140
Default: float64default.StaticFloat64(0.0),
148141
},
149-
"triggered": schema.BoolAttribute{
150-
Optional: true,
151-
Computed: true,
152-
Default: booldefault.StaticBool(false),
153-
},
154142
},
155143
},
156144
},
@@ -308,13 +296,9 @@ func budgetModelToBudget(budgetModel *budgetResourceModel, ctx context.Context)
308296
var budget Budget
309297
var alerts []ExternalBudgetAlert
310298
for _, alert := range budgetModel.Alerts {
311-
forecastedDate := alert.ForecastedDate.ValueInt64()
312299
percentage := alert.Percentage.ValueFloat64()
313-
triggered := alert.Triggered.ValueBool()
314300
alerts = append(alerts, ExternalBudgetAlert{
315-
ForecastedDate: forecastedDate,
316-
Percentage: percentage,
317-
Triggered: triggered,
301+
Percentage: percentage,
318302
})
319303
}
320304
budget.Alerts = alerts
@@ -433,9 +417,7 @@ func budgetToBudgetResourceModel(budget *Budget, budgetModel *budgetResourceMode
433417
budgetModel.Alerts = []ExternalBudgetAlertModel{}
434418
for _, alert := range budget.Alerts {
435419
budgetModel.Alerts = append(budgetModel.Alerts, ExternalBudgetAlertModel{
436-
ForecastedDate: types.Int64Value(alert.ForecastedDate),
437-
Percentage: types.Float64Value(alert.Percentage),
438-
Triggered: types.BoolValue(alert.Triggered),
420+
Percentage: types.Float64Value(alert.Percentage),
439421
})
440422
}
441423
budgetModel.Amount = types.Float64Value(budget.Amount)
@@ -549,76 +531,6 @@ func (r *budgetResource) Update(ctx context.Context, req resource.UpdateRequest,
549531
// Generate API request body from plan
550532
var budget Budget
551533
budget = budgetModelToBudget(&plan, ctx)
552-
/*budget.Id = state.Id.ValueString()
553-
var alerts []ExternalBudgetAlert
554-
for _, alert := range plan.Alerts {
555-
forecastedDate := alert.ForecastedDate.ValueInt64()
556-
percentage := alert.Percentage.ValueFloat64()
557-
triggered := alert.Triggered.ValueBool()
558-
alerts = append(alerts, ExternalBudgetAlert{
559-
ForecastedDate: forecastedDate,
560-
Percentage: percentage,
561-
Triggered: triggered,
562-
})
563-
}
564-
budget.Alerts = alerts
565-
amount := plan.Amount.ValueFloat64()
566-
budget.Amount = amount
567-
var collaborators []Collaborator
568-
for _, collaborator := range plan.Collaborators {
569-
email := collaborator.Email.ValueString()
570-
role := collaborator.Role.ValueString()
571-
collaborators = append(collaborators, Collaborator{
572-
Email: email,
573-
Role: role,
574-
})
575-
}
576-
budget.Collaborators = collaborators
577-
budget.Currency = plan.Currency.ValueString()
578-
description := plan.Description.ValueString()
579-
budget.Description = description
580-
endPeriod := plan.EndPeriod.ValueInt64()
581-
budget.EndPeriod = endPeriod
582-
growthPerPeriod := plan.GrowthPerPeriod.ValueFloat64()
583-
budget.GrowthPerPeriod = growthPerPeriod
584-
metric := plan.Metric.ValueString()
585-
budget.Metric = metric
586-
budget.Name = plan.Name.ValueString()
587-
public := plan.Public.ValueString()
588-
budget.Public = &public
589-
var recipients []string
590-
for _, recipient := range plan.Recipients {
591-
recipients = append(recipients, recipient.ValueString())
592-
}
593-
budget.Recipients = recipients
594-
var slackChannels []SlackChannel
595-
for _, slackChannel := range plan.RecipientsSlackChannels {
596-
customerId := slackChannel.CustomerId.ValueString()
597-
id := slackChannel.Id.ValueString()
598-
name := slackChannel.Name.ValueString()
599-
shared := slackChannel.Shared.ValueBool()
600-
typee := slackChannel.Type.ValueString()
601-
workspace := slackChannel.Workspace.ValueString()
602-
slackChannels = append(slackChannels, SlackChannel{
603-
CustomerId: customerId,
604-
Id: id,
605-
Name: name,
606-
Shared: shared,
607-
Type: typee,
608-
Workspace: workspace,
609-
})
610-
}
611-
budget.RecipientsSlackChannels = slackChannels
612-
var scope []string
613-
for _, scopee := range plan.Scope {
614-
scope = append(scope, scopee.ValueString())
615-
}
616-
budget.Scope = scope
617-
budget.StartPeriod = plan.StartPeriod.ValueInt64()
618-
budget.TimeInterval = plan.TimeInterval.ValueString()
619-
budget.Type = plan.Type.ValueString()
620-
usePrevSpend := plan.UsePrevSpend.ValueBool()
621-
budget.UsePrevSpend = usePrevSpend*/
622534

623535
// Update existing budget
624536
_, err := r.client.UpdateBudget(state.Id.ValueString(), budget)
@@ -641,57 +553,6 @@ func (r *budgetResource) Update(ctx context.Context, req resource.UpdateRequest,
641553
return
642554
}
643555
budgetToBudgetResourceModel(budgetResponse, &plan, ctx)
644-
// Update resource state with updated items and timestamp
645-
/*plan.Id = types.StringValue(budgetResponse.Id)
646-
plan.Alerts = []ExternalBudgetAlertModel{}
647-
for _, alert := range budgetResponse.Alerts {
648-
plan.Alerts = append(plan.Alerts, ExternalBudgetAlertModel{
649-
ForecastedDate: types.Int64Value(alert.ForecastedDate),
650-
Percentage: types.Float64Value(alert.Percentage),
651-
Triggered: types.BoolValue(alert.Triggered),
652-
})
653-
}
654-
plan.Amount = types.Float64Value(budgetResponse.Amount)
655-
plan.Collaborators = []CollaboratorModel{}
656-
for _, collaborator := range budgetResponse.Collaborators {
657-
plan.Collaborators = append(plan.Collaborators, CollaboratorModel{
658-
Email: types.StringValue(collaborator.Email),
659-
Role: types.StringValue(collaborator.Role),
660-
})
661-
}
662-
plan.Currency = types.StringValue(budgetResponse.Currency)
663-
plan.Description = types.StringValue(budgetResponse.Description)
664-
plan.EndPeriod = types.Int64Value(budgetResponse.EndPeriod)
665-
plan.GrowthPerPeriod = types.Float64Value(budgetResponse.GrowthPerPeriod)
666-
plan.Metric = types.StringValue(budgetResponse.Metric)
667-
plan.Type = types.StringValue(budgetResponse.Type)
668-
plan.Name = types.StringValue(budgetResponse.Name)
669-
publicResponse := budgetResponse.Public
670-
plan.Public = types.StringValue(*publicResponse)
671-
plan.Recipients = []types.String{}
672-
for _, recipient := range budgetResponse.Recipients {
673-
plan.Recipients = append(plan.Recipients, types.StringValue(recipient))
674-
}
675-
plan.RecipientsSlackChannels = []SlackChannelModel{}
676-
for _, recipient := range budgetResponse.RecipientsSlackChannels {
677-
plan.RecipientsSlackChannels = append(plan.RecipientsSlackChannels, SlackChannelModel{
678-
CustomerId: types.StringValue(recipient.CustomerId),
679-
Id: types.StringValue(recipient.Id),
680-
Name: types.StringValue(recipient.Name),
681-
Shared: types.BoolValue(recipient.Shared),
682-
Type: types.StringValue(recipient.Type),
683-
Workspace: types.StringValue(recipient.Workspace),
684-
})
685-
}
686-
plan.Scope = []types.String{}
687-
for _, scope := range budgetResponse.Scope {
688-
plan.Scope = append(plan.Scope, types.StringValue(scope))
689-
}
690-
plan.StartPeriod = types.Int64Value(budgetResponse.StartPeriod)
691-
plan.TimeInterval = types.StringValue(budgetResponse.TimeInterval)
692-
plan.Type = types.StringValue(budgetResponse.Type)
693-
plan.UsePrevSpend = types.BoolValue(budgetResponse.UsePrevSpend)
694-
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))*/
695556

696557
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
697558

internal/provider/model.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ type Budget struct {
277277

278278
// ExternalBudgetAlert defines model for ExternalBudgetAlert.
279279
type ExternalBudgetAlert struct {
280-
ForecastedDate int64 `json:"forecastedDate"`
281-
Percentage float64 `json:"percentage,omitempty"`
282-
Triggered bool `json:"triggered,omitempty"`
280+
Percentage float64 `json:"percentage,omitempty"`
283281
}
284282

285283
type Collaborator struct {

0 commit comments

Comments
 (0)