-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCP_BillingAccountTools.py
31 lines (28 loc) · 1.32 KB
/
GCP_BillingAccountTools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Import libraries
from oauth2client.client import GoogleCredentials
from googleapiclient import discovery
import GCP_iam
import GCP_clients
billingClient = GCP_clients.billing
def createBillingSubaccount(projectId, masterBillingAccount):
masterBillingAccount = "billingAccounts/"+masterBillingAccount
## Create Billing Account
billingAccountBody = {
"displayName": projectId+"_billing",
# "open": True,
"masterBillingAccount": masterBillingAccount
}
billingAccountRequest = billingClient.billingAccounts().create(body = billingAccountBody)
billingAccountResponse = billingAccountRequest.execute()
print('Creating %s billing subaccount with billingId: %s' %(billingAccountResponse['displayName'], billingAccountResponse['name']))
return(billingAccountResponse)
def setProjectBillingAccount(projectId, billingId):
billingBody = {
"billingAccountName":"billingAccounts/"+billingId,
"billingEnabled": True,
"projectId": projectId
}
billingProjectUpdateRequest = billingClient.projects().updateBillingInfo(name='projects/'+projectId, body = billingBody)
billingProjectUpdateResponse = billingProjectUpdateRequest.execute()
print('Attaching project: %s to billing account: %s' %(projectId, billingId))
return(billingProjectUpdateResponse)