Get channel (v1)

Get the details of an existing Eventarc channel.

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"
)

// getChannel retrieves details for an existing Eventarc channel.
func getChannel(ctx context.Context, w io.Writer, projectID string, locationID string, channelID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/channels/%s", projectID, locationID, channelID)

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

	req := &eventarcpb.GetChannelRequest{
		Name: name,
	}

	channel, err := client.GetChannel(ctx, req)
	if err != nil {
		return fmt.Errorf("GetChannel: %w", err)
	}

	fmt.Fprintf(w, "Retrieved channel: %s\n", channel.GetName())
	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.Channel;
import com.google.cloud.eventarc.v1.ChannelName;
import com.google.cloud.eventarc.v1.EventarcClient;

public class GetChannel {

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

  public static void getChannel(String projectId, String locationId, String channelId)
      throws Exception {
    try (EventarcClient client = EventarcClient.create()) {
      String name = ChannelName.of(projectId, locationId, channelId).toString();
      Channel channel = client.getChannel(name);

      System.out.println("Channel name: " + channel.getName());
      System.out.println("Channel state: " + channel.getState());
    }
  }
}

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();

/**
 * Retrieves an Eventarc channel.
 *
 * @param {string} projectId - The Google Cloud project ID where the channel is located.
 * @param {string} locationId - The Google Cloud region where the channel is located (for example, 'us-central1').
 * @param {string} channelId - The user-provided identifier of the Eventarc channel.
 */
async function getChannel(projectId, locationId, channelId) {
  const name = `projects/${projectId}/locations/${locationId}/channels/${channelId}`;

  try {
    const request = {
      name,
    };

    const [channel] = await client.getChannel(request);

    console.log(`Channel retrieved: ${channel.name}`);
    console.log(`State: ${channel.state}`);
    console.log(`Provider: ${channel.provider}`);

    return channel;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Channel not found at path: ${name}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied accessing channel: ${name}`);
    } else if (err.code) {
      console.error(`RPC error [${err.code}]: ${err.message}`);
    } else {
      console.error(`Unexpected error: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { getChannel };

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

client = eventarc_v1.EventarcClient()


def get_channel(project_id: str, location_id: str, channel_id: str) -> None:
    """Retrieve an Eventarc channel.

    Args:
        project_id: The Google Cloud project ID.
        location_id: The Google Cloud region where the channel is located.
        channel_id: The user-specified ID of the Eventarc channel to retrieve.
    """
    name = f"projects/{project_id}/locations/{location_id}/channels/{channel_id}"

    try:
        request = eventarc_v1.GetChannelRequest(name=name)
        channel = client.get_channel(request=request)
        print(f"Retrieved channel: {channel.name}")
        print(f"Channel state: {channel.state}")
    except google.api_core.exceptions.NotFound:
        print(f"Channel not found: {name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"API call error occurred: {error}")

What's next

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