Google Cloud Pub/Sub Extension

You're viewing Apigee Edge documentation.
Go to the Apigee X documentation.
info

Version: 1.5.1

Publish messages to a topic in the Google Cloud Pub/Sub service.

This content provides reference for configuring and using this extension.

Prerequisites

Before using the extension from an API proxy using the ExtensionCallout policy, you must:

  1. Create a Cloud Pub/Sub topic, as described in Quickstart: Using the console.

  2. Once you have the topic, grant permission to publish to your Cloud Pub/Sub topic. For example, you can use the roles/pubsub.publisher role to publish to topics. For more on Cloud Pub/Sub roles, see Access Control.

  3. Use the GCP Console to generate a key for the service account.

  4. Use the contents of the resulting key JSON file when adding and configuring the extension using the configuration reference.

About Cloud Pub/Sub

Cloud Pub/Sub provides many-to-many, asynchronous messaging that decouples senders and receivers. Cloud Pub/Sub delivers low-latency, durable messaging that helps developers quickly integrate systems hosted on the Google Cloud Platform and externally.

If you're just getting started with Cloud Pub/Sub, take a look at the quickstarts in the Cloud Pub/Sub documentation.

Samples

The following examples illustrate how to configure support for Cloud Pub/Sub extension actions using the ExtensionCallout policy.

Text message

In this example, the publish action publishes a message as a simple string to a topic specified in the extension's configuration. The publish action returns the ID of the published message in the topic to which it was published and writes it to the variable specified by the <Output> tag:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ConnectorCallout async="false" continueOnError="true" enabled="true" name="Publish-Message">
    <DisplayName>Publish Message</DisplayName>
    <Connector>cloud-pub-sub-extension-example</Connector>
    <Action>publish</Action>
    <Input><![CDATA[
      {
          "message" : "My test message."
      }
    ]]></Input>
    <Output>topic.message</Output>
</ConnectorCallout>

The following Assign Message policy uses the value of the variable storing the extension's response to assign the response payload. The message ID is stored in the messageId property of the output variable:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Get-Topic-Message-ID">
    <DisplayName>Get Topic Message ID</DisplayName>
    <AssignTo type="response" createNew="false"/>
    <Set>
        <Payload contentType="application/json">{topic.message.messageId}</Payload>
    </Set>
</AssignMessage>

The response data is returned as JSON:

{"messageId":"165918654986954"}

Therefore, the value of topic.message.messageId is 165918654986954.

JSON message

In this example, the publish action publishes a message as JSON to a topic specified in the extension's configuration. The publish action returns the ID of the published message in the topic to which it was published and writes it to the variable specified by the <Output> tag:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ConnectorCallout async="false" continueOnError="true" enabled="true" name="Publish-Message">
    <DisplayName>Publish Message</DisplayName>
    <Connector>cloud-pub-sub-extension-example</Connector>
    <Action>publish</Action>
    <Input><![CDATA[
      {
          "message" : { "info" : "My test message" }
      }
    ]]></Input>
    <Output>topic.message</Output>
</ConnectorCallout>

The following Assign Message policy uses the value of the variable storing the extension's response to assign the response payload. The message ID is stored in the messageId property of the output variable:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Get-Topic-Message-ID">
    <DisplayName>Get Topic Message ID</DisplayName>
    <AssignTo type="response" createNew="false"/>
    <Set>
        <Payload contentType="application/json">{topic.message.messageId}</Payload>
    </Set>
</AssignMessage>

The response data is returned as JSON:

{"messageId":"165918654986954"}

Therefore, the value of topic.message.messageId is 165918654986954.

Actions

publish

Publishes a message to the topic specified in this extension's configuration.

Syntax

<Action>publish</Action>
<Input><![CDATA[{
  "message" : "message-to-publish" | JSON-structured-data,
  "customAttributes": JSON-array
}]]></Input>

String Example

<Action>publish</Action>
<Input><![CDATA[
  {
      "message" : "My test message."
  }
]]></Input>

JSON Structured Data Example

<Action>publish</Action>
<Input><![CDATA[
  {
      "message" : { "info" : "My test message" }
  }
]]></Input>

Custom Attributes Example

<Action>publish</Action>
<Input><![CDATA[
  {
      "message" : { "info" : "My test message" },
      "customAttributes": {"attr1": "foo", "attr2": "bar"}
  }
]]></Input>

Request parameters

Parameter Description Type Default Required
message The message to publish. You can use a simple string or structure your message with JSON of your own design. String or JSON None Yes
customAttributes

Custom attributes to add to the message in the form:

"customAttributes": {"attr1": "foo", "attr2": "bar"}.
JSON array None No

Response

JSON containing the ID of the message as published to the topic. For example:

{"messageId":"165918654986954"}

Configuration Reference

Use the following when you're configuring and deploying this extension for use in API proxies.

Common extension properties

The following properties are present for every extension.

Property Description Default Required
name Name you're giving this configuration of the extension. None Yes
packageName Name of the extension package as given by Apigee Edge. None Yes
version Version number for the extension package from which you're configuring an extension. None Yes
configuration Configuration value specific to the extension you're adding. See Properties for this extension package None Yes

Properties for this extension package

Property Description Default Required
topic The topic to publish to. Get this value from the Pub/Sub service, such as from the GCP console for the service. For example, the topic name might take the form projects/GCP-PROJECT-ID/topics/TOPIC-NAME None. Yes.
credentials When entered in the Apigee Edge console, this is the content of your service account key file. When sent via the management API, it is a base64-encoded value generated from the service account key file. None. Yes.