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

Use protobuf struct in create function of service SDK #1305

Merged
merged 2 commits into from
Sep 8, 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
10 changes: 5 additions & 5 deletions config/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +30,7 @@ var (
type ServiceConfig struct {
Key string
Env map[string]string
Definition *service.Service
Definition *api.CreateServiceRequest
Instance *instance.Instance
}

Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions sdk/service/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions sdk/service/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion sdk/service/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
}

Expand Down
3 changes: 2 additions & 1 deletion sdk/service/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions sdk/service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ 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 {
return nil, err
}
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
}
Expand All @@ -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))
Expand Down
13 changes: 1 addition & 12 deletions server/grpc/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down