Get provider (v1)

Retrieve details for a specific Eventarc provider.

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

	eventarc "cloud.google.com/go/eventarc/apiv1"
	"cloud.google.com/go/eventarc/apiv1/eventarcpb"
)

// getProvider retrieves an Eventarc event provider.
func getProvider(ctx context.Context, w io.Writer) error {
	projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
	if projectID == "" {
		projectID = "example-project-id"
	}
	location := os.Getenv("GOOGLE_CLOUD_LOCATION")
	if location == "" {
		location = os.Getenv("GOOGLE_CLOUD_REGION")
	}
	if location == "" {
		location = "us-central1"
	}

	name := fmt.Sprintf("projects/%s/locations/%s/providers/storage.googleapis.com", projectID, location)

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

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

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

	fmt.Fprintf(w, "Retrieved provider: %s\n", response.GetName())

	return nil
}

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

/**
 * Use the getProvider method
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The location ID (e.g., 'global' or a supported Google Cloud region).
 * @param {string} providerId - The identifier of the Eventarc provider (e.g., 'storage.googleapis.com'). This is server-defined; retrieve it from a ListProviders response or specify a known Google Cloud provider.
 */
async function getProvider(projectId, locationId, providerId) {
  const name = `projects/${projectId}/locations/${locationId}/providers/${providerId}`;

  try {
    const [provider] = await client.getProvider({name});

    console.log(`Provider name: ${provider.name}`);
    console.log(`Display name: ${provider.displayName}`);

    return provider;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Provider not found: ${name}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied accessing provider: ${name}`);
    } else {
      console.error(`API error retrieving provider: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { getProvider };

What's next

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