refactor: Protobuf messages

This commit is contained in:
Ochieng' Got
2020-02-06 12:05:01 +01:00
parent ae5283e1d4
commit 1832a3a763
7 changed files with 1960 additions and 59 deletions
+23 -6
View File
@@ -3,6 +3,7 @@ package app
import (
"context"
"fmt"
"github.com/spotify/backstage/inventory/storage"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@@ -18,24 +19,40 @@ type Server struct {
func (s *Server) CreateEntity(ctx context.Context, req *pb.CreateEntityRequest) (*pb.CreateEntityReply, error) {
err := s.Storage.CreateEntity(req.GetEntity().GetUri())
if err != nil {
return nil, status.Error(codes.Internal, "could not create entity")
return nil, status.Error(codes.Internal, "could not create entity")
}
return &pb.CreateEntityReply{Entity: req.GetEntity()} , nil
return &pb.CreateEntityReply{Entity: req.GetEntity()}, nil
}
// GetEntity returns an inventory Entity with the selected facts
func (s *Server) GetEntity(ctx context.Context, req *pb.GetEntityRequest) (*pb.GetEntityReply, error) {
entityUri, err := s.Storage.GetEntity(req.GetEntity().GetUri())
numberOfIncludedFacts := len(req.GetIncludeFacts())
var facts = make([]pb.Fact, numberOfIncludedFacts)
entityURI, err := s.Storage.GetEntity(req.GetEntity().GetUri())
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("could not get entity %v", err))
}
return &pb.GetEntityReply{Entity: &pb.Entity{Uri: entityUri}}, nil
for _, factName := range req.GetIncludeFacts() {
value, err := s.Storage.GetFact(entityURI, factName)
// TODO: handle errors
append(facts, pb.Fact{Name: factName, Value: value})
}
return &pb.GetEntityReply{Entity: &pb.Entity{Uri: entityURI}, Facts: facts}, nil
}
func (s *Server) SetFact(ctx context.Context, req *pb.SetFactRequest) (*pb.SetFactReply, error) {
factUri, err := s.Storage.SetFact(req.EntityUri, req.Name, req.Value)
err := s.Storage.SetFact(req.EntityUri, req.Name, req.Value)
if err != nil {
return nil, status.Error(codes.Internal, "could not set fact")
}
return &pb.SetFactReply{Fact: &pb.Fact{Uri: factUri, Value: req.Value}} , nil
return &pb.SetFactReply{Fact: &pb.Fact{Name: req.GetName(), Value: req.GetValue()}}, nil
}
func (s *Server) GetFact(ctx context.Context, req *pb.GetFactRequest) (*pb.GetFactReply, error) {
val, err := s.Storage.GetFact(req.EntityUri, req.Name)
if err != nil {
return nil, status.Error(codes.Internal, "could not set fact")
}
return &pb.GetFactReply{Fact: &pb.Fact{Name: req.GetName(), Value: val}}, nil
}
+12 -12
View File
@@ -3,12 +3,13 @@ package app
import (
"context"
"fmt"
"github.com/spotify/backstage/inventory/storage"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/spotify/backstage/inventory/storage"
pb "github.com/spotify/backstage/proto/inventory/v1"
)
@@ -23,7 +24,7 @@ func TestServerCreateEntity(t *testing.T) {
if err != nil {
t.Errorf("ServerTest(CreateEntity) got unexpected error %v", err)
}
if resp.GetEntity().GetUri() != entity.GetUri() {
if resp.GetEntity().GetUri() != entity.GetUri() {
t.Errorf("ServerTest(CreateEntity) expected %v, but got %v", entity.GetUri(), resp.GetEntity().GetUri())
}
}
@@ -59,7 +60,6 @@ func TestServerGetEntityWithIncludedFacts(t *testing.T) {
setFactReq := &pb.SetFactRequest{EntityUri: entityUri, Name: "test-name", Value: "test-value"}
s.SetFact(context.Background(), setFactReq)
entity := &pb.Entity{Uri: entityUri}
req := &pb.GetEntityRequest{Entity: entity, IncludeFacts: []string{"test-name"}}
@@ -70,7 +70,7 @@ func TestServerGetEntityWithIncludedFacts(t *testing.T) {
if resp == nil {
t.Errorf("ServerTest(GetEntity) returned nil")
}
expectedFacts := []*pb.Fact{{Uri: "boss://test/test/test-name", Value: "test-value"}}
expectedFacts := []*pb.Fact{{Name: "test-name", Value: "test-value"}}
if !reflect.DeepEqual(resp.GetFacts(), expectedFacts) {
t.Errorf("ServerTest(GetEntity) got %v, wanted %v", resp.GetFacts(), expectedFacts)
}
@@ -93,9 +93,9 @@ func TestServerSetFactForExistingEntity(t *testing.T) {
if resp == nil {
t.Errorf("ServerTest(SetFact) returned nil")
}
fact := &pb.Fact{Uri: entity.GetUri() + "/" + req.Name, Value: "test-value"}
fact := &pb.Fact{Name: req.GetName(), Value: req.GetValue()}
if !reflect.DeepEqual(resp.GetFact(), fact) {
t.Errorf("ServerTest(SetFact) got %v, wanted %v", resp.GetFact() , fact)
t.Errorf("ServerTest(SetFact) got %v, wanted %v", resp.GetFact(), fact)
}
}
@@ -104,8 +104,8 @@ func TestServerSetFactForNonExistingEntity(t *testing.T) {
defer testStorage.Close()
s := Server{Storage: testStorage.Storage}
entityUri := "boss://test/test"
req := &pb.SetFactRequest{EntityUri: entityUri, Name: "test-name", Value: "test-value"}
entityURI := "boss://test/test"
req := &pb.SetFactRequest{EntityUri: entityURI, Name: "test-name", Value: "test-value"}
resp, err := s.SetFact(context.Background(), req)
if err != nil {
t.Errorf("ServerTest(SetFact) got unexpected error %v", err)
@@ -113,15 +113,16 @@ func TestServerSetFactForNonExistingEntity(t *testing.T) {
if resp == nil {
t.Errorf("ServerTest(SetFact) returned nil")
}
fact := &pb.Fact{Uri: entityUri + "/" + req.Name, Value: "test-value"}
fact := &pb.Fact{Name: req.GetName(), Value: req.GetValue()}
if !reflect.DeepEqual(resp.GetFact(), fact) {
t.Errorf("ServerTest(SetFact) got %v, wanted %v", resp.GetFact() , fact)
t.Errorf("ServerTest(SetFact) got %v, wanted %v", resp.GetFact(), fact)
}
}
type TestStorage struct {
Storage *storage.Storage
Path string
Path string
}
// NewTestStorage returns a TestStorage using a temporary path.
@@ -143,4 +144,3 @@ func (db *TestStorage) Close() {
defer os.Remove(db.Path)
db.Storage.Close()
}
-3
View File
@@ -8,8 +8,5 @@ require (
github.com/golang/protobuf v1.3.3
github.com/spotify/backstage/proto v0.0.0-00010101000000-000000000000
go.etcd.io/bbolt v1.3.3
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect
google.golang.org/grpc v1.27.0
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect
)
+2 -7
View File
@@ -37,8 +37,8 @@ func (s *Storage) Close() error {
return s.db.Close()
}
func (s *Storage) SetFact(entityUri, name, value string) (factUri string, err error) {
err = s.db.Update(func(tx *bbolt.Tx) error {
func (s *Storage) SetFact(entityUri, name, value string) (err error) {
return s.db.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(entityUri))
if err != nil {
return err
@@ -49,11 +49,6 @@ func (s *Storage) SetFact(entityUri, name, value string) (factUri string, err er
}
return nil
})
if err != nil {
return "", err
}
return entityUri + "/" + name, nil
}
func (s *Storage) GetFact(entityUri, name string) (string, error) {
+29 -29
View File
@@ -416,8 +416,8 @@ func (m *Entity) GetUri() string {
}
type Fact struct {
Uri string `protobuf:"bytes,1,opt,name=uri,proto3" json:"uri,omitempty"`
Value string `protobuf:"bytes,3,opt,name=value,proto3" json:"value,omitempty"`
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@@ -448,9 +448,9 @@ func (m *Fact) XXX_DiscardUnknown() {
var xxx_messageInfo_Fact proto.InternalMessageInfo
func (m *Fact) GetUri() string {
func (m *Fact) GetName() string {
if m != nil {
return m.Uri
return m.Name
}
return ""
}
@@ -478,32 +478,32 @@ func init() {
func init() { proto.RegisterFile("inventory/v1/inventory.proto", fileDescriptor_70be9028e322f9d8) }
var fileDescriptor_70be9028e322f9d8 = []byte{
// 386 bytes of a gzipped FileDescriptorProto
// 390 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x54, 0x4d, 0x4f, 0xc2, 0x40,
0x10, 0x0d, 0x14, 0x30, 0x1d, 0x3e, 0x82, 0xab, 0x87, 0xa6, 0x21, 0x86, 0xac, 0xc6, 0x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94, 0xab, 0x17, 0x32, 0x9b, 0xa0, 0x24, 0xbd, 0x91, 0xef,
0xf0, 0xa5, 0x8a, 0xb5, 0x2a, 0x20, 0x04, 0x45, 0xd7, 0x5e, 0x10, 0xb5, 0xc0, 0x1b, 0xfc, 0x8c,
0x4e, 0xa1, 0x14, 0xd9, 0xf3, 0x90, 0xa8, 0x32, 0x2f, 0x26, 0x17, 0xfc, 0x08, 0xd5, 0x74, 0x33,
0x53, 0x7a, 0x0d, 0x45, 0x66, 0x8f, 0xd0, 0xb9, 0x9d, 0xa1, 0x1c, 0x81, 0x07, 0xfc, 0xed, 0xee,
0xa5, 0x91, 0xa9, 0x31, 0x0f, 0xa3, 0x46, 0x83, 0x72, 0xe2, 0x21, 0x6a, 0x80, 0x1c, 0xa6, 0xfc,
0xec, 0x88, 0xbb, 0x50, 0x64, 0x93, 0xa9, 0x02, 0x69, 0x93, 0x4b, 0x85, 0x8c, 0x4b, 0xbd, 0x6f,
0x19, 0x94, 0xa7, 0x5f, 0x26, 0xb4, 0x00, 0x25, 0xcd, 0x31, 0xea, 0xe6, 0x89, 0xfa, 0xfb, 0xb1,
0x69, 0xfa, 0x0e, 0x08, 0x66, 0x42, 0x04, 0xd5, 0x6c, 0xa2, 0x50, 0x3f, 0x0f, 0xbf, 0x21, 0xd6,
0x9a, 0xb1, 0x1b, 0x88, 0xf1, 0xce, 0xe0, 0x48, 0x44, 0x03, 0xe5, 0x4a, 0x5e, 0x4f, 0xa7, 0x76,
0xb5, 0xf5, 0xbc, 0x20, 0x32, 0xb7, 0x25, 0x32, 0x77, 0x24, 0xca, 0xc6, 0x69, 0x50, 0x7b, 0xa9,
0xa4, 0xcd, 0xc8, 0x18, 0x97, 0xf9, 0x0f, 0xb3, 0xff, 0x13, 0x00, 0x00, 0xff, 0xff, 0x4f, 0xab,
0xd5, 0x54, 0x50, 0x05, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
+1892
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -53,6 +53,6 @@ message Entity {
}
message Fact {
string uri = 1;
string value = 3;
string name = 1;
string value = 2;
}