Update channel (v1)

Update the labels 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"
	"google.golang.org/protobuf/types/known/fieldmaskpb"
)

// updateChannel updates an existing Eventarc channel's labels.
func updateChannel(ctx context.Context, w io.Writer, projectID string, locationID string, channelID string) error {
	channel := &eventarcpb.Channel{
		Name: fmt.Sprintf("projects/%s/locations/%s/channels/%s", projectID, locationID, channelID),
		Labels: map[string]string{
			"env": "prod",
		},
	}
	updateMask := &fieldmaskpb.FieldMask{
		Paths: []string{"labels"},
	}

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

	req := &eventarcpb.UpdateChannelRequest{
		Channel:    channel,
		UpdateMask: updateMask,
	}

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

	updatedChannel, err := op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait failed: %w", err)
	}

	fmt.Fprintf(w, "Updated channel: %s\n", updatedChannel.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;
import com.google.protobuf.FieldMask;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class UpdateChannel {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project_id";
    String locationId = "example-location_id";
    String channelId = "example-channel_id";
    updateChannel(projectId, locationId, channelId);
  }

  public static void updateChannel(String projectId, String locationId, String channelId) throws Exception {
    try (EventarcClient client = EventarcClient.create()) {
      Channel channel =
          Channel.newBuilder()
              .setName(ChannelName.of(projectId, locationId, channelId).toString())
              .putLabels("updated", "true")
              .build();
      FieldMask updateMask = FieldMask.newBuilder().addPaths("labels").build();

      Channel response = client.updateChannelAsync(channel, updateMask).get(5, TimeUnit.MINUTES);
      System.out.println("Updated channel: " + response.getName());
    }
  }
}

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
from google.protobuf import field_mask_pb2

client = eventarc_v1.EventarcClient()


def update_channel(project_id: str, location_id: str, channel_id: str) -> None:
    """Update an Eventarc channel using the update_channel method.

    Args:
        project_id: The Google Cloud project ID.
        location_id: The Google Cloud region where the channel is located.
        channel_id: The identifier of the existing channel to update.
    """
    channel_name = f"projects/{project_id}/locations/{location_id}/channels/{channel_id}"

    channel = eventarc_v1.Channel(
        name=channel_name,
        labels={"updated": "true"},
    )
    update_mask = field_mask_pb2.FieldMask(paths=["labels"])

    try:
        # Initiate the long-running operation to update the channel.
        operation = client.update_channel(
            channel=channel,
            update_mask=update_mask,
        )
        response = operation.result()
        print(f"Updated channel: {response.name}")
        print(f"Channel state: {response.state.name}")
    except google.api_core.exceptions.GoogleAPICallError as e:
        print(f"Failed to update channel: {e}")

What's next

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