Move api key from pdb client method calls to pdb client constructor (#194)

* move api key from pdb client method calls to pdb client constructor

* update pdb example client
This commit is contained in:
Maximillian von Briesen 2018-08-02 15:09:50 -04:00 committed by JT Olio
parent f6fb41c5cc
commit d934733c4d
5 changed files with 39 additions and 37 deletions

View File

@ -35,7 +35,8 @@ func main() {
logger, _ := zap.NewDevelopment()
defer logger.Sync()
pdbclient, err := client.NewClient(pointerdbClientPort)
APIKey := []byte("abc123")
pdbclient, err := client.NewClient(pointerdbClientPort, APIKey)
if err != nil {
logger.Error("Failed to dial: ", zap.Error(err))
@ -51,10 +52,9 @@ func main() {
Type: proto.Pointer_INLINE,
InlineSegment: []byte("popcorn"),
}
APIKey := []byte("abc123")
// Example Put1
err = pdbclient.Put(ctx, path, pointer, APIKey)
err = pdbclient.Put(ctx, path, pointer)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("couldn't put pointer in db", zap.Error(err))
@ -63,7 +63,7 @@ func main() {
}
// Example Put2
err = pdbclient.Put(ctx, p.New("fold1/fold2"), pointer, APIKey)
err = pdbclient.Put(ctx, p.New("fold1/fold2"), pointer)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("couldn't put pointer in db", zap.Error(err))
@ -72,7 +72,7 @@ func main() {
}
// Example Get
getRes, err := pdbclient.Get(ctx, path, APIKey)
getRes, err := pdbclient.Get(ctx, path)
if err != nil {
logger.Error("couldn't GET pointer from db", zap.Error(err))
@ -84,7 +84,7 @@ func main() {
// Example List with pagination
prefix := p.New("fold1")
items, more, err := pdbclient.List(ctx, prefix, nil, nil, true, 1, meta.None, APIKey)
items, more, err := pdbclient.List(ctx, prefix, nil, nil, true, 1, meta.None)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("failed to list file paths", zap.Error(err))
@ -97,7 +97,7 @@ func main() {
}
// Example Delete
err = pdbclient.Delete(ctx, path, APIKey)
err = pdbclient.Delete(ctx, path)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("Error in deleteing file from db", zap.Error(err))

View File

@ -124,7 +124,7 @@ func (c Config) action(ctx context.Context, cliCtx *cli.Context,
// TODO(jt): pointerdb.NewClient should dial the pointerdb server with the
// transport client. probably should use the same connection as the
// overlay client
pdb, err := pointerdb.NewClient(c.PointerDBAddr)
pdb, err := pointerdb.NewClient(c.PointerDBAddr, nil)
if err != nil {
return err
}

View File

@ -21,6 +21,7 @@ var (
// PointerDB creates a grpcClient
type PointerDB struct {
grpcClient pb.PointerDBClient
APIKey []byte
}
// a compiler trick to make sure *Overlay implements Client
@ -34,16 +35,16 @@ type ListItem struct {
// Client services offerred for the interface
type Client interface {
Put(ctx context.Context, path p.Path, pointer *pb.Pointer, APIKey []byte) error
Get(ctx context.Context, path p.Path, APIKey []byte) (*pb.Pointer, error)
Put(ctx context.Context, path p.Path, pointer *pb.Pointer) error
Get(ctx context.Context, path p.Path) (*pb.Pointer, error)
List(ctx context.Context, prefix, startAfter, endBefore p.Path,
recursive bool, limit int, metaFlags uint32, APIKey []byte) (
recursive bool, limit int, metaFlags uint32) (
items []ListItem, more bool, err error)
Delete(ctx context.Context, path p.Path, APIKey []byte) error
Delete(ctx context.Context, path p.Path) error
}
// NewClient initializes a new pointerdb client
func NewClient(address string) (*PointerDB, error) {
func NewClient(address string, APIKey []byte) (*PointerDB, error) {
c, err := clientConnection(address, grpc.WithInsecure())
if err != nil {
@ -51,6 +52,7 @@ func NewClient(address string) (*PointerDB, error) {
}
return &PointerDB{
grpcClient: c,
APIKey: APIKey,
}, nil
}
@ -68,19 +70,19 @@ func clientConnection(serverAddr string, opts ...grpc.DialOption) (pb.PointerDBC
}
// Put is the interface to make a PUT request, needs Pointer and APIKey
func (pdb *PointerDB) Put(ctx context.Context, path p.Path, pointer *pb.Pointer, APIKey []byte) (err error) {
func (pdb *PointerDB) Put(ctx context.Context, path p.Path, pointer *pb.Pointer) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = pdb.grpcClient.Put(ctx, &pb.PutRequest{Path: path.String(), Pointer: pointer, APIKey: APIKey})
_, err = pdb.grpcClient.Put(ctx, &pb.PutRequest{Path: path.String(), Pointer: pointer, APIKey: pdb.APIKey})
return err
}
// Get is the interface to make a GET request, needs PATH and APIKey
func (pdb *PointerDB) Get(ctx context.Context, path p.Path, APIKey []byte) (pointer *pb.Pointer, err error) {
func (pdb *PointerDB) Get(ctx context.Context, path p.Path) (pointer *pb.Pointer, err error) {
defer mon.Task()(&ctx)(&err)
res, err := pdb.grpcClient.Get(ctx, &pb.GetRequest{Path: path.String(), APIKey: APIKey})
res, err := pdb.grpcClient.Get(ctx, &pb.GetRequest{Path: path.String(), APIKey: pdb.APIKey})
if err != nil {
return nil, err
}
@ -93,7 +95,7 @@ func (pdb *PointerDB) Get(ctx context.Context, path p.Path, APIKey []byte) (poin
// List is the interface to make a LIST request, needs StartingPathKey, Limit, and APIKey
func (pdb *PointerDB) List(ctx context.Context, prefix, startAfter, endBefore p.Path,
recursive bool, limit int, metaFlags uint32, APIKey []byte) (
recursive bool, limit int, metaFlags uint32) (
items []ListItem, more bool, err error) {
defer mon.Task()(&ctx)(&err)
@ -104,7 +106,7 @@ func (pdb *PointerDB) List(ctx context.Context, prefix, startAfter, endBefore p.
Recursive: recursive,
Limit: int32(limit),
MetaFlags: metaFlags,
APIKey: APIKey,
APIKey: pdb.APIKey,
})
if err != nil {
return nil, false, err
@ -123,10 +125,10 @@ func (pdb *PointerDB) List(ctx context.Context, prefix, startAfter, endBefore p.
}
// Delete is the interface to make a Delete request, needs Path and APIKey
func (pdb *PointerDB) Delete(ctx context.Context, path p.Path, APIKey []byte) (err error) {
func (pdb *PointerDB) Delete(ctx context.Context, path p.Path) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = pdb.grpcClient.Delete(ctx, &pb.DeleteRequest{Path: path.String(), APIKey: APIKey})
_, err = pdb.grpcClient.Delete(ctx, &pb.DeleteRequest{Path: path.String(), APIKey: pdb.APIKey})
return err
}

View File

@ -96,12 +96,12 @@ func TestPut(t *testing.T) {
errTag := fmt.Sprintf("Test case #%d", i)
gc := NewMockPointerDBClient(ctrl)
pdb := PointerDB{grpcClient: gc}
pdb := PointerDB{grpcClient: gc, APIKey: tt.APIKey}
// here we don't care what type of context we pass
gc.EXPECT().Put(gomock.Any(), &putRequest).Return(nil, tt.err)
err := pdb.Put(ctx, tt.path, putRequest.Pointer, tt.APIKey)
err := pdb.Put(ctx, tt.path, putRequest.Pointer)
if err != nil {
assert.EqualError(t, err, tt.errString, errTag)
@ -142,11 +142,11 @@ func TestGet(t *testing.T) {
errTag := fmt.Sprintf("Test case #%d", i)
gc := NewMockPointerDBClient(ctrl)
pdb := PointerDB{grpcClient: gc}
pdb := PointerDB{grpcClient: gc, APIKey: tt.APIKey}
gc.EXPECT().Get(gomock.Any(), &getRequest).Return(&getResponse, tt.err)
pointer, err := pdb.Get(ctx, tt.path, tt.APIKey)
pointer, err := pdb.Get(ctx, tt.path)
if err != nil {
assert.EqualError(t, err, tt.errString, errTag)
@ -169,7 +169,7 @@ func TestList(t *testing.T) {
recursive bool
limit int
metaFlags uint32
apiKey string
APIKey string
items []*pb.ListResponse_Item
more bool
err error
@ -211,18 +211,18 @@ func TestList(t *testing.T) {
Recursive: tt.recursive,
Limit: int32(tt.limit),
MetaFlags: tt.metaFlags,
APIKey: []byte(tt.apiKey),
APIKey: []byte(tt.APIKey),
}
listResponse := pb.ListResponse{Items: tt.items, More: tt.more}
gc := NewMockPointerDBClient(ctrl)
pdb := PointerDB{grpcClient: gc}
pdb := PointerDB{grpcClient: gc, APIKey: []byte(tt.APIKey)}
gc.EXPECT().List(gomock.Any(), &listRequest).Return(&listResponse, tt.err)
items, more, err := pdb.List(ctx, p.New(tt.prefix), p.New(tt.startAfter),
p.New(tt.endBefore), tt.recursive, tt.limit, tt.metaFlags, []byte(tt.apiKey))
p.New(tt.endBefore), tt.recursive, tt.limit, tt.metaFlags)
if err != nil {
assert.EqualError(t, err, tt.errString, errTag)
@ -267,11 +267,11 @@ func TestDelete(t *testing.T) {
errTag := fmt.Sprintf("Test case #%d", i)
gc := NewMockPointerDBClient(ctrl)
pdb := PointerDB{grpcClient: gc}
pdb := PointerDB{grpcClient: gc, APIKey: tt.APIKey}
gc.EXPECT().Delete(gomock.Any(), &deleteRequest).Return(nil, tt.err)
err := pdb.Delete(ctx, tt.path, tt.APIKey)
err := pdb.Delete(ctx, tt.path)
if err != nil {
assert.EqualError(t, err, tt.errString, errTag)

View File

@ -75,7 +75,7 @@ func (s *segmentStore) Meta(ctx context.Context, path paths.Path) (meta Meta,
err error) {
defer mon.Task()(&ctx)(&err)
pr, err := s.pdb.Get(ctx, path, nil)
pr, err := s.pdb.Get(ctx, path)
if err != nil {
return Meta{}, Error.Wrap(err)
}
@ -129,7 +129,7 @@ func (s *segmentStore) Put(ctx context.Context, path paths.Path, data io.Reader,
}
// puts pointer to pointerDB
err = s.pdb.Put(ctx, path, p, nil)
err = s.pdb.Put(ctx, path, p)
if err != nil {
return Meta{}, Error.Wrap(err)
}
@ -178,7 +178,7 @@ func (s *segmentStore) Get(ctx context.Context, path paths.Path) (
rr ranger.RangeCloser, meta Meta, err error) {
defer mon.Task()(&ctx)(&err)
pr, err := s.pdb.Get(ctx, path, nil)
pr, err := s.pdb.Get(ctx, path)
if err != nil {
return nil, Meta{}, Error.Wrap(err)
}
@ -206,7 +206,7 @@ func (s *segmentStore) Get(ctx context.Context, path paths.Path) (
func (s *segmentStore) Delete(ctx context.Context, path paths.Path) (err error) {
defer mon.Task()(&ctx)(&err)
pr, err := s.pdb.Get(ctx, path, nil)
pr, err := s.pdb.Get(ctx, path)
if err != nil {
return Error.Wrap(err)
}
@ -227,7 +227,7 @@ func (s *segmentStore) Delete(ctx context.Context, path paths.Path) (err error)
}
// deletes pointer from pointerdb
return s.pdb.Delete(ctx, path, nil)
return s.pdb.Delete(ctx, path)
}
// lookupNodes calls Lookup to get node addresses from the overlay
@ -253,7 +253,7 @@ func (s *segmentStore) List(ctx context.Context, prefix, startAfter,
defer mon.Task()(&ctx)(&err)
pdbItems, more, err := s.pdb.List(ctx, prefix, startAfter, endBefore,
recursive, limit, metaFlags, nil)
recursive, limit, metaFlags)
if err != nil {
return nil, false, err
}