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
d53f33f
commit 1f2905e
Showing
29 changed files
with
844 additions
and
850 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,88 @@ | ||
package dapr | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
daprClient "github.com/dapr/go-sdk/client" | ||
) | ||
|
||
type Connection struct { | ||
// Name of the component object to use in Dapr | ||
component string | ||
|
||
client daprClient.Client | ||
} | ||
|
||
// Dapr represents driver to use Dapr. | ||
type Dapr struct { | ||
openConnections map[string]Connection | ||
} | ||
|
||
const ( | ||
Name = "dapr" | ||
) | ||
|
||
var Connections = &Dapr{ | ||
openConnections: make(map[string]Connection), | ||
} | ||
|
||
func (r *Dapr) Publish(_ context.Context, connectionName string, data interface{}, topic string) error { | ||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("error marshaling data: %w", err) | ||
} | ||
|
||
conn, ok := r.openConnections[connectionName] | ||
if !ok { | ||
return fmt.Errorf("connection not found: %s for Dapr driver", connectionName) | ||
} | ||
err = conn.client.PublishEvent(context.Background(), conn.component, topic, jsonData) | ||
if err != nil { | ||
return fmt.Errorf("error publishing message to dapr: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *Dapr) CloseConnection(connectionName string) error { | ||
delete(r.openConnections, connectionName) | ||
return nil | ||
} | ||
|
||
func (r *Dapr) UpdateConnection(_ context.Context, connectionName string, config interface{}) error { | ||
cfg, ok := config.(map[string]interface{}) | ||
if !ok { | ||
return fmt.Errorf("invalid type assertion, config is not in expected format") | ||
} | ||
component, ok := cfg["component"].(string) | ||
if !ok { | ||
return fmt.Errorf("failed to get value of component") | ||
} | ||
conn := r.openConnections[connectionName] | ||
conn.component = component | ||
r.openConnections[connectionName] = conn | ||
return nil | ||
} | ||
|
||
func (r *Dapr) CreateConnection(_ context.Context, connectionName string, config interface{}) error { | ||
var conn Connection | ||
cfg, ok := config.(map[string]interface{}) | ||
if !ok { | ||
return fmt.Errorf("invalid type assertion, config is not in expected format") | ||
} | ||
conn.component, ok = cfg["component"].(string) | ||
if !ok { | ||
return fmt.Errorf("failed to get value of component") | ||
} | ||
|
||
tmp, err := daprClient.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
conn.client = tmp | ||
r.openConnections[connectionName] = conn | ||
return nil | ||
} |
Oops, something went wrong.