使用 Cloud Billing Budget API 建立僅限快訊的預算

瞭解如何向 Cloud Billing Budget API 傳送一些簡單的要求。

如需完整的方法清單,請參閱 REST API 參考文件。

事前準備

閱讀本指南之前,請先完成下列工作:

  1. 請參閱「Cloud Billing Budget API 總覽」。
  2. 請參閱「Cloud Billing Budget API 必要條件」。
  3. 執行設定步驟。

找出 Cloud Billing 帳戶 ID

每次呼叫 Cloud Billing Budget API 時,都需要 Cloud Billing 帳戶 ID。

  1. 前往 Google Cloud 控制台的「管理帳單帳戶」頁面。
  2. 在「Your billing accounts」(您的帳單帳戶) 分頁中,您會看到 Cloud Billing 帳戶清單,其中列有名稱和 ID。找出管理預算的帳戶帳戶 ID 值。

「管理帳單」頁面,顯示帳單帳戶 ID 的位置。

預算重要概念

  • 無支出上限的預算可套用至整個帳單帳戶,或使用精細的預算篩選器設定範圍 (篩選),監控特定部分的費用。
  • 如果將無支出上限的預算套用至整個 Cloud Billing 帳戶,則會納入由該帳單帳戶支付費用的所有Google Cloud 專案。
  • 如果您對 Cloud Billing 帳戶的存取權有限或沒有存取權,可以為擁有的專案建立單一專案預算。如果您是 Cloud Billing 帳戶層級使用者,可以建立單一專案的無支出上限預算,並使用預算中的 OwnershipScope 欄位,防止專案層級使用者修改預算。
  • 您可以使用篩選器縮小無支出上限預算的範圍,例如 Cloud Billing 帳戶適用的機構、資料夾、專案、服務和其他預算篩選器子集。
  • 您可以指定無支出上限預算的時間範圍。根據預設,預算會監控當月產生的費用。使用 Cloud Billing Budget API 提供的 usage_period 篩選器,您可以將無支出上限的預算的時間範圍設為 CalendarPeriod 或 CustomPeriod,這樣就能建立無支出上限的預算,監控預設日曆月份以外的時間範圍,例如一季、一年或您指定的自訂日期範圍。
  • 如果是無支出上限的預算,您可以將預算金額設為指定總額,或根據上個日曆週期的支出設定預算金額。
  • 您可以設定預算門檻規則,觸發電子郵件快訊通知。當費用 (實際或預估費用) 超過一定百分比的預算 (以您設定的規則為準),系統會傳送快訊電子郵件給您指定的收件者。
  • 如果是無支出上限的預算,您可以設定預算的通知規則,指定預算快訊的電子郵件收件者,並使用 Pub/Sub 啟用程式輔助通知。
  • 如果是無支出上限的預算,您可以使用 Pub/Sub 主題傳送程式輔助通知,例如將預算訊息轉寄至 Slack 等其他媒介,或自動執行成本管理工作。

配額限制:每個 Cloud Billing 帳戶一次可連結數千個預算。如要瞭解目前的限制和更多資訊,請參閱「配額與限制」。

呼叫 API

下列範例說明如何將一些簡單的要求傳送至 Cloud Billing Budget API。

列出預算

這個 API 方法會列出特定 Cloud Billing 帳戶的所有可用預算。

REST

使用任何要求資料之前,請先修改下列項目的值:

  • billing-account-id:預算適用的 Cloud Billing 帳戶 ID。
  • project-id:已啟用 Cloud Billing Budget API 的 Google Cloud 專案。

HTTP 方法和網址:

GET https://billingbudgets.googleapis.com/v1/billingAccounts/billing-account-id/budgets

請展開以下其中一個選項,以傳送要求:

您應該會收到如下的 JSON 回覆:

{
  "budgets": [
   {
      "name": "billingAccounts/000000-111111-222222/budgets/33333333-4444-5555-6666-777777777777",
      "displayName": "Forecasted spend budget with Service Filter",
      "budgetFilter": {
        "projects": [
          "projects/123456789"
        ],
        "services": [
          "services/A1E8-BE35-7EBC"
        ],
        "creditTypesTreatment": "EXCLUDE_ALL_CREDITS",
        "calendarPeriod": "MONTH"
      },
      "amount": {
        "lastPeriodAmount": {}
      },
      "thresholdRules": [
        {
          "thresholdPercent": 0.9,
          "spendBasis": "FORECASTED_SPEND"
        }
      ],
      "allUpdatesRule": {},
      "etag": "c9d6c011f6fa6b5c"
    }
  ]
}

取得預算

這個 API 方法會取得特定預算的詳細資料。

REST

如要呼叫這個方法,您需要提供要更新的預算 budget-id。建立預算時,您可以從 createBudget 輸出內容取得預算 ID;如果列出所有預算,則可從 listBudgets 輸出內容取得預算 ID。

使用任何要求資料之前,請先修改下列項目的值:

  • billing-account-id:這項預算適用的 Cloud Billing 帳戶 ID。
  • budget-id:要取得的預算 ID。
  • project-id:已啟用 Cloud Billing Budget API 的 Google Cloud 專案。

HTTP 方法和網址:

GET https://billingbudgets.googleapis.com/v1/billingAccounts/billing-account-id/budgets/budget-id

請展開以下其中一個選項,以傳送要求:

您應該會收到如下的 JSON 回覆:

{
  "name": "billingAccounts/000000-111111-222222/budgets/33333333-4444-5555-6666-777777777777",
  "displayName": "Forecasted spend budget with Service Filter",
  "budgetFilter": {
    "projects": [
      "projects/123456789"
    ],
    "services": [
      "services/A1E8-BE35-7EBC"
    ],
    "creditTypesTreatment": "EXCLUDE_ALL_CREDITS",
    "calendarPeriod": "MONTH"
  },
  "amount": {
    "lastPeriodAmount": {}
  },
  "thresholdRules": [
    {
      "thresholdPercent": 0.9,
      "spendBasis": "FORECASTED_SPEND"
    }
  ],
  "allUpdatesRule": {},
  "etag": "c9d6c011f6fa6b5c"
}

設定預算

這個 API 方法會建立僅限快訊的 Cloud Billing 預算,並套用至指定的 Cloud Billing 帳戶。

REST

這個範例說明如何為特定 Cloud Billing 帳戶 建立預算,並 依您指定的 Google Cloud 專案 範圍 (篩選),以及設定 一季的 日曆時間範圍。

使用任何要求資料之前,請先修改下列項目的值:

  • projects/budget-scope-project-id:要設為預算範圍 (budgetFilter) 的 Google Cloud 專案 ID。
  • billing-account-id:這項預算適用的 Cloud Billing 帳戶 ID。
  • api-user-project-id:已啟用 Cloud Billing Budget API 的 Google Cloud 專案。

HTTP 方法和網址:

POST https://billingbudgets.googleapis.com/v1/billingAccounts/billing-account-id/budgets

JSON 要求主體:

{
  "displayName": "Keep my spending down",
  "budgetFilter": {
    "projects": [
      "projects/budget-scope-project-id"
    ],
    "calendar_period": "QUARTER"
  },
  "amount": {
    "lastPeriodAmount": {}
  },
  "thresholdRules": {
    "thresholdPercent": 0.8,
    "spendBasis": "CURRENT_SPEND"
  }
}

請展開以下其中一個選項,以傳送要求:

您應該會收到如下的 JSON 回覆:

{
  "name": "billingAccounts/000000-111111-222222/budgets/33333333-4444-5555-6666-777777777777",
  "displayName": "Keep my spending down",
  "budgetFilter": {
    "projects": [
      "projects/123456789"
    ],
    "creditTypesTreatment": "INCLUDE_ALL_CREDITS",
    "calendar_period": "QUARTER"
  },
  "amount": {
    "lastPeriodAmount": {}
  },
  "thresholdRules": {
    "thresholdPercent": 0.8,
    "spendBasis": "CURRENT_SPEND"
  },
  "allUpdatesRule": {},
  "etag": "f7928d8010160668"
}

更新預算

使用這個 API 方法修改現有的 Cloud Billing 預算,變更預算金額、預算篩選器 (預算範圍)、預算時間範圍或門檻規則。

REST

這個範例說明如何更新現有無支出上限的預算,依您指定的 Google Cloud 專案設定範圍 (篩選器),並將預算的時間範圍從每月預算更新為每季預算。

如要呼叫這個方法,您需要提供要更新的預算 budget-id。建立預算時,您可以從 createBudget 輸出內容取得預算 ID;如果列出所有預算,則可從 listBudgets 輸出內容取得預算 ID。

使用任何要求資料之前,請先修改下列項目的值:

  • projects/budget-scope-project-id:要設為預算範圍 (budgetFilter) 的 Google Cloud 專案 ID。
  • billing-account-id:這項預算適用的 Cloud Billing 帳戶 ID。
  • budget-id:要更新的預算 ID。
  • api-user-project-id:已啟用 Cloud Billing Budget API 的 Google Cloud 專案。

HTTP 方法和網址:

PATCH https://billingbudgets.googleapis.com/v1/billingAccounts/billing-account-id/budgets/budget-id

JSON 要求主體:

{
  "budget": {
    "displayName": "My fancy budget",
    "budgetFilter": {
      "projects": [
        "projects/budget-scope-project-id"
      ],
      "calendar_period": "QUARTER"
    },
   "amount": {
      "specifiedAmount": {
        "units": 500
      }
    },
    "thresholdRules": {
      "thresholdPercent": 0.8,
      "spendBasis": "CURRENT_SPEND"
    }
  }
}

請展開以下其中一個選項,以傳送要求:

您應該會收到如下的 JSON 回覆:

{
  "name": "billingAccounts/000000-111111-222222/budgets/33333333-4444-5555-6666-777777777777",
  "displayName": "My fancy budget",
  "budgetFilter": {
    "projects": [
      "projects/123456789"
    ],
    "creditTypesTreatment": "INCLUDE_ALL_CREDITS",
    "calendar_period": "QUARTER"
  },
  "amount": {
    "specifiedAmount": {
      "units": 500
    }
  },
  "thresholdRules": {
    "thresholdPercent": 0.8,
    "spendBasis": "CURRENT_SPEND"
  },
  "allUpdatesRule": {},
  "etag": "f7928d8010160668"
}

刪除預算

使用這個 API 方法刪除現有的 Cloud Billing 預算。

REST

如要呼叫這個方法,您需要提供要更新的預算 budget-id。建立預算時,您可以從 createBudget 輸出內容取得預算 ID;如果列出所有預算,則可從 listBudgets 輸出內容取得預算 ID。

使用任何要求資料之前,請先修改下列項目的值:

  • billing-account-id:這項預算適用的 Cloud Billing 帳戶 ID。
  • budget-id:要刪除的預算 ID。
  • project-id:已啟用 Cloud Billing Budget API 的 Google Cloud 專案。

HTTP 方法和網址:

DELETE https://billingbudgets.googleapis.com/v1/billingAccounts/billing-account-id/budgets/budget-id

請展開以下其中一個選項,以傳送要求:

您應該會收到如下的 JSON 回覆:

{}