forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EventGrid] Move to Samples v2 Framework (Azure#14532)
This change moves our samples to be generated via `devtool`, as part of the samples quality effort. Much of the diff here is just moving code around to confirm to the new patterns, as outlined in the migration guide, but one interesting outcome is that since the samples are now part of the ts compliation for the package itself, and our samples use service bus (which itself uses AsyncIterators) we have to add a `lib` in our `tsconfig.json`. We do not add any polyfill or anything like that to our `package.json` because our implementation does not use these features. After talking about this with will, I did manually patch the *generated* tsconfig.json for our samples to say we target ES2018 (since the samples will need this), and he will add support for that for the tool going forward. In addition, we add some more resources to our `test-resources.json` so that these samples can run in CI and be validated. Fixes Azure#14471 Fixes Azure#14573
- Loading branch information
Showing
29 changed files
with
581 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sdk/eventgrid/eventgrid/samples-dev/consumeEventsFromServiceBusQueue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* @summary Consume events delivered by Event Grid to a Service Bus Queue | ||
* @azsdk-weight 3 | ||
*/ | ||
|
||
import { EventGridDeserializer, isSystemEvent } from "@azure/eventgrid"; | ||
import { ServiceBusClient, ServiceBusReceivedMessage } from "@azure/service-bus"; | ||
import * as dotenv from "dotenv"; | ||
|
||
// Load the .env file if it exists | ||
dotenv.config(); | ||
|
||
// Create a Event Grid Consumer which will decode the payload of service bus message into an array of EventGridEvent objects. | ||
const consumer = new EventGridDeserializer(); | ||
|
||
// The connection string for Service Bus namespace that Event Grid will deliver messages to. | ||
// You can find the connection string in the Azure portal. | ||
// Navigate to Settings > Shared access policies > RootManageSharedAccessKey in your Service Bus Namespace's menu blade to see | ||
// the connection string. | ||
const serviceBusClientConnectionString = process.env["SERVICE_BUS_CONNECTION_STRING"] || ""; | ||
|
||
// The name of the queue within the Service Bus namespace that Event Grid will deliver messages to. You should ensure that | ||
// events sent to this queue are sent using the Event Grid schema. | ||
const serviceBusQueueName = process.env["SERVICE_BUS_QUEUE_NAME"] || ""; | ||
|
||
// Create a receiver to read messages from the Service Bus Queue. | ||
const receiver = new ServiceBusClient(serviceBusClientConnectionString).createReceiver( | ||
serviceBusQueueName | ||
); | ||
|
||
// The handler function which will be run on each message we remove from the Service Bus Queue. | ||
async function processMessage(message: ServiceBusReceivedMessage): Promise<void> { | ||
// Convert the message into an array of Event Grid Events. | ||
const events = await consumer.deserializeEventGridEvents(message.body); | ||
|
||
// Process each message, printing the type and ID. In addition, if the event is a system event generated by Azure when | ||
// a blob is created, print the URL from the event. | ||
for (const event of events) { | ||
console.log(`Processing event of type ${event.eventType} with id: ${event.id}`); | ||
if (isSystemEvent("Microsoft.Storage.BlobCreated", event)) { | ||
console.log(`A blob was created with URL: ${event.data.url}`); | ||
} | ||
} | ||
} | ||
|
||
async function main() { | ||
// Start processing events. | ||
const closer = receiver.subscribe({ | ||
processMessage, | ||
processError: async (err) => { | ||
console.error("Error while processing events:", err); | ||
} | ||
}); | ||
|
||
// Run for 10 seconds, allowing events to be processed. | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 1000 * 10); | ||
}); | ||
|
||
// Stop processing events and exit. | ||
await closer.close(); | ||
await receiver.close(); | ||
process.exit(); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* @summary Send events to Event Grid using the Cloud Events 1.0 Schema. | ||
* @azsdk-weight 1 | ||
*/ | ||
|
||
import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid"; | ||
import * as dotenv from "dotenv"; | ||
|
||
// Load the .env file if it exists | ||
dotenv.config(); | ||
|
||
// The URL of the endpoint of the Event Grid topic. | ||
const endpoint = process.env["EVENT_GRID_CLOUD_EVENT_SCHEMA_ENDPOINT"] || ""; | ||
|
||
// You can find the access keys in the Azure portal. | ||
// Navigate to Settings > Access keys in your Event Grid topic's menu blade to see both access keys (you may use either). | ||
const accessKey = process.env["EVENT_GRID_CLOUD_EVENT_SCHEMA_API_KEY"] || ""; | ||
|
||
export async function main(): Promise<void> { | ||
// Create the client used to publish events to the Event Grid Service | ||
const client = new EventGridPublisherClient( | ||
endpoint, | ||
"CloudEvent", | ||
new AzureKeyCredential(accessKey) | ||
); | ||
|
||
// Send an event to the Event Grid Service, using the Cloud Event schema. | ||
// A random ID will be generated for this event, since one is not provided. | ||
await client.send([ | ||
{ | ||
type: "azure.sdk.eventgrid.samples.cloudevent", | ||
source: "/azure/sdk/eventgrid/samples/sendEventSample", | ||
data: { | ||
message: "this is a sample event" | ||
} | ||
} | ||
]); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/** | ||
* @summary Send events to Event Grid using the Event Grid Schema. | ||
* @azsdk-weight 2 | ||
*/ | ||
|
||
import { EventGridPublisherClient, AzureKeyCredential } from "@azure/eventgrid"; | ||
import * as dotenv from "dotenv"; | ||
|
||
// Load the .env file if it exists | ||
dotenv.config(); | ||
|
||
// The URL of the endpoint of the Event Grid topic. | ||
const endpoint = process.env["EVENT_GRID_EVENT_GRID_SCHEMA_ENDPOINT"] || ""; | ||
|
||
// You can find the access keys in the Azure portal. | ||
// Navigate to Settings > Access keys in your Event Grid topic's menu blade to see both access keys (you may use either). | ||
const accessKey = process.env["EVENT_GRID_EVENT_GRID_SCHEMA_API_KEY"] || ""; | ||
|
||
export async function main(): Promise<void> { | ||
// Create the client used to publish events to the Event Grid Service | ||
const client = new EventGridPublisherClient( | ||
endpoint, | ||
"EventGrid", | ||
new AzureKeyCredential(accessKey) | ||
); | ||
|
||
// Send an event to the Event Grid Service, using the Event Grid schema. | ||
// A random ID will be generated for this event, since one is not provided. | ||
await client.send([ | ||
{ | ||
eventType: "Azure.SDK.Samples.CustomEvent", | ||
subject: "azure/sdk/eventgrid/samples/sendEventSample", | ||
dataVersion: "1.0", | ||
data: { | ||
message: "this is a sample event" | ||
} | ||
} | ||
]); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.