List enrollments (v1)

List Eventarc enrollments in a specified project and location.

Code sample

Node.js

To authenticate to Eventarc, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'use strict';

const {EventarcClient} = require('@google-cloud/eventarc');
const {status} = require('@grpc/grpc-js');

const client = new EventarcClient();

/**
 * Lists Eventarc enrollments in a specified project and location.
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud region or location ID (for example, 'us-central1' or 'global').
 */
async function listEnrollments(projectId, locationId) {
  try {
    const parent = `projects/${projectId}/locations/${locationId}`;

    const [enrollments] = await client.listEnrollments({
      parent,
    });

    for (const enrollment of enrollments) {
      console.log(`Enrollment: ${enrollment.name}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument: ${err.message}`);
    } else {
      console.error(`API error (${err.code}): ${err.message}`);
    }
  }
}

module.exports = { listEnrollments };

Python

To authenticate to Eventarc, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import os

import google.api_core.exceptions
from google.cloud import eventarc_v1


def list_enrollments() -> None:
    """List Eventarc enrollments in a location."""
    client = eventarc_v1.EventarcClient()

    project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project")
    location = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
    parent = f"projects/{project_id}/locations/{location}"

    request = eventarc_v1.ListEnrollmentsRequest(
        parent=parent,
    )

    try:
        response = client.list_enrollments(request=request)
        count = 0
        for enrollment in response:
            print(f"Enrollment: {enrollment.name}")
            count += 1
        if count == 0:
            print(f"No enrollments found in {parent}.")
    except google.api_core.exceptions.GoogleAPICallError as e:
        print(f"Failed to list enrollments: {e}")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.