0
votes

I'm trying to create Google cloud project using ResourceManager API python client but not success.

Here's what I have tried:

  1. Setup gcloud
  2. RUN gcloud beta auth application-default login to setup credentials
  3. also setup service account
  4. Enable resource manager api from GCP console.

Here's my python code: From views.py

from google.cloud import resource_manager
...

# GCP Client
client = resource_manager.Client()
# List down all gcp projects
for project in client.list_projects():
    print(project)
# Create a new project
client.new_project(project_id='test-123', name='api')

it list down all of my GCP projects but new project doesn't created on my gcp console.

Also it doesn't return any error.

Help me, please!

Thanks in advance!

1

1 Answers

0
votes

Successfully managed to create 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)