Skip to content

Commit

Permalink
fixes #6 by removing the arguments forecasted_date and triggered from…
Browse files Browse the repository at this point in the history
… the alert, fixes #4 by adding a budget example"
  • Loading branch information
dianibar committed Feb 27, 2024
1 parent f4e1d27 commit d5e7421
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 154 deletions.
40 changes: 37 additions & 3 deletions docs/resources/budget.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,43 @@ description: |-




## Example Usage

```terraform
resource "doit_budget" "my_budget" {
name = "test budget terraform"
description = "hellogo test2"
alerts = [
{
percentage = 50
},
{
percentage = 85,
},
{
percentage = 100,
}
]
recipients = [
"[email protected]"
]
collaborators = [
{
"email" : "[email protected]",
"role" : "owner"
},
]
scope = [
"Evct3J0DYcyXIVuAXORd"
]
amount = 200
currency = "AUD"
growth_per_period = 10
time_interval = "month"
type = "recurring"
use_prev_spend = false
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down Expand Up @@ -57,9 +93,7 @@ Required:

Optional:

- `forecasted_date` (Number)
- `percentage` (Number)
- `triggered` (Boolean)


<a id="nestedatt--recipients_slack_channels"></a>
Expand Down
12 changes: 5 additions & 7 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ resource "doit_budget" "my_budget" {
alerts = [
{
percentage = 50
triggered = false
},
{
"percentage" = 85,
"triggered" = false
percentage = 85,
},
{
"percentage" = 100,
"triggered" = false
percentage = 100,
}
]
recipients = [
Expand All @@ -41,8 +38,9 @@ resource "doit_budget" "my_budget" {
amount = 200
currency = "AUD"
growth_per_period = 10
time_interval = "month"
type = "recurring"
time_interval = "month"
type = "recurring"
use_prev_spend = false
}

resource "doit_attribution" "attri" {
Expand Down
33 changes: 33 additions & 0 deletions examples/resources/doit_budget/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
resource "doit_budget" "my_budget" {
name = "test budget terraform"
description = "hellogo test2"
alerts = [
{
percentage = 50
},
{
percentage = 85,
},
{
percentage = 100,
}
]
recipients = [
"[email protected]"
]
collaborators = [
{
"email" : "[email protected]",
"role" : "owner"
},
]
scope = [
"Evct3J0DYcyXIVuAXORd"
]
amount = 200
currency = "AUD"
growth_per_period = 10
time_interval = "month"
type = "recurring"
use_prev_spend = false
}
143 changes: 2 additions & 141 deletions internal/provider/budget_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ type budgetResourceModel struct {
}

type ExternalBudgetAlertModel struct {
ForecastedDate types.Int64 `tfsdk:"forecasted_date"`
Percentage types.Float64 `tfsdk:"percentage"`
Triggered types.Bool `tfsdk:"triggered"`
}

type CollaboratorModel struct {
Expand Down Expand Up @@ -136,21 +134,11 @@ func (r *budgetResource) Schema(_ context.Context, _ resource.SchemaRequest, res
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"forecasted_date": schema.Int64Attribute{
Optional: true,
Computed: true,
Default: int64default.StaticInt64(0),
},
"percentage": schema.Float64Attribute{
Optional: true,
Computed: true,
Default: float64default.StaticFloat64(0.0),
},
"triggered": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
},
},
},
Expand Down Expand Up @@ -308,13 +296,9 @@ func budgetModelToBudget(budgetModel *budgetResourceModel, ctx context.Context)
var budget Budget
var alerts []ExternalBudgetAlert
for _, alert := range budgetModel.Alerts {
forecastedDate := alert.ForecastedDate.ValueInt64()
percentage := alert.Percentage.ValueFloat64()
triggered := alert.Triggered.ValueBool()
alerts = append(alerts, ExternalBudgetAlert{
ForecastedDate: forecastedDate,
Percentage: percentage,
Triggered: triggered,
Percentage: percentage,
})
}
budget.Alerts = alerts
Expand Down Expand Up @@ -433,9 +417,7 @@ func budgetToBudgetResourceModel(budget *Budget, budgetModel *budgetResourceMode
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),
Percentage: types.Float64Value(alert.Percentage),
})
}
budgetModel.Amount = types.Float64Value(budget.Amount)
Expand Down Expand Up @@ -549,76 +531,6 @@ func (r *budgetResource) Update(ctx context.Context, req resource.UpdateRequest,
// Generate API request body from plan
var budget Budget
budget = budgetModelToBudget(&plan, ctx)
/*budget.Id = state.Id.ValueString()
var alerts []ExternalBudgetAlert
for _, alert := range plan.Alerts {
forecastedDate := alert.ForecastedDate.ValueInt64()
percentage := alert.Percentage.ValueFloat64()
triggered := alert.Triggered.ValueBool()
alerts = append(alerts, ExternalBudgetAlert{
ForecastedDate: forecastedDate,
Percentage: percentage,
Triggered: triggered,
})
}
budget.Alerts = alerts
amount := plan.Amount.ValueFloat64()
budget.Amount = amount
var collaborators []Collaborator
for _, collaborator := range plan.Collaborators {
email := collaborator.Email.ValueString()
role := collaborator.Role.ValueString()
collaborators = append(collaborators, Collaborator{
Email: email,
Role: role,
})
}
budget.Collaborators = collaborators
budget.Currency = plan.Currency.ValueString()
description := plan.Description.ValueString()
budget.Description = description
endPeriod := plan.EndPeriod.ValueInt64()
budget.EndPeriod = endPeriod
growthPerPeriod := plan.GrowthPerPeriod.ValueFloat64()
budget.GrowthPerPeriod = growthPerPeriod
metric := plan.Metric.ValueString()
budget.Metric = metric
budget.Name = plan.Name.ValueString()
public := plan.Public.ValueString()
budget.Public = &public
var recipients []string
for _, recipient := range plan.Recipients {
recipients = append(recipients, recipient.ValueString())
}
budget.Recipients = recipients
var slackChannels []SlackChannel
for _, slackChannel := range plan.RecipientsSlackChannels {
customerId := slackChannel.CustomerId.ValueString()
id := slackChannel.Id.ValueString()
name := slackChannel.Name.ValueString()
shared := slackChannel.Shared.ValueBool()
typee := slackChannel.Type.ValueString()
workspace := slackChannel.Workspace.ValueString()
slackChannels = append(slackChannels, SlackChannel{
CustomerId: customerId,
Id: id,
Name: name,
Shared: shared,
Type: typee,
Workspace: workspace,
})
}
budget.RecipientsSlackChannels = slackChannels
var scope []string
for _, scopee := range plan.Scope {
scope = append(scope, scopee.ValueString())
}
budget.Scope = scope
budget.StartPeriod = plan.StartPeriod.ValueInt64()
budget.TimeInterval = plan.TimeInterval.ValueString()
budget.Type = plan.Type.ValueString()
usePrevSpend := plan.UsePrevSpend.ValueBool()
budget.UsePrevSpend = usePrevSpend*/

// Update existing budget
_, err := r.client.UpdateBudget(state.Id.ValueString(), budget)
Expand All @@ -641,57 +553,6 @@ func (r *budgetResource) Update(ctx context.Context, req resource.UpdateRequest,
return
}
budgetToBudgetResourceModel(budgetResponse, &plan, ctx)
// Update resource state with updated items and timestamp
/*plan.Id = types.StringValue(budgetResponse.Id)
plan.Alerts = []ExternalBudgetAlertModel{}
for _, alert := range budgetResponse.Alerts {
plan.Alerts = append(plan.Alerts, ExternalBudgetAlertModel{
ForecastedDate: types.Int64Value(alert.ForecastedDate),
Percentage: types.Float64Value(alert.Percentage),
Triggered: types.BoolValue(alert.Triggered),
})
}
plan.Amount = types.Float64Value(budgetResponse.Amount)
plan.Collaborators = []CollaboratorModel{}
for _, collaborator := range budgetResponse.Collaborators {
plan.Collaborators = append(plan.Collaborators, CollaboratorModel{
Email: types.StringValue(collaborator.Email),
Role: types.StringValue(collaborator.Role),
})
}
plan.Currency = types.StringValue(budgetResponse.Currency)
plan.Description = types.StringValue(budgetResponse.Description)
plan.EndPeriod = types.Int64Value(budgetResponse.EndPeriod)
plan.GrowthPerPeriod = types.Float64Value(budgetResponse.GrowthPerPeriod)
plan.Metric = types.StringValue(budgetResponse.Metric)
plan.Type = types.StringValue(budgetResponse.Type)
plan.Name = types.StringValue(budgetResponse.Name)
publicResponse := budgetResponse.Public
plan.Public = types.StringValue(*publicResponse)
plan.Recipients = []types.String{}
for _, recipient := range budgetResponse.Recipients {
plan.Recipients = append(plan.Recipients, types.StringValue(recipient))
}
plan.RecipientsSlackChannels = []SlackChannelModel{}
for _, recipient := range budgetResponse.RecipientsSlackChannels {
plan.RecipientsSlackChannels = append(plan.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),
})
}
plan.Scope = []types.String{}
for _, scope := range budgetResponse.Scope {
plan.Scope = append(plan.Scope, types.StringValue(scope))
}
plan.StartPeriod = types.Int64Value(budgetResponse.StartPeriod)
plan.TimeInterval = types.StringValue(budgetResponse.TimeInterval)
plan.Type = types.StringValue(budgetResponse.Type)
plan.UsePrevSpend = types.BoolValue(budgetResponse.UsePrevSpend)
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))*/

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

Expand Down
4 changes: 1 addition & 3 deletions internal/provider/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ type Budget struct {

// ExternalBudgetAlert defines model for ExternalBudgetAlert.
type ExternalBudgetAlert struct {
ForecastedDate int64 `json:"forecastedDate"`
Percentage float64 `json:"percentage,omitempty"`
Triggered bool `json:"triggered,omitempty"`
Percentage float64 `json:"percentage,omitempty"`
}

type Collaborator struct {
Expand Down

0 comments on commit d5e7421

Please sign in to comment.