2019-11-02 22:54:35 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/errors"
|
|
|
|
"github.com/micro/go-micro/runtime"
|
|
|
|
pb "github.com/micro/go-micro/runtime/service/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Runtime struct {
|
|
|
|
Runtime runtime.Runtime
|
|
|
|
}
|
|
|
|
|
|
|
|
func toProto(s *runtime.Service) *pb.Service {
|
|
|
|
return &pb.Service{
|
2019-11-22 17:10:00 +00:00
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Source: s.Source,
|
|
|
|
Path: s.Path,
|
|
|
|
Exec: s.Exec,
|
|
|
|
Metadata: s.Metadata,
|
2019-11-02 22:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toService(s *pb.Service) *runtime.Service {
|
|
|
|
return &runtime.Service{
|
2019-11-22 17:10:00 +00:00
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Source: s.Source,
|
|
|
|
Path: s.Path,
|
|
|
|
Exec: s.Exec,
|
|
|
|
Metadata: s.Metadata,
|
2019-11-02 22:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
func toCreateOptions(opts *pb.CreateOptions) []runtime.CreateOption {
|
|
|
|
options := []runtime.CreateOption{}
|
|
|
|
// command options
|
|
|
|
l := len(opts.Command)
|
|
|
|
if l == 1 {
|
|
|
|
options = append(options, runtime.WithCommand(opts.Command[0]))
|
|
|
|
}
|
|
|
|
if l > 1 {
|
|
|
|
options = append(options, runtime.WithCommand(opts.Command[0], opts.Command[1:]...))
|
|
|
|
}
|
|
|
|
// env options
|
|
|
|
if len(opts.Env) > 0 {
|
|
|
|
options = append(options, runtime.WithEnv(opts.Env))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: output options
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
func toReadOptions(opts *pb.ReadOptions) []runtime.ReadOption {
|
|
|
|
options := []runtime.ReadOption{}
|
2019-11-15 13:41:40 +00:00
|
|
|
// version options
|
|
|
|
if len(opts.Version) > 0 {
|
|
|
|
options = append(options, runtime.WithVersion(opts.Version))
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2019-11-02 22:54:35 +00:00
|
|
|
func (r *Runtime) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error {
|
|
|
|
if req.Service == nil {
|
|
|
|
return errors.BadRequest("go.micro.runtime", "blank service")
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
var options []runtime.CreateOption
|
|
|
|
if req.Options != nil {
|
|
|
|
options = toCreateOptions(req.Options)
|
|
|
|
}
|
|
|
|
|
2019-11-02 22:54:35 +00:00
|
|
|
service := toService(req.Service)
|
2019-11-15 13:41:40 +00:00
|
|
|
err := r.Runtime.Create(service, options...)
|
2019-11-02 22:54:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.runtime", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
func (r *Runtime) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
|
2019-11-15 13:41:40 +00:00
|
|
|
if len(req.Name) == 0 {
|
|
|
|
return errors.BadRequest("go.micro.runtime", "blank service")
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
var options []runtime.ReadOption
|
2019-11-15 13:41:40 +00:00
|
|
|
if req.Options != nil {
|
2019-11-25 16:31:14 +00:00
|
|
|
options = toReadOptions(req.Options)
|
2019-11-15 13:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
services, err := r.Runtime.Read(req.Name, options...)
|
2019-11-15 13:41:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.runtime", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
rsp.Services = append(rsp.Services, toProto(service))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 22:54:35 +00:00
|
|
|
func (r *Runtime) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error {
|
|
|
|
if req.Service == nil {
|
|
|
|
return errors.BadRequest("go.micro.runtime", "blank service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add opts
|
|
|
|
service := toService(req.Service)
|
|
|
|
err := r.Runtime.Update(service)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.runtime", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runtime) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
|
|
|
|
if req.Service == nil {
|
|
|
|
return errors.BadRequest("go.micro.runtime", "blank service")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add opts
|
|
|
|
service := toService(req.Service)
|
|
|
|
err := r.Runtime.Delete(service)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.runtime", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runtime) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
|
|
|
|
services, err := r.Runtime.List()
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.runtime", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
rsp.Services = append(rsp.Services, toProto(service))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|