Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get service new API #1067

Merged
merged 11 commits into from
Jun 21, 2019
113 changes: 95 additions & 18 deletions protobuf/api/service.pb.go

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

8 changes: 8 additions & 0 deletions protobuf/api/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ package api;
// right now we have conflicts with api.Service.
service ServiceX {
rpc Create (CreateServiceRequest) returns (CreateServiceResponse) {}

// Get returns a single Service specified in a request.
rpc Get(GetServiceRequest) returns (definition.Service) {}
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
}

message CreateServiceRequest {
Expand All @@ -18,3 +21,8 @@ message CreateServiceResponse {
string sid = 1;
string hash = 2;
}

// GetServiceRequest defines request to retrieve a single service.
message GetServiceRequest {
string hash = 1;
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions sdk/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ func (s *Service) Create(srv *service.Service) error {

return s.db.Save(srv)
}

// Get returns the service that matches given hash.
func (s *Service) Get(hash string) (*service.Service, error) {
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
return s.db.Get(hash)
}
108 changes: 108 additions & 0 deletions server/grpc/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,111 @@ func fromProtoDependencies(deps []*definition.Dependency) []*service.Dependency
}
return ds
}

// ToProtoServices converts internal services struct to their protobuf definition
// TODO: should not be public. Need to move server/grpc/service.go to server/grpc/api/service.go and delete server/grpc/core package
func ToProtoServices(ss []*service.Service) []*definition.Service {
services := make([]*definition.Service, len(ss))
for i, s := range ss {
services[i] = ToProtoService(s)
}
return services
}

// ToProtoService converts an internal service struct to the protobuf definition
// TODO: should not be public. Need to move server/grpc/service.go to server/grpc/api/service.go and delete server/grpc/core package
func ToProtoService(s *service.Service) *definition.Service {
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
return &definition.Service{
Hash: s.Hash,
Sid: s.Sid,
Name: s.Name,
Description: s.Description,
Repository: s.Repository,
Source: s.Source,
Tasks: toProtoTasks(s.Tasks),
Events: toProtoEvents(s.Events),
Configuration: toProtoConfiguration(s.Configuration),
Dependencies: toProtoDependencies(s.Dependencies),
}
}

func toProtoTasks(tasks []*service.Task) []*definition.Task {
ts := make([]*definition.Task, len(tasks))
for i, task := range tasks {
ts[i] = &definition.Task{
Key: task.Key,
Name: task.Name,
Description: task.Description,
Inputs: toProtoParameters(task.Inputs),
Outputs: toProtoParameters(task.Outputs),
}
}
return ts
}

func toProtoEvents(events []*service.Event) []*definition.Event {
es := make([]*definition.Event, len(events))
for i, event := range events {
es[i] = &definition.Event{
Key: event.Key,
Name: event.Name,
Description: event.Description,
Data: toProtoParameters(event.Data),
}
}
return es
}

func toProtoParameters(params []*service.Parameter) []*definition.Parameter {
ps := make([]*definition.Parameter, len(params))
for i, param := range params {
ps[i] = &definition.Parameter{
Key: param.Key,
Name: param.Name,
Description: param.Description,
Type: param.Type,
Repeated: param.Repeated,
Optional: param.Optional,
Object: toProtoParameters(param.Object),
}
}
return ps
}

func toProtoConfiguration(configuration *service.Dependency) *definition.Configuration {
if configuration == nil {
return nil
}
return &definition.Configuration{
Args: configuration.Args,
Command: configuration.Command,
Ports: configuration.Ports,
Volumes: configuration.Volumes,
VolumesFrom: configuration.VolumesFrom,
Env: configuration.Env,
}
}

func toProtoDependency(dep *service.Dependency) *definition.Dependency {
if dep == nil {
return nil
}
return &definition.Dependency{
Key: dep.Key,
Image: dep.Image,
Volumes: dep.Volumes,
VolumesFrom: dep.VolumesFrom,
Ports: dep.Ports,
Command: dep.Command,
Args: dep.Args,
Env: dep.Env,
}
}

func toProtoDependencies(deps []*service.Dependency) []*definition.Dependency {
ds := make([]*definition.Dependency, len(deps))
for i, dep := range deps {
ds[i] = toProtoDependency(dep)
}
return ds
}
5 changes: 3 additions & 2 deletions server/grpc/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/mesg-foundation/core/sdk"
eventsdk "github.com/mesg-foundation/core/sdk/event"
executionsdk "github.com/mesg-foundation/core/sdk/execution"
"github.com/mesg-foundation/core/server/grpc/api"
"github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/version"
"github.com/mesg-foundation/core/x/xerrors"
Expand All @@ -40,7 +41,7 @@ func (s *Server) GetService(ctx context.Context, request *coreapi.GetServiceRequ
return nil, err
}
details := &coreapi.Service{
Definition: toProtoService(ss),
Definition: api.ToProtoService(ss),
Status: toProtoServiceStatusType(status),
}
return &coreapi.GetServiceReply{Service: details}, nil
Expand Down Expand Up @@ -72,7 +73,7 @@ func (s *Server) ListServices(ctx context.Context, request *coreapi.ListServices
return
}
details := &coreapi.Service{
Definition: toProtoService(ss),
Definition: api.ToProtoService(ss),
Status: toProtoServiceStatusType(status),
}
mp.Lock()
Expand Down
3 changes: 2 additions & 1 deletion server/grpc/core/core_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/mesg-foundation/core/config"
"github.com/mesg-foundation/core/protobuf/coreapi"
executionsdk "github.com/mesg-foundation/core/sdk/execution"
"github.com/mesg-foundation/core/server/grpc/api"
"github.com/mesg-foundation/core/service"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestListServices(t *testing.T) {
services, err := server.sdk.ListServices()
require.NoError(t, err)

apiProtoServices := toProtoServices(services)
apiProtoServices := api.ToProtoServices(services)

require.Len(t, apiProtoServices, 1)
require.Equal(t, reply.Services[0].Definition.Hash, apiProtoServices[0].Hash)
Expand Down
Loading