-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into PMF-22-destination-reloading
# Conflicts: # adapters/google_cloud_storage.go # adapters/s3.go # main.go # main_test.go
- Loading branch information
Showing
11 changed files
with
202 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 |
---|---|---|
|
@@ -59,7 +59,7 @@ Please see our extensive documentation [here](https://eventnative-docs.ksense.io | |
## Community | ||
We are made for developers, by developers and would love to have you join our community. | ||
* [Wiki](https://github.com/ksensehq/eventnative/wiki) - Check out our development wiki. | ||
* [Slack](https://join.slack.com/t/eventnative/shared_invite/zt-gincgy2s-ZYwXXBjw_GIN1PhVzgaUNA) - Join our slack. | ||
* [Slack](https://join.slack.com/t/eventnative/shared_invite/zt-ick4jl3k-Hj1SYVHCyJrZmyJAzaO~Uw) - Join our slack. | ||
* [Email](mailto:[email protected]) - Send us an email. | ||
* Submit a pull request! | ||
|
||
|
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,133 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"github.com/gin-gonic/gin" | ||
"github.com/ksensehq/eventnative/adapters" | ||
"github.com/ksensehq/eventnative/logging" | ||
"github.com/ksensehq/eventnative/middleware" | ||
"net/http" | ||
) | ||
|
||
type ConnectionConfig struct { | ||
DestinationType string `json:"type"` | ||
ConnectionConfig interface{} `json:"config"` | ||
} | ||
|
||
type ConnectionTestHandler struct { | ||
} | ||
|
||
type RedshiftConfig struct { | ||
DbConfig *adapters.DataSourceConfig `json:"database"` | ||
S3Config *adapters.S3Config `json:"s3"` | ||
} | ||
|
||
type SnowflakeExternalConfig struct { | ||
SnowflakeConfig adapters.SnowflakeConfig `json:"snowflake"` | ||
S3Config adapters.S3Config `json:"s3"` | ||
} | ||
|
||
func testConnection(config ConnectionConfig) error { | ||
switch config.DestinationType { | ||
case "postgres": | ||
var postgresConfig adapters.DataSourceConfig | ||
body, err := json.Marshal(config.ConnectionConfig) | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal(body, &postgresConfig) | ||
if err != nil { | ||
return err | ||
} | ||
if err := postgresConfig.Validate(); err != nil { | ||
return err | ||
} | ||
postgres, err := adapters.NewPostgres(context.Background(), &postgresConfig) | ||
if err != nil { | ||
return err | ||
} | ||
defer postgres.Close() | ||
return postgres.Test() | ||
case "clickhouse": | ||
var chConfig adapters.ClickHouseConfig | ||
body, err := json.Marshal(config.ConnectionConfig) | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal(body, &chConfig) | ||
if err != nil { | ||
return err | ||
} | ||
if err = chConfig.Validate(); err != nil { | ||
return err | ||
} | ||
tableStatementFactory, err := adapters.NewTableStatementFactory(&chConfig) | ||
if err != nil { | ||
return err | ||
} | ||
nonNullFields := map[string]bool{"eventn_ctx_event_id": true, "_timestamp": true} | ||
for i := range chConfig.Dsns { | ||
var resultError error | ||
resultError = nil | ||
ch, err := adapters.NewClickHouse(context.Background(), chConfig.Dsns[i], chConfig.Database, chConfig.Cluster, chConfig.Tls, tableStatementFactory, nonNullFields) | ||
if err != nil { | ||
resultError = err | ||
continue | ||
} | ||
resultError = ch.Test() | ||
if err = ch.Close(); err != nil { | ||
logging.Warn("Failed to close clickhouse datasource %s", err) | ||
} | ||
if resultError == nil { | ||
return nil | ||
} | ||
} | ||
return nil | ||
case "redshift": | ||
var rsConfig RedshiftConfig | ||
body, err := json.Marshal(config.ConnectionConfig) | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal(body, &rsConfig) | ||
if err != nil { | ||
return err | ||
} | ||
if err = rsConfig.DbConfig.Validate(); err != nil { | ||
return err | ||
} | ||
if rsConfig.S3Config != nil { | ||
if err = rsConfig.S3Config.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
redshift, err := adapters.NewAwsRedshift(context.Background(), rsConfig.DbConfig, rsConfig.S3Config) | ||
if err != nil { | ||
return err | ||
} | ||
defer redshift.Close() | ||
return redshift.Test() | ||
default: | ||
return errors.New("unsupported destination type " + config.DestinationType) | ||
} | ||
} | ||
|
||
func NewConnectionTestHandler() *ConnectionTestHandler { | ||
return &ConnectionTestHandler{} | ||
} | ||
|
||
func (h *ConnectionTestHandler) Handler(c *gin.Context) { | ||
connectionConfig := ConnectionConfig{} | ||
if err := c.BindJSON(&connectionConfig); err != nil { | ||
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{Message: "Failed to parse body", Error: err}) | ||
return | ||
} | ||
err := testConnection(connectionConfig) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{Message: err.Error(), Error: err}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, middleware.OkResponse{Status: "Connection established"}) | ||
} |
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,25 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
type AdminToken struct { | ||
Token string | ||
} | ||
|
||
func (a *AdminToken) AdminAuth(main gin.HandlerFunc, errMsg string) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
if a.Token == "" { | ||
c.JSON(http.StatusUnauthorized, ErrorResponse{Message: "admin_token must be configured"}) | ||
return | ||
} | ||
token := c.GetHeader("X-Admin-Token") | ||
if token != a.Token { | ||
c.JSON(http.StatusUnauthorized, ErrorResponse{Message: errMsg}) | ||
return | ||
} | ||
main(c) | ||
} | ||
} |
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,10 @@ | ||
package middleware | ||
|
||
type OkResponse struct { | ||
Status string `json:"status"` | ||
} | ||
|
||
type ErrorResponse struct { | ||
Message string `json:"message"` | ||
Error error `json:"error"` | ||
} |