Skip to content

Commit cf44a5d

Browse files
authored
Merge pull request #32 from dsrhub/zz/add-dsrhub-grpc-header
Add grpc header support
2 parents 9935d7f + faa3196 commit cf44a5d

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

plugins/dsrhub_grpc/dsrhub_grpc.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,36 @@ import (
55
"fmt"
66
"time"
77

8+
"google.golang.org/grpc"
9+
"google.golang.org/grpc/metadata"
10+
811
"github.com/dsrhub/dsrhub/idl_dsrhub"
912
"github.com/ovh/utask/pkg/plugins/taskplugin"
10-
"google.golang.org/grpc"
1113
)
1214

1315
var (
1416
// Plugin is the dsrhub_grpc plugin that we can use as a customized plugin
1517
// nolint
1618
Plugin = taskplugin.New(
1719
"dsrhub_grpc", "1.0", exec,
18-
taskplugin.WithConfig(validConfig, DsrHubGRPCConfig{}),
20+
taskplugin.WithConfig(validConfig, DSRHubGRPCConfig{}),
1921
)
2022
)
2123

2224
const (
2325
defaultTimeoutSeconds = 10
2426
)
2527

26-
// DsrHubGRPCConfig is the configuration needed to perform an gRPC client side call
27-
type DsrHubGRPCConfig struct {
28+
// DSRHubGRPCConfig is the configuration needed to perform an gRPC client side call
29+
type DSRHubGRPCConfig struct {
2830
URL string `json:"url"`
2931
Request idl_dsrhub.CreateDSRRequest `json:"request"`
3032
Timeout int `json:"timeout,omitempty"` // timeout in seconds
33+
Header map[string]string `json:"header,omitempty"`
3134
}
3235

3336
func validConfig(config interface{}) error {
34-
cfg := config.(*DsrHubGRPCConfig)
37+
cfg := config.(*DSRHubGRPCConfig)
3538
if cfg.URL == "" {
3639
return fmt.Errorf("invalid dsrhub_grpc config url: empty url")
3740
}
@@ -50,8 +53,8 @@ func validConfig(config interface{}) error {
5053
return nil
5154
}
5255

53-
func exec(stepName string, config interface{}, execCtx interface{}) (output interface{}, metadata interface{}, err error) {
54-
cfg := config.(*DsrHubGRPCConfig)
56+
func exec(stepName string, config interface{}, execCtx interface{}) (output interface{}, _ interface{}, err error) {
57+
cfg := config.(*DSRHubGRPCConfig)
5558

5659
// TODO: support secure connection with configuration
5760
conn, err := grpc.Dial(cfg.URL, grpc.WithInsecure())
@@ -69,7 +72,8 @@ func exec(stepName string, config interface{}, execCtx interface{}) (output inte
6972
)
7073
defer cancel()
7174

72-
res, err := idl_dsrhub.NewDSRHubServiceClient(conn).CreateDSR(ctx, &cfg.Request)
75+
header := metadata.New(cfg.Header)
76+
res, err := idl_dsrhub.NewDSRHubServiceClient(conn).CreateDSR(ctx, &cfg.Request, grpc.Header(&header))
7377
if err != nil {
7478
return nil, nil, err
7579
}

plugins/dsrhub_grpc/dsrhub_grpc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
)
99

1010
func TestValidConfig(t *testing.T) {
11-
var cfg DsrHubGRPCConfig
11+
var cfg DSRHubGRPCConfig
1212

13-
cfg = DsrHubGRPCConfig{}
13+
cfg = DSRHubGRPCConfig{}
1414
assert.Error(t, validConfig(&cfg))
1515

16-
cfg = DsrHubGRPCConfig{
16+
cfg = DSRHubGRPCConfig{
1717
URL: "localhost:50051",
1818
Request: idl_dsrhub.CreateDSRRequest{
1919
Regulation: "gdpr",

0 commit comments

Comments
 (0)