-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interface for event subscription plugins; SQS plugin (#24352)
Initial version of an internal plugin interface for event subscription plugins, and an AWS SQS plugin as an example. Co-authored-by: Tom Proctor <[email protected]>
- Loading branch information
Showing
14 changed files
with
656 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
events: Add support for event subscription plugins, including SQS | ||
``` |
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,95 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package event | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hashicorp/vault/sdk/helper/backoff" | ||
) | ||
|
||
type Factory func(context.Context) (SubscriptionPlugin, error) | ||
|
||
// SubscriptionPlugin is the interface implemented by plugins that can subscribe to and receive events. | ||
type SubscriptionPlugin interface { | ||
// Subscribe is used to set up a new connection. | ||
Subscribe(context.Context, *SubscribeRequest) error | ||
// Send is used to send events to a connection. | ||
Send(context.Context, *SendRequest) error | ||
// Unsubscribe is used to teardown a connection. | ||
Unsubscribe(context.Context, *UnsubscribeRequest) error | ||
// PluginMetadata returns the name and version for the particular event subscription plugin. | ||
// The name is usually set as a constant the backend, e.g., "sqs" for the | ||
// AWS SQS backend. | ||
PluginMetadata() *PluginMetadata | ||
// Close closes all connections. | ||
Close(ctx context.Context) error | ||
} | ||
|
||
type Request struct { | ||
Subscribe *SubscribeRequest | ||
Unsubscribe *UnsubscribeRequest | ||
Event *SendRequest | ||
} | ||
|
||
type SubscribeRequest struct { | ||
SubscriptionID string | ||
Config map[string]interface{} | ||
VerifyConnection bool | ||
} | ||
|
||
type UnsubscribeRequest struct { | ||
SubscriptionID string | ||
} | ||
|
||
type SendRequest struct { | ||
SubscriptionID string | ||
EventJSON string | ||
} | ||
|
||
type PluginMetadata struct { | ||
Name string | ||
Version string | ||
} | ||
|
||
// SubscribeConfigDefaults defines configuration map keys for common default options. | ||
// Embed this in your own config struct to pick up these default options. | ||
type SubscribeConfigDefaults struct { | ||
Retries *int `mapstructure:"retries"` | ||
RetryMinBackoff *time.Duration `mapstructure:"retry_min_backoff"` | ||
RetryMaxBackoff *time.Duration `mapstructure:"retry_max_backoff"` | ||
} | ||
|
||
// default values for common configuration keys | ||
const ( | ||
DefaultRetries = 3 | ||
DefaultRetryMinBackoff = 100 * time.Millisecond | ||
DefaultRetryMaxBackoff = 5 * time.Second | ||
) | ||
|
||
func (c *SubscribeConfigDefaults) GetRetries() int { | ||
if c.Retries == nil { | ||
return DefaultRetries | ||
} | ||
return *c.Retries | ||
} | ||
|
||
func (c *SubscribeConfigDefaults) GetRetryMinBackoff() time.Duration { | ||
if c.RetryMinBackoff == nil { | ||
return DefaultRetryMinBackoff | ||
} | ||
return *c.RetryMinBackoff | ||
} | ||
|
||
func (c *SubscribeConfigDefaults) GetRetryMaxBackoff() time.Duration { | ||
if c.RetryMaxBackoff == nil { | ||
return DefaultRetryMaxBackoff | ||
} | ||
return *c.RetryMaxBackoff | ||
} | ||
|
||
func (c *SubscribeConfigDefaults) NewRetryBackoff() *backoff.Backoff { | ||
return backoff.NewBackoff(c.GetRetries(), c.GetRetryMinBackoff(), c.GetRetryMaxBackoff()) | ||
} |
Oops, something went wrong.