Skip to content

Commit 1af1c40

Browse files
committed
chore: Configure publishmq in emulator for local dev & local publishmq helper
1 parent 0aaa83d commit 1af1c40

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

build/dev/azure/config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@
3232
}
3333
]
3434
},
35+
// publishmq
36+
{
37+
"Name": "outpost-publish",
38+
"Subscriptions": [
39+
{
40+
"Name": "outpost-publish-sub"
41+
}
42+
]
43+
},
3544

3645
// tests
3746
{

cmd/publish/declare_handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ func handleDeclare(w http.ResponseWriter, r *http.Request) {
1010
switch r.URL.Query().Get("method") {
1111
case "aws_sqs":
1212
err = declareAWS()
13+
case "azure_servicebus":
14+
err = declareAzureServiceBus()
1315
case "gcp_pubsub":
1416
err = declareGCP()
1517
case "rabbitmq":

cmd/publish/publish_azure.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
9+
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
10+
)
11+
12+
const (
13+
AzureServiceBusConnectionString = "Endpoint=sb://localhost;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"
14+
AzureServiceBusTopic = "outpost-publish"
15+
AzureServiceBusSubscription = "outpost-publish-sub"
16+
)
17+
18+
func publishAzureServiceBus(body map[string]interface{}) error {
19+
log.Printf("[x] Publishing Azure Service Bus")
20+
21+
ctx := context.Background()
22+
client, err := azservicebus.NewClientFromConnectionString(AzureServiceBusConnectionString, nil)
23+
if err != nil {
24+
return fmt.Errorf("failed to create Azure Service Bus client: %w", err)
25+
}
26+
defer client.Close(ctx)
27+
28+
sender, err := client.NewSender(AzureServiceBusTopic, nil)
29+
if err != nil {
30+
return fmt.Errorf("failed to create sender: %w", err)
31+
}
32+
defer sender.Close(ctx)
33+
34+
messageBody, err := json.Marshal(body)
35+
if err != nil {
36+
return err
37+
}
38+
39+
message := &azservicebus.Message{
40+
Body: messageBody,
41+
ApplicationProperties: map[string]interface{}{
42+
"source": "outpost-publish",
43+
},
44+
}
45+
46+
err = sender.SendMessage(ctx, message, nil)
47+
if err != nil {
48+
return fmt.Errorf("failed to send message: %w", err)
49+
}
50+
51+
log.Printf("[x] Published message to Azure Service Bus topic %s", AzureServiceBusTopic)
52+
return nil
53+
}
54+
55+
func declareAzureServiceBus() error {
56+
log.Printf("[*] Declaring Azure Service Bus Publish infra")
57+
return fmt.Errorf("azure sb emulator does not support declaring topics and subscriptions. Use `%s` and `%s` for the publishmq topic and subscription", AzureServiceBusTopic, AzureServiceBusSubscription)
58+
}

cmd/publish/publish_handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func handlePublish(w http.ResponseWriter, r *http.Request) {
1717
switch r.URL.Query().Get("method") {
1818
case "aws_sqs":
1919
err = publishAWS(body)
20+
case "azure_servicebus":
21+
err = publishAzureServiceBus(body)
2022
case "gcp_pubsub":
2123
err = publishGCP(body)
2224
case "rabbitmq":

0 commit comments

Comments
 (0)