Billing budget module

This commit is contained in:
Julio Castillo
2021-08-12 19:43:09 +02:00
parent cb7c65135e
commit 3a8a040ff3
9 changed files with 472 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
# Copyright 2021 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.

View File

@@ -0,0 +1,29 @@
/**
* Copyright 2021 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.
*/
module "budget" {
source = "../../../../modules/billing-budget"
billing_account = "123456-123456-123456"
name = "my budget"
projects = var.projects
services = var.services
notify_default_recipients = var.notify_default_recipients
amount = var.amount
credit_treatment = var.credit_treatment
pubsub_topic = var.pubsub_topic
notification_channels = var.notification_channels
thresholds = var.thresholds
}

View File

@@ -0,0 +1,61 @@
/**
* Copyright 2021 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.
*/
variable "amount" {
type = number
default = 0
}
variable "credit_treatment" {
type = string
default = "INCLUDE_ALL_CREDITS"
}
variable "notification_channels" {
type = list(string)
default = null
}
variable "notify_default_recipients" {
type = bool
default = false
}
variable "projects" {
type = list(string)
default = null
}
variable "pubsub_topic" {
type = string
default = null
}
variable "services" {
type = list(string)
default = null
}
variable "thresholds" {
type = object({
current = list(number)
forecasted = list(number)
})
default = {
current = [0.5, 1.0]
forecasted = [1.0]
}
}

View File

@@ -0,0 +1,63 @@
# Copyright 2021 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.
import os
import pytest
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixture')
def test_resource_count(plan_runner):
"Test number of resources created."
_, resources = plan_runner(FIXTURES_DIR, pubsub_topic='topic')
assert len(resources) == 1
resource = resources[0]
assert resource['values']['all_updates_rule'] == [
{'disable_default_iam_recipients': False,
'monitoring_notification_channels': None,
'pubsub_topic': 'topic',
'schema_version': '1.0'}
]
_, resources = plan_runner(FIXTURES_DIR, notification_channels='["channel"]')
assert len(resources) == 1
resource = resources[0]
assert resource['values']['all_updates_rule'] == [
{'disable_default_iam_recipients': True,
'monitoring_notification_channels': ['channel'],
'pubsub_topic': None,
'schema_version': '1.0'}
]
def test_absolute_amount(plan_runner):
"Test absolute amount budget."
_, resources = plan_runner(FIXTURES_DIR, pubsub_topic='topic', amount="100")
assert len(resources) == 1
resource = resources[0]
amount = resource['values']['amount'][0]
assert amount['last_period_amount'] is None
assert amount['specified_amount'] == [{'nanos': None, 'units': '100'}]
assert resource['values']['threshold_rules'] == [
{'spend_basis': 'CURRENT_SPEND',
'threshold_percent': 0.5},
{'spend_basis': 'CURRENT_SPEND',
'threshold_percent': 1},
{'spend_basis': 'FORECASTED_SPEND',
'threshold_percent': 1}
]