Ask Your Question
1

How can the services in v1beta1 be listed using the gcloud python SDK for serviceusage.googleapis.com with the filter 'state:ENABLED' in PROJECT_ID?

asked 2021-12-05 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-01-19 11:00:00 +0000

bukephalos gravatar image

You can use the following code to list the services in v1beta1 using the gcloud python SDK for serviceusage.googleapis.com with the filter 'state:ENABLED' in PROJECT_ID:

from google.cloud import service_usage
from google.oauth2 import service_account

# Replace with your project id
project_id = 'YOUR_PROJECT_ID'

# Replace with your service account key file path
key_file_path = '/path/to/your/key_file.json'

# Define the filter to only include ENABLED services
filter = 'state:ENABLED'

# Create a service usage client using the credentials from the key file
credentials = service_account.Credentials.from_service_account_file(key_file_path)
service_usage_client = service_usage.ServiceUsageClient(credentials=credentials)

# List the services in the project with the specified filter
services = service_usage_client.list_services(parent=f"projects/{project_id}", filter_=filter)

# Print the names of the enabled services
for service in services:
    print(service.name)

Note: You must replace YOUR_PROJECT_ID and /path/to/your/key_file.json with your actual project ID and service account key file path, respectively.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-05 11:00:00 +0000

Seen: 7 times

Last updated: Jan 19 '22