-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support templating for http sinks (#493)
- Loading branch information
Showing
5 changed files
with
235 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
version: 1 | ||
interactions: | ||
- request: | ||
body: '{"details":{"another_key":"index1","some_key":"elasticsearch.index1"}}' | ||
form: {} | ||
headers: | ||
Accept: | ||
- application/json | ||
Content-Type: | ||
- application/json | ||
url: http://127.0.0.1:54943/table/elasticsearch.index1 | ||
method: POST | ||
response: | ||
body: "" | ||
headers: | ||
Content-Length: | ||
- "0" | ||
Date: | ||
- Mon, 11 Jul 2022 09:32:21 GMT | ||
status: 200 OK | ||
code: 200 | ||
duration: 274.68µs | ||
- request: | ||
body: '{"details":{"another_key":"index2","some_key":"elasticsearch.index2"}}' | ||
form: {} | ||
headers: | ||
Accept: | ||
- application/json | ||
Content-Type: | ||
- application/json | ||
url: http://127.0.0.1:54943/table/elasticsearch.index2 | ||
method: POST | ||
response: | ||
body: "" | ||
headers: | ||
Content-Length: | ||
- "0" | ||
Date: | ||
- Mon, 11 Jul 2022 09:32:21 GMT | ||
status: 200 OK | ||
code: 200 | ||
duration: 173.792µs |
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,71 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/d5/tengo/v2" | ||
v1beta2 "github.com/raystack/meteor/models/raystack/assets/v1beta2" | ||
"github.com/raystack/meteor/plugins/internal/tengoutil" | ||
"github.com/raystack/meteor/plugins/internal/tengoutil/structmap" | ||
) | ||
|
||
var errUserExit = errors.New("user exit") | ||
|
||
func (s *Sink) executeScript(ctx context.Context, url string, asset *v1beta2.Asset) error { | ||
scriptCfg := s.config.Script | ||
script, err := tengoutil.NewSecureScript( | ||
([]byte)(scriptCfg.Source), s.scriptGlobals(ctx, url), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
c, err := script.Compile() | ||
if err != nil { | ||
return fmt.Errorf("compile: %w", err) | ||
} | ||
assetMap, err := structmap.AsMap(asset) | ||
if err != nil { | ||
return fmt.Errorf("convert asset to map: %w", err) | ||
} | ||
if err := c.Set("asset", assetMap); err != nil { | ||
return fmt.Errorf("set asset into vm: %w", err) | ||
} | ||
if err := c.RunContext(ctx); err != nil && !errors.Is(err, errUserExit) { | ||
return fmt.Errorf("run: %w", err) | ||
} | ||
return nil | ||
} | ||
func (s *Sink) scriptGlobals(ctx context.Context, url string) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"asset": map[string]interface{}{}, | ||
"sink": &tengo.UserFunction{ | ||
Name: "sink", | ||
Value: s.executeRequestWrapper(ctx, url), | ||
}, | ||
"exit": &tengo.UserFunction{ | ||
Name: "exit", | ||
Value: func(...tengo.Object) (tengo.Object, error) { | ||
return nil, errUserExit | ||
}, | ||
}, | ||
} | ||
} | ||
func (s *Sink) executeRequestWrapper(ctx context.Context, url string) tengo.CallableFunc { | ||
return func(args ...tengo.Object) (tengo.Object, error) { | ||
if len(args) != 1 { | ||
return nil, fmt.Errorf("execute request: invalid number of arguments of sink function, expected 1, got %d", len(args)) | ||
} | ||
payloadObj, ok := args[0].(*tengo.Map) | ||
if !ok { | ||
return nil, fmt.Errorf("execute request: invalid type of argument of sink function, expected map, got %T", args[0]) | ||
} | ||
payload, err := json.Marshal(tengo.ToInterface(payloadObj)) | ||
if err != nil { | ||
return nil, fmt.Errorf("execute request: marshal payload: %w", err) | ||
} | ||
return nil, s.makeRequest(ctx, url, payload) | ||
} | ||
} |