Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Added Initializing auth client from config method #140

Merged
merged 7 commits into from
Apr 14, 2021
27 changes: 27 additions & 0 deletions clients/go/admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/flyteorg/flyteidl/clients/go/admin/mocks"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/service"
"github.com/flyteorg/flytestdlib/logger"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
Expand All @@ -31,6 +32,11 @@ func NewAdminClient(ctx context.Context, conn *grpc.ClientConn) service.AdminSer
return service.NewAdminServiceClient(conn)
}

func NewAuthClient(ctx context.Context, conn *grpc.ClientConn) service.AuthServiceClient {
logger.Infof(ctx, "Initialized Auth client")
return service.NewAuthServiceClient(conn)
}

func GetAdditionalAdminClientConfigOptions(cfg Config) []grpc.DialOption {
opts := make([]grpc.DialOption, 0, 2)
backoffConfig := grpc.BackoffConfig{
Expand Down Expand Up @@ -144,6 +150,18 @@ func InitializeAdminClient(ctx context.Context, cfg Config) service.AdminService
return NewAdminClient(ctx, adminConnection)
}

// Create an AdminClient and AuthServiceClient with a shared Admin connection for the process
func InitializeClients(ctx context.Context, cfg Config) (service.AdminServiceClient, service.AuthServiceClient) {
pmahindrakar-oss marked this conversation as resolved.
Show resolved Hide resolved
once.Do(func() {
var err error
adminConnection, err = NewAdminConnection(ctx, cfg)
if err != nil {
logger.Panicf(ctx, "failed to initialize Admin connection. Err: %s", err.Error())
}
})
return NewAdminClient(ctx, adminConnection), NewAuthClient(ctx, adminConnection)
}

func InitializeAdminClientFromConfig(ctx context.Context) (service.AdminServiceClient, error) {
cfg := GetConfig(ctx)
if cfg == nil {
Expand All @@ -152,6 +170,15 @@ func InitializeAdminClientFromConfig(ctx context.Context) (service.AdminServiceC
return InitializeAdminClient(ctx, *cfg), nil
}

func InitializeClientsFromConfig(ctx context.Context) (service.AdminServiceClient, service.AuthServiceClient, error) {
cfg := GetConfig(ctx)
if cfg == nil {
return nil, nil, fmt.Errorf("retrieved Nil config for [%s] key", configSectionKey)
}
adminClient, authClient := InitializeClients(ctx, *cfg)
return adminClient, authClient, nil
}

func InitializeMockAdminClient() service.AdminServiceClient {
logger.Infof(context.TODO(), "Initialized Mock Admin client")
return &mocks.AdminServiceClient{}
Expand Down
25 changes: 25 additions & 0 deletions clients/go/admin/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/flyteorg/flytestdlib/config"

"github.com/stretchr/testify/assert"
)

Expand All @@ -22,6 +23,12 @@ func TestInitializeAndGetAdminClient(t *testing.T) {
}))
})

t.Run("legal-from-config", func(t *testing.T) {
authClient, err := InitializeAdminClientFromConfig(ctx)
assert.NoError(t, err)
assert.NotNil(t, authClient)
})

t.Run("illegal", func(t *testing.T) {
adminConnection = nil
once = sync.Once{}
Expand All @@ -45,3 +52,21 @@ func TestGetAdditionalAdminClientConfigOptions(t *testing.T) {
opts := GetAdditionalAdminClientConfigOptions(adminServiceConfig)
assert.Equal(t, 2, len(opts))
}

func TestInitializeAndGetClients(t *testing.T) {
ctx := context.TODO()
t.Run("legal", func(t *testing.T) {
u, err := url.Parse("http://localhost:8089")
assert.NoError(t, err)
aClient, auClient := InitializeClients(ctx, Config{Endpoint: config.URL{URL: *u}})
assert.NotNil(t, aClient)
assert.NotNil(t, auClient)
})

t.Run("legal-from-config", func(t *testing.T) {
aClient, auClient, err := InitializeClientsFromConfig(ctx)
assert.NoError(t, err)
assert.NotNil(t, aClient)
assert.NotNil(t, auClient)
})
}
162 changes: 162 additions & 0 deletions clients/go/admin/mocks/AuthServiceClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generate_mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ set -e
set -x

mockery -dir=gen/pb-go/flyteidl/service/ -name=AdminServiceClient -output=clients/go/admin/mocks
mockery -dir=gen/pb-go/flyteidl/service/ -name=AuthServiceClient -output=clients/go/admin/mocks
mockery -dir=gen/pb-go/flyteidl/datacatalog/ -name=DataCatalogClient -output=clients/go/datacatalog/mocks