From 3151905ce3d2694017b6a052a7d6d2c410f8b0a8 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 12:21:38 -0700 Subject: [PATCH 01/17] update proto build script --- scripts/build-proto.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/scripts/build-proto.sh b/scripts/build-proto.sh index d65521737..7a05be0ad 100755 --- a/scripts/build-proto.sh +++ b/scripts/build-proto.sh @@ -6,9 +6,6 @@ PROJECT=github.com/mesg-foundation/core protoc --go_out=./ $PROJECT/service/service.proto -for x in $PROJECT/api/*/; do - protoc \ - --go_out=plugins=grpc:./ \ - --proto_path=./ \ - $x*.proto -done \ No newline at end of file +# build Proto API +protoc --go_out=plugins=grpc:./ --proto_path=./ $PROJECT/api/core/api.proto +protoc --go_out=plugins=grpc:./ --proto_path=./ $PROJECT/api/service/api.proto From 24e97882263cdda9e89926f3e1558a7301520420 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 16:33:40 -0700 Subject: [PATCH 02/17] update test command to test existing service and keep it alive --- cmd/service/test.go | 40 +++++++++++-------- docs/cli/mesg-cli.md | 19 --------- docs/cli/mesg-cli_daemon.md | 21 ---------- docs/cli/mesg-core.md | 5 +-- docs/cli/mesg-core_daemon.md | 22 ++++++++++ docs/cli/mesg-core_daemon_logs.md | 22 ++++++++++ ...mon_start.md => mesg-core_daemon_start.md} | 6 +-- ...n_status.md => mesg-core_daemon_status.md} | 6 +-- ...aemon_stop.md => mesg-core_daemon_stop.md} | 6 +-- docs/cli/mesg-core_service.md | 3 ++ docs/cli/mesg-core_service_delete.md | 28 +++++++++++++ docs/cli/mesg-core_service_deploy.md | 30 ++++++++++++++ docs/cli/mesg-core_service_detail.md | 4 +- docs/cli/mesg-core_service_list.md | 30 ++++++++++++++ docs/cli/mesg-core_service_start.md | 4 +- docs/cli/mesg-core_service_stop.md | 4 +- docs/cli/mesg-core_service_test.md | 10 +++-- 17 files changed, 180 insertions(+), 80 deletions(-) delete mode 100644 docs/cli/mesg-cli.md delete mode 100644 docs/cli/mesg-cli_daemon.md create mode 100644 docs/cli/mesg-core_daemon.md create mode 100644 docs/cli/mesg-core_daemon_logs.md rename docs/cli/{mesg-cli_daemon_start.md => mesg-core_daemon_start.md} (50%) rename docs/cli/{mesg-cli_daemon_status.md => mesg-core_daemon_status.md} (51%) rename docs/cli/{mesg-cli_daemon_stop.md => mesg-core_daemon_stop.md} (50%) create mode 100644 docs/cli/mesg-core_service_delete.md create mode 100644 docs/cli/mesg-core_service_deploy.md create mode 100644 docs/cli/mesg-core_service_list.md diff --git a/cmd/service/test.go b/cmd/service/test.go index 8573c7857..7c7a04617 100644 --- a/cmd/service/test.go +++ b/cmd/service/test.go @@ -2,7 +2,6 @@ package cmdService import ( "context" - "fmt" "io/ioutil" "log" "time" @@ -83,37 +82,44 @@ func executeTask(serviceID string, task string, dataPath string) (execution *cor } func testHandler(cmd *cobra.Command, args []string) { - service := loadService(defaultPath(args)) - _, err := buildDockerImage(defaultPath(args), service.Name) - handleError(err) - deployment, err := cli.DeployService(context.Background(), &core.DeployServiceRequest{ - Service: service, - }) - handleError(err) + serviceID := cmd.Flag("service").Value.String() + if serviceID == "" { + service := loadService(defaultPath(args)) + _, err := buildDockerImage(defaultPath(args), service.Name) + handleError(err) + deployment, err := cli.DeployService(context.Background(), &core.DeployServiceRequest{ + Service: service, + }) + handleError(err) + serviceID = deployment.ServiceID + } - _, err = cli.StartService(context.Background(), &core.StartServiceRequest{ - ServiceID: deployment.ServiceID, + _, err := cli.StartService(context.Background(), &core.StartServiceRequest{ + ServiceID: serviceID, }) handleError(err) - go listenEvents(deployment.ServiceID, cmd.Flag("event").Value.String()) + go listenEvents(serviceID, cmd.Flag("event").Value.String()) - go listenResults(deployment.ServiceID) + go listenResults(serviceID) time.Sleep(10 * time.Second) - executeTask(deployment.ServiceID, cmd.Flag("task").Value.String(), cmd.Flag("data").Value.String()) + executeTask(serviceID, cmd.Flag("task").Value.String(), cmd.Flag("data").Value.String()) <-cmdUtils.WaitForCancel() - _, err = cli.StopService(context.Background(), &core.StopServiceRequest{ - ServiceID: deployment.ServiceID, - }) - fmt.Println(err) + if cmd.Flag("keep-alive").Value.String() != "true" { + _, err = cli.StopService(context.Background(), &core.StopServiceRequest{ + ServiceID: serviceID, + }) + } } func init() { Test.Flags().StringP("event", "e", "*", "Only log a specific event") Test.Flags().StringP("task", "t", "", "Run a specific task") + Test.Flags().StringP("service", "s", "", "Debug a deployed service") + Test.Flags().BoolP("keep-alive", "", false, "Do not stop the service") Test.Flags().StringP("data", "d", "", "Path to the file containing the data required to run the task") } diff --git a/docs/cli/mesg-cli.md b/docs/cli/mesg-cli.md deleted file mode 100644 index d1ad554c4..000000000 --- a/docs/cli/mesg-cli.md +++ /dev/null @@ -1,19 +0,0 @@ -## mesg-cli - -MESG CLI - -### Synopsis - -MESG CLI - -### Options - -``` - -h, --help help for mesg-cli -``` - -### SEE ALSO - -* [mesg-cli daemon](mesg-cli_daemon.md) - Manage your MESG daemon -* [mesg-cli service](mesg-cli_service.md) - Manage your services - diff --git a/docs/cli/mesg-cli_daemon.md b/docs/cli/mesg-cli_daemon.md deleted file mode 100644 index 34d227e8d..000000000 --- a/docs/cli/mesg-cli_daemon.md +++ /dev/null @@ -1,21 +0,0 @@ -## mesg-cli daemon - -Manage your MESG daemon - -### Synopsis - -Manage your MESG daemon - -### Options - -``` - -h, --help help for daemon -``` - -### SEE ALSO - -* [mesg-cli](mesg-cli.md) - MESG CLI -* [mesg-cli daemon start](mesg-cli_daemon_start.md) - Start the daemon -* [mesg-cli daemon status](mesg-cli_daemon_status.md) - Status of the daemon -* [mesg-cli daemon stop](mesg-cli_daemon_stop.md) - Stop the daemon - diff --git a/docs/cli/mesg-core.md b/docs/cli/mesg-core.md index 8ecd7b065..0ebaa1bf0 100644 --- a/docs/cli/mesg-core.md +++ b/docs/cli/mesg-core.md @@ -6,10 +6,6 @@ MESG CORE MESG CORE -``` -mesg-core [flags] -``` - ### Options ``` @@ -18,5 +14,6 @@ mesg-core [flags] ### SEE ALSO +* [mesg-core daemon](mesg-core_daemon.md) - Manage the MESG daemon * [mesg-core service](mesg-core_service.md) - Manage your services diff --git a/docs/cli/mesg-core_daemon.md b/docs/cli/mesg-core_daemon.md new file mode 100644 index 000000000..08bcec0b5 --- /dev/null +++ b/docs/cli/mesg-core_daemon.md @@ -0,0 +1,22 @@ +## mesg-core daemon + +Manage the MESG daemon + +### Synopsis + +Manage the MESG daemon + +### Options + +``` + -h, --help help for daemon +``` + +### SEE ALSO + +* [mesg-core](mesg-core.md) - MESG CORE +* [mesg-core daemon logs](mesg-core_daemon_logs.md) - Show the daemon's logs +* [mesg-core daemon start](mesg-core_daemon_start.md) - Start the daemon +* [mesg-core daemon status](mesg-core_daemon_status.md) - Status of the daemon +* [mesg-core daemon stop](mesg-core_daemon_stop.md) - Stop the daemon + diff --git a/docs/cli/mesg-core_daemon_logs.md b/docs/cli/mesg-core_daemon_logs.md new file mode 100644 index 000000000..05b97c7c1 --- /dev/null +++ b/docs/cli/mesg-core_daemon_logs.md @@ -0,0 +1,22 @@ +## mesg-core daemon logs + +Show the daemon's logs + +### Synopsis + +Show the daemon's logs + +``` +mesg-core daemon logs [flags] +``` + +### Options + +``` + -h, --help help for logs +``` + +### SEE ALSO + +* [mesg-core daemon](mesg-core_daemon.md) - Manage the MESG daemon + diff --git a/docs/cli/mesg-cli_daemon_start.md b/docs/cli/mesg-core_daemon_start.md similarity index 50% rename from docs/cli/mesg-cli_daemon_start.md rename to docs/cli/mesg-core_daemon_start.md index c361a5ab3..25053e932 100644 --- a/docs/cli/mesg-cli_daemon_start.md +++ b/docs/cli/mesg-core_daemon_start.md @@ -1,4 +1,4 @@ -## mesg-cli daemon start +## mesg-core daemon start Start the daemon @@ -7,7 +7,7 @@ Start the daemon Start the daemon ``` -mesg-cli daemon start [flags] +mesg-core daemon start [flags] ``` ### Options @@ -18,5 +18,5 @@ mesg-cli daemon start [flags] ### SEE ALSO -* [mesg-cli daemon](mesg-cli_daemon.md) - Manage your MESG daemon +* [mesg-core daemon](mesg-core_daemon.md) - Manage the MESG daemon diff --git a/docs/cli/mesg-cli_daemon_status.md b/docs/cli/mesg-core_daemon_status.md similarity index 51% rename from docs/cli/mesg-cli_daemon_status.md rename to docs/cli/mesg-core_daemon_status.md index 7c1c4b1c4..ea80125d2 100644 --- a/docs/cli/mesg-cli_daemon_status.md +++ b/docs/cli/mesg-core_daemon_status.md @@ -1,4 +1,4 @@ -## mesg-cli daemon status +## mesg-core daemon status Status of the daemon @@ -7,7 +7,7 @@ Status of the daemon Status of the daemon ``` -mesg-cli daemon status [flags] +mesg-core daemon status [flags] ``` ### Options @@ -18,5 +18,5 @@ mesg-cli daemon status [flags] ### SEE ALSO -* [mesg-cli daemon](mesg-cli_daemon.md) - Manage your MESG daemon +* [mesg-core daemon](mesg-core_daemon.md) - Manage the MESG daemon diff --git a/docs/cli/mesg-cli_daemon_stop.md b/docs/cli/mesg-core_daemon_stop.md similarity index 50% rename from docs/cli/mesg-cli_daemon_stop.md rename to docs/cli/mesg-core_daemon_stop.md index b94368f67..c3f2ac8aa 100644 --- a/docs/cli/mesg-cli_daemon_stop.md +++ b/docs/cli/mesg-core_daemon_stop.md @@ -1,4 +1,4 @@ -## mesg-cli daemon stop +## mesg-core daemon stop Stop the daemon @@ -7,7 +7,7 @@ Stop the daemon Stop the daemon ``` -mesg-cli daemon stop [flags] +mesg-core daemon stop [flags] ``` ### Options @@ -18,5 +18,5 @@ mesg-cli daemon stop [flags] ### SEE ALSO -* [mesg-cli daemon](mesg-cli_daemon.md) - Manage your MESG daemon +* [mesg-core daemon](mesg-core_daemon.md) - Manage the MESG daemon diff --git a/docs/cli/mesg-core_service.md b/docs/cli/mesg-core_service.md index a3cb0b7be..1066f049b 100644 --- a/docs/cli/mesg-core_service.md +++ b/docs/cli/mesg-core_service.md @@ -15,8 +15,11 @@ Manage your services ### SEE ALSO * [mesg-core](mesg-core.md) - MESG CORE +* [mesg-core service delete](mesg-core_service_delete.md) - Delete a service +* [mesg-core service deploy](mesg-core_service_deploy.md) - Deploy a service * [mesg-core service detail](mesg-core_service_detail.md) - Show details of a published service * [mesg-core service init](mesg-core_service_init.md) - Initialize a service +* [mesg-core service list](mesg-core_service_list.md) - List all published services * [mesg-core service start](mesg-core_service_start.md) - Start a service * [mesg-core service status](mesg-core_service_status.md) - List started services * [mesg-core service stop](mesg-core_service_stop.md) - Stop a service diff --git a/docs/cli/mesg-core_service_delete.md b/docs/cli/mesg-core_service_delete.md new file mode 100644 index 000000000..764305bfc --- /dev/null +++ b/docs/cli/mesg-core_service_delete.md @@ -0,0 +1,28 @@ +## mesg-core service delete + +Delete a service + +### Synopsis + +Delete a service + +``` +mesg-core service delete [flags] +``` + +### Examples + +``` +mesg-core service delete +``` + +### Options + +``` + -h, --help help for delete +``` + +### SEE ALSO + +* [mesg-core service](mesg-core_service.md) - Manage your services + diff --git a/docs/cli/mesg-core_service_deploy.md b/docs/cli/mesg-core_service_deploy.md new file mode 100644 index 000000000..fd2e00803 --- /dev/null +++ b/docs/cli/mesg-core_service_deploy.md @@ -0,0 +1,30 @@ +## mesg-core service deploy + +Deploy a service + +### Synopsis + +Deploy a service on the Network. + +To get more information, see the [deploy page from the documentation](https://docs.mesg.tech/service/publish-a-service) + +``` +mesg-core service deploy [flags] +``` + +### Examples + +``` +mesg-core sevice deploy +``` + +### Options + +``` + -h, --help help for deploy +``` + +### SEE ALSO + +* [mesg-core service](mesg-core_service.md) - Manage your services + diff --git a/docs/cli/mesg-core_service_detail.md b/docs/cli/mesg-core_service_detail.md index 07f6522cc..c03769448 100644 --- a/docs/cli/mesg-core_service_detail.md +++ b/docs/cli/mesg-core_service_detail.md @@ -7,13 +7,13 @@ Show details of a published service Show details of a published service ``` -mesg-core service detail SERVICE_FOLDER [flags] +mesg-core service detail SERVICE_ID [flags] ``` ### Examples ``` -mesg-core service detail SERVICE_FOLDER +mesg-core service detail SERVICE_ID ``` ### Options diff --git a/docs/cli/mesg-core_service_list.md b/docs/cli/mesg-core_service_list.md new file mode 100644 index 000000000..1cb2417d1 --- /dev/null +++ b/docs/cli/mesg-core_service_list.md @@ -0,0 +1,30 @@ +## mesg-core service list + +List all published services + +### Synopsis + +This command returns all published services with basic information. +Optionally, you can filter the services published by a specific developer: +To have more details, see the [detail command](mesg-core_service_detail.md). + +``` +mesg-core service list [flags] +``` + +### Examples + +``` +mesg-core service list +``` + +### Options + +``` + -h, --help help for list +``` + +### SEE ALSO + +* [mesg-core service](mesg-core_service.md) - Manage your services + diff --git a/docs/cli/mesg-core_service_start.md b/docs/cli/mesg-core_service_start.md index 5936283a0..9f853663f 100644 --- a/docs/cli/mesg-core_service_start.md +++ b/docs/cli/mesg-core_service_start.md @@ -7,13 +7,13 @@ Start a service Start a service from the published available services. You have to provide a stake value and duration. ``` -mesg-core service start SERVICE_FOLDER [flags] +mesg-core service start SERVICE_ID [flags] ``` ### Examples ``` -mesg-core service start SERVICE_FOLDER +mesg-core service start SERVICE_ID ``` ### Options diff --git a/docs/cli/mesg-core_service_stop.md b/docs/cli/mesg-core_service_stop.md index 0d08b5a96..2b0043f48 100644 --- a/docs/cli/mesg-core_service_stop.md +++ b/docs/cli/mesg-core_service_stop.md @@ -11,13 +11,13 @@ You will **NOT** get your stake back immediately. You will get your remaining st To have more explanation, see the page [stake explanation from the documentation](). ``` -mesg-core service stop SERVICE_FOLDER [flags] +mesg-core service stop SERVICE_ID [flags] ``` ### Examples ``` -mesg-core service stop SERVICE_FOLDER +mesg-core service stop SERVICE_ID ``` ### Options diff --git a/docs/cli/mesg-core_service_test.md b/docs/cli/mesg-core_service_test.md index 82a280547..d7275109d 100644 --- a/docs/cli/mesg-core_service_test.md +++ b/docs/cli/mesg-core_service_test.md @@ -25,10 +25,12 @@ mesg-core service test --keep-alive ### Options ``` - -d, --data string Path to the file containing the data required to run the task - -e, --event string Only log a specific event (default "*") - -h, --help help for test - -t, --task string Run a specific task + -d, --data string Path to the file containing the data required to run the task + -e, --event string Only log a specific event (default "*") + -h, --help help for test + --keep-alive Do not stop the service + -s, --service string Debug a deployed service + -t, --task string Run a specific task ``` ### SEE ALSO From f9eff43fb9dd0febc79d2c8aad9d42bee624ae1e Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 16:34:22 -0700 Subject: [PATCH 03/17] add api to filter service events --- api/core/api.pb.go | 77 ++++++++++++++++++++---------------- api/core/api.proto | 1 + api/core/listen_event.go | 29 +++++++++++--- cmd/service/test.go | 5 +-- examples/services/test-event | 1 + 5 files changed, 71 insertions(+), 42 deletions(-) create mode 160000 examples/services/test-event diff --git a/api/core/api.pb.go b/api/core/api.pb.go index a5b4b0f7d..494f17bf6 100644 --- a/api/core/api.pb.go +++ b/api/core/api.pb.go @@ -48,6 +48,7 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package type ListenEventRequest struct { ServiceID string `protobuf:"bytes,1,opt,name=serviceID" json:"serviceID,omitempty"` + EventKey string `protobuf:"bytes,2,opt,name=eventKey" json:"eventKey,omitempty"` } func (m *ListenEventRequest) Reset() { *m = ListenEventRequest{} } @@ -62,6 +63,13 @@ func (m *ListenEventRequest) GetServiceID() string { return "" } +func (m *ListenEventRequest) GetEventKey() string { + if m != nil { + return m.EventKey + } + return "" +} + type ExecuteTaskRequest struct { ServiceID string `protobuf:"bytes,1,opt,name=serviceID" json:"serviceID,omitempty"` TaskKey string `protobuf:"bytes,2,opt,name=taskKey" json:"taskKey,omitempty"` @@ -679,38 +687,39 @@ var _Core_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("github.com/mesg-foundation/core/api/core/api.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 526 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0xcb, 0x6e, 0xd3, 0x40, - 0x14, 0xcd, 0xab, 0x2d, 0xb9, 0x2e, 0x10, 0x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42, 0xf2, 0x16, 0x2c, 0x43, 0xa5, 0xe4, 0x78, 0x22, 0xc5, 0xdf, 0xd4, - 0xad, 0xf3, 0x40, 0x05, 0xf4, 0x51, 0xd2, 0xce, 0xab, 0x2e, 0x39, 0x07, 0xcb, 0x38, 0xa4, 0xbc, - 0xb6, 0xa9, 0x5f, 0xe7, 0x71, 0x33, 0xc0, 0x83, 0x94, 0x76, 0xc8, 0x3b, 0x38, 0x34, 0x45, 0x48, - 0x6c, 0x63, 0x7a, 0x45, 0x97, 0xce, 0x43, 0x15, 0x29, 0x2f, 0x4d, 0xcd, 0x9f, 0xc3, 0xa1, 0xc9, - 0x6e, 0x5e, 0xde, 0xa2, 0x50, 0xe7, 0x49, 0x4b, 0x24, 0x5b, 0xe1, 0x1c, 0x2c, 0x83, 0xf6, 0x1c, - 0x43, 0x53, 0xaf, 0x39, 0x86, 0xfa, 0x85, 0xd0, 0x0e, 0x59, 0xc2, 0xfd, 0x0a, 0x93, 0xe4, 0xa9, - 0xca, 0x6c, 0xbb, 0x10, 0xe7, 0xb8, 0x2d, 0x64, 0xb4, 0x31, 0x68, 0xd2, 0x6d, 0x9a, 0x84, 0xeb, - 0x36, 0x75, 0x56, 0x69, 0x67, 0xbe, 0xff, 0x75, 0x20, 0x3f, 0xc9, 0x17, 0xfb, 0xea, 0x1b, 0x3c, - 0xfb, 0x1b, 0x00, 0x00, 0xff, 0xff, 0xc3, 0x7d, 0x2c, 0xd2, 0xf5, 0x05, 0x00, 0x00, + // 531 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x4b, 0x6f, 0xd3, 0x40, + 0x10, 0xce, 0xab, 0x2d, 0x19, 0x17, 0x08, 0x4b, 0xa0, 0xc6, 0xaa, 0x50, 0xb5, 0xa7, 0x82, 0x44, + 0x82, 0x52, 0xb8, 0x20, 0xa1, 0x4a, 0x21, 0x39, 0xf0, 0x10, 0x87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53, 0x8e, 0x62, 0x28, 0x69, 0xe7, 0x65, 0x97, 0x9c, 0x83, + 0x65, 0x0c, 0x52, 0x96, 0xdb, 0xdc, 0x5f, 0xe7, 0x51, 0xd3, 0xc1, 0x83, 0x94, 0x76, 0xc8, 0x5b, + 0x38, 0x34, 0x97, 0x90, 0xd8, 0x46, 0xf5, 0xca, 0x5e, 0x3a, 0xf7, 0x95, 0xa7, 0x9c, 0x34, 0x55, + 0x7f, 0x0e, 0x87, 0xa6, 0xba, 0x59, 0x7a, 0xcb, 0x86, 0x3a, 0x8f, 0x5b, 0x3c, 0x9a, 0xc2, 0x39, + 0x58, 0x86, 0xec, 0x59, 0x0f, 0xcd, 0x7d, 0xcd, 0x7a, 0xa8, 0x4f, 0x08, 0xed, 0x90, 0x25, 0xdc, + 0xad, 0x28, 0x49, 0x9e, 0xa8, 0xc8, 0xb6, 0x09, 0x71, 0x8e, 0xda, 0x5c, 0x06, 0x8c, 0x21, 0x53, + 0x01, 0xd3, 0x14, 0xbc, 0x80, 0xa9, 0xab, 0x4a, 0x3b, 0xf3, 0xfd, 0xaf, 0x03, 0xf9, 0x5c, 0x5f, + 0xec, 0xab, 0xf7, 0xf9, 0xec, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xf3, 0x80, 0x9f, 0xc6, 0x11, + 0x06, 0x00, 0x00, } diff --git a/api/core/api.proto b/api/core/api.proto index 22e8a1340..afe157507 100644 --- a/api/core/api.proto +++ b/api/core/api.proto @@ -17,6 +17,7 @@ service Core { message ListenEventRequest { string serviceID = 1; + string eventKey = 2; } message ExecuteTaskRequest { diff --git a/api/core/listen_event.go b/api/core/listen_event.go index c5705835a..362d4a71f 100644 --- a/api/core/listen_event.go +++ b/api/core/listen_event.go @@ -2,6 +2,7 @@ package core import ( "encoding/json" + "errors" "github.com/mesg-foundation/core/database/services" @@ -15,14 +16,32 @@ func (s *Server) ListenEvent(request *ListenEventRequest, stream Core_ListenEven if err != nil { return } + if request.EventKey != "" && request.EventKey != "*" { + _, ok := service.Events[request.EventKey] + if !ok { + err = errors.New("Invalid eventKey: " + request.EventKey) + return + } + } subscription := pubsub.Subscribe(service.EventSubscriptionChannel()) for data := range subscription { event := data.(*event.Event) - eventData, _ := json.Marshal(event.Data) - stream.Send(&EventData{ - EventKey: event.Key, - EventData: string(eventData), - }) + if isSubscribedEvent(request, event) { + eventData, _ := json.Marshal(event.Data) + stream.Send(&EventData{ + EventKey: event.Key, + EventData: string(eventData), + }) + } } return } + +func isSubscribedEvent(request *ListenEventRequest, e *event.Event) bool { + if request.EventKey != "" && request.EventKey != "*" && request.EventKey != e.Key { + return false + } + // Possibility to add more filters here like filters on data, awlays return the + // falsy value and go until the end to have the truth value + return true +} diff --git a/cmd/service/test.go b/cmd/service/test.go index 7c7a04617..3a1cf59c9 100644 --- a/cmd/service/test.go +++ b/cmd/service/test.go @@ -32,6 +32,7 @@ mesg-core service test --keep-alive`, func listenEvents(serviceID string, filter string) { stream, err := cli.ListenEvent(context.Background(), &core.ListenEventRequest{ ServiceID: serviceID, + EventKey: filter, }) handleError(err) for { @@ -40,9 +41,7 @@ func listenEvents(serviceID string, filter string) { log.Println(aurora.Red(err)) return } - if filter == "*" || filter == event.EventKey { - log.Println("Receive event", aurora.Green(event.EventKey), ":", aurora.Bold(event.EventData)) - } + log.Println("Receive event", aurora.Green(event.EventKey), ":", aurora.Bold(event.EventData)) } } diff --git a/examples/services/test-event b/examples/services/test-event new file mode 160000 index 000000000..639f2ddce --- /dev/null +++ b/examples/services/test-event @@ -0,0 +1 @@ +Subproject commit 639f2ddcec008ad743291ab8dc7ffd6b1c338de5 From 38d808da33e7777278753baddd9e64bee7b21c69 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 16:48:33 -0700 Subject: [PATCH 04/17] add test for parameter filter --- api/core/listen_event.go | 24 +++++++++++++++------ api/core/listen_event_test.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 api/core/listen_event_test.go diff --git a/api/core/listen_event.go b/api/core/listen_event.go index 362d4a71f..65d21bc4a 100644 --- a/api/core/listen_event.go +++ b/api/core/listen_event.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/mesg-foundation/core/database/services" + service "github.com/mesg-foundation/core/service" "github.com/mesg-foundation/core/event" "github.com/mesg-foundation/core/pubsub" @@ -16,12 +17,8 @@ func (s *Server) ListenEvent(request *ListenEventRequest, stream Core_ListenEven if err != nil { return } - if request.EventKey != "" && request.EventKey != "*" { - _, ok := service.Events[request.EventKey] - if !ok { - err = errors.New("Invalid eventKey: " + request.EventKey) - return - } + if err = validateEventKey(&service, request.EventKey); err != nil { + return } subscription := pubsub.Subscribe(service.EventSubscriptionChannel()) for data := range subscription { @@ -37,6 +34,21 @@ func (s *Server) ListenEvent(request *ListenEventRequest, stream Core_ListenEven return } +func validateEventKey(service *service.Service, eventKey string) (err error) { + if eventKey == "" { + return + } + if eventKey == "*" { + return + } + _, ok := service.Events[eventKey] + if ok { + return + } + err = errors.New("Invalid eventKey: " + eventKey) + return +} + func isSubscribedEvent(request *ListenEventRequest, e *event.Event) bool { if request.EventKey != "" && request.EventKey != "*" && request.EventKey != e.Key { return false diff --git a/api/core/listen_event_test.go b/api/core/listen_event_test.go new file mode 100644 index 000000000..57105a485 --- /dev/null +++ b/api/core/listen_event_test.go @@ -0,0 +1,39 @@ +package core + +import ( + "testing" + + "github.com/mesg-foundation/core/event" + "github.com/mesg-foundation/core/service" + "github.com/stvp/assert" +) + +func TestValidateEventKey(t *testing.T) { + s := &service.Service{ + Events: map[string]*service.Event{ + "test": &service.Event{}, + }, + } + assert.Nil(t, validateEventKey(s, "")) + assert.Nil(t, validateEventKey(s, "*")) + assert.Nil(t, validateEventKey(s, "test")) + assert.NotNil(t, validateEventKey(s, "xxx")) +} + +func TestIsSubscribedEvent(t *testing.T) { + e := &event.Event{Key: "test"} + r := &ListenEventRequest{} + assert.True(t, isSubscribedEvent(r, e)) + + r = &ListenEventRequest{EventKey: ""} + assert.True(t, isSubscribedEvent(r, e)) + + r = &ListenEventRequest{EventKey: "*"} + assert.True(t, isSubscribedEvent(r, e)) + + r = &ListenEventRequest{EventKey: "test"} + assert.True(t, isSubscribedEvent(r, e)) + + r = &ListenEventRequest{EventKey: "xxx"} + assert.False(t, isSubscribedEvent(r, e)) +} From 6202eb32926a72bb6d0acf98f4906580aadb2b91 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 17:28:51 -0700 Subject: [PATCH 05/17] add filter on subscription for results --- api/core/api.pb.go | 86 +++++++++++++++++++-------------- api/core/api.proto | 2 + api/core/listen_result.go | 81 ++++++++++++++++++++++++++++--- api/core/listen_result_test.go | 88 ++++++++++++++++++++++++++++++++++ 4 files changed, 215 insertions(+), 42 deletions(-) create mode 100644 api/core/listen_result_test.go diff --git a/api/core/api.pb.go b/api/core/api.pb.go index 494f17bf6..30faa2a30 100644 --- a/api/core/api.pb.go +++ b/api/core/api.pb.go @@ -104,6 +104,8 @@ func (m *ExecuteTaskRequest) GetTaskData() string { type ListenResultRequest struct { ServiceID string `protobuf:"bytes,1,opt,name=serviceID" json:"serviceID,omitempty"` + TaskKey string `protobuf:"bytes,2,opt,name=taskKey" json:"taskKey,omitempty"` + OutputKey string `protobuf:"bytes,3,opt,name=outputKey" json:"outputKey,omitempty"` } func (m *ListenResultRequest) Reset() { *m = ListenResultRequest{} } @@ -118,6 +120,20 @@ func (m *ListenResultRequest) GetServiceID() string { return "" } +func (m *ListenResultRequest) GetTaskKey() string { + if m != nil { + return m.TaskKey + } + return "" +} + +func (m *ListenResultRequest) GetOutputKey() string { + if m != nil { + return m.OutputKey + } + return "" +} + type StartServiceRequest struct { ServiceID string `protobuf:"bytes,1,opt,name=serviceID" json:"serviceID,omitempty"` } @@ -687,39 +703,39 @@ var _Core_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("github.com/mesg-foundation/core/api/core/api.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 531 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x4b, 0x6f, 0xd3, 0x40, - 0x10, 0xce, 0xab, 0x2d, 0x19, 0x17, 0x08, 0x4b, 0xa0, 0xc6, 0xaa, 0x50, 0xb5, 0xa7, 0x82, 0x44, - 0x82, 0x52, 0xb8, 0x20, 0xa1, 0x4a, 0x21, 0x39, 0xf0, 0x10, 0x87, 0x84, 0x13, 0x9c, 0x5c, 0x33, - 0xb4, 0x56, 0x5d, 0xef, 0x62, 0xaf, 0x2b, 0xfc, 0x5f, 0xf8, 0x5b, 0xfc, 0x1f, 0xb4, 0xbb, 0x7e, - 0xac, 0x1f, 0x84, 0xe4, 0x64, 0xef, 0x3c, 0xbe, 0xf9, 0x66, 0xbf, 0x99, 0x85, 0xd9, 0xa5, 0x2f, - 0xae, 0x92, 0x8b, 0x89, 0xc7, 0x6e, 0xa6, 0x37, 0x18, 0x5f, 0xbe, 0xf8, 0xc1, 0x92, 0xf0, 0xbb, - 0x2b, 0x7c, 0x16, 0x4e, 0x3d, 0x16, 0xe1, 0xd4, 0xe5, 0x7e, 0xf1, 0x33, 0xe1, 0x11, 0x13, 0x8c, - 0xf4, 0x5d, 0xee, 0x3b, 0xaf, 0xff, 0x97, 0x18, 0x63, 0x74, 0xeb, 0x7b, 0xc5, 0x57, 0xe7, 0xd2, - 0xcf, 0x40, 0x3e, 0xf9, 0xb1, 0xc0, 0x70, 0x79, 0x8b, 0xa1, 0x58, 0xe1, 0xcf, 0x04, 0x63, 0x41, - 0x8e, 0x61, 0x98, 0x85, 0xbd, 0x5f, 0xd8, 0xdd, 0x93, 0xee, 0xe9, 0x70, 0x55, 0x1a, 0x88, 0x03, - 0x77, 0x50, 0x46, 0x7f, 0xc4, 0xd4, 0xee, 0x29, 0x67, 0x71, 0xa6, 0x57, 0x40, 0x96, 0xbf, 0xd0, - 0x4b, 0x04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 0x91, 0x79, 0x80, 0x1c, 0x8a, 0x1a, 0x86, 0xac, 0xb7, - 0x79, 0x28, 0x5e, 0xc9, 0xba, 0x01, 0x0a, 0xdc, 0x69, 0x94, 0xc6, 0xb2, 0x52, 0x25, 0x8b, 0x07, - 0xe9, 0xec, 0x4f, 0x1f, 0x06, 0xef, 0x58, 0x84, 0xe4, 0x0d, 0x58, 0xc6, 0x06, 0x93, 0xa3, 0x89, - 0x7c, 0x18, 0x9a, 0x3b, 0xed, 0xdc, 0x53, 0x8e, 0x62, 0x28, 0x69, 0xe7, 0x65, 0x97, 0x9c, 0x83, - 0x65, 0x0c, 0x52, 0x96, 0xdb, 0xdc, 0x5f, 0xe7, 0x51, 0xd3, 0xc1, 0x83, 0x94, 0x76, 0xc8, 0x5b, - 0x38, 0x34, 0x97, 0x90, 0xd8, 0x46, 0xf5, 0xca, 0x5e, 0x3a, 0xf7, 0x95, 0xa7, 0x9c, 0x34, 0x55, - 0x7f, 0x0e, 0x87, 0xa6, 0xba, 0x59, 0x7a, 0xcb, 0x86, 0x3a, 0x8f, 0x5b, 0x3c, 0x9a, 0xc2, 0x39, - 0x58, 0x86, 0xec, 0x59, 0x0f, 0xcd, 0x7d, 0xcd, 0x7a, 0xa8, 0x4f, 0x08, 0xed, 0x90, 0x25, 0xdc, - 0xad, 0x28, 0x49, 0x9e, 0xa8, 0xc8, 0xb6, 0x09, 0x71, 0x8e, 0xda, 0x5c, 0x06, 0x8c, 0x21, 0x53, - 0x01, 0xd3, 0x14, 0xbc, 0x80, 0xa9, 0xab, 0x4a, 0x3b, 0xf3, 0xfd, 0xaf, 0x03, 0xf9, 0x5c, 0x5f, - 0xec, 0xab, 0xf7, 0xf9, 0xec, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xf3, 0x80, 0x9f, 0xc6, 0x11, - 0x06, 0x00, 0x00, + // 536 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x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0x52, 0xbc, 0x94, 0xbc, 0x3e, 0x0a, 0xbc, 0xd5, 0x2a, 0x8d, + 0x25, 0x53, 0xa9, 0x8a, 0xfb, 0xc9, 0xec, 0x77, 0x1f, 0x06, 0x6f, 0x59, 0x88, 0xe4, 0x35, 0x58, + 0xc6, 0xe5, 0x93, 0xc3, 0x89, 0x7c, 0x50, 0xea, 0x6f, 0x81, 0x73, 0x4f, 0x05, 0xf2, 0xa5, 0xa4, + 0x9d, 0x17, 0x5d, 0x72, 0x06, 0x96, 0xb1, 0x48, 0x69, 0x6d, 0xfd, 0xee, 0x9d, 0x87, 0xf5, 0x00, + 0xf7, 0x13, 0xda, 0x21, 0x6f, 0x60, 0xdf, 0x3c, 0x5e, 0x62, 0x1b, 0xec, 0xa5, 0x7b, 0x76, 0xee, + 0xab, 0x48, 0xb1, 0x69, 0x8a, 0x7f, 0x0e, 0xfb, 0xa6, 0xba, 0x69, 0x79, 0xc3, 0x85, 0x3a, 0x8f, + 0x1a, 0x22, 0xba, 0x85, 0x33, 0xb0, 0x0c, 0xd9, 0xd3, 0x19, 0xea, 0xf7, 0x9a, 0xce, 0x50, 0xdd, + 0x10, 0xda, 0x21, 0x4b, 0xb8, 0x5b, 0x52, 0x92, 0x3c, 0x56, 0x99, 0x4d, 0x1b, 0xe2, 0x1c, 0x36, + 0x85, 0x0c, 0x18, 0x43, 0xa6, 0x1c, 0xa6, 0x2e, 0x78, 0x0e, 0x53, 0x55, 0x95, 0x76, 0xe6, 0xbb, + 0x5f, 0x06, 0xf2, 0x99, 0x3f, 0xdf, 0x55, 0xef, 0xfa, 0xe9, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xc9, 0xc6, 0xe2, 0xbc, 0x49, 0x06, 0x00, 0x00, } diff --git a/api/core/api.proto b/api/core/api.proto index afe157507..2d8bd3794 100644 --- a/api/core/api.proto +++ b/api/core/api.proto @@ -28,6 +28,8 @@ message ExecuteTaskRequest { message ListenResultRequest { string serviceID = 1; + string taskKey = 2; + string outputKey = 3; } message StartServiceRequest { diff --git a/api/core/listen_result.go b/api/core/listen_result.go index 52ebf398c..88dfcab7a 100644 --- a/api/core/listen_result.go +++ b/api/core/listen_result.go @@ -2,8 +2,10 @@ package core import ( "encoding/json" + "errors" "github.com/mesg-foundation/core/database/services" + service "github.com/mesg-foundation/core/service" "github.com/mesg-foundation/core/execution" "github.com/mesg-foundation/core/pubsub" @@ -15,16 +17,81 @@ func (s *Server) ListenResult(request *ListenResultRequest, stream Core_ListenRe if err != nil { return } + if err = validateTaskKey(&service, request.TaskKey); err != nil { + return + } + if err = validateOutputKey(&service, request.TaskKey, request.OutputKey); err != nil { + return + } subscription := pubsub.Subscribe(service.ResultSubscriptionChannel()) for data := range subscription { execution := data.(*execution.Execution) - outputs, _ := json.Marshal(execution.OutputData) - stream.Send(&ResultData{ - ExecutionID: execution.ID, - TaskKey: execution.Task, - OutputKey: execution.Output, - OutputData: string(outputs), - }) + if isSubscribedTask(request, execution) && isSubscribedOutput(request, execution) { + outputs, _ := json.Marshal(execution.OutputData) + stream.Send(&ResultData{ + ExecutionID: execution.ID, + TaskKey: execution.Task, + OutputKey: execution.Output, + OutputData: string(outputs), + }) + } + } + return +} + +func validateTaskKey(service *service.Service, taskKey string) (err error) { + if taskKey == "" { + return + } + if taskKey == "*" { + return + } + _, ok := service.Tasks[taskKey] + if ok { + return + } + err = errors.New("Invalid taskKey: " + taskKey) + return +} + +func validateOutputKey(service *service.Service, taskKey string, outputKey string) (err error) { + if outputKey == "" { + return + } + if outputKey == "*" { + return + } + task, ok := service.Tasks[taskKey] + if !ok { + err = errors.New("Invalid taskKey: " + taskKey) + return + } + _, ok = task.Outputs[outputKey] + if ok { + return } + err = errors.New("Invalid outputKey: " + outputKey) return } + +func isSubscribedTask(request *ListenResultRequest, e *execution.Execution) bool { + return includedIn([]string{"", "*", e.Task}, request.TaskKey) +} + +func isSubscribedOutput(request *ListenResultRequest, e *execution.Execution) bool { + return includedIn([]string{"", "*", e.Output}, request.OutputKey) +} + +func includedIn(arr []string, value string) bool { + if len(arr) == 0 { + return false + } + i := 0 + for _, item := range arr { + if item == value { + break + } + i++ + } + return i != len(arr) +} diff --git a/api/core/listen_result_test.go b/api/core/listen_result_test.go new file mode 100644 index 000000000..ba1847d5a --- /dev/null +++ b/api/core/listen_result_test.go @@ -0,0 +1,88 @@ +package core + +import ( + "testing" + + "github.com/mesg-foundation/core/execution" + "github.com/mesg-foundation/core/service" + "github.com/stvp/assert" +) + +func TestValidateTaskKey(t *testing.T) { + s := &service.Service{ + Tasks: map[string]*service.Task{ + "test": &service.Task{}, + }, + } + assert.Nil(t, validateTaskKey(s, "")) + assert.Nil(t, validateTaskKey(s, "*")) + assert.Nil(t, validateTaskKey(s, "test")) + assert.NotNil(t, validateTaskKey(s, "xxx")) +} + +func TestValidateOutputKey(t *testing.T) { + s := &service.Service{ + Tasks: map[string]*service.Task{ + "test": &service.Task{ + Outputs: map[string]*service.Output{ + "outputx": &service.Output{}, + }, + }, + }, + } + assert.Nil(t, validateOutputKey(s, "test", "")) + assert.Nil(t, validateOutputKey(s, "test", "*")) + assert.Nil(t, validateOutputKey(s, "test", "outputx")) + assert.NotNil(t, validateOutputKey(s, "test", "xxx")) + assert.Nil(t, validateOutputKey(s, "xxx", "")) + assert.Nil(t, validateOutputKey(s, "xxx", "*")) + assert.NotNil(t, validateOutputKey(s, "xxx", "outputX")) + assert.NotNil(t, validateOutputKey(s, "xxx", "xxx")) +} + +func TestIncludedIn(t *testing.T) { + assert.False(t, includedIn([]string{}, "")) + assert.True(t, includedIn([]string{""}, "")) + assert.False(t, includedIn([]string{"a"}, "")) + assert.True(t, includedIn([]string{"a"}, "a")) + assert.False(t, includedIn([]string{""}, "a")) + assert.True(t, includedIn([]string{"a", "b"}, "a")) + assert.True(t, includedIn([]string{"a", "b"}, "b")) + assert.False(t, includedIn([]string{"a", "b"}, "c")) +} + +func TestIsSubscribedTask(t *testing.T) { + x := &execution.Execution{Task: "task"} + r := &ListenResultRequest{} + assert.True(t, isSubscribedTask(r, x)) + + r = &ListenResultRequest{TaskKey: ""} + assert.True(t, isSubscribedTask(r, x)) + + r = &ListenResultRequest{TaskKey: "*"} + assert.True(t, isSubscribedTask(r, x)) + + r = &ListenResultRequest{TaskKey: "task"} + assert.True(t, isSubscribedTask(r, x)) + + r = &ListenResultRequest{TaskKey: "xxx"} + assert.False(t, isSubscribedTask(r, x)) +} + +func TestIsSubscribedOutput(t *testing.T) { + x := &execution.Execution{Output: "output"} + r := &ListenResultRequest{} + assert.True(t, isSubscribedOutput(r, x)) + + r = &ListenResultRequest{OutputKey: ""} + assert.True(t, isSubscribedOutput(r, x)) + + r = &ListenResultRequest{OutputKey: "*"} + assert.True(t, isSubscribedOutput(r, x)) + + r = &ListenResultRequest{OutputKey: "output"} + assert.True(t, isSubscribedOutput(r, x)) + + r = &ListenResultRequest{OutputKey: "xxx"} + assert.False(t, isSubscribedOutput(r, x)) +} From adaa87b65347fd0b46062777ab0ecceb6f3fec70 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 17:46:00 -0700 Subject: [PATCH 06/17] update test command to listen resutl from task and/or output --- cmd/service/test.go | 10 +++++++--- docs/cli/mesg-core_service_test.md | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/service/test.go b/cmd/service/test.go index 3a1cf59c9..c8b33380f 100644 --- a/cmd/service/test.go +++ b/cmd/service/test.go @@ -45,9 +45,11 @@ func listenEvents(serviceID string, filter string) { } } -func listenResults(serviceID string) { +func listenResults(serviceID string, result string, output string) { stream, err := cli.ListenResult(context.Background(), &core.ListenResultRequest{ ServiceID: serviceID, + TaskKey: result, + OutputKey: output, }) handleError(err) for { @@ -100,7 +102,7 @@ func testHandler(cmd *cobra.Command, args []string) { go listenEvents(serviceID, cmd.Flag("event").Value.String()) - go listenResults(serviceID) + go listenResults(serviceID, cmd.Flag("result").Value.String(), cmd.Flag("output").Value.String()) time.Sleep(10 * time.Second) @@ -118,7 +120,9 @@ func testHandler(cmd *cobra.Command, args []string) { func init() { Test.Flags().StringP("event", "e", "*", "Only log a specific event") Test.Flags().StringP("task", "t", "", "Run a specific task") + Test.Flags().StringP("data", "d", "", "Path to the file containing the data required to run the task") + Test.Flags().StringP("result", "r", "", "Filter the result of a specific task") + Test.Flags().StringP("output", "o", "", "Filter output of a task") Test.Flags().StringP("service", "s", "", "Debug a deployed service") Test.Flags().BoolP("keep-alive", "", false, "Do not stop the service") - Test.Flags().StringP("data", "d", "", "Path to the file containing the data required to run the task") } diff --git a/docs/cli/mesg-core_service_test.md b/docs/cli/mesg-core_service_test.md index d7275109d..ae50802a0 100644 --- a/docs/cli/mesg-core_service_test.md +++ b/docs/cli/mesg-core_service_test.md @@ -29,6 +29,8 @@ mesg-core service test --keep-alive -e, --event string Only log a specific event (default "*") -h, --help help for test --keep-alive Do not stop the service + -o, --output string Filter output of a task + -r, --result string Filter the result of a specific task -s, --service string Debug a deployed service -t, --task string Run a specific task ``` From 8e2b1a7b7690fea11b8b67270fd462a33f166b15 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 17:54:16 -0700 Subject: [PATCH 07/17] update name of filter parameters --- api/core/api.pb.go | 93 +++++++++++++++++----------------- api/core/api.proto | 6 +-- api/core/listen_event.go | 14 ++--- api/core/listen_event_test.go | 8 +-- api/core/listen_result.go | 28 +++++----- api/core/listen_result_test.go | 16 +++--- cmd/service/test.go | 10 ++-- 7 files changed, 88 insertions(+), 87 deletions(-) diff --git a/api/core/api.pb.go b/api/core/api.pb.go index 30faa2a30..6cbe50383 100644 --- a/api/core/api.pb.go +++ b/api/core/api.pb.go @@ -47,8 +47,8 @@ var _ = math.Inf const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package type ListenEventRequest struct { - ServiceID string `protobuf:"bytes,1,opt,name=serviceID" json:"serviceID,omitempty"` - EventKey string `protobuf:"bytes,2,opt,name=eventKey" json:"eventKey,omitempty"` + ServiceID string `protobuf:"bytes,1,opt,name=serviceID" json:"serviceID,omitempty"` + EventFilter string `protobuf:"bytes,2,opt,name=eventFilter" json:"eventFilter,omitempty"` } func (m *ListenEventRequest) Reset() { *m = ListenEventRequest{} } @@ -63,9 +63,9 @@ func (m *ListenEventRequest) GetServiceID() string { return "" } -func (m *ListenEventRequest) GetEventKey() string { +func (m *ListenEventRequest) GetEventFilter() string { if m != nil { - return m.EventKey + return m.EventFilter } return "" } @@ -103,9 +103,9 @@ func (m *ExecuteTaskRequest) GetTaskData() string { } type ListenResultRequest struct { - ServiceID string `protobuf:"bytes,1,opt,name=serviceID" json:"serviceID,omitempty"` - TaskKey string `protobuf:"bytes,2,opt,name=taskKey" json:"taskKey,omitempty"` - OutputKey string `protobuf:"bytes,3,opt,name=outputKey" json:"outputKey,omitempty"` + ServiceID string `protobuf:"bytes,1,opt,name=serviceID" json:"serviceID,omitempty"` + TaskFilter string `protobuf:"bytes,2,opt,name=taskFilter" json:"taskFilter,omitempty"` + OutputFilter string `protobuf:"bytes,3,opt,name=outputFilter" json:"outputFilter,omitempty"` } func (m *ListenResultRequest) Reset() { *m = ListenResultRequest{} } @@ -120,16 +120,16 @@ func (m *ListenResultRequest) GetServiceID() string { return "" } -func (m *ListenResultRequest) GetTaskKey() string { +func (m *ListenResultRequest) GetTaskFilter() string { if m != nil { - return m.TaskKey + return m.TaskFilter } return "" } -func (m *ListenResultRequest) GetOutputKey() string { +func (m *ListenResultRequest) GetOutputFilter() string { if m != nil { - return m.OutputKey + return m.OutputFilter } return "" } @@ -703,39 +703,40 @@ var _Core_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("github.com/mesg-foundation/core/api/core/api.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 536 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x54, 0x4b, 0x6f, 0xd3, 0x40, - 0x10, 0xce, 0xab, 0x2d, 0x19, 0x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a9, 0x8a, 0xfb, 0xc9, 0xec, 0x77, 0x1f, 0x06, 0x6f, 0x59, 0x88, 0xe4, 0x35, 0x58, - 0xc6, 0xe5, 0x93, 0xc3, 0x89, 0x7c, 0x50, 0xea, 0x6f, 0x81, 0x73, 0x4f, 0x05, 0xf2, 0xa5, 0xa4, - 0x9d, 0x17, 0x5d, 0x72, 0x06, 0x96, 0xb1, 0x48, 0x69, 0x6d, 0xfd, 0xee, 0x9d, 0x87, 0xf5, 0x00, - 0xf7, 0x13, 0xda, 0x21, 0x6f, 0x60, 0xdf, 0x3c, 0x5e, 0x62, 0x1b, 0xec, 0xa5, 0x7b, 0x76, 0xee, - 0xab, 0x48, 0xb1, 0x69, 0x8a, 0x7f, 0x0e, 0xfb, 0xa6, 0xba, 0x69, 0x79, 0xc3, 0x85, 0x3a, 0x8f, - 0x1a, 0x22, 0xba, 0x85, 0x33, 0xb0, 0x0c, 0xd9, 0xd3, 0x19, 0xea, 0xf7, 0x9a, 0xce, 0x50, 0xdd, - 0x10, 0xda, 0x21, 0x4b, 0xb8, 0x5b, 0x52, 0x92, 0x3c, 0x56, 0x99, 0x4d, 0x1b, 0xe2, 0x1c, 0x36, - 0x85, 0x0c, 0x18, 0x43, 0xa6, 0x1c, 0xa6, 0x2e, 0x78, 0x0e, 0x53, 0x55, 0x95, 0x76, 0xe6, 0xbb, - 0x5f, 0x06, 0xf2, 0x99, 0x3f, 0xdf, 0x55, 0xef, 0xfa, 0xe9, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff, - 0xc9, 0xc6, 0xe2, 0xbc, 0x49, 0x06, 0x00, 0x00, + // 559 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x4b, 0x6f, 0xd3, 0x40, + 0x10, 0xce, 0xab, 0x2d, 0x19, 0x07, 0x08, 0xdb, 0x40, 0x8d, 0x55, 0xa1, 0x6a, 0x4f, 0x05, 0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fd, 0x2f, 0x31, 0x4e, 0xfc, 0x0d, 0x07, 0x78, 0x06, 0x20, + 0x00, 0x8c, 0xfe, 0x35, 0x0b, 0xa1, 0x30, 0x60, 0x09, 0x0f, 0x93, 0x7c, 0x42, 0x55, 0xd4, 0xb0, + 0xd1, 0x13, 0xd8, 0x5f, 0x71, 0x37, 0xe2, 0x2b, 0x85, 0xba, 0x51, 0x61, 0x3a, 0x05, 0xb2, 0xe2, + 0x2c, 0xdc, 0x2a, 0xe7, 0x1b, 0xf4, 0x25, 0x37, 0x62, 0x5c, 0x32, 0x82, 0x1d, 0x8c, 0x22, 0x16, + 0x65, 0x61, 0xea, 0x20, 0x2e, 0x48, 0xde, 0x7e, 0x79, 0x77, 0xc5, 0x59, 0x80, 0x63, 0x9e, 0x9e, + 0x0d, 0x52, 0x1a, 0xe8, 0x27, 0x18, 0x1a, 0x44, 0x85, 0x7e, 0x2a, 0xe9, 0x95, 0x36, 0x8f, 0x05, + 0x45, 0x43, 0xba, 0xa9, 0xec, 0xa2, 0xa3, 0x75, 0x41, 0xff, 0xb4, 0x01, 0x14, 0x0b, 0xb7, 0xb4, + 0x5a, 0x01, 0xef, 0xd4, 0xc1, 0x35, 0x1d, 0x74, 0x4d, 0x1d, 0x1c, 0x42, 0x5f, 0x51, 0x20, 0x7c, + 0x3d, 0x35, 0x4a, 0x61, 0x10, 0xa4, 0xaa, 0x83, 0x9c, 0x74, 0x47, 0x91, 0x5a, 0x5a, 0xe8, 0x73, + 0x78, 0x64, 0x12, 0x26, 0x66, 0x6d, 0x6c, 0x92, 0x1e, 0xc3, 0xd0, 0xa0, 0xe9, 0xff, 0x91, 0x33, + 0x18, 0xcd, 0x31, 0xf4, 0x59, 0x5a, 0xa1, 0xf4, 0x05, 0xec, 0x65, 0x0c, 0xca, 0x78, 0x6b, 0x3a, + 0x1c, 0xe7, 0xef, 0x2e, 0x8f, 0xcc, 0x03, 0x84, 0x28, 0x2a, 0x18, 0xa2, 0xde, 0xed, 0xa2, 0x78, + 0x2d, 0xea, 0xfa, 0xc8, 0x71, 0x2b, 0x29, 0x8d, 0x44, 0x25, 0x23, 0x2b, 0xf4, 0xd3, 0xe9, 0xdf, + 0x2e, 0xf4, 0xde, 0xb3, 0x08, 0xc9, 0x5b, 0xb0, 0xb4, 0x5d, 0x40, 0x0e, 0xc6, 0x62, 0xc5, 0xd4, + 0xb7, 0x83, 0xf3, 0x40, 0x3a, 0x0a, 0x51, 0xd2, 0xd6, 0xab, 0x36, 0x39, 0x05, 0x4b, 0x13, 0x52, + 0x96, 0x5b, 0xdf, 0x01, 0xce, 0xe3, 0xba, 0x23, 0xf4, 0x53, 0xda, 0x22, 0xef, 0x60, 0xa0, 0x3f, + 0x64, 0x62, 0x6b, 0xd5, 0x8d, 0xb7, 0xed, 0x3c, 0x94, 0x9e, 0x52, 0x69, 0xb2, 0xfe, 0x0c, 0x06, + 0x3a, 0xbb, 0x59, 0x7a, 0xc3, 0x0b, 0x75, 0x9e, 0x34, 0x78, 0x54, 0x0b, 0xa7, 0x60, 0x69, 0xb4, + 0x67, 0x33, 0xd4, 0xdf, 0x6b, 0x36, 0x43, 0x55, 0x21, 0xb4, 0x45, 0x16, 0x70, 0xdf, 0x60, 0x92, + 0x3c, 0x95, 0x91, 0x4d, 0x0a, 0x71, 0x0e, 0x9a, 0x5c, 0x1a, 0x8c, 0x46, 0x53, 0x01, 0x53, 0x27, + 0xbc, 0x80, 0xa9, 0xb2, 0x4a, 0x5b, 0xb3, 0xdd, 0xaf, 0x3d, 0xb1, 0xf8, 0xcf, 0x77, 0xe5, 0xa6, + 0x3f, 0xf9, 0x17, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x3f, 0x12, 0xf8, 0x5b, 0x06, 0x00, 0x00, } diff --git a/api/core/api.proto b/api/core/api.proto index 2d8bd3794..6c26fcc88 100644 --- a/api/core/api.proto +++ b/api/core/api.proto @@ -17,7 +17,7 @@ service Core { message ListenEventRequest { string serviceID = 1; - string eventKey = 2; + string eventFilter = 2; } message ExecuteTaskRequest { @@ -28,8 +28,8 @@ message ExecuteTaskRequest { message ListenResultRequest { string serviceID = 1; - string taskKey = 2; - string outputKey = 3; + string taskFilter = 2; + string outputFilter = 3; } message StartServiceRequest { diff --git a/api/core/listen_event.go b/api/core/listen_event.go index 65d21bc4a..a7e9a571c 100644 --- a/api/core/listen_event.go +++ b/api/core/listen_event.go @@ -17,7 +17,7 @@ func (s *Server) ListenEvent(request *ListenEventRequest, stream Core_ListenEven if err != nil { return } - if err = validateEventKey(&service, request.EventKey); err != nil { + if err = validateEventKey(&service, request.EventFilter); err != nil { return } subscription := pubsub.Subscribe(service.EventSubscriptionChannel()) @@ -34,23 +34,23 @@ func (s *Server) ListenEvent(request *ListenEventRequest, stream Core_ListenEven return } -func validateEventKey(service *service.Service, eventKey string) (err error) { - if eventKey == "" { +func validateEventKey(service *service.Service, eventFilter string) (err error) { + if eventFilter == "" { return } - if eventKey == "*" { + if eventFilter == "*" { return } - _, ok := service.Events[eventKey] + _, ok := service.Events[eventFilter] if ok { return } - err = errors.New("Invalid eventKey: " + eventKey) + err = errors.New("Invalid eventFilter: " + eventFilter) return } func isSubscribedEvent(request *ListenEventRequest, e *event.Event) bool { - if request.EventKey != "" && request.EventKey != "*" && request.EventKey != e.Key { + if request.EventFilter != "" && request.EventFilter != "*" && request.EventFilter != e.Key { return false } // Possibility to add more filters here like filters on data, awlays return the diff --git a/api/core/listen_event_test.go b/api/core/listen_event_test.go index 57105a485..bf32a4f60 100644 --- a/api/core/listen_event_test.go +++ b/api/core/listen_event_test.go @@ -25,15 +25,15 @@ func TestIsSubscribedEvent(t *testing.T) { r := &ListenEventRequest{} assert.True(t, isSubscribedEvent(r, e)) - r = &ListenEventRequest{EventKey: ""} + r = &ListenEventRequest{EventFilter: ""} assert.True(t, isSubscribedEvent(r, e)) - r = &ListenEventRequest{EventKey: "*"} + r = &ListenEventRequest{EventFilter: "*"} assert.True(t, isSubscribedEvent(r, e)) - r = &ListenEventRequest{EventKey: "test"} + r = &ListenEventRequest{EventFilter: "test"} assert.True(t, isSubscribedEvent(r, e)) - r = &ListenEventRequest{EventKey: "xxx"} + r = &ListenEventRequest{EventFilter: "xxx"} assert.False(t, isSubscribedEvent(r, e)) } diff --git a/api/core/listen_result.go b/api/core/listen_result.go index 88dfcab7a..8a1f8781f 100644 --- a/api/core/listen_result.go +++ b/api/core/listen_result.go @@ -17,10 +17,10 @@ func (s *Server) ListenResult(request *ListenResultRequest, stream Core_ListenRe if err != nil { return } - if err = validateTaskKey(&service, request.TaskKey); err != nil { + if err = validateTaskKey(&service, request.TaskFilter); err != nil { return } - if err = validateOutputKey(&service, request.TaskKey, request.OutputKey); err != nil { + if err = validateOutputKey(&service, request.TaskFilter, request.OutputFilter); err != nil { return } subscription := pubsub.Subscribe(service.ResultSubscriptionChannel()) @@ -39,26 +39,26 @@ func (s *Server) ListenResult(request *ListenResultRequest, stream Core_ListenRe return } -func validateTaskKey(service *service.Service, taskKey string) (err error) { - if taskKey == "" { +func validateTaskKey(service *service.Service, taskFilter string) (err error) { + if taskFilter == "" { return } - if taskKey == "*" { + if taskFilter == "*" { return } - _, ok := service.Tasks[taskKey] + _, ok := service.Tasks[taskFilter] if ok { return } - err = errors.New("Invalid taskKey: " + taskKey) + err = errors.New("Invalid taskFilter: " + taskFilter) return } -func validateOutputKey(service *service.Service, taskKey string, outputKey string) (err error) { - if outputKey == "" { +func validateOutputKey(service *service.Service, taskKey string, outputFilter string) (err error) { + if outputFilter == "" { return } - if outputKey == "*" { + if outputFilter == "*" { return } task, ok := service.Tasks[taskKey] @@ -66,20 +66,20 @@ func validateOutputKey(service *service.Service, taskKey string, outputKey strin err = errors.New("Invalid taskKey: " + taskKey) return } - _, ok = task.Outputs[outputKey] + _, ok = task.Outputs[outputFilter] if ok { return } - err = errors.New("Invalid outputKey: " + outputKey) + err = errors.New("Invalid outputFilter: " + outputFilter) return } func isSubscribedTask(request *ListenResultRequest, e *execution.Execution) bool { - return includedIn([]string{"", "*", e.Task}, request.TaskKey) + return includedIn([]string{"", "*", e.Task}, request.TaskFilter) } func isSubscribedOutput(request *ListenResultRequest, e *execution.Execution) bool { - return includedIn([]string{"", "*", e.Output}, request.OutputKey) + return includedIn([]string{"", "*", e.Output}, request.OutputFilter) } func includedIn(arr []string, value string) bool { diff --git a/api/core/listen_result_test.go b/api/core/listen_result_test.go index ba1847d5a..33eb3028a 100644 --- a/api/core/listen_result_test.go +++ b/api/core/listen_result_test.go @@ -56,16 +56,16 @@ func TestIsSubscribedTask(t *testing.T) { r := &ListenResultRequest{} assert.True(t, isSubscribedTask(r, x)) - r = &ListenResultRequest{TaskKey: ""} + r = &ListenResultRequest{TaskFilter: ""} assert.True(t, isSubscribedTask(r, x)) - r = &ListenResultRequest{TaskKey: "*"} + r = &ListenResultRequest{TaskFilter: "*"} assert.True(t, isSubscribedTask(r, x)) - r = &ListenResultRequest{TaskKey: "task"} + r = &ListenResultRequest{TaskFilter: "task"} assert.True(t, isSubscribedTask(r, x)) - r = &ListenResultRequest{TaskKey: "xxx"} + r = &ListenResultRequest{TaskFilter: "xxx"} assert.False(t, isSubscribedTask(r, x)) } @@ -74,15 +74,15 @@ func TestIsSubscribedOutput(t *testing.T) { r := &ListenResultRequest{} assert.True(t, isSubscribedOutput(r, x)) - r = &ListenResultRequest{OutputKey: ""} + r = &ListenResultRequest{OutputFilter: ""} assert.True(t, isSubscribedOutput(r, x)) - r = &ListenResultRequest{OutputKey: "*"} + r = &ListenResultRequest{OutputFilter: "*"} assert.True(t, isSubscribedOutput(r, x)) - r = &ListenResultRequest{OutputKey: "output"} + r = &ListenResultRequest{OutputFilter: "output"} assert.True(t, isSubscribedOutput(r, x)) - r = &ListenResultRequest{OutputKey: "xxx"} + r = &ListenResultRequest{OutputFilter: "xxx"} assert.False(t, isSubscribedOutput(r, x)) } diff --git a/cmd/service/test.go b/cmd/service/test.go index c8b33380f..6eddb8b4c 100644 --- a/cmd/service/test.go +++ b/cmd/service/test.go @@ -31,8 +31,8 @@ mesg-core service test --keep-alive`, func listenEvents(serviceID string, filter string) { stream, err := cli.ListenEvent(context.Background(), &core.ListenEventRequest{ - ServiceID: serviceID, - EventKey: filter, + ServiceID: serviceID, + EventFilter: filter, }) handleError(err) for { @@ -47,9 +47,9 @@ func listenEvents(serviceID string, filter string) { func listenResults(serviceID string, result string, output string) { stream, err := cli.ListenResult(context.Background(), &core.ListenResultRequest{ - ServiceID: serviceID, - TaskKey: result, - OutputKey: output, + ServiceID: serviceID, + TaskFilter: result, + OutputFilter: output, }) handleError(err) for { From 2405b3bff17486e25a4de8c19bb0eb797790f4e2 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 17:54:22 -0700 Subject: [PATCH 08/17] add changelog --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bf37d28c..8a435fd96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ #### Added - (#174) Add CHANGELOG.md file +- (#179) [API] Add `eventFilter` on `ListenEvent` API to get notification when an event with a specific name occurs + [API] Add `taskFilter` on `ListenResult` API to get notification when a result from a specific task occurs + [API] Add `outputFilter` on `ListenResult` API to get notification when a result returns a specific output #### Removed -#### Fixed \ No newline at end of file +#### Fixed +- (#179) [Doc] Outdated documentation for the CLI \ No newline at end of file From 1c45d9c2d1b069ad557cecb55f02ba7d72d205a5 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Thu, 31 May 2018 18:00:37 -0700 Subject: [PATCH 09/17] refacto of the includedin method --- api/core/listen_event.go | 8 ++------ api/core/listen_result.go | 19 +++---------------- api/core/listen_result_test.go | 11 ----------- utils/array/include.go | 16 ++++++++++++++++ utils/array/include_test.go | 18 ++++++++++++++++++ 5 files changed, 39 insertions(+), 33 deletions(-) create mode 100644 utils/array/include.go create mode 100644 utils/array/include_test.go diff --git a/api/core/listen_event.go b/api/core/listen_event.go index a7e9a571c..5ee2b8035 100644 --- a/api/core/listen_event.go +++ b/api/core/listen_event.go @@ -6,6 +6,7 @@ import ( "github.com/mesg-foundation/core/database/services" service "github.com/mesg-foundation/core/service" + "github.com/mesg-foundation/core/utils/array" "github.com/mesg-foundation/core/event" "github.com/mesg-foundation/core/pubsub" @@ -50,10 +51,5 @@ func validateEventKey(service *service.Service, eventFilter string) (err error) } func isSubscribedEvent(request *ListenEventRequest, e *event.Event) bool { - if request.EventFilter != "" && request.EventFilter != "*" && request.EventFilter != e.Key { - return false - } - // Possibility to add more filters here like filters on data, awlays return the - // falsy value and go until the end to have the truth value - return true + return array.IncludedIn([]string{"", "*", e.Key}, request.EventFilter) } diff --git a/api/core/listen_result.go b/api/core/listen_result.go index 8a1f8781f..77472b6e9 100644 --- a/api/core/listen_result.go +++ b/api/core/listen_result.go @@ -6,6 +6,7 @@ import ( "github.com/mesg-foundation/core/database/services" service "github.com/mesg-foundation/core/service" + "github.com/mesg-foundation/core/utils/array" "github.com/mesg-foundation/core/execution" "github.com/mesg-foundation/core/pubsub" @@ -75,23 +76,9 @@ func validateOutputKey(service *service.Service, taskKey string, outputFilter st } func isSubscribedTask(request *ListenResultRequest, e *execution.Execution) bool { - return includedIn([]string{"", "*", e.Task}, request.TaskFilter) + return array.IncludedIn([]string{"", "*", e.Task}, request.TaskFilter) } func isSubscribedOutput(request *ListenResultRequest, e *execution.Execution) bool { - return includedIn([]string{"", "*", e.Output}, request.OutputFilter) -} - -func includedIn(arr []string, value string) bool { - if len(arr) == 0 { - return false - } - i := 0 - for _, item := range arr { - if item == value { - break - } - i++ - } - return i != len(arr) + return array.IncludedIn([]string{"", "*", e.Output}, request.OutputFilter) } diff --git a/api/core/listen_result_test.go b/api/core/listen_result_test.go index 33eb3028a..b8ccba14c 100644 --- a/api/core/listen_result_test.go +++ b/api/core/listen_result_test.go @@ -40,17 +40,6 @@ func TestValidateOutputKey(t *testing.T) { assert.NotNil(t, validateOutputKey(s, "xxx", "xxx")) } -func TestIncludedIn(t *testing.T) { - assert.False(t, includedIn([]string{}, "")) - assert.True(t, includedIn([]string{""}, "")) - assert.False(t, includedIn([]string{"a"}, "")) - assert.True(t, includedIn([]string{"a"}, "a")) - assert.False(t, includedIn([]string{""}, "a")) - assert.True(t, includedIn([]string{"a", "b"}, "a")) - assert.True(t, includedIn([]string{"a", "b"}, "b")) - assert.False(t, includedIn([]string{"a", "b"}, "c")) -} - func TestIsSubscribedTask(t *testing.T) { x := &execution.Execution{Task: "task"} r := &ListenResultRequest{} diff --git a/utils/array/include.go b/utils/array/include.go new file mode 100644 index 000000000..31908403a --- /dev/null +++ b/utils/array/include.go @@ -0,0 +1,16 @@ +package array + +// IncludedIn returns true if the element value is in the arr, otherwise return false +func IncludedIn(arr []string, value string) bool { + if len(arr) == 0 { + return false + } + i := 0 + for _, item := range arr { + if item == value { + break + } + i++ + } + return i != len(arr) +} diff --git a/utils/array/include_test.go b/utils/array/include_test.go new file mode 100644 index 000000000..f7b845dc6 --- /dev/null +++ b/utils/array/include_test.go @@ -0,0 +1,18 @@ +package array + +import ( + "testing" + + "github.com/stvp/assert" +) + +func TestIncludedIn(t *testing.T) { + assert.False(t, IncludedIn([]string{}, "")) + assert.True(t, IncludedIn([]string{""}, "")) + assert.False(t, IncludedIn([]string{"a"}, "")) + assert.True(t, IncludedIn([]string{"a"}, "a")) + assert.False(t, IncludedIn([]string{""}, "a")) + assert.True(t, IncludedIn([]string{"a", "b"}, "a")) + assert.True(t, IncludedIn([]string{"a", "b"}, "b")) + assert.False(t, IncludedIn([]string{"a", "b"}, "c")) +} From c129726bb87602bdb6ce0b3d342924947144db77 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Fri, 1 Jun 2018 10:34:51 -0700 Subject: [PATCH 10/17] test pgp signature From 09c1b88651256952b47dd91375e6f4082f3b9d0c Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Fri, 1 Jun 2018 10:41:55 -0700 Subject: [PATCH 11/17] test pgp signature From 40b9496d37b48a4c6f6beaec8b97a8721b4b3457 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Sat, 2 Jun 2018 12:12:25 -0700 Subject: [PATCH 12/17] simplify validate output key function --- api/core/listen_result.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/api/core/listen_result.go b/api/core/listen_result.go index 77472b6e9..0f8a26880 100644 --- a/api/core/listen_result.go +++ b/api/core/listen_result.go @@ -56,10 +56,7 @@ func validateTaskKey(service *service.Service, taskFilter string) (err error) { } func validateOutputKey(service *service.Service, taskKey string, outputFilter string) (err error) { - if outputFilter == "" { - return - } - if outputFilter == "*" { + if outputFilter == "" || outputFilter == "*" { return } task, ok := service.Tasks[taskKey] From b0e36f7bc8157374e6aca9addc410bca7cd7fe36 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Tue, 5 Jun 2018 17:21:13 +0700 Subject: [PATCH 13/17] Improve errors in api/core/listen_result. --- api/core/listen_result.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/api/core/listen_result.go b/api/core/listen_result.go index 0f8a26880..d7d4219de 100644 --- a/api/core/listen_result.go +++ b/api/core/listen_result.go @@ -12,7 +12,7 @@ import ( "github.com/mesg-foundation/core/pubsub" ) -// ListenResult will listne for results from a services +// ListenResult will listen for results from a services func (s *Server) ListenResult(request *ListenResultRequest, stream Core_ListenResultServer) (err error) { service, err := services.Get(request.ServiceID) if err != nil { @@ -40,18 +40,15 @@ func (s *Server) ListenResult(request *ListenResultRequest, stream Core_ListenRe return } -func validateTaskKey(service *service.Service, taskFilter string) (err error) { - if taskFilter == "" { +func validateTaskKey(service *service.Service, taskKey string) (err error) { + if taskKey == "" || taskKey == "*" { return } - if taskFilter == "*" { - return - } - _, ok := service.Tasks[taskFilter] + _, ok := service.Tasks[taskKey] if ok { return } - err = errors.New("Invalid taskFilter: " + taskFilter) + err = errors.New("Task '" + taskKey + "' doesn't exist in this service") return } @@ -59,16 +56,20 @@ func validateOutputKey(service *service.Service, taskKey string, outputFilter st if outputFilter == "" || outputFilter == "*" { return } + if taskKey == "" { + err = errors.New("Cannot filter output without specifying a task") + return + } task, ok := service.Tasks[taskKey] if !ok { - err = errors.New("Invalid taskKey: " + taskKey) + err = errors.New("Task '" + taskKey + "' doesn't exist in this service") return } _, ok = task.Outputs[outputFilter] if ok { return } - err = errors.New("Invalid outputFilter: " + outputFilter) + err = errors.New("Output '" + outputFilter + "' doesn't exist in the task '" + taskKey + "' of this service") return } From 3b1c5e24f14ed3ba996041d5638b1bec845f606b Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Tue, 5 Jun 2018 17:21:47 +0700 Subject: [PATCH 14/17] Rename service to serviceID in command test. Improve flag description. --- cmd/service/test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/service/test.go b/cmd/service/test.go index 3db9b3be7..a17e69abf 100644 --- a/cmd/service/test.go +++ b/cmd/service/test.go @@ -59,7 +59,7 @@ func listenResults(serviceID string, result string, output string) { log.Println(aurora.Red(err)) return } - log.Println("Receive result", aurora.Green(result.TaskKey), aurora.Green(result.OutputKey), ":", aurora.Bold(result.OutputData)) + log.Println("Receive result", aurora.Green(result.TaskKey), aurora.Cyan(result.OutputKey), ":", aurora.Bold(result.OutputData)) } } @@ -85,7 +85,7 @@ func executeTask(serviceID string, task string, dataPath string) (execution *cor func testHandler(cmd *cobra.Command, args []string) { var err error - serviceID := cmd.Flag("service").Value.String() + serviceID := cmd.Flag("serviceID").Value.String() if serviceID == "" { service := loadService(defaultPath(args)) imageHash := buildDockerImage(defaultPath(args)) @@ -126,11 +126,11 @@ func testHandler(cmd *cobra.Command, args []string) { } func init() { - Test.Flags().StringP("event", "e", "*", "Only log a specific event") - Test.Flags().StringP("task", "t", "", "Run a specific task") + Test.Flags().StringP("event", "e", "*", "Only log the data of the given event") + Test.Flags().StringP("task", "t", "", "Run the given task") Test.Flags().StringP("data", "d", "", "Path to the file containing the data required to run the task") - Test.Flags().StringP("result", "r", "", "Filter the result of a specific task") - Test.Flags().StringP("output", "o", "", "Filter output of a task") - Test.Flags().StringP("service", "s", "", "Debug a deployed service") - Test.Flags().BoolP("keep-alive", "", false, "Do not stop the service") + Test.Flags().StringP("result", "r", "", "Only log the result of the given task") + Test.Flags().StringP("output", "o", "", "Only log the data of the given output of a task result. If set, you also need to set the task in --result") + Test.Flags().StringP("serviceID", "s", "", "ID of a previously deployed service") + Test.Flags().BoolP("keep-alive", "", false, "Do not stop the service at the end of this command") } From f055462f9b442b4cb254066b604e601b69c874cd Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Tue, 5 Jun 2018 17:24:02 +0700 Subject: [PATCH 15/17] Improve errors in api/core/listen_event --- api/core/listen_event.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/api/core/listen_event.go b/api/core/listen_event.go index 5ee2b8035..7a3731db6 100644 --- a/api/core/listen_event.go +++ b/api/core/listen_event.go @@ -35,18 +35,15 @@ func (s *Server) ListenEvent(request *ListenEventRequest, stream Core_ListenEven return } -func validateEventKey(service *service.Service, eventFilter string) (err error) { - if eventFilter == "" { +func validateEventKey(service *service.Service, eventKey string) (err error) { + if eventKey == "" || eventKey == "*" { return } - if eventFilter == "*" { - return - } - _, ok := service.Events[eventFilter] + _, ok := service.Events[eventKey] if ok { return } - err = errors.New("Invalid eventFilter: " + eventFilter) + err = errors.New("Event '" + eventKey + "' doesn't exist in this service") return } From 5f2b1a3fa5ab751f26c469493e28477985298eda Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Tue, 5 Jun 2018 17:27:26 +0700 Subject: [PATCH 16/17] Rename service test flags event, result and output to event-filter, task-filter, output-filter to match with the API. --- cmd/service/test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/service/test.go b/cmd/service/test.go index a17e69abf..1e9527385 100644 --- a/cmd/service/test.go +++ b/cmd/service/test.go @@ -107,8 +107,8 @@ func testHandler(cmd *cobra.Command, args []string) { fmt.Println(aurora.Green("Service started")) } - go listenEvents(serviceID, cmd.Flag("event").Value.String()) - go listenResults(serviceID, cmd.Flag("result").Value.String(), cmd.Flag("output").Value.String()) + go listenEvents(serviceID, cmd.Flag("event-filter").Value.String()) + go listenResults(serviceID, cmd.Flag("task-filter").Value.String(), cmd.Flag("output-filter").Value.String()) time.Sleep(time.Second) executeTask(serviceID, cmd.Flag("task").Value.String(), cmd.Flag("data").Value.String()) @@ -126,11 +126,11 @@ func testHandler(cmd *cobra.Command, args []string) { } func init() { - Test.Flags().StringP("event", "e", "*", "Only log the data of the given event") + Test.Flags().StringP("event-filter", "e", "*", "Only log the data of the given event") Test.Flags().StringP("task", "t", "", "Run the given task") Test.Flags().StringP("data", "d", "", "Path to the file containing the data required to run the task") - Test.Flags().StringP("result", "r", "", "Only log the result of the given task") - Test.Flags().StringP("output", "o", "", "Only log the data of the given output of a task result. If set, you also need to set the task in --result") + Test.Flags().StringP("task-filter", "r", "", "Only log the result of the given task") + Test.Flags().StringP("output-filter", "o", "", "Only log the data of the given output of a task result. If set, you also need to set the task in --result") Test.Flags().StringP("serviceID", "s", "", "ID of a previously deployed service") Test.Flags().BoolP("keep-alive", "", false, "Do not stop the service at the end of this command") } From dacfc34f8bec3d2bf2494c7f8da2802024707a3f Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Tue, 5 Jun 2018 17:34:10 +0700 Subject: [PATCH 17/17] Fix CodeClimate issue --- api/core/listen_result.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/api/core/listen_result.go b/api/core/listen_result.go index d7d4219de..7cc3e2c89 100644 --- a/api/core/listen_result.go +++ b/api/core/listen_result.go @@ -66,10 +66,9 @@ func validateOutputKey(service *service.Service, taskKey string, outputFilter st return } _, ok = task.Outputs[outputFilter] - if ok { - return + if !ok { + err = errors.New("Output '" + outputFilter + "' doesn't exist in the task '" + taskKey + "' of this service") } - err = errors.New("Output '" + outputFilter + "' doesn't exist in the task '" + taskKey + "' of this service") return }