Skip to content

Commit e39a0d0

Browse files
authored
Merge branch 'master' into optimize_vault_error
2 parents 4a7f715 + 74af5b6 commit e39a0d0

File tree

29 files changed

+473
-58
lines changed

29 files changed

+473
-58
lines changed

Readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Components Contrib
22

3-
[![Go Report Card](https://goreportcard.com/badge/github.com/dapr/components-contrib)](https://goreportcard.com/report/github.com/dapr/components-contrib)
43
[![Build Status](https://github.com/dapr/components-contrib/workflows/components-contrib/badge.svg?event=push&branch=master)](https://github.com/dapr/components-contrib/actions?workflow=components-contrib)
54
[![Discord](https://img.shields.io/discord/778680217417809931)](https://discord.com/channels/778680217417809931/781589820128493598)
65
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

bindings/alicloud/dingtalk/webhook/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package webhook
1212
import (
1313
"errors"
1414

15-
"github.com/dapr/components-contrib/internal/config"
15+
"github.com/dapr/kit/config"
1616
)
1717

1818
type Settings struct {

bindings/alicloud/nacos/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"fmt"
1515
"time"
1616

17-
"github.com/dapr/components-contrib/internal/config"
17+
"github.com/dapr/kit/config"
1818
)
1919

2020
type Settings struct {

bindings/alicloud/rocketmq/rocketmq.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"github.com/apache/rocketmq-client-go/v2/primitive"
2020
mqw "github.com/cinience/go_rocketmq"
2121
"github.com/dapr/components-contrib/bindings"
22-
"github.com/dapr/components-contrib/internal/retry"
2322
"github.com/dapr/kit/logger"
23+
"github.com/dapr/kit/retry"
2424
)
2525

2626
type AliCloudRocketMQ struct {

bindings/alicloud/rocketmq/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111

1212
rocketmq "github.com/cinience/go_rocketmq"
13-
"github.com/dapr/components-contrib/internal/config"
13+
"github.com/dapr/kit/config"
1414
)
1515

1616
// rocketmq

bindings/graphql/graphql.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation and Dapr Contributors.
3+
// Licensed under the MIT License.
4+
// ------------------------------------------------------------
5+
6+
package graphql
7+
8+
import (
9+
"context"
10+
"encoding/json"
11+
"fmt"
12+
"regexp"
13+
"strings"
14+
"time"
15+
16+
"github.com/dapr/components-contrib/bindings"
17+
"github.com/dapr/kit/logger"
18+
graphql "github.com/machinebox/graphql"
19+
)
20+
21+
const (
22+
// configurations to connect to GraphQL
23+
connectionEndPointKey = "endpoint"
24+
25+
// keys from request's metadata
26+
commandQuery = "query"
27+
commandMutation = "mutation"
28+
29+
// keys from response's metadata
30+
respOpKey = "operation"
31+
respStartTimeKey = "start-time"
32+
respEndTimeKey = "end-time"
33+
respDurationKey = "duration"
34+
35+
QueryOperation bindings.OperationKind = "query"
36+
MutationOperation bindings.OperationKind = "mutation"
37+
)
38+
39+
// GraphQL represents GraphQL output bindings
40+
type GraphQL struct {
41+
client *graphql.Client
42+
header map[string]string
43+
logger logger.Logger
44+
}
45+
46+
var _ = bindings.OutputBinding(&GraphQL{})
47+
48+
// NewGraphQL returns a new GraphQL binding instance
49+
func NewGraphQL(logger logger.Logger) *GraphQL {
50+
return &GraphQL{logger: logger}
51+
}
52+
53+
// Init initializes the GraphQL binding
54+
func (gql *GraphQL) Init(metadata bindings.Metadata) error {
55+
gql.logger.Debug("GraphQL Error: Initializing GraphQL binding")
56+
57+
p := metadata.Properties
58+
ep, ok := p[connectionEndPointKey]
59+
if !ok || ep == "" {
60+
return fmt.Errorf("GraphQL Error: Missing GraphQL URL")
61+
}
62+
63+
// Connect to GraphQL Server
64+
client := graphql.NewClient(ep)
65+
66+
gql.client = client
67+
gql.header = make(map[string]string)
68+
for k, v := range p {
69+
if strings.HasPrefix(k, "header:") {
70+
gql.header[strings.TrimPrefix(k, "header:")] = v
71+
}
72+
}
73+
74+
return nil
75+
}
76+
77+
// Operations returns list of operations supported by GraphQL binding
78+
func (gql *GraphQL) Operations() []bindings.OperationKind {
79+
return []bindings.OperationKind{
80+
QueryOperation,
81+
MutationOperation,
82+
}
83+
}
84+
85+
// Invoke handles all invoke operations
86+
func (gql *GraphQL) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
87+
if req == nil {
88+
return nil, fmt.Errorf("GraphQL Error: Invoke request required")
89+
}
90+
91+
if req.Metadata == nil {
92+
return nil, fmt.Errorf("GraphQL Error: Metadata required")
93+
}
94+
gql.logger.Debugf("operation: %v", req.Operation)
95+
96+
startTime := time.Now().UTC()
97+
98+
resp := &bindings.InvokeResponse{
99+
Metadata: map[string]string{
100+
respOpKey: string(req.Operation),
101+
respStartTimeKey: startTime.Format(time.RFC3339Nano),
102+
},
103+
Data: []byte{},
104+
}
105+
106+
var graphqlResponse interface{}
107+
108+
switch req.Operation { // nolint: exhaustive
109+
case QueryOperation:
110+
if err := gql.runRequest(commandQuery, req, &graphqlResponse); err != nil {
111+
return nil, err
112+
}
113+
114+
case MutationOperation:
115+
if err := gql.runRequest(commandMutation, req, &graphqlResponse); err != nil {
116+
return nil, err
117+
}
118+
119+
default:
120+
return nil, fmt.Errorf("GraphQL Error: invalid operation type: %s. Expected %s or %s",
121+
req.Operation, QueryOperation, MutationOperation)
122+
}
123+
124+
b, err := json.Marshal(graphqlResponse)
125+
if err != nil {
126+
return nil, fmt.Errorf("GraphQL Error: %w", err)
127+
}
128+
129+
resp.Data = b
130+
131+
endTime := time.Now().UTC()
132+
resp.Metadata[respEndTimeKey] = endTime.Format(time.RFC3339Nano)
133+
resp.Metadata[respDurationKey] = endTime.Sub(startTime).String()
134+
135+
return resp, nil
136+
}
137+
138+
func (gql *GraphQL) runRequest(requestKey string, req *bindings.InvokeRequest, response interface{}) error {
139+
requestString, ok := req.Metadata[requestKey]
140+
if !ok || requestString == "" {
141+
return fmt.Errorf("GraphQL Error: required %q not set", requestKey)
142+
}
143+
144+
// Check that the command is either a query or mutation based on the first keyword.
145+
requestString = strings.TrimSpace(requestString)
146+
re := regexp.MustCompile(`(?m)` + requestKey + `\b`)
147+
matches := re.FindAllStringIndex(requestString, 1)
148+
if len(matches) != 1 || matches[0][0] != 0 {
149+
return fmt.Errorf("GraphQL Error: command is not a %s", requestKey)
150+
}
151+
152+
request := graphql.NewRequest(requestString)
153+
154+
for headerKey, headerValue := range gql.header {
155+
request.Header.Set(headerKey, headerValue)
156+
}
157+
158+
for k, v := range req.Metadata {
159+
if strings.HasPrefix(k, "header:") {
160+
request.Header.Set(strings.TrimPrefix(k, "header:"), v)
161+
}
162+
}
163+
164+
if err := gql.client.Run(context.Background(), request, response); err != nil {
165+
return fmt.Errorf("GraphQL Error: %w", err)
166+
}
167+
168+
return nil
169+
}

bindings/graphql/graphql_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation and Dapr Contributors.
3+
// Licensed under the MIT License.
4+
// ------------------------------------------------------------
5+
6+
package graphql
7+
8+
import (
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestOperations(t *testing.T) {
15+
t.Parallel()
16+
t.Run("Get operation list", func(t *testing.T) {
17+
t.Parallel()
18+
b := NewGraphQL(nil)
19+
assert.NotNil(t, b)
20+
l := b.Operations()
21+
assert.Equal(t, 2, len(l))
22+
})
23+
}

bindings/mqtt/mqtt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
mqtt "github.com/eclipse/paho.mqtt.golang"
2323

2424
"github.com/dapr/components-contrib/bindings"
25-
"github.com/dapr/components-contrib/internal/retry"
2625
"github.com/dapr/kit/logger"
26+
"github.com/dapr/kit/retry"
2727
)
2828

2929
const (

go.mod

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.16
55
require (
66
cloud.google.com/go v0.65.0
77
cloud.google.com/go/datastore v1.1.0
8-
cloud.google.com/go/pubsub v1.3.1
8+
cloud.google.com/go/pubsub v1.5.0
99
cloud.google.com/go/storage v1.10.0
1010
github.com/Azure/azure-amqp-common-go/v3 v3.1.0 // indirect
1111
github.com/Azure/azure-event-hubs-go v1.3.1
@@ -31,16 +31,16 @@ require (
3131
github.com/apache/pulsar-client-go v0.1.0
3232
github.com/apache/rocketmq-client-go/v2 v2.1.0
3333
github.com/apache/thrift v0.14.0 // indirect
34-
github.com/aws/aws-sdk-go v1.27.0
34+
github.com/aws/aws-sdk-go v1.36.30
3535
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
3636
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
3737
github.com/camunda-cloud/zeebe/clients/go v1.0.0
38-
github.com/cenkalti/backoff/v4 v4.1.0
38+
github.com/cenkalti/backoff/v4 v4.1.1
3939
github.com/cinience/go_rocketmq v0.0.2
4040
github.com/coreos/go-oidc v2.1.0+incompatible
4141
github.com/cyphar/filepath-securejoin v0.2.2
4242
github.com/dancannon/gorethink v4.0.0+incompatible
43-
github.com/dapr/kit v0.0.1
43+
github.com/dapr/kit v0.0.2-0.20210614175626-b9074b64d233
4444
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73
4545
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f
4646
github.com/dghubble/oauth1 v0.6.0
@@ -50,19 +50,21 @@ require (
5050
github.com/eapache/go-resiliency v1.2.0 // indirect
5151
github.com/eclipse/paho.mqtt.golang v1.3.2
5252
github.com/fasthttp-contrib/sessions v0.0.0-20160905201309-74f6ac73d5d5
53+
github.com/fatih/color v1.10.0 // indirect
5354
github.com/fatih/structs v1.1.0 // indirect
5455
github.com/gavv/httpexpect v2.0.0+incompatible // indirect
5556
github.com/ghodss/yaml v1.0.0
5657
github.com/go-logr/logr v0.3.0 // indirect
5758
github.com/go-ole/go-ole v1.2.5 // indirect
5859
github.com/go-redis/redis/v8 v8.8.0
5960
github.com/go-sql-driver/mysql v1.5.0
60-
github.com/gocql/gocql v0.0.0-20191018090344-07ace3bab0f8
61+
github.com/gocql/gocql v0.0.0-20210515062232-b7ef815b4556
6162
github.com/golang/mock v1.5.0
6263
github.com/golang/protobuf v1.4.3
64+
github.com/golang/snappy v0.0.3 // indirect
6365
github.com/google/uuid v1.2.0
6466
github.com/googleapis/gnostic v0.5.1 // indirect
65-
github.com/gorilla/mux v1.7.3
67+
github.com/gorilla/mux v1.8.0
6668
github.com/grandcat/zeroconf v0.0.0-20190424104450-85eadb44205c
6769
github.com/hashicorp/consul/api v1.3.0
6870
github.com/hashicorp/go-multierror v1.0.0
@@ -73,20 +75,25 @@ require (
7375
github.com/influxdata/influxdb-client-go v1.4.0
7476
github.com/jackc/pgx/v4 v4.6.0
7577
github.com/jcmturner/gofork v1.0.0 // indirect
78+
github.com/jonboulle/clockwork v0.2.0 // indirect
7679
github.com/json-iterator/go v1.1.10
7780
github.com/kataras/go-errors v0.0.3 // indirect
7881
github.com/kataras/go-serializer v0.0.4 // indirect
7982
github.com/keighl/postmark v0.0.0-20190821160221-28358b1a94e3
8083
github.com/kr/text v0.2.0 // indirect
84+
github.com/machinebox/graphql v0.2.2
85+
github.com/matryer/is v1.4.0 // indirect
8186
github.com/microcosm-cc/bluemonday v1.0.7 // indirect
82-
github.com/mitchellh/mapstructure v1.3.3
87+
github.com/miekg/dns v1.1.35 // indirect
88+
github.com/mitchellh/mapstructure v1.4.1
8389
github.com/moul/http2curl v1.0.0 // indirect
8490
github.com/nacos-group/nacos-sdk-go v1.0.7
8591
github.com/nats-io/nats-server/v2 v2.2.1 // indirect
8692
github.com/nats-io/nats-streaming-server v0.21.2 // indirect
8793
github.com/nats-io/nats.go v1.10.1-0.20210330225420-a0b1f60162f8
8894
github.com/nats-io/stan.go v0.8.3
8995
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
96+
github.com/nxadm/tail v1.4.8 // indirect
9097
github.com/open-policy-agent/opa v0.23.2
9198
github.com/opentracing/opentracing-go v1.2.0 // indirect
9299
github.com/patrickmn/go-cache v2.1.0+incompatible
@@ -113,8 +120,10 @@ require (
113120
go.opencensus.io v0.22.5 // indirect
114121
goji.io v2.0.2+incompatible // indirect
115122
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b
123+
golang.org/x/mod v0.4.1 // indirect
116124
golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c
117125
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93
126+
golang.org/x/text v0.3.5 // indirect
118127
google.golang.org/api v0.32.0
119128
google.golang.org/genproto v0.0.0-20201204160425-06b3db808446
120129
google.golang.org/grpc v1.36.0
@@ -132,6 +141,7 @@ require (
132141
gopkg.in/kataras/go-serializer.v0 v0.0.4 // indirect
133142
gopkg.in/square/go-jose.v2 v2.5.0 // indirect
134143
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
144+
honnef.co/go/tools v0.1.3 // indirect
135145
k8s.io/api v0.20.0
136146
k8s.io/apiextensions-apiserver v0.20.0
137147
k8s.io/apimachinery v0.20.0
@@ -140,3 +150,5 @@ require (
140150
)
141151

142152
replace k8s.io/client => github.com/kubernetes-client/go v0.0.0-20190928040339-c757968c4c36
153+
154+
replace github.com/dapr/components-contrib => ../components-contrib

0 commit comments

Comments
 (0)