azeventgrid

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 13 Imported by: 0

README

Azure Event Grid Publisher Client Module for Go

Azure Event Grid is a highly scalable, fully managed Pub Sub message distribution service that offers flexible message consumption patterns. For more information about Event Grid see: link.

The client in this package can publish events to Event Grid topics.

NOTE: This client does NOT work with Event Grid namespaces. Use the aznamespaces module instead.

Key links:

Getting started

Install the package

Install the Azure Event Grid client module for Go with go get:

go get github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/azeventgrid
Prerequisites
Authenticate the client

Event Grid publisher clients authenticate using either:

Key concepts

The client in this package can publish events to Azure Event Grid topics. Topics are published to using the publisher.Client. The topic you publish to will be configured to accept events of a certain format: EventGrid, CloudEvent or Custom. Separate functions are available on the publisher client for each format.

Examples

Examples for various scenarios can be found on pkg.go.dev or in the example*_test.go files in our GitHub repo for azeventgrid.

Troubleshooting

Logging

This module uses the classification-based logging implementation in azcore. To enable console logging for all SDK modules, set the environment variable AZURE_SDK_GO_LOGGING to all.

Use the azcore/log package to control log event output.

import (
  "fmt"
  azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
)

// print log output to stdout
azlog.SetListener(func(event azlog.Event, s string) {
    fmt.Printf("[%s] %s\n", event, s)
})

Next steps

More sample code should go here, along with links out to the appropriate example tests.

Contributing

For details on contributing to this repository, see the contributing guide.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Many people all over the world have helped make this project better. You'll want to check out:

Reporting security issues and security bugs

Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) secure@microsoft.com. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter.

License

Azure SDK for Go is licensed under the MIT license.

Documentation

Overview

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See License.txt in the project root for license information.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client contains the methods for the Client group. Don't use this type directly, use a constructor function instead.

func NewClient

func NewClient(endpoint string, tokenCredential azcore.TokenCredential, options *ClientOptions) (*Client, error)

NewClient creates a Client that authenticates using a TokenCredential.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/azeventgrid"
)

func main() {
	// ex: https://<topic-name>.<region>.eventgrid.azure.net/api/events
	endpoint := os.Getenv("EVENTGRID_TOPIC_ENDPOINT")

	if endpoint == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	// DefaultAzureCredential is a simplified credential type that tries to authenticate via several
	// different authentication mechanisms. For more control (or more credential types) see the documentation
	// for the azidentity module: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity
	tokenCred, err := azidentity.NewDefaultAzureCredential(nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	client, err := azeventgrid.NewClient(endpoint, tokenCred, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client

}
Output:

func NewClientWithSAS

func NewClientWithSAS(endpoint string, sasCred *azcore.SASCredential, options *ClientOptions) (*Client, error)

NewClientWithSAS creates a Client using a shared access signature credential.

Example
// ex: https://<topic-name>.<region>.eventgrid.azure.net/api/events
endpoint := os.Getenv("EVENTGRID_TOPIC_ENDPOINT")
key := os.Getenv("EVENTGRID_TOPIC_KEY")

if endpoint == "" || key == "" {
	fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
	return
}

client, err := azeventgrid.NewClientWithSAS(endpoint, azcore.NewSASCredential(key), &azeventgrid.ClientOptions{
	ClientOptions: policy.ClientOptions{
		PerCallPolicies: []policy.Policy{
			dumpFullPolicy{"EventGridEvent"},
		},
	},
})

if err != nil {
	//  TODO: Update the following line with your application specific error handling logic
	log.Fatalf("ERROR: %s", err)
}

_ = client
Output:

func NewClientWithSharedKeyCredential

func NewClientWithSharedKeyCredential(endpoint string, keyCred *azcore.KeyCredential, options *ClientOptions) (*Client, error)

NewClientWithSharedKeyCredential creates a Client using a shared key credential.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/azeventgrid"
)

func main() {
	// ex: https://<topic-name>.<region>.eventgrid.azure.net/api/events
	endpoint := os.Getenv("EVENTGRID_TOPIC_ENDPOINT")
	key := os.Getenv("EVENTGRID_TOPIC_KEY")

	if endpoint == "" || key == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	client, err := azeventgrid.NewClientWithSharedKeyCredential(endpoint, azcore.NewKeyCredential(key), nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client

}
Output:

func (*Client) PublishCloudEvents

func (client *Client) PublishCloudEvents(ctx context.Context, events []messaging.CloudEvent, options *PublishCloudEventsOptions) (PublishCloudEventsResponse, error)

PublishCloudEvents - Publishes a batch of events to an Azure Event Grid topic. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2018-01-01

  • events - An array of events to be published to Event Grid.
  • options - ClientPublishCloudEventEventsOptions contains the optional parameters for the Client.PublishCloudEvents method.
Example

PublishCloudEvents publishes events using the CloudEvent schema to a topic. The topic must be configured to use the CloudEvent schema or this will fail.

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/messaging"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/azeventgrid"
)

func main() {
	// ex: https://<topic-name>.<region>.eventgrid.azure.net/api/events
	endpoint := os.Getenv("EVENTGRID_CE_TOPIC_ENDPOINT")
	key := os.Getenv("EVENTGRID_CE_TOPIC_KEY")

	if endpoint == "" || key == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	// Other authentication methods:
	// - azeventgrid.NewClient(): authenticate using a TokenCredential from azidentity.
	// - azeventgrid.NewClientWithSAS(): authenticate using a SAS token.
	client, err := azeventgrid.NewClientWithSharedKeyCredential(endpoint, azcore.NewKeyCredential(key), nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	cloudEvent, err := messaging.NewCloudEvent("source", "eventtype", "data", nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	events := []messaging.CloudEvent{
		cloudEvent,
	}

	_, err = client.PublishCloudEvents(context.TODO(), events, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

}
Output:

func (*Client) PublishCustomEventEvents

func (client *Client) PublishCustomEventEvents(ctx context.Context, events []any, options *PublishCustomEventEventsOptions) (PublishCustomEventEventsResponse, error)

PublishCustomEventEvents - Publishes a batch of events to an Azure Event Grid topic. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2018-01-01

  • events - An array of events to be published to Event Grid.
  • options - PublishCustomEventEventsOptions contains the optional parameters for the Client.PublishCustomEventEvents method.

func (*Client) PublishEvents

func (client *Client) PublishEvents(ctx context.Context, events []Event, options *PublishEventsOptions) (PublishEventsResponse, error)

PublishEvents - Publishes a batch of events to an Azure Event Grid topic. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2018-01-01

  • events - An array of events to be published to Event Grid.
  • options - PublishEventsOptions contains the optional parameters for the Client.PublishEvents method.
Example

PublishEvents publishes events using the EventGrid schema to a topic. The topic must be configured to use the EventGrid schema or this will fail.

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"time"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/azeventgrid"
)

func main() {
	// ex: https://<topic-name>.<region>.eventgrid.azure.net/api/events
	endpoint := os.Getenv("EVENTGRID_TOPIC_ENDPOINT")
	key := os.Getenv("EVENTGRID_TOPIC_KEY")

	if endpoint == "" || key == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	// Other authentication methods:
	// - azeventgrid.NewClient(): authenticate using a TokenCredential from azidentity.
	// - azeventgrid.NewClientWithSAS(): authenticate using a SAS token.
	client, err := azeventgrid.NewClientWithSharedKeyCredential(endpoint, azcore.NewKeyCredential(key), nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	events := []azeventgrid.Event{
		{
			Data:        "data for this event",
			DataVersion: to.Ptr("1.0"),
			EventType:   to.Ptr("event-type"),
			EventTime:   to.Ptr(time.Now()),
			ID:          to.Ptr("unique-id"),
			Subject:     to.Ptr("subject"),
		},
	}

	_, err = client.PublishEvents(context.TODO(), events, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

}
Output:

type ClientOptions

type ClientOptions struct {
	azcore.ClientOptions
}

ClientOptions contains optional settings for Client

type Event

type Event struct {
	// REQUIRED; Event data specific to the event type.
	Data any

	// REQUIRED; The schema version of the data object.
	DataVersion *string

	// REQUIRED; The time (in UTC) the event was generated.
	EventTime *time.Time

	// REQUIRED; The type of the event that occurred.
	EventType *string

	// REQUIRED; An unique identifier for the event.
	ID *string

	// REQUIRED; A resource path relative to the topic path.
	Subject *string

	// The resource path of the event source.
	Topic *string

	// READ-ONLY; The schema version of the event metadata.
	MetadataVersion *string
}

Event - Properties of an event published to an Event Grid topic using the EventGrid Schema.

func (Event) MarshalJSON

func (e Event) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type Event.

func (*Event) UnmarshalJSON

func (e *Event) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type Event.

type PublishCloudEventsOptions

type PublishCloudEventsOptions struct {
	// Required only when publishing to partner namespaces with partner topic routing mode ChannelNameHeader.
	AegChannelName *string
}

PublishCloudEventsOptions contains the optional parameters for the Client.PublishCloudEvents method.

type PublishCloudEventsResponse

type PublishCloudEventsResponse struct {
}

PublishCloudEventsResponse contains the response from method Client.PublishCloudEvents.

type PublishCustomEventEventsOptions

type PublishCustomEventEventsOptions struct {
}

PublishCustomEventEventsOptions contains the optional parameters for the Client.PublishCustomEventEvents method.

type PublishCustomEventEventsResponse

type PublishCustomEventEventsResponse struct {
}

PublishCustomEventEventsResponse contains the response from method Client.PublishCustomEventEvents.

type PublishEventsOptions

type PublishEventsOptions struct {
}

PublishEventsOptions contains the optional parameters for the Client.PublishEvents method.

type PublishEventsResponse

type PublishEventsResponse struct {
}

PublishEventsResponse contains the response from method Client.PublishEvents.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL