I'd like to leverage the beta BillingBudgets API via a Cloud Function written in Python. With the Billing API, it is pretty straightforward to use the API to update the billing on a project...
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
from apiclient import discovery
service = discovery.build('cloudbilling', 'v1', credentials=credentials, cache_discovery=False)
billing_info = service.projects().updateBillingInfo(name='projects/{}'.format(projectID), body={'billingAccountName': 'billingAccounts/000000-AAAAAA-BBBBBB'}).execute()
However, trying to create a budget with the BillingBudgets API with something like...
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
from apiclient import discovery
service = discovery.build('billingbudgets', 'v1beta1', credentials=credentials, cache_discovery=False)
budget_info = service.budgets().create('billingAccounts/000000-AAAAAA-BBBBBB/budgets', body={longJSONStringHere}).execute()
...fails with "'Resource' object has no attribute 'billing'". Looking through the API and playing with the object syntax and structure hasn't yielded any results. It appears from the documentation that there aren't client libraries yet for this API, so I'm currently going on the assumption that this will work at a future point and I'm now looking for an alternative way to leverage the API.
I can directly exercise the API successfully using REST and OAuth, but am struggling to formulate how to accomplish that within a Cloud Function. At the moment, I have the following in my requirements.txt and am trying to work through how to leverage the Rest API with OAuth.
google-api-python-client==1.7.4
oauth2client==4.1.3
google-auth-httplib2==0.0.3
Any code snippets, thoughts or suggestions are welcome.
I have tried myself, and it seems that the error is on this line:
budget_info = service.budgets().create('billingAccounts/000000-AAAAAA-BBBBBB/budgets', body={longJSONStringHere}).execute()
You are missing the billingAccounts
resource on this service path, you can fix it by adding it like this, and it should work:
budget_info = service.billingAccounts().budgets().create('billingAccounts/000000-AAAAAA-BBBBBB/budgets', body={longJSONStringHere}).execute()
Another thing that I have noticed, is that you are using the oauth2client
library to retrieve the application default credentials, however this library is deprecated.
Instead, you should use the google-auth
library for this, you can just change the code to retrieve the credentials to look like this:
import google.auth
credentials, project_id = google.auth.default()