forked from open-policy-agent/gatekeeper
-
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.
Signed-off-by: Jaydip Gabani <[email protected]>
- Loading branch information
1 parent
bd96c52
commit f5581d4
Showing
160 changed files
with
26,023 additions
and
71 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package eventhub | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"encoding/json" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs" | ||
"github.com/open-policy-agent/gatekeeper/v3/pkg/pubsub/connection" | ||
) | ||
|
||
|
||
// Dapr represents driver for interacting with pub sub using dapr. | ||
type EventHub struct { | ||
// Array of clients to talk to different endpoints | ||
producerClient *azeventhubs.ProducerClient | ||
|
||
// Name of the pubsub component | ||
ConnectionString string `json:"connectionString"` | ||
EventHubName string `json:"eventHubName"` | ||
} | ||
|
||
const ( | ||
Name = "eventhub" | ||
) | ||
|
||
func (r *EventHub) Publish(ctx context.Context, data interface{}, topic string) error { | ||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("error marshaling data: %w", err) | ||
} | ||
|
||
newBatchOptions := &azeventhubs.EventDataBatchOptions{} | ||
|
||
batch, err := r.producerClient.NewEventDataBatch(context.TODO(), newBatchOptions) | ||
err = batch.AddEventData(&azeventhubs.EventData{ | ||
Body: jsonData, | ||
}, nil) | ||
if err != nil { | ||
return fmt.Errorf("error adding event data to batch: %w", err) | ||
} | ||
|
||
err = r.producerClient.SendEventDataBatch(ctx, batch, nil) | ||
if err != nil { | ||
return fmt.Errorf("error publishing message to dapr: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *EventHub) CloseConnection() error { | ||
return nil | ||
} | ||
|
||
func (r *EventHub) UpdateConnection(_ context.Context, config interface{}) error { | ||
cfg, ok := config.(string) | ||
if !ok { | ||
return fmt.Errorf("invalid type assertion, config is not in expected format") | ||
} | ||
|
||
err := json.Unmarshal([]byte(cfg), &r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.producerClient, err = azeventhubs.NewProducerClientFromConnectionString(r.ConnectionString, r.EventHubName, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Returns a new client for dapr. | ||
func NewConnection(_ context.Context, config interface{}) (connection.Connection, error) { | ||
cfg, ok := config.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid type assertion, config is not in expected format") | ||
} | ||
client := &EventHub{} | ||
err := json.Unmarshal([]byte(cfg), &client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client.producerClient, err = azeventhubs.NewProducerClientFromConnectionString(client.ConnectionString, client.EventHubName, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return client, nil | ||
} |
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
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
Oops, something went wrong.