Skip to content

Commit

Permalink
feat!: Add support for custom time periods in budget module (#738)
Browse files Browse the repository at this point in the history
* Add support for custom time periods in budget module

* Add support for custom time periods in budget module

* Add support for custom time periods in budget module

* Add support for custom time periods in budget module

* Add support for custom time periods in budget module

* Add support for custom time periods in budget module

* Add support for custom time periods in budget module

* Add support for custom time periods in budget module

Co-authored-by: Bharath KKB <bharathkrishnakb@gmail.com>
  • Loading branch information
Abhisek Purwar and bharathkkb committed Aug 18, 2022
1 parent 748dea1 commit 9273052
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ determining that location is as follows:
| budget\_alert\_spend\_basis | The type of basis used to determine if spend has passed the threshold | `string` | `"CURRENT_SPEND"` | no |
| budget\_alert\_spent\_percents | A list of percentages of the budget to alert on when threshold is exceeded | `list(number)` | <pre>[<br> 0.5,<br> 0.7,<br> 1<br>]</pre> | no |
| budget\_amount | The amount to use for a budget alert | `number` | `null` | no |
| budget\_calendar\_period | Specifies the calendar period for the budget. Possible values are MONTH, QUARTER, YEAR, CALENDAR\_PERIOD\_UNSPECIFIED, CUSTOM. custom\_period\_start\_date and custom\_period\_end\_date must be set if CUSTOM | `string` | `null` | no |
| budget\_custom\_period\_end\_date | Specifies the end date (DD-MM-YYYY) for the calendar\_period CUSTOM | `string` | `null` | no |
| budget\_custom\_period\_start\_date | Specifies the start date (DD-MM-YYYY) for the calendar\_period CUSTOM | `string` | `null` | no |
| budget\_display\_name | The display name of the budget. If not set defaults to `Budget For <projects[0]|All Projects>` | `string` | `null` | no |
| budget\_labels | A single label and value pair specifying that usage from only this set of labeled resources should be included in the budget. | `map(string)` | `{}` | no |
| budget\_monitoring\_notification\_channels | A list of monitoring notification channels in the form `[projects/{project_id}/notificationChannels/{channel_id}]`. A maximum of 5 channels are allowed. | `list(string)` | `[]` | no |
Expand Down
3 changes: 3 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ module "budget" {
monitoring_notification_channels = var.budget_monitoring_notification_channels
display_name = var.budget_display_name != null ? var.budget_display_name : null
labels = var.budget_labels
calendar_period = var.budget_calendar_period
custom_period_start_date = var.budget_custom_period_start_date
custom_period_end_date = var.budget_custom_period_end_date
}

/******************************************
Expand Down
3 changes: 3 additions & 0 deletions modules/budget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ module "project_myproject" {
| alert\_spent\_percents | A list of percentages of the budget to alert on when threshold is exceeded | `list(number)` | <pre>[<br> 0.5,<br> 0.7,<br> 1<br>]</pre> | no |
| amount | The amount to use as the budget | `number` | n/a | yes |
| billing\_account | ID of the billing account to set a budget on | `string` | n/a | yes |
| calendar\_period | Specifies the calendar period for the budget. Possible values are MONTH, QUARTER, YEAR, CALENDAR\_PERIOD\_UNSPECIFIED, CUSTOM. custom\_period\_start\_date and custom\_period\_end\_date must be set if CUSTOM | `string` | `null` | no |
| create\_budget | If the budget should be created | `bool` | `true` | no |
| credit\_types\_treatment | Specifies how credits should be treated when determining spend for threshold calculations | `string` | `"INCLUDE_ALL_CREDITS"` | no |
| custom\_period\_end\_date | Specifies the end date (DD-MM-YYYY) for the calendar\_period CUSTOM | `string` | `null` | no |
| custom\_period\_start\_date | Specifies the start date (DD-MM-YYYY) for the calendar\_period CUSTOM | `string` | `null` | no |
| display\_name | The display name of the budget. If not set defaults to `Budget For <projects[0]|All Projects>` | `string` | `null` | no |
| labels | A single label and value pair specifying that usage from only this set of labeled resources should be included in the budget. | `map(string)` | `{}` | no |
| monitoring\_notification\_channels | A list of monitoring notification channels in the form `[projects/{project_id}/notificationChannels/{channel_id}]`. A maximum of 5 channels are allowed. | `list(string)` | `[]` | no |
Expand Down
20 changes: 20 additions & 0 deletions modules/budget/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ locals {
project_name = length(var.projects) == 0 ? "All Projects" : var.projects[0]
display_name = var.display_name == null ? "Budget For ${local.project_name}" : var.display_name
all_updates_rule = var.alert_pubsub_topic == null && length(var.monitoring_notification_channels) == 0 ? [] : ["1"]
custom_period = var.calendar_period == "CUSTOM" ? [1] : []
start_date = length(local.custom_period) != 0 ? split("-", var.custom_period_start_date) : null
end_date = length(local.custom_period) != 0 ? split("-", var.custom_period_end_date) : null

projects = length(var.projects) == 0 ? null : [
for project in data.google_project.project :
Expand Down Expand Up @@ -46,6 +49,23 @@ resource "google_billing_budget" "budget" {
credit_types_treatment = var.credit_types_treatment
services = local.services
labels = var.labels
calendar_period = length(local.custom_period) == 0 ? var.calendar_period : null

dynamic "custom_period" {
for_each = local.custom_period
content {
start_date {
day = local.start_date[0]
month = local.start_date[1]
year = local.start_date[2]
}
end_date {
day = local.end_date[0]
month = local.end_date[1]
year = local.end_date[2]
}
}
}
}

amount {
Expand Down
18 changes: 18 additions & 0 deletions modules/budget/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ variable "services" {
default = null
}

variable "calendar_period" {
description = "Specifies the calendar period for the budget. Possible values are MONTH, QUARTER, YEAR, CALENDAR_PERIOD_UNSPECIFIED, CUSTOM. custom_period_start_date and custom_period_end_date must be set if CUSTOM"
type = string
default = null
}

variable "custom_period_start_date" {
description = "Specifies the start date (DD-MM-YYYY) for the calendar_period CUSTOM"
type = string
default = null
}

variable "custom_period_end_date" {
description = "Specifies the end date (DD-MM-YYYY) for the calendar_period CUSTOM"
type = string
default = null
}

variable "alert_spent_percents" {
description = "A list of percentages of the budget to alert on when threshold is exceeded"
type = list(number)
Expand Down
2 changes: 1 addition & 1 deletion modules/budget/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.5"
version = "~> 4.28"
}
}

Expand Down
18 changes: 18 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ variable "budget_labels" {
}
}

variable "budget_calendar_period" {
description = "Specifies the calendar period for the budget. Possible values are MONTH, QUARTER, YEAR, CALENDAR_PERIOD_UNSPECIFIED, CUSTOM. custom_period_start_date and custom_period_end_date must be set if CUSTOM"
type = string
default = null
}

variable "budget_custom_period_start_date" {
description = "Specifies the start date (DD-MM-YYYY) for the calendar_period CUSTOM"
type = string
default = null
}

variable "budget_custom_period_end_date" {
description = "Specifies the end date (DD-MM-YYYY) for the calendar_period CUSTOM"
type = string
default = null
}

variable "vpc_service_control_attach_enabled" {
description = "Whether the project will be attached to a VPC Service Control Perimeter"
type = bool
Expand Down
4 changes: 2 additions & 2 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.11"
version = "~> 4.28"
}
google-beta = {
source = "hashicorp/google-beta"
version = "~> 4.11"
version = "~> 4.28"
}
}
provider_meta "google" {
Expand Down

0 comments on commit 9273052

Please sign in to comment.