-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* billing budget factory * review comment changes
- Loading branch information
Showing
4 changed files
with
159 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ provider "google" { | |
- [Billing budgets](#billing-budgets) | ||
- [PubSub update rules](#pubsub-update-rules) | ||
- [Monitoring channels](#monitoring-channels) | ||
- [Budget factory](#budget-factory) | ||
- [Variables](#variables) | ||
- [Outputs](#outputs) | ||
<!-- END TOC --> | ||
|
@@ -212,21 +213,63 @@ module "billing-account" { | |
# tftest modules=1 resources=2 inventory=budget-monitoring-channel.yaml | ||
``` | ||
|
||
#### Budget factory | ||
|
||
This module also exposes a factory for billing budgets, that works in a similar way to factories in other modules: a specific folder is searched for YAML files, which contain one budget description per file. The file name is used to generate the key of the resulting map of budgets, which is merged with the one coming from the `budgets` variable. The YAML files support the same type of the `budgets` variable. | ||
|
||
```hcl | ||
module "billing-account" { | ||
source = "./fabric/modules/billing-account" | ||
id = "012345-ABCDEF-012345" | ||
budget_notification_channels = { | ||
billing-default = { | ||
project_id = "tf-playground-simple" | ||
type = "email" | ||
labels = { | ||
email_address = "[email protected]" | ||
} | ||
} | ||
} | ||
} | ||
# tftest modules=1 resources=2 files=test-1 inventory=budget-monitoring-channel.yaml | ||
``` | ||
|
||
```yaml | ||
# tftest-file id=test-1 path=data/billing-budgets/folder-net-month-current-100.yaml | ||
display_name: 100 dollars in current spend | ||
amount: | ||
units: 100 | ||
filter: | ||
period: | ||
calendar: MONTH | ||
resource_ancestors: | ||
- folders/1234567890 | ||
threshold_rules: | ||
- percent: 0.5 | ||
- percent: 0.75 | ||
update_rules: | ||
default: | ||
disable_default_iam_recipients: true | ||
monitoring_notification_channels: | ||
- billing-default | ||
``` | ||
<!-- markdownlint-enable --> | ||
<!-- BEGIN TFDOC --> | ||
## Variables | ||
| name | description | type | required | default | | ||
|---|---|:---:|:---:|:---:| | ||
| [id](variables.tf#L165) | Billing account id. | <code>string</code> | ✓ | | | ||
| [id](variables.tf#L175) | Billing account id. | <code>string</code> | ✓ | | | ||
| [budget_notification_channels](variables.tf#L17) | Notification channels used by budget alerts. | <code title="map(object({ project_id = string type = string description = optional(string) display_name = optional(string) enabled = optional(bool, true) force_delete = optional(bool) labels = optional(map(string)) sensitive_labels = optional(list(object({ auth_token = optional(string) password = optional(string) service_key = optional(string) }))) user_labels = optional(map(string)) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [budgets](variables.tf#L47) | Billing budgets. Notification channels are either keys in corresponding variable, or external ids. | <code title="map(object({ amount = object({ currency_code = optional(string) nanos = optional(number) units = optional(number) use_last_period = optional(bool) }) display_name = optional(string) filter = optional(object({ credit_types_treatment = optional(object({ exclude_all = optional(bool) include_specified = optional(list(string)) })) label = optional(object({ key = string value = string })) period = optional(object({ calendar = optional(string) custom = optional(object({ start_date = object({ day = number month = number year = number }) end_date = optional(object({ day = number month = number year = number })) })) })) projects = optional(list(string)) resource_ancestors = optional(list(string)) services = optional(list(string)) subaccounts = optional(list(string)) })) threshold_rules = optional(list(object({ percent = number forecasted_spend = optional(bool) })), []) update_rules = optional(map(object({ disable_default_iam_recipients = optional(bool) monitoring_notification_channels = optional(list(string)) pubsub_topic = optional(string) })), {}) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [group_iam](variables.tf#L121) | Authoritative IAM binding for organization groups, in {GROUP_EMAIL => [ROLES]} format. Group emails need to be static. Can be used in combination with the `iam` variable. | <code>map(list(string))</code> | | <code>{}</code> | | ||
| [iam](variables.tf#L128) | IAM bindings in {ROLE => [MEMBERS]} format. | <code>map(list(string))</code> | | <code>{}</code> | | ||
| [iam_bindings](variables.tf#L135) | Authoritative IAM bindings in {KEY => {role = ROLE, members = [], condition = {}}}. Keys are arbitrary. | <code title="map(object({ members = list(string) role = string condition = optional(object({ expression = string title = string description = optional(string) })) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [iam_bindings_additive](variables.tf#L150) | Individual additive IAM bindings. Keys are arbitrary. | <code title="map(object({ member = string role = string condition = optional(object({ expression = string title = string description = optional(string) })) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [logging_sinks](variables.tf#L170) | Logging sinks to create for the organization. | <code title="map(object({ destination = string type = string bq_partitioned_table = optional(bool) description = optional(string) disabled = optional(bool, false) exclusions = optional(map(object({ filter = string description = optional(string) disabled = optional(bool) })), {}) filter = optional(string) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [projects](variables.tf#L203) | Projects associated with this billing account. | <code>list(string)</code> | | <code>[]</code> | | ||
| [factory_config](variables.tf#L121) | Path to folder containing budget alerts data files. | <code title="object({ budgets_data_path = optional(string, "data/billing-budgets") })">object({…})</code> | | <code>{}</code> | | ||
| [group_iam](variables.tf#L131) | Authoritative IAM binding for organization groups, in {GROUP_EMAIL => [ROLES]} format. Group emails need to be static. Can be used in combination with the `iam` variable. | <code>map(list(string))</code> | | <code>{}</code> | | ||
| [iam](variables.tf#L138) | IAM bindings in {ROLE => [MEMBERS]} format. | <code>map(list(string))</code> | | <code>{}</code> | | ||
| [iam_bindings](variables.tf#L145) | Authoritative IAM bindings in {KEY => {role = ROLE, members = [], condition = {}}}. Keys are arbitrary. | <code title="map(object({ members = list(string) role = string condition = optional(object({ expression = string title = string description = optional(string) })) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [iam_bindings_additive](variables.tf#L160) | Individual additive IAM bindings. Keys are arbitrary. | <code title="map(object({ member = string role = string condition = optional(object({ expression = string title = string description = optional(string) })) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [logging_sinks](variables.tf#L180) | Logging sinks to create for the organization. | <code title="map(object({ destination = string type = string bq_partitioned_table = optional(bool) description = optional(string) disabled = optional(bool, false) exclusions = optional(map(object({ filter = string description = optional(string) disabled = optional(bool) })), {}) filter = optional(string) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [projects](variables.tf#L213) | Projects associated with this billing account. | <code>list(string)</code> | | <code>[]</code> | | ||
|
||
## Outputs | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
locals { | ||
_factory_data = { | ||
for f in fileset("${local._factory_path}", "**/*.yaml") : | ||
trimsuffix(f, ".yaml") => yamldecode(file("${local._factory_path}/${f}")) | ||
} | ||
_factory_path = var.factory_config.budgets_data_path | ||
factory_budgets = { | ||
for k, v in local._factory_data : k => merge(v, { | ||
amount = merge( | ||
{ | ||
currency_code = null | ||
nanos = null | ||
units = null | ||
use_last_period = null | ||
}, | ||
try(v.amount, {}) | ||
) | ||
display_name = try(v.display_name, null) | ||
filter = try(v.filter, null) == null ? null : { | ||
credit_types_treatment = ( | ||
try(v.filter.credit_types_treatment, null) == null | ||
? null | ||
: merge( | ||
{ exclude_all = null, include_specified = null }, | ||
v.filter.credit_types_treatment | ||
) | ||
) | ||
label = try(v.filter.label, null) | ||
projects = try(v.filter.projects, null) | ||
resource_ancestors = try(v.filter.resource_ancestors, null) | ||
services = try(v.filter.services, null) | ||
subaccounts = try(v.filter.subaccounts, null) | ||
} | ||
threshold_rules = [ | ||
for vv in try(v.threshold_rules, []) : merge({ | ||
percent = null | ||
forecasted_spend = null | ||
}, vv) | ||
] | ||
update_rules = { | ||
for kk, vv in try(v.update_rules, {}) : kk => merge({ | ||
disable_default_iam_recipients = null | ||
monitoring_notification_channels = null | ||
pubsub_topic = null | ||
}, vv) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
# check data coming from the factory as it bypasses variable validation rules | ||
|
||
check "factory_budgets" { | ||
assert { | ||
condition = alltrue([ | ||
for k, v in local.factory_budgets : v.amount != null && ( | ||
try(v.amount.use_last_period, null) == true || | ||
try(v.amount.units, null) != null | ||
) | ||
]) | ||
error_message = "Factory budgets need either amount units or last period set." | ||
} | ||
assert { | ||
condition = alltrue([ | ||
for k, v in local.factory_budgets : | ||
v.threshold_rules == null || try(v.threshold_rules.percent, null) != null | ||
]) | ||
error_message = "Threshold rules need percent set." | ||
} | ||
assert { | ||
condition = alltrue(flatten([ | ||
for k, v in local.factory_budgets : [ | ||
for kk, vv in v.update_rules : [ | ||
vv.monitoring_notification_channels != null | ||
|| | ||
vv.pubsub_topic != null | ||
] | ||
] | ||
])) | ||
error_message = "Notification rules need either a pubsub topic or monitoring channels defined." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters