Ce contenu n'est pas disponible dans la langue sélectionnée.

Chapter 3. Resources for filtered integrations


Important

After you create a filtered Google Cloud integration, you can automate creating and sending reports. The main tasks involve querying your data, formatting and exporting a CSV file, and sending the data to cost management. The following examples provide code snippets to guide you, but you should adapt the process to reflect your unique environments. If you follow the documentation exactly as written without customizations, the automation might not work for your specific setup.

The following code writes CSV files to your Google Cloud bucket from the query data:

import csv
from google.cloud import storage

storage_client = storage.Client()
bucket = storage_client.bucket(“my_bucket”)
for rows in query_job:
    csv_file = "my_report_location/2025-04-01.csv"
    blob = bucket.blob(csv_file)
    with blob.open(mode='w') as f:
        writer = csv.writer(f)
        writer.writerow([my_col_1, my_col_col2])
        writer.writerows(rows)
Copy to Clipboard Toggle word wrap

The following code adds batching to restrict the CSV file size:

def batch(iterable, n):
"""Yields successive n-sized chunks from iterable"""
	it = iter(iterable)
	while chunk := tuple(islice(it, n)):
    	yield chunk

for i, rows in enumerate(batch(query_job, 200000)):
    # write csv file code block #
Copy to Clipboard Toggle word wrap

The following code authenticates and sends reports to cost management for processing by fetching and using your service account access token:

import os
import requests

# os.getenv(var) used to fetch Google Cloud stored secrets shared with your function
CLIENT_ID = os.getenv('client_id')
CLIENT_SECRET = os.getenv('client_secret')

# Get access token
token_url = 'https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token'
token_headers = {'Content-Type': 'application/x-www-form-urlencoded'}
token_data = {"client_id": CLIENT_ID, "client_secret": CLIENT_SECRET, "grant_type": "client_credentials"}
access_token = requests.post(token_url, headers=token_headers, data=token_data).json().get("access_token")

# Send reports to Red Hat
json_data = {"source": 0, "reports_list": ["my-file1.csv", "my-file-2.csv"], "bill_year": "2025", "bill_month": "07"}
headers = {'Authorization': f'Bearer {access_token}', 'Accept': 'application/json'}
requests.post("https://console.redhat.com/api/cost-management/v1/ingress/reports/", json=json_data, headers=headers)
Copy to Clipboard Toggle word wrap

3.1.1. Additional resources

For reference only, this python script provides additional logic such as restricting CSV file sizes and using variables for client secrets.

For additional help with automation, see Google’s documentation:

3.2. Troubleshooting a Google Cloud integration

3.2.1. Incorrect data is displayed

If your data is displaying incorrectly in cost management, first download your CSV files and validate the data that you sent. If that does not solve the issue, review the following common scenarios:

  • If you upload data for the same day multiple times, the most recent upload is what is displayed. Send the data again in the correct order.
  • Google Cloud uses a method called crossover data. For each day, Google Cloud continues to add billing data 72 hours after it had accrued. To ensure that you are capturing the correct billing data, consider a rolling window to capture these additions. For example, you can maintain a five day rolling window, or never query current day data and instead always query n-5.
  • At the start of a new month, Google Cloud finishes billing for the previous month. To ensure that there are no gaps in your billing data, send the data for the previous month up to three days into the following month.
  • If your custom table does not have partitions by day (_PARTITIONTIME), use usage_start_time as the partition date.
Retour au début
Red Hat logoGithubredditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance. Découvrez nos récentes mises à jour.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez le Blog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

Theme

© 2025 Red Hat