Skip to content

Commit 97f4fee

Browse files
Update google_billing_budget resource to add notifications (#3817) (#2366)
Signed-off-by: Modular Magician <[email protected]>
1 parent 14ad733 commit 97f4fee

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed

.changelog/3817.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
billing_budget: Added support for `monitoring_notification_channels` to allow sending budget notifications to Cloud Monitoring email notification channels.
3+
```

google-beta/resource_billing_budget.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,26 @@ using threshold rules.`,
128128
MaxItems: 1,
129129
Elem: &schema.Resource{
130130
Schema: map[string]*schema.Schema{
131+
"monitoring_notification_channels": {
132+
Type: schema.TypeList,
133+
Optional: true,
134+
Description: `The full resource name of a monitoring notification
135+
channel in the form
136+
projects/{project_id}/notificationChannels/{channel_id}.
137+
A maximum of 5 channels are allowed.`,
138+
Elem: &schema.Schema{
139+
Type: schema.TypeString,
140+
},
141+
AtLeastOneOf: []string{"all_updates_rule.0.pubsub_topic", "all_updates_rule.0.monitoring_notification_channels"},
142+
},
131143
"pubsub_topic": {
132144
Type: schema.TypeString,
133-
Required: true,
145+
Optional: true,
134146
Description: `The name of the Cloud Pub/Sub topic where budget related
135147
messages will be published, in the form
136148
projects/{project_id}/topics/{topic_id}. Updates are sent
137149
at regular intervals to the topic.`,
150+
AtLeastOneOf: []string{"all_updates_rule.0.pubsub_topic", "all_updates_rule.0.monitoring_notification_channels"},
138151
},
139152
"schema_version": {
140153
Type: schema.TypeString,
@@ -509,6 +522,8 @@ func flattenBillingBudgetBudgetAllUpdatesRule(v interface{}, d *schema.ResourceD
509522
flattenBillingBudgetBudgetAllUpdatesRulePubsubTopic(original["pubsubTopic"], d, config)
510523
transformed["schema_version"] =
511524
flattenBillingBudgetBudgetAllUpdatesRuleSchemaVersion(original["schemaVersion"], d, config)
525+
transformed["monitoring_notification_channels"] =
526+
flattenBillingBudgetBudgetAllUpdatesRuleMonitoringNotificationChannels(original["monitoringNotificationChannels"], d, config)
512527
return []interface{}{transformed}
513528
}
514529
func flattenBillingBudgetBudgetAllUpdatesRulePubsubTopic(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -519,6 +534,10 @@ func flattenBillingBudgetBudgetAllUpdatesRuleSchemaVersion(v interface{}, d *sch
519534
return v
520535
}
521536

537+
func flattenBillingBudgetBudgetAllUpdatesRuleMonitoringNotificationChannels(v interface{}, d *schema.ResourceData, config *Config) interface{} {
538+
return v
539+
}
540+
522541
func expandBillingBudgetBudget(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
523542
transformed := make(map[string]interface{})
524543
transformedDisplayName, err := expandBillingBudgetBudgetDisplayName(d.Get("display_name"), d, config)
@@ -732,6 +751,13 @@ func expandBillingBudgetBudgetAllUpdatesRule(v interface{}, d TerraformResourceD
732751
transformed["schemaVersion"] = transformedSchemaVersion
733752
}
734753

754+
transformedMonitoringNotificationChannels, err := expandBillingBudgetBudgetAllUpdatesRuleMonitoringNotificationChannels(original["monitoring_notification_channels"], d, config)
755+
if err != nil {
756+
return nil, err
757+
} else if val := reflect.ValueOf(transformedMonitoringNotificationChannels); val.IsValid() && !isEmptyValue(val) {
758+
transformed["monitoringNotificationChannels"] = transformedMonitoringNotificationChannels
759+
}
760+
735761
return transformed, nil
736762
}
737763

@@ -742,3 +768,7 @@ func expandBillingBudgetBudgetAllUpdatesRulePubsubTopic(v interface{}, d Terrafo
742768
func expandBillingBudgetBudgetAllUpdatesRuleSchemaVersion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
743769
return v, nil
744770
}
771+
772+
func expandBillingBudgetBudgetAllUpdatesRuleMonitoringNotificationChannels(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
773+
return v, nil
774+
}

google-beta/resource_billing_budget_generated_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,77 @@ resource "google_billing_budget" "budget" {
124124
`, context)
125125
}
126126

127+
func TestAccBillingBudget_billingBudgetNotifyExample(t *testing.T) {
128+
t.Parallel()
129+
130+
context := map[string]interface{}{
131+
"billing_acct": getTestBillingAccountFromEnv(t),
132+
"project": getTestProjectFromEnv(),
133+
"random_suffix": randString(t, 10),
134+
}
135+
136+
vcrTest(t, resource.TestCase{
137+
PreCheck: func() { testAccPreCheck(t) },
138+
Providers: testAccProvidersOiCS,
139+
CheckDestroy: testAccCheckBillingBudgetDestroyProducer(t),
140+
Steps: []resource.TestStep{
141+
{
142+
Config: testAccBillingBudget_billingBudgetNotifyExample(context),
143+
},
144+
},
145+
})
146+
}
147+
148+
func testAccBillingBudget_billingBudgetNotifyExample(context map[string]interface{}) string {
149+
return Nprintf(`
150+
data "google_billing_account" "account" {
151+
provider = google-beta
152+
billing_account = "%{billing_acct}"
153+
}
154+
155+
resource "google_billing_budget" "budget" {
156+
provider = google-beta
157+
billing_account = data.google_billing_account.account.id
158+
display_name = "Example Billing Budget%{random_suffix}"
159+
160+
budget_filter {
161+
projects = ["projects/%{project}"]
162+
}
163+
164+
amount {
165+
specified_amount {
166+
currency_code = "USD"
167+
units = "100000"
168+
}
169+
}
170+
171+
threshold_rules {
172+
threshold_percent = 1.0
173+
}
174+
threshold_rules {
175+
threshold_percent = 1.0
176+
spend_basis = "FORECASTED_SPEND"
177+
}
178+
179+
all_updates_rule {
180+
monitoring_notification_channels = [
181+
google_monitoring_notification_channel.notification_channel.id,
182+
]
183+
}
184+
}
185+
186+
resource "google_monitoring_notification_channel" "notification_channel" {
187+
provider = google-beta
188+
display_name = "Example Notification Channel%{random_suffix}"
189+
type = "email"
190+
191+
labels = {
192+
email_address = "[email protected]"
193+
}
194+
}
195+
`, context)
196+
}
197+
127198
func testAccCheckBillingBudgetDestroyProducer(t *testing.T) func(s *terraform.State) error {
128199
return func(s *terraform.State) error {
129200
for name, rs := range s.RootModule().Resources {

website/docs/r/billing_budget.html.markdown

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,61 @@ resource "google_billing_budget" "budget" {
103103
}
104104
}
105105
```
106+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
107+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=billing_budget_notify&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
108+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
109+
</a>
110+
</div>
111+
## Example Usage - Billing Budget Notify
112+
113+
114+
```hcl
115+
data "google_billing_account" "account" {
116+
provider = google-beta
117+
billing_account = "000000-0000000-0000000-000000"
118+
}
119+
120+
resource "google_billing_budget" "budget" {
121+
provider = google-beta
122+
billing_account = data.google_billing_account.account.id
123+
display_name = "Example Billing Budget"
124+
125+
budget_filter {
126+
projects = ["projects/my-project-name"]
127+
}
128+
129+
amount {
130+
specified_amount {
131+
currency_code = "USD"
132+
units = "100000"
133+
}
134+
}
135+
136+
threshold_rules {
137+
threshold_percent = 1.0
138+
}
139+
threshold_rules {
140+
threshold_percent = 1.0
141+
spend_basis = "FORECASTED_SPEND"
142+
}
143+
144+
all_updates_rule {
145+
monitoring_notification_channels = [
146+
google_monitoring_notification_channel.notification_channel.id,
147+
]
148+
}
149+
}
150+
151+
resource "google_monitoring_notification_channel" "notification_channel" {
152+
provider = google-beta
153+
display_name = "Example Notification Channel"
154+
type = "email"
155+
156+
labels = {
157+
email_address = "[email protected]"
158+
}
159+
}
160+
```
106161

107162
## Argument Reference
108163

@@ -202,7 +257,7 @@ The `threshold_rules` block supports:
202257
The `all_updates_rule` block supports:
203258

204259
* `pubsub_topic` -
205-
(Required)
260+
(Optional)
206261
The name of the Cloud Pub/Sub topic where budget related
207262
messages will be published, in the form
208263
projects/{project_id}/topics/{topic_id}. Updates are sent
@@ -214,6 +269,13 @@ The `all_updates_rule` block supports:
214269
accepted. It represents the JSON schema as defined in
215270
https://cloud.google.com/billing/docs/how-to/budgets#notification_format.
216271

272+
* `monitoring_notification_channels` -
273+
(Optional)
274+
The full resource name of a monitoring notification
275+
channel in the form
276+
projects/{project_id}/notificationChannels/{channel_id}.
277+
A maximum of 5 channels are allowed.
278+
217279
- - -
218280

219281

0 commit comments

Comments
 (0)