Skip to content

Commit

Permalink
Forward connector utilities address and token to plugin executable (#112
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hariso authored Jul 16, 2024
1 parent 35d371d commit 8ef04cf
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
test:
go test $(GOTEST_FLAGS) -race ./...

.PHONY: fmt
fmt:
gofumpt -l -w .

.PHONY: lint
lint:
golangci-lint run
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
go.uber.org/mock v0.4.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
mvdan.cc/gofumpt v0.6.0
)

require (
Expand Down Expand Up @@ -257,6 +258,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/tools v0.4.7 // indirect
mvdan.cc/gofumpt v0.6.0 // indirect
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f // indirect
)
6 changes: 5 additions & 1 deletion pconduit/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package pconduit

import "context"
import (
"context"
)

// -- Connector token ----------------------------------------------------------

type connectorTokenCtxKey struct{}

Expand Down
33 changes: 33 additions & 0 deletions pconduit/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pconduit

import (
"context"
"testing"

"github.com/matryer/is"
)

func TestContextUtils_ConnectorToken(t *testing.T) {
is := is.New(t)
ctx := context.Background()

want := "test-token"
ctx = ContextWithConnectorToken(ctx, want)
got := ConnectorTokenFromContext(ctx)

is.Equal(want, got)
}
4 changes: 3 additions & 1 deletion pconduit/env_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ package pconduit

const (
EnvConduitConnectorUtilitiesGRPCTarget = "CONDUIT_CONNECTOR_UTILITIES_GRPC_TARGET"
EnvConduitConnectorSchemaToken = "CONDUIT_CONNECTOR_SCHEMA_TOKEN"
EnvConduitConnectorSchemaToken = "CONDUIT_CONNECTOR_TOKEN"
EnvConduitConnectorID = "CONDUIT_CONNECTOR_ID"
EnvConduitLogLevel = "CONDUIT_LOG_LEVEL"
)
4 changes: 3 additions & 1 deletion pconduit/v1/client/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ type SchemaServiceClient struct {
var _ pconduit.SchemaService = (*SchemaServiceClient)(nil)

func NewSchemaServiceClient(cc *grpc.ClientConn) *SchemaServiceClient {
return &SchemaServiceClient{grpcClient: conduitv1.NewSchemaServiceClient(cc)}
return &SchemaServiceClient{
grpcClient: conduitv1.NewSchemaServiceClient(cc),
}
}

func (c *SchemaServiceClient) CreateSchema(ctx context.Context, request pconduit.CreateSchemaRequest) (pconduit.CreateSchemaResponse, error) {
Expand Down
12 changes: 12 additions & 0 deletions pconnector/client/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package client

import (
"errors"
"fmt"
"net"
"os/exec"
Expand Down Expand Up @@ -76,6 +77,17 @@ func WithDelve(port int) Option {
})
}

func WithEnvVar(key, value string) Option {
return serveConfigFunc(func(in *plugin.ClientConfig) error {
if in.Cmd == nil {
return errors.New("plugin client config has no Cmd set (tip: check if WithReattachConfig is used)")
}

in.Cmd.Env = append(in.Cmd.Env, key+"="+value)
return nil
})
}

func getFreePort() int {
// Excerpt from net.Listen godoc:
// If the port in the address parameter is empty or "0", as in
Expand Down
42 changes: 42 additions & 0 deletions pconnector/client/option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"os/exec"
"testing"

"github.com/conduitio/conduit-connector-protocol/pconduit"
"github.com/hashicorp/go-plugin"
"github.com/matryer/is"
)

func TestOption_WithEnvVar(t *testing.T) {
is := is.New(t)
cmd := exec.Command("test-path")
cmd.Env = []string{"FOO=BAR"}
cc := &plugin.ClientConfig{Cmd: cmd}

underTest := WithEnvVar(pconduit.EnvConduitConnectorUtilitiesGRPCTarget, "localhost:12345")

err := underTest.ApplyOption(cc)
is.NoErr(err)

want := []string{
"FOO=BAR",
pconduit.EnvConduitConnectorUtilitiesGRPCTarget + "=localhost:12345",
}
is.Equal(want, cc.Cmd.Env)
}
21 changes: 21 additions & 0 deletions pconnector/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pconnector

type PluginConfig struct {
Token string
ConnectorID string
LogLevel string
}
1 change: 1 addition & 0 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ import (
_ "github.com/bufbuild/buf/cmd/buf"
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "go.uber.org/mock/mockgen"
_ "mvdan.cc/gofumpt"
)

0 comments on commit 8ef04cf

Please sign in to comment.