|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// This is a generated sample, using the typeless sample bot. Please |
| 16 | +// look for the source TypeScript sample (.ts) for modifications. |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +/** |
| 20 | + * This sample demonstrates how to perform basic operations on topics with |
| 21 | + * the Google Cloud Pub/Sub API. |
| 22 | + * |
| 23 | + * For more information, see the README.md under /pubsub and the documentation |
| 24 | + * at https://cloud.google.com/pubsub/docs. |
| 25 | + */ |
| 26 | + |
| 27 | +// sample-metadata: |
| 28 | +// title: Create Topic With Azure Event Hubs Ingestion |
| 29 | +// description: Creates a new topic, with Azure Event Hubs ingestion enabled. |
| 30 | +// usage: node createTopicWithAzureEventHubsIngestion.js <topic-name> <cluster-arn> <msk-topic> <role-arn> <gcp-service-account> |
| 31 | + |
| 32 | +// [START pubsub_create_topic_with_azure_event_hubs_ingestion] |
| 33 | +/** |
| 34 | + * TODO(developer): Uncomment these variables before running the sample. |
| 35 | + */ |
| 36 | +// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID'; |
| 37 | +// const resourceGroup = 'YOUR_RESOURCE_GROUP'; |
| 38 | +// const namespace = 'YOUR_NAMESPACE'; |
| 39 | +// const eventHub = 'YOUR_EVENT_HUB'; |
| 40 | +// const clientId = 'YOUR_CLIENT_ID'; |
| 41 | +// const tenantId = 'YOUR_TENANT_ID'; |
| 42 | +// const subscriptionId = 'YOUR_SUBSCRIPTION_ID'; |
| 43 | +// const gcpServiceAccount = 'ingestion-account@...'; |
| 44 | + |
| 45 | +// Imports the Google Cloud client library |
| 46 | +const {PubSub} = require('@google-cloud/pubsub'); |
| 47 | + |
| 48 | +// Creates a client; cache this for further use |
| 49 | +const pubSubClient = new PubSub(); |
| 50 | + |
| 51 | +async function createTopicWithAzureEventHubsIngestion( |
| 52 | + topicNameOrId, |
| 53 | + resourceGroup, |
| 54 | + namespace, |
| 55 | + eventHub, |
| 56 | + clientId, |
| 57 | + tenantId, |
| 58 | + subscriptionId, |
| 59 | + gcpServiceAccount |
| 60 | +) { |
| 61 | + // Creates a new topic with Azure Event Hubs ingestion. |
| 62 | + await pubSubClient.createTopic({ |
| 63 | + name: topicNameOrId, |
| 64 | + ingestionDataSourceSettings: { |
| 65 | + azureEventHubs: { |
| 66 | + resourceGroup, |
| 67 | + namespace, |
| 68 | + eventHub, |
| 69 | + clientId, |
| 70 | + tenantId, |
| 71 | + subscriptionId, |
| 72 | + gcpServiceAccount, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }); |
| 76 | + console.log( |
| 77 | + `Topic ${topicNameOrId} created with Azure Event Hubs ingestion.` |
| 78 | + ); |
| 79 | +} |
| 80 | +// [END pubsub_create_topic_with_azure_event_hubs_ingestion] |
| 81 | + |
| 82 | +function main( |
| 83 | + topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID', |
| 84 | + resourceGroup = 'YOUR_RESOURCE_GROUP', |
| 85 | + namespace = 'YOUR_NAMESPACE', |
| 86 | + eventHub = 'YOUR_EVENT_HUB', |
| 87 | + clientId = 'YOUR_CLIENT_ID', |
| 88 | + tenantId = 'YOUR_TENANT_ID', |
| 89 | + subscriptionId = 'YOUR_SUBSCRIPTION_ID', |
| 90 | + gcpServiceAccount = 'ingestion-account@...' |
| 91 | +) { |
| 92 | + createTopicWithAzureEventHubsIngestion( |
| 93 | + topicNameOrId, |
| 94 | + resourceGroup, |
| 95 | + namespace, |
| 96 | + eventHub, |
| 97 | + clientId, |
| 98 | + tenantId, |
| 99 | + subscriptionId, |
| 100 | + gcpServiceAccount |
| 101 | + ).catch(err => { |
| 102 | + console.error(err.message); |
| 103 | + process.exitCode = 1; |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +main(...process.argv.slice(2)); |
0 commit comments