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"
Hi @psagar,
Welcome to the Google Cloud Community!
The reason you're having the "AttributeError: 'Resource' object has no attribute 'dashboards'" error is because the Cloud Monitoring API v3 doesn't have an instance method for "dashboards()" as seen in the official documentation. Instead, you should use Cloud Monitoring API v1.
Here is the modified code:
monitoring = build('monitoring', 'v1', credentials=credentials)
Once edited, you can verify by running the code:
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Replace with your credentials file path and project ID
credentials_file = '/home/user/projects/xxxxx.json' # Replace with the actual path to your service account credentials JSON file
project_id = 'xxxxx' # 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', 'v1', credentials=credentials)
# Define your dashboard configuration here
dashboard_config = {
'displayName': 'My Dashboard',
'widgets': [
# Define your widgets here
# Example: {'xyChart': {'dataSets': [{'timeSeriesQuery': {...}}]}}
]
}
# Verify the list of instance methods for v1
created_dashboard = monitoring.projects()
print(dir(created_dashboard))
The output of the code is (you may click on the image to zoom):
Now, there are some inconsistencies in your current code with respect to the Cloud Monitoring v1 API. Therefore, I've made some modifications. Using the revised code below, I was able to successfully create a dashboard:
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Replace with your credentials file path and project ID
credentials_file = '/home/user/projects/xxxxx.json' # Replace with the actual path to your service account credentials JSON file
project_id = 'xxxxx' # 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', 'v1', credentials=credentials)
# Define your dashboard configuration here
dashboard_config = {
"displayName": "my-dashboard",
"columnLayout": {}
}
# Create the dashboard
created_dashboard = monitoring.projects().dashboards().create(
parent=f"projects/{project_id}",
body=dashboard_config
).execute()
print(f"Dashboard created: {created_dashboard['name']}")
You can also use the official documentation to test the method by clicking on the "Try it!" on the right side of the page.
Let me know if this helps, thanks.
Thank you for your suggestion. I tried running your modified code, however, I am getting another error:
Traceback (most recent call last):
File "/home/hisagar123/my_script.py", line 24, in <module>
created_dashboard = monitoring.projects().dashboards().create(
File "/usr/local/lib/python3.9/dist-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/googleapiclient/http.py", line 938, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://monitoring.googleapis.com/v1/projects/506869214187/dashboards?alt=json returned "The caller does not have permission". Details: "The caller does not have permission">
Hi @psagar,
According to the official document that I have linked earlier, your account may not have the monitoring.dashboards.create
role.
Kindly verify that your account has the right permission. You can refer to this document to check the roles/permissions related to your account.
User | Count |
---|---|
3 | |
3 | |
1 | |
1 |