0

is there anyway to enable IAM API through python. However, I dont see any methods mentioned in Google documentaion. I have gone through this link https://cloud.google.com/service-usage/docs/enable-disable#gcloud but it only describes enabling service API via, gcloud or curl.

Is there anyway I can enable it via Python?

1 Answer 1

2

The API you want to use is Service Usage

Link to the REST API:

Service Usage API

Link to the Python API:

Python API

Example code that I wrote to use the Python API to list services:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

# https://cloud.google.com/service-usage/docs/reference/rest/v1/services
# https://cloud.google.com/service-usage/docs/list-services

credentials = GoogleCredentials.get_application_default()

project = 'projects/development-xxxxxx'

service = discovery.build('serviceusage', 'v1', credentials=credentials)

next = None

totalCount = 0

while 1:
    request = service.services().list(parent=project, pageToken=next)

    response = ''

    try:
        response = request.execute()
    except Exception as e:
        print(e)
        break

    services = response.get('services')

    for index in range(len(services)):
        totalCount += 1

        item = services[index]

        name = item['config']['name']
        state = item['state']

        print("%-50s %s" % (name, state))

    next = response.get('nextPageToken')

    # print('NEXT:', next)

    if next == None:
        break;

print('')
print('Total number of services:', totalCount)

To enable a service call the enable method:

API to enable a service

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.