Skip to content

Commit

Permalink
feat: add document service
Browse files Browse the repository at this point in the history
  • Loading branch information
medgar-nitric authored and tjholm committed Jul 1, 2021
1 parent 01fa66c commit 7a4577a
Show file tree
Hide file tree
Showing 75 changed files with 4,209 additions and 2,806 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ jobs:
uses: arduino/setup-protoc@v1
- name: Install modules
run: make install-tools
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Setup local DynamoDB
run: make install-test-tools
- name: Setup Cloud SDK
uses: google-github-actions/setup-gcloud@master
- name: Install Google Cloud SDK components
run: yes | gcloud components install beta cloud-firestore-emulator
- name: Check Source License Headers
run: make license-header-check
- name: Run Tests
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ bin/
*.coverprofile

# Ignore proto generated interfaces
interfaces/
interfaces/

# GCP integration testing key
plugins/document/firestore/gcp-firestore-testing.json
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ provider as an alternative to the fixed set of plugins in the static membranes.
- Make
- Docker
- Google Protocol Buffers Compiler
- Google Cloud SDK (for Firestore testing)
- JRE (for DynamoDB testing)

### Getting Started

Expand All @@ -89,12 +91,27 @@ provider as an alternative to the fixed set of plugins in the static membranes.
make install-tools
```

##### Protocol Buffers
#### Install integration testing tools
```bash
make install-test-tools
```

##### Install Protocol Buffers
Download the Google Protobuf Compiler (standalone binary called `protoc`) from https://github.com/protocolbuffers/protobuf and add it to your $PATH.

> On Mac OS with Homebrew, you can run `brew install protobuf`
#### Run tests
##### Install Google Cloud SDK
Install the Google Cloud SDK following in the instructions at: https://cloud.google.com/sdk/docs/install

#### Install JRE
Install a Java Runtime Environment (JRE) version 11 or later for your OS. For example on Ubuntu Linux run:

```bash
sudo apt-get install openjdk-11-jdk
```

### Run tests
```bash
make tests
```
Expand Down
62 changes: 0 additions & 62 deletions adapters/grpc/auth_grpc.go

This file was deleted.

166 changes: 166 additions & 0 deletions adapters/grpc/document_grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright 2021 Nitric Pty Ltd.
//
// 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 grpc

import (
"context"

pb "github.com/nitric-dev/membrane/interfaces/nitric/v1"
"github.com/nitric-dev/membrane/sdk"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/structpb"
)

// GRPC Interface for registered Nitric Document Plugin
type DocumentServer struct {
pb.UnimplementedDocumentServer
// TODO: Support multiple plugin registrations
// Just need to settle on a way of addressing them on calls
documentPlugin sdk.DocumentService
}

func (s *DocumentServer) checkPluginRegistered() (bool, error) {
if s.documentPlugin == nil {
return false, status.Errorf(codes.Unimplemented, "Document plugin not registered")
}

return true, nil
}

func (s *DocumentServer) Set(ctx context.Context, req *pb.DocSetRequest) (*pb.DocSetResponse, error) {
if ok, err := s.checkPluginRegistered(); ok {
key := toSdkKey(req.Key)
subKey := toSdkKey(req.SubKey)

if err := s.documentPlugin.Set(*key, subKey, req.GetValue().AsMap()); err == nil {
return &pb.DocSetResponse{}, nil
} else {
// Case: Failed to create the key
// TODO: Translate from internal Document Service Error
return nil, err
}
} else {
// Case: Plugin was not registered
return nil, err
}
}

func (s *DocumentServer) Get(ctx context.Context, req *pb.DocGetRequest) (*pb.DocGetResponse, error) {
if ok, err := s.checkPluginRegistered(); ok {
key := toSdkKey(req.Key)
subKey := toSdkKey(req.SubKey)

if val, err := s.documentPlugin.Get(*key, subKey); err == nil {
if valStruct, err := structpb.NewStruct(val); err == nil {
return &pb.DocGetResponse{
Value: valStruct,
}, nil
} else {
// Case: Failed to create PB struct from stored value
// TODO: Translate from internal Document Plugin Error
return nil, err
}
} else {
// Case: There was an error retrieving the keyvalue
// TODO: Translate from internal Document Plugin Error
return nil, err
}
} else {
// Case: The Document plugin was not registered
// TODO: Translate from internal Document Plugin Error
return nil, err
}
}

func (s *DocumentServer) Delete(ctx context.Context, req *pb.DocDeleteRequest) (*pb.DocDeleteResponse, error) {
if ok, err := s.checkPluginRegistered(); ok {
key := toSdkKey(req.Key)
subKey := toSdkKey(req.SubKey)

if err := s.documentPlugin.Delete(*key, subKey); err == nil {
return &pb.DocDeleteResponse{}, nil
} else {
// Case: Failed to create the keyvalue
// TODO: Translate from internal Document Plugin Error
return nil, err
}
} else {
// Case: Plugin was not registered
return nil, err
}
}

func (s *DocumentServer) Query(ctx context.Context, req *pb.DocQueryRequest) (*pb.DocQueryResponse, error) {
if ok, err := s.checkPluginRegistered(); ok {
key := toSdkKey(req.Key)
subcoll := req.GetSubCollection()
expressions := make([]sdk.QueryExpression, len(req.GetExpressions()))
for i, exp := range req.GetExpressions() {
expressions[i] = sdk.QueryExpression{
Operand: exp.GetOperand(),
Operator: exp.GetOperator(),
Value: exp.GetValue(),
}
}
limit := int(req.GetLimit())
pagingMap := req.GetPagingToken()

if qr, err := s.documentPlugin.Query(*key, subcoll, expressions, limit, pagingMap); err == nil {

valStructs := make([]*structpb.Struct, len(qr.Data))
for i, valMap := range qr.Data {
if valStruct, err := structpb.NewStruct(valMap); err == nil {
valStructs[i] = valStruct

} else {
// Case: Failed to create PB struct from stored value
// TODO: Translate from internal Document Plugin Error
return nil, err
}
}

return &pb.DocQueryResponse{
Values: valStructs,
PagingToken: qr.PagingToken,
}, nil

} else {
// Case: Failed to create the keyvalue
// TODO: Translate from internal Document Plugin Error
return nil, err
}

} else {
// Case: Plugin was not registered
return nil, err
}
}

func NewDocumentServer(docPlugin sdk.DocumentService) pb.DocumentServer {
return &DocumentServer{
documentPlugin: docPlugin,
}
}

func toSdkKey(key *pb.Key) *sdk.Key {
if key != nil {
return &sdk.Key{
Collection: key.GetCollection(),
Id: key.GetId(),
}
}
return nil
}
2 changes: 1 addition & 1 deletion contracts
25 changes: 12 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@ require (
cloud.google.com/go/firestore v1.5.0
cloud.google.com/go/pubsub v1.3.1
cloud.google.com/go/storage v1.10.0
firebase.google.com/go v3.13.0+incompatible
firebase.google.com/go/v4 v4.5.0
github.com/Azure/azure-sdk-for-go v51.3.0+incompatible
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/DataDog/zstd v1.4.8 // indirect
github.com/Knetic/govaluate v3.0.0+incompatible
github.com/Sereal/Sereal v0.0.0-20200820125258-a016b7cda3f3 // indirect
github.com/asdine/storm v2.1.2+incompatible
github.com/aws/aws-lambda-go v1.20.0
github.com/aws/aws-sdk-go v1.36.12
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/garyburd/redigo v1.6.2 // indirect
github.com/golang/protobuf v1.5.2
github.com/google/addlicense v0.0.0-20210428195630-6d92264d7170 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/addlicense v0.0.0-20210428195630-6d92264d7170
github.com/google/uuid v1.1.2
github.com/googleapis/gax-go/v2 v2.0.5
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 // indirect
github.com/mitchellh/golicense v0.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.1
github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975
github.com/onsi/ginkgo v1.16.4
Expand All @@ -34,16 +29,20 @@ require (
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/uw-labs/lichen v0.1.4 // indirect
github.com/uw-labs/lichen v0.1.4
github.com/valyala/fasthttp v1.23.0
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
github.com/vmihailenco/msgpack v3.3.3+incompatible // indirect
go.etcd.io/bbolt v1.3.5
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/tools v0.1.2 // indirect
golang.org/x/tools v0.1.4
google.golang.org/api v0.40.0
google.golang.org/genproto v0.0.0-20210222152913-aa3ee6e6a81c
google.golang.org/grpc v1.35.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
google.golang.org/protobuf v1.26.0
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit 7a4577a

Please sign in to comment.