List channel connections (v1)

List all channel connections in a specified project and location.

Code sample

Go

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


import (
	"context"
	"fmt"
	"io"

	eventarc "cloud.google.com/go/eventarc/apiv1"
	eventarcpb "cloud.google.com/go/eventarc/apiv1/eventarcpb"
	"google.golang.org/api/iterator"
)

// listChannelConnections retrieves all channel connections in a specified project and location.
func listChannelConnections(ctx context.Context, w io.Writer, projectID string, locationID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)

	client, err := eventarc.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient failed: %w", err)
	}
	defer client.Close()

	req := &eventarcpb.ListChannelConnectionsRequest{
		Parent: parent,
	}

	it := client.ListChannelConnections(ctx, req)
	var count int
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListChannelConnections iteration failed: %w", err)
		}
		fmt.Fprintf(w, "Channel connection: %s\n", resp.GetName())
		count++
	}

	if count == 0 {
		fmt.Fprintf(w, "No channel connections found in %s\n", parent)
	}

	return nil
}

Java

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

import com.google.cloud.eventarc.v1.ChannelConnection;
import com.google.cloud.eventarc.v1.EventarcClient;
import java.io.IOException;

public class ListChannelConnections {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project-id";
    String locationId = "example-location-id";
    listChannelConnections(projectId, locationId);
  }

  public static void listChannelConnections(String projectId, String locationId) throws Exception {
    try (EventarcClient client = EventarcClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, locationId);
      for (ChannelConnection channelConnection : client.listChannelConnections(parent).iterateAll()) {
        System.out.println("Channel connection: " + channelConnection.getName());
      }
    }
  }
}

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 channel connections in a specific project and location.
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud region or location.
 */
async function listChannelConnections(projectId, locationId) {
  try {
    const parent = `projects/${projectId}/locations/${locationId}`;
    const [channelConnections] = await client.listChannelConnections({
      parent,
    });

    for (const connection of channelConnections) {
      console.log(`Channel connection: ${connection.name}`);
      console.log(`Channel: ${connection.channel}`);
    }

    console.log(`Total channel connections: ${channelConnections.length}`);
  } 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 {
      console.error(`API error: ${err.message}`);
    }
  }
}

module.exports = { listChannelConnections };

Python

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

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


def list_channel_connections(project_id: str, location_id: str) -> None:
    """Lists Eventarc channel connections in a specified location.

    Args:
        project_id: The Google Cloud project ID where channel connections reside.
        location_id: The Google Cloud region where the channel connections are hosted (for example, 'us-central1').
    """
    client = eventarc_v1.EventarcClient()
    parent = f"projects/{project_id}/locations/{location_id}"

    try:
        request = eventarc_v1.ListChannelConnectionsRequest(parent=parent)
        response = client.list_channel_connections(request=request)

        count = 0
        for channel_connection in response:
            count += 1
            print(f"Channel connection: {channel_connection.name}")
            print(f"Channel: {channel_connection.channel}")
            print(f"Activation token: {channel_connection.activation_token}")

        if count == 0:
            print(f"No channel connections found in {parent}.")
    except google.api_core.exceptions.GoogleAPICallError as e:
        print(f"Failed to list channel connections: {e}")

What's next

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