Get trigger (v1)

Retrieve an existing Eventarc trigger.

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"
	"cloud.google.com/go/eventarc/apiv1/eventarcpb"
)

// getTrigger retrieves an Eventarc trigger.
func getTrigger(ctx context.Context, w io.Writer, projectID string, locationID string, triggerID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/triggers/%s", projectID, locationID, triggerID)

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

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

	trigger, err := client.GetTrigger(ctx, req)
	if err != nil {
		return fmt.Errorf("failed to get trigger: %w", err)
	}

	fmt.Fprintf(w, "Retrieved trigger: %s\n", trigger.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();

/**
 * Retrieves an Eventarc trigger.
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud location or region for the trigger.
 * @param {string} triggerId - The ID of the trigger to retrieve.
 */
async function getTrigger(projectId, locationId, triggerId) {
  const name = client.triggerPath(projectId, locationId, triggerId);

  try {
    const [trigger] = await client.getTrigger({name});

    console.log(`Trigger name: ${trigger.name}`);
    console.log(`Event type: ${trigger.eventFilters?.find(f => f.attribute === 'type')?.value}`);
    return trigger;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Trigger not found: ${name}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied for trigger: ${name}`);
    } else {
      console.error(`API error retrieving trigger: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { getTrigger };

What's next

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