Hi all, I am new to Google Cloud SDK and I am trying to create a simple dashboard using my python script. However, I am having troubld and getting this error:
AttributeError: 'Resource' object has no attribute 'dashboards
Below is my python script.
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Replace with your credentials file path and project ID
credentials_file = '/home/hisagar123/precipitation-forecast-daf6cf5e****.json' # Replace with the actual path to your service account credentials JSON file
project_id = '5068692***' # Replace with your actual project ID
# Authenticate using service account credentials
credentials = service_account.Credentials.from_service_account_file(
credentials_file, scopes=['https://www.googleapis.com/auth/cloud-platform']
)
# Create a client for the Google Cloud Monitoring API
monitoring = build('monitoring', 'v3', credentials=credentials)
# Define your dashboard configuration here
dashboard_config = {
'displayName': 'My Dashboard',
'widgets': [
# Define your widgets here
# Example: {'xyChart': {'dataSets': [{'timeSeriesQuery': {...}}]}}
]
}
# Create the dashboard
created_dashboard = monitoring.projects().dashboards().create(
name=f"projects/{project_id}",
body={'dashboard': dashboard_config}
).execute()
print(f"Dashboard created: {created_dashboard['name']}")
I am also copying the content of .json file:
{
"type": "service_account",
"project_id": "precipitation-forecast",
"private_key_id": "daf6cf5e730edd17ab61edd87f80788fd7e****",
"private_key": "-----BEGIN PRIVATE KEY-----****-END PRIVATE KEY-----\n",
"client_email": "sagar-precip-forecast@precipitation-forecast.iam.gserviceaccount.com",
"client_id": "111246562582570479***",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/sagar-precip-forecast%40precipitation-forecast.iam...",
"universe_domain": "googleapis.com"