Create
Creates a budget for the specified billing account.
- TypeScript
- Python
import {
cloudApi,
decodeMessage,
serviceClients,
Session,
waitForOperation,
} from "@yandex-cloud/nodejs-sdk";
const Budget = cloudApi.billing.budget.Budget;
const CreateBudgetRequest = cloudApi.billing.budget_service.CreateBudgetRequest;
const ResetPeriodType = cloudApi.billing.budget.ResetPeriodType;
const ThresholdType = cloudApi.billing.budget.ThresholdType;
(async () => {
const authToken = process.env["YC_OAUTH_TOKEN"];
const session = new Session({ oauthToken: authToken });
const client = session.client(serviceClients.BudgetServiceClient);
const operation = await client.create(
CreateBudgetRequest.fromPartial({
billingAccountId: "billingAccountId",
name: "name",
// costBudgetSpec: {
// amount: "amount",
// notificationUserAccountIds: ["notificationUserAccountIds"],
// thresholdRules: [{
// type: ThresholdType.PERCENT,
// amount: "amount",
// notificationUserAccountIds: ["notificationUserAccountIds"]
// }],
// filter: {
// serviceIds: ["serviceIds"],
// cloudFoldersFilters: [{
// cloudId: "cloudId",
// folderIds: ["folderIds"]
// }]
// },
// resetPeriod: ResetPeriodType.MONTHLY,
// startDate: "startDate",
// endDate: "endDate"
// },
// expenseBudgetSpec: {
// amount: "amount",
// notificationUserAccountIds: ["notificationUserAccountIds"],
// thresholdRules: [{
// type: ThresholdType.PERCENT,
// amount: "amount",
// notificationUserAccountIds: ["notificationUserAccountIds"]
// }],
// filter: {
// serviceIds: ["serviceIds"],
// cloudFoldersFilters: [{
// cloudId: "cloudId",
// folderIds: ["folderIds"]
// }]
// },
// resetPeriod: ResetPeriodType.MONTHLY,
// startDate: "startDate",
// endDate: "endDate"
// },
// balanceBudgetSpec: {
// amount: "amount",
// notificationUserAccountIds: ["notificationUserAccountIds"],
// thresholdRules: [{
// type: ThresholdType.PERCENT,
// amount: "amount",
// notificationUserAccountIds: ["notificationUserAccountIds"]
// }],
// startDate: "startDate",
// endDate: "endDate"
// }
})
);
const finishedOp = await waitForOperation(operation, session);
if (finishedOp.response) {
const result = decodeMessage<typeof Budget>(finishedOp.response);
console.log(result);
}
})();
import os
import grpc
import yandexcloud
from yandex.cloud.billing.v1.budget_pb2 import BalanceBudgetSpec
from yandex.cloud.billing.v1.budget_pb2 import Budget
from yandex.cloud.billing.v1.budget_service_pb2_grpc import BudgetServiceStub
from yandex.cloud.billing.v1.budget_pb2 import CloudFoldersConsumptionFilter
from yandex.cloud.billing.v1.budget_pb2 import ConsumptionFilter
from yandex.cloud.billing.v1.budget_pb2 import CostBudgetSpec
from yandex.cloud.billing.v1.budget_service_pb2 import CreateBudgetMetadata
from yandex.cloud.billing.v1.budget_service_pb2 import CreateBudgetRequest
from yandex.cloud.billing.v1.budget_pb2 import ExpenseBudgetSpec
from yandex.cloud.billing.v1.budget_pb2 import ResetPeriodType
from yandex.cloud.billing.v1.budget_pb2 import ThresholdRule
from yandex.cloud.billing.v1.budget_pb2 import ThresholdType
token = os.getenv("YC_OAUTH_TOKEN")
sdk = yandexcloud.SDK(token=token)
service = sdk.client(BudgetServiceStub)
operation = service.Create(
CreateBudgetRequest(
billing_account_id="billingAccountId",
name="name",
# cost_budget_spec = CostBudgetSpec(
# amount = "amount",
# notification_user_account_ids = ["notificationUserAccountIds"],
# threshold_rules = [ThresholdRule(
# type = ThresholdType.PERCENT,
# amount = "amount",
# notification_user_account_ids = ["notificationUserAccountIds"]
# )],
# filter = ConsumptionFilter(
# service_ids = ["serviceIds"],
# cloud_folders_filters = [CloudFoldersConsumptionFilter(
# cloud_id = "cloudId",
# folder_ids = ["folderIds"]
# )]
# ),
# reset_period = ResetPeriodType.MONTHLY,
# start_date = "startDate",
# end_date = "endDate"
# ),
# expense_budget_spec = ExpenseBudgetSpec(
# amount = "amount",
# notification_user_account_ids = ["notificationUserAccountIds"],
# threshold_rules = [ThresholdRule(
# type = ThresholdType.PERCENT,
# amount = "amount",
# notification_user_account_ids = ["notificationUserAccountIds"]
# )],
# filter = ConsumptionFilter(
# service_ids = ["serviceIds"],
# cloud_folders_filters = [CloudFoldersConsumptionFilter(
# cloud_id = "cloudId",
# folder_ids = ["folderIds"]
# )]
# ),
# reset_period = ResetPeriodType.MONTHLY,
# start_date = "startDate",
# end_date = "endDate"
# ),
# balance_budget_spec = BalanceBudgetSpec(
# amount = "amount",
# notification_user_account_ids = ["notificationUserAccountIds"],
# threshold_rules = [ThresholdRule(
# type = ThresholdType.PERCENT,
# amount = "amount",
# notification_user_account_ids = ["notificationUserAccountIds"]
# )],
# start_date = "startDate",
# end_date = "endDate"
# )
)
)
operation_result = sdk.wait_operation_and_get_result(
operation,
response_type=Budget,
meta_type=CreateBudgetMetadata,
)
print(operation_result)
CreateBudgetRequest
billingAccountId
: string
ID of the billing account to list budgets corresponding to. To get the billing account ID, use yandex.cloud.billing.v1.BillingAccountService.List request.
name
: string
Name of the budget.
One of budgetSpec
Specification of the budget.
costBudgetSpec
: CostBudgetSpecCost budget specification.
expenseBudgetSpec
: ExpenseBudgetSpecExpense budget specification.
balanceBudgetSpec
: BalanceBudgetSpecBalance budget specification.
CostBudgetSpec
Cost budget specification describes budget that can be used to control cost of cloud resources usage.
amount
: string
Max cost threshold of the budget. Amount currency is the same as corresponding yandex.cloud.billing.v1.BillingAccount.currency.
notificationUserAccountIds
: string
IDs of the yandex.cloud.iam.v1.UserAccount. Specified users will be be notified if the budget exceeds.
thresholdRules
: ThresholdRule
List of the ThresholdRule. Rules define intermediate cost thresholds of the budget.
filter
: ConsumptionFilter
Filter that can be used for specific resources selection. Only consumption cost of selected resources are used for the budget calculation.
One of startType
Start type of the budget.
resetPeriod
: ResetPeriodTypePeriodic start type that resets budget after specified period is finished.
First time budget is calculated in the current period, i.e. current month, quarter or year.
startDate
: stringCustom start date of the budget.
Must be the first day of a month and must be formatted like YYYY-MM-DD.
endDate
: string
End date of the budget. Must be the last day of a month and must be formatted like YYYY-MM-DD.
ExpenseBudgetSpec
Expense budget specification describes budget that can be used to control expense of cloud resources usage.
amount
: string
Max expense threshold of the budget. Amount currency is the same as corresponding yandex.cloud.billing.v1.BillingAccount.currency.
notificationUserAccountIds
: string
IDs of the yandex.cloud.iam.v1.UserAccount. Specified users will be be notified if the budget exceeds.
thresholdRules
: ThresholdRule
List of the ThresholdRule. Rules define intermediate expense thresholds of the budget.
filter
: ConsumptionFilter
Filter that can be used for specific resources selection. Only consumption expense of selected resources are used for the budget calculation.
resetPeriod
: ResetPeriodTypePeriodic start type that resets budget after specified period is finished.
First time budget is calculated in the current period, i.e. current month, quarter or year.
startDate
: stringCustom start date of the budget.
Must be the first day of a month and must be formatted like YYYY-MM-DD.
endDate
: string
End date of the budget. Must be the last day of a month and must be formatted like YYYY-MM-DD.
BalanceBudgetSpec
Balance budget specification describes budget that can be used to control yandex.cloud.billing.v1.BillingAccount.balance.
amount
: string
Max balance threshold of the budget. Amount currency is the same as corresponding yandex.cloud.billing.v1.BillingAccount.currency.
notificationUserAccountIds
: string
IDs of the yandex.cloud.iam.v1.UserAccount. Specified users will be be notified if the budget exceeds.
thresholdRules
: ThresholdRule
List of the ThresholdRule. Rules define intermediate balance thresholds of the budget.
startDate
: string
Start_date of the budget. Must be the first day of a month and must be formatted like YYYY-MM-DD.
endDate
: string
End date of the budget. Must be the last day of a month and must be formatted like YYYY-MM-DD.
ThresholdRule
Rules that define intermediate cost thresholds of the budget.
type
: ThresholdType
Type of the rule.
amount
: string
Amount of the rule.
- Must be less than 100 if type is PERCENT.
- Must be less than budget's amount if type is AMOUNT.
notificationUserAccountIds
: string
IDs of the yandex.cloud.iam.v1.UserAccount. Specified users will be be notified if the threshold exceeds.
ConsumptionFilter
Filter that can be used for specific resources selection.
serviceIds
: string
IDs of the yandex.cloud.billing.v1.Service. Only consumption of resources corresponding to the given services is used for the budget calculation. Empty sequence means no services filters.
cloudFoldersFilters
: CloudFoldersConsumptionFilter
Cloud and folders consumption filter. Only consumption within specified clouds and folders is used for the budget calculation. Empty sequence means no cloud and folders filters.
CloudFoldersConsumptionFilter
Filter that can be used for specific cloud and its folders selection.
cloudId
: string
ID of the yandex.cloud.resourcemanager.v1.Cloud. Only consumption within specified cloud is used for the budget calculation.
folderIds
: string
IDs of the yandex.cloud.resourcemanager.v1.Folder. Only consumption within specified folders of the given cloud is used for the budget calculation. Empty sequence means no folders filters and the whole cloud consumption will be used.
Operation
An Operation resource. For more information, see Operation.
id
: string
ID of the operation.
description
: string
Description of the operation. 0-256 characters long.
createdAt
: google.protobuf.Timestamp
Creation timestamp.
createdBy
: string
ID of the user or service account who initiated the operation.
modifiedAt
: google.protobuf.Timestamp
The time when the Operation resource was last modified.
done
: bool
If the value is false
, it means the operation is still in progress.
If true
, the operation is completed, and either error
or response
is available.
metadata
: google.protobuf.Any
Service-specific metadata associated with the operation. It typically contains the ID of the target resource that the operation is performed on. Any method that returns a long-running operation should document the metadata type, if any.
One of result
The operation result.
If done == false
and there was no failure detected, neither error
nor response
is set.
If done == false
and there was a failure detected, error
is set.
If done == true
, exactly one of error
or response
is set.
error
: google.rpc.StatusThe error result of the operation in case of failure or cancellation.
response
: google.protobuf.AnyThe normal response of the operation in case of success.
If the original method returns no data on success, such as Delete, the response is google.protobuf.Empty. If the original method is the standard Create/Update, the response should be the target resource of the operation. Any method that returns a long-running operation should document the response type, if any.