1
votes

I'm working on a project to automate google cloud setup, is there any way to create a google cloud platform project using Google-Api-Client for python?

Help me, please! Thanks in advance!

Here's what I have tried:

From views.py

from google.cloud import resource_manager
...

    client = resource_manager.Client()
         # List down all gcp projects
         for project in client.list_projects():
            print(project)
         new_project = client.new_project('project11-372', name='My new project')
         new_project.create()

It list down all of my gcp projects but doesn't create new project.

Here's the error I have received:

File "/Users/abdul/Documents/IGui/rest/views.py", line 60, in post new_project.create() File "/Users/abdul/IstioVirEnv/lib/python3.6/site-packages/google/cloud/resource_manager/project.py", line 138, in create data=data) File "/Users/abdul/IstioVirEnv/lib/python3.6/site-packages/google/cloud/_http.py", line 293, in api_request raise exceptions.from_http_response(response) google.cloud.exceptions.Forbidden: 403 POST https://cloudresourcemanager.googleapis.com/v1beta1/projects: The caller does not have permission.
[20/Aug/2017 05:55:02] "POST /user/deployment/create/new HTTP/1.1" 500 14960

1

1 Answers

0
votes

I have successfully created google cloud project by using Google cloud API python client.

Steps

  • run gcloud beta auth application-default login command to get application default credentials by login onto the browser
  • Enable Resource Manager API on my gcp console

Here's the working python code:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
...


credentials = GoogleCredentials.get_application_default()
    service = discovery.build('cloudresourcemanager', 'v1', credentials=credentials)
            project_body = {
                'name': 'Api Project',
                'projectId': 'api-9837'
            }
            request = service.projects().create(body=project_body)
            request.execute()
            pprint(request)