From 984195dc23de4278127ebb6637fbda0cf2c60f4b Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Wed, 4 Sep 2019 18:26:53 +0700 Subject: [PATCH] Use CreateServiceRequest struct to create a service instead of the service struct itself --- config/services.go | 10 +++++----- sdk/service/deprecated.go | 5 +++-- sdk/service/module.go | 5 +++-- sdk/service/sdk.go | 3 ++- sdk/service/type.go | 3 ++- sdk/service/utils.go | 18 ++++++++++++++++-- server/grpc/api/service.go | 13 +------------ 7 files changed, 32 insertions(+), 25 deletions(-) diff --git a/config/services.go b/config/services.go index d66eca2db..5505e4b01 100644 --- a/config/services.go +++ b/config/services.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/mesg-foundation/engine/instance" - "github.com/mesg-foundation/engine/service" + "github.com/mesg-foundation/engine/protobuf/api" ) // Default compiled version of the service. These compiled versions are overritten by the build @@ -30,7 +30,7 @@ var ( type ServiceConfig struct { Key string Env map[string]string - Definition *service.Service + Definition *api.CreateServiceRequest Instance *instance.Instance } @@ -58,13 +58,13 @@ func (c *Config) setupServices() error { } func (c *Config) createServiceConfig(key string, compilatedJSON string, env map[string]string) (*ServiceConfig, error) { - var srv service.Service - if err := json.Unmarshal([]byte(compilatedJSON), &srv); err != nil { + var req api.CreateServiceRequest + if err := json.Unmarshal([]byte(compilatedJSON), &req); err != nil { return nil, err } return &ServiceConfig{ Key: key, - Definition: &srv, + Definition: &req, Env: env, }, nil } diff --git a/sdk/service/deprecated.go b/sdk/service/deprecated.go index 6f5c470d8..88a420e84 100644 --- a/sdk/service/deprecated.go +++ b/sdk/service/deprecated.go @@ -4,6 +4,7 @@ import ( "github.com/mesg-foundation/engine/container" "github.com/mesg-foundation/engine/database" "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/service" ) @@ -22,8 +23,8 @@ func NewDeprecated(c container.Container, serviceDB *database.ServiceDB) Service } // Create creates a new service from definition. -func (s *deprecated) Create(srv *service.Service) (*service.Service, error) { - return create(s.container, s.serviceDB, srv) +func (s *deprecated) Create(req *api.CreateServiceRequest) (*service.Service, error) { + return create(s.container, s.serviceDB, req) } // Delete deletes the service by hash. diff --git a/sdk/service/module.go b/sdk/service/module.go index 811d8cf98..63e906560 100644 --- a/sdk/service/module.go +++ b/sdk/service/module.go @@ -8,6 +8,7 @@ import ( "github.com/mesg-foundation/engine/database" "github.com/mesg-foundation/engine/database/store" "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/service" abci "github.com/tendermint/tendermint/abci/types" ) @@ -78,8 +79,8 @@ func (s *Module) querier(request cosmostypes.Request, path []string, req abci.Re } // Create creates a new service from definition. -func (s *Module) Create(request cosmostypes.Request, srv *service.Service) (*service.Service, error) { - return create(s.container, s.db(request), srv) +func (s *Module) Create(request cosmostypes.Request, req *api.CreateServiceRequest) (*service.Service, error) { + return create(s.container, s.db(request), req) } // Delete deletes the service by hash. diff --git a/sdk/service/sdk.go b/sdk/service/sdk.go index 59630a99c..31b0fda31 100644 --- a/sdk/service/sdk.go +++ b/sdk/service/sdk.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/service" ) @@ -17,7 +18,7 @@ func NewSDK() Service { } // Create creates a new service from definition. -func (s *sdk) Create(srv *service.Service) (*service.Service, error) { +func (s *sdk) Create(req *api.CreateServiceRequest) (*service.Service, error) { return nil, fmt.Errorf("create not implemented") } diff --git a/sdk/service/type.go b/sdk/service/type.go index 651033ec6..5e4e7433b 100644 --- a/sdk/service/type.go +++ b/sdk/service/type.go @@ -4,12 +4,13 @@ import ( "fmt" "github.com/mesg-foundation/engine/hash" + "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/service" ) // Service is the interface of this sdk type Service interface { - Create(srv *service.Service) (*service.Service, error) + Create(*api.CreateServiceRequest) (*service.Service, error) Delete(hash hash.Hash) error Get(hash hash.Hash) (*service.Service, error) List() ([]*service.Service, error) diff --git a/sdk/service/utils.go b/sdk/service/utils.go index 4d45afc76..eda0654b6 100644 --- a/sdk/service/utils.go +++ b/sdk/service/utils.go @@ -11,12 +11,13 @@ import ( "github.com/mesg-foundation/engine/database" "github.com/mesg-foundation/engine/hash" "github.com/mesg-foundation/engine/hash/dirhash" + "github.com/mesg-foundation/engine/protobuf/api" "github.com/mesg-foundation/engine/service" "github.com/mesg-foundation/engine/service/validator" "github.com/mr-tron/base58" ) -func create(container container.Container, db *database.ServiceDB, srv *service.Service) (*service.Service, error) { +func create(container container.Container, db *database.ServiceDB, req *api.CreateServiceRequest) (*service.Service, error) { // download and untar service context into path. path, err := ioutil.TempDir("", "mesg") if err != nil { @@ -24,7 +25,7 @@ func create(container container.Container, db *database.ServiceDB, srv *service. } defer os.RemoveAll(path) - resp, err := http.Get("http://ipfs.app.mesg.com:8080/ipfs/" + srv.Source) + resp, err := http.Get("http://ipfs.app.mesg.com:8080/ipfs/" + req.Source) if err != nil { return nil, err } @@ -37,6 +38,19 @@ func create(container container.Container, db *database.ServiceDB, srv *service. return nil, err } + // create service + srv := &service.Service{ + Sid: req.Sid, + Name: req.Name, + Description: req.Description, + Configuration: req.Configuration, + Tasks: req.Tasks, + Events: req.Events, + Dependencies: req.Dependencies, + Repository: req.Repository, + Source: req.Source, + } + // calculate and apply hash to service. dh := dirhash.New(path) srv.Hash, err = dh.Sum(hash.Dump(srv)) diff --git a/server/grpc/api/service.go b/server/grpc/api/service.go index 74977dee1..22b4b3871 100644 --- a/server/grpc/api/service.go +++ b/server/grpc/api/service.go @@ -22,18 +22,7 @@ func NewServiceServer(sdk *sdk.SDK) *ServiceServer { // Create creates a new service from definition. func (s *ServiceServer) Create(ctx context.Context, req *protobuf_api.CreateServiceRequest) (*protobuf_api.CreateServiceResponse, error) { - definition := &service.Service{ - Sid: req.Sid, - Name: req.Name, - Description: req.Description, - Configuration: req.Configuration, - Tasks: req.Tasks, - Events: req.Events, - Dependencies: req.Dependencies, - Repository: req.Repository, - Source: req.Source, - } - srv, err := s.sdk.Service.Create(definition) + srv, err := s.sdk.Service.Create(req) if err != nil { return nil, err }