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

Transform service sdk to accept cosmos service step 1 #1250

Merged
merged 4 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sdk/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const (
type Event struct {
ps *pubsub.PubSub
instance *instancesdk.Instance
service *servicesdk.Service
service servicesdk.Service
}

// New creates a new Event SDK with given options.
func New(ps *pubsub.PubSub, service *servicesdk.Service, instance *instancesdk.Instance) *Event {
func New(ps *pubsub.PubSub, service servicesdk.Service, instance *instancesdk.Instance) *Event {
return &Event{
ps: ps,
service: service,
Expand Down
4 changes: 2 additions & 2 deletions sdk/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const (
// Execution exposes execution APIs of MESG.
type Execution struct {
ps *pubsub.PubSub
service *servicesdk.Service
service servicesdk.Service
instance *instancesdk.Instance
workflow *workflowsdk.Workflow
execDB database.ExecutionDB
}

// New creates a new Execution SDK with given options.
func New(ps *pubsub.PubSub, service *servicesdk.Service, instance *instancesdk.Instance, workflow *workflowsdk.Workflow, execDB database.ExecutionDB) *Execution {
func New(ps *pubsub.PubSub, service servicesdk.Service, instance *instancesdk.Instance, workflow *workflowsdk.Workflow, execDB database.ExecutionDB) *Execution {
return &Execution{
ps: ps,
service: service,
Expand Down
2 changes: 1 addition & 1 deletion sdk/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func newTesting(t *testing.T) (*Execution, *apiTesting) {
serviceStore, err := store.NewLevelDBStore(servicedbname)
require.NoError(t, err)
db := database.NewServiceDB(serviceStore)
service := servicesdk.New(container, db)
service := servicesdk.NewDeprecated(container, db)

instDB, err := database.NewInstanceDB(instdbname)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions sdk/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
// Instance exposes service instance APIs of MESG.
type Instance struct {
container container.Container
service *servicesdk.Service
service servicesdk.Service
instanceDB database.InstanceDB

endpoint string
engineName string
}

// New creates a new Instance SDK with given options.
func New(c container.Container, service *servicesdk.Service, instanceDB database.InstanceDB, engineName, port string) *Instance {
func New(c container.Container, service servicesdk.Service, instanceDB database.InstanceDB, engineName, port string) *Instance {
return &Instance{
container: c,
service: service,
Expand Down
4 changes: 2 additions & 2 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// SDK exposes all functionalities of MESG Engine.
type SDK struct {
Service *servicesdk.Service
Service servicesdk.Service
Instance *instancesdk.Instance
Execution *executionsdk.Execution
Event *eventsdk.Event
Expand All @@ -23,7 +23,7 @@ type SDK struct {
// New creates a new SDK with given options.
func New(c container.Container, serviceDB *database.ServiceDB, instanceDB database.InstanceDB, execDB database.ExecutionDB, workflowDB database.WorkflowDB, engineName, port string) *SDK {
ps := pubsub.New(0)
serviceSDK := servicesdk.New(c, serviceDB)
serviceSDK := servicesdk.NewDeprecated(c, serviceDB)
instanceSDK := instancesdk.New(c, serviceSDK, instanceDB, engineName, port)
workflowSDK := workflowsdk.New(instanceSDK, workflowDB)
executionSDK := executionsdk.New(ps, serviceSDK, instanceSDK, workflowSDK, execDB)
Expand Down
32 changes: 9 additions & 23 deletions sdk/service/service.go → sdk/service/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package servicesdk

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/cskr/pubsub"
"github.com/docker/docker/pkg/archive"
"github.com/mesg-foundation/engine/container"
"github.com/mesg-foundation/engine/database"
Expand All @@ -17,25 +15,22 @@ import (
"github.com/mesg-foundation/engine/service/validator"
)

// Service exposes service APIs of MESG.
type Service struct {
ps *pubsub.PubSub

// deprecated exposes service APIs of MESG.
type deprecated struct {
container container.Container
serviceDB *database.ServiceDB
}

// New creates a new Service SDK with given options.
func New(c container.Container, serviceDB *database.ServiceDB) *Service {
return &Service{
ps: pubsub.New(0),
// NewDeprecated creates a new Service SDK with given options.
func NewDeprecated(c container.Container, serviceDB *database.ServiceDB) Service {
return &deprecated{
container: c,
serviceDB: serviceDB,
}
}

// Create creates a new service from definition.
func (s *Service) Create(srv *service.Service) (*service.Service, error) {
func (s *deprecated) Create(srv *service.Service) (*service.Service, error) {
if srv.Configuration == nil {
srv.Configuration = &service.Configuration{}
}
Expand Down Expand Up @@ -91,25 +86,16 @@ func (s *Service) Create(srv *service.Service) (*service.Service, error) {
}

// Delete deletes the service by hash.
func (s *Service) Delete(hash hash.Hash) error {
func (s *deprecated) Delete(hash hash.Hash) error {
return s.serviceDB.Delete(hash)
}

// Get returns the service that matches given hash.
func (s *Service) Get(hash hash.Hash) (*service.Service, error) {
func (s *deprecated) Get(hash hash.Hash) (*service.Service, error) {
return s.serviceDB.Get(hash)
}

// List returns all services.
func (s *Service) List() ([]*service.Service, error) {
func (s *deprecated) List() ([]*service.Service, error) {
return s.serviceDB.All()
}

// AlreadyExistsError is an not found error.
type AlreadyExistsError struct {
Hash hash.Hash
}

func (e *AlreadyExistsError) Error() string {
return fmt.Sprintf("service %q already exists", e.Hash.String())
}
25 changes: 25 additions & 0 deletions sdk/service/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package servicesdk

import (
"fmt"

"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/service"
)

// Service is the interface of this sdk
type Service interface {
Create(srv *service.Service) (*service.Service, error)
Delete(hash hash.Hash) error
Get(hash hash.Hash) (*service.Service, error)
List() ([]*service.Service, error)
}

// AlreadyExistsError is an not found error.
type AlreadyExistsError struct {
Hash hash.Hash
}

func (e *AlreadyExistsError) Error() string {
return fmt.Sprintf("service %q already exists", e.Hash.String())
}