latest oclib that shared workflow on update / delete + comments

This commit is contained in:
mr 2024-08-30 10:49:07 +02:00
parent 5b320bb409
commit fe865b517b
4 changed files with 42 additions and 15 deletions

View File

@ -3,7 +3,6 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
oclib "cloud.o-forge.io/core/oc-lib" oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
@ -35,7 +34,7 @@ func (o *WorkflowController) Search() {
// @Success 200 {object} models.workflow // @Success 200 {object} models.workflow
// @router /:id [put] // @router /:id [put]
func (o *WorkflowController) Put() { func (o *WorkflowController) Put() {
var paths = map[string]map[tools.METHOD]string{ var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services
utils.BOOKING.String(): { utils.BOOKING.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/", tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id", tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id",
@ -43,12 +42,16 @@ func (o *WorkflowController) Put() {
utils.PEER.String(): { utils.PEER.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc", tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
}, },
utils.WORKFLOW.String(): {
tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id",
},
} }
// store and return Id or post with UUID // store and return Id or post with UUID
var res map[string]interface{} var res map[string]interface{}
id := o.Ctx.Input.Param(":id") id := o.Ctx.Input.Param(":id")
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res) json.Unmarshal(o.Ctx.Input.CopyBody(10000000), &res)
caller := tools.NewHTTPCaller(paths) caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
data := oclib.UpdateOne(oclib.LibDataEnum(oclib.WORKFLOW), res, id, caller) data := oclib.UpdateOne(oclib.LibDataEnum(oclib.WORKFLOW), res, id, caller)
o.Data["json"] = data o.Data["json"] = data
o.ServeJSON() o.ServeJSON()
@ -60,7 +63,7 @@ func (o *WorkflowController) Put() {
// @Success 200 {object} models.workflow // @Success 200 {object} models.workflow
// @router / [post] // @router / [post]
func (o *WorkflowController) Post() { func (o *WorkflowController) Post() {
var paths = map[string]map[tools.METHOD]string{ var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services
utils.BOOKING.String(): { utils.BOOKING.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/", tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id", tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id",
@ -70,8 +73,8 @@ func (o *WorkflowController) Post() {
}, },
} }
var res map[string]interface{} var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res) json.Unmarshal(o.Ctx.Input.CopyBody(10000000), &res)
caller := tools.NewHTTPCaller(paths) caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
data := oclib.StoreOne(oclib.LibDataEnum(oclib.WORKFLOW), res, caller) data := oclib.StoreOne(oclib.LibDataEnum(oclib.WORKFLOW), res, caller)
o.Data["json"] = data o.Data["json"] = data
o.ServeJSON() o.ServeJSON()
@ -85,7 +88,7 @@ func (o *WorkflowController) Post() {
func (o *WorkflowController) Publish() { func (o *WorkflowController) Publish() {
id := o.Ctx.Input.Param(":id") id := o.Ctx.Input.Param(":id")
data := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id) data := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id)
if data.Data != nil { if data.Data != nil { // copy existing workflow as a resource
o.Data["json"] = oclib.CopyOne(oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE), data.Data.Serialize()) o.Data["json"] = oclib.CopyOne(oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE), data.Data.Serialize())
} else { } else {
o.Data["json"] = data o.Data["json"] = data
@ -119,7 +122,7 @@ func (o *WorkflowController) Get() {
// @Success 200 {string} delete success! // @Success 200 {string} delete success!
// @router /:id [delete] // @router /:id [delete]
func (o *WorkflowController) Delete() { func (o *WorkflowController) Delete() {
var paths = map[string]map[tools.METHOD]string{ var paths = map[string]map[tools.METHOD]string{ // paths to call other OC services
utils.BOOKING.String(): { utils.BOOKING.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/", tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id", tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/:id",
@ -127,9 +130,13 @@ func (o *WorkflowController) Delete() {
utils.PEER.String(): { utils.PEER.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc", tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
}, },
utils.WORKFLOW.String(): {
tools.PUT: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id",
tools.DELETE: oclib.GetPath(oclib.LibDataEnum(oclib.WORKFLOW)) + "/oc/workflow/:id",
},
} }
id := o.Ctx.Input.Param(":id") id := o.Ctx.Input.Param(":id")
caller := tools.NewHTTPCaller(paths) caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.WORKFLOW), id, caller) o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.WORKFLOW), id, caller)
o.ServeJSON() o.ServeJSON()
} }
@ -143,6 +150,9 @@ func (o *WorkflowController) Delete() {
// @router /check/:id/:start_date/:end_date [get] // @router /check/:id/:start_date/:end_date [get]
func (o *WorkflowController) Check() { func (o *WorkflowController) Check() {
// store and return Id or post with UUID // store and return Id or post with UUID
/*
* Check if the booking is available
*/
id := o.Ctx.Input.Param(":id") id := o.Ctx.Input.Param(":id")
if id == "" { if id == "" {
o.Data["json"] = map[string]interface{}{ o.Data["json"] = map[string]interface{}{
@ -153,11 +163,10 @@ func (o *WorkflowController) Check() {
"error": errors.New("invalid date format"), "error": errors.New("invalid date format"),
} }
} else { } else {
fmt.Println("id", id)
res := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id) res := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), id)
if res.Code == 200 { if res.Code == 200 {
workflow := res.ToWorkflow() workflow := res.ToWorkflow()
caller := tools.NewHTTPCaller(map[string]map[tools.METHOD]string{ caller := tools.NewHTTPCaller(map[string]map[tools.METHOD]string{ // paths to call other OC services
utils.PEER.String(): { utils.PEER.String(): {
tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc", tools.POST: oclib.GetPath(oclib.LibDataEnum(oclib.PEER)) + "/oc",
}, },
@ -165,9 +174,9 @@ func (o *WorkflowController) Check() {
tools.GET: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/check/:id/" + o.Ctx.Input.Param(":start_date") + "/" + o.Ctx.Input.Param(":end_date"), tools.GET: oclib.GetPath(oclib.LibDataEnum(oclib.BOOKING)) + "/oc/booking/check/:id/" + o.Ctx.Input.Param(":start_date") + "/" + o.Ctx.Input.Param(":end_date"),
}, },
}) })
isAvailable, err := workflow.CheckBooking(caller) isAvailable, err := workflow.CheckBooking(caller) // check booking
code := 200 code := 200
if !isAvailable { if !isAvailable { // if not available then its a conflict
code = 409 code = 409
} }
o.Data["json"] = map[string]interface{}{ o.Data["json"] = map[string]interface{}{

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.22.0
toolchain go1.22.4 toolchain go1.22.4
require ( require (
cloud.o-forge.io/core/oc-lib v0.0.0-20240826085916-d0e1474f8f34 cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349
github.com/beego/beego/v2 v2.2.2 github.com/beego/beego/v2 v2.2.2
github.com/goraz/onion v0.1.3 github.com/goraz/onion v0.1.3
github.com/nats-io/nats.go v1.37.0 github.com/nats-io/nats.go v1.37.0

12
go.sum
View File

@ -234,6 +234,18 @@ cloud.o-forge.io/core/oc-lib v0.0.0-20240826082633-7c44365f527c h1:qo27sWEWWPeVy
cloud.o-forge.io/core/oc-lib v0.0.0-20240826082633-7c44365f527c/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU= cloud.o-forge.io/core/oc-lib v0.0.0-20240826082633-7c44365f527c/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU=
cloud.o-forge.io/core/oc-lib v0.0.0-20240826085916-d0e1474f8f34 h1:40XQgwR9HxXSnouY+ZqE/xYCM4qa+U+RLA5GA5JSNyQ= cloud.o-forge.io/core/oc-lib v0.0.0-20240826085916-d0e1474f8f34 h1:40XQgwR9HxXSnouY+ZqE/xYCM4qa+U+RLA5GA5JSNyQ=
cloud.o-forge.io/core/oc-lib v0.0.0-20240826085916-d0e1474f8f34/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU= cloud.o-forge.io/core/oc-lib v0.0.0-20240826085916-d0e1474f8f34/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828114159-ede91cde4583 h1:5DVFZLJ4tySspQwk0H1HRR+arWSRsYsujP4N8zxvy2Q=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828114159-ede91cde4583/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828124456-4a3707efcf3b h1:HyaQI47lu7n3+nwOCPivYqjuO64Fj162o0eClQKN0yA=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828124456-4a3707efcf3b/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828135227-14d6a5f11c4e h1:/KWO/gIcP5f7T4r00715fNz0Y/Hil6Bj3J1ycuES1Zw=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828135227-14d6a5f11c4e/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828144353-2f44ad0effcb h1:W4Zcfg9oO+HzZtfQ+kiCl5G416dUYWc5qKCHyhsUWDQ=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828144353-2f44ad0effcb/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828151401-a53e3c987d67 h1:d3wYOjR4rc86h4vdeTJ64eOM6KfaqpQXjWSsbpoaGWg=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828151401-a53e3c987d67/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0=
cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349 h1:bEIY1lCsA78/mJqFE0gV6likAv5ZifH3RMnLJxiSk3o=
cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=

View File

@ -34,9 +34,15 @@ func main() {
o.GetStringDefault("NATS_URL", "nats://localhost:4222"), o.GetStringDefault("NATS_URL", "nats://localhost:4222"),
) )
fmt.Println("Config file:", o.GetStringDefault("HOSTNAME", "localhost")) fmt.Println("Config file:", o.GetStringDefault("HOSTNAME", "localhost"))
// Init OC with hostname and port for discovery purpose
oclib.Init(hostname, o.GetStringDefault("HOSTNAME", "localhost"), o.GetStringDefault("PORT", "8088")) oclib.Init(hostname, o.GetStringDefault("HOSTNAME", "localhost"), o.GetStringDefault("PORT", "8088"))
/* PATHS ARE REFERENCE FOR INNER SERVICE OF DISTANT OC
* PATHS ARE USED TO CALL OTHER OC SERVICES
* NAMES ARE CANONICAL NAMES OF THE SERVICES
*/
oclib.AddPath(oclib.LibDataEnum(oclib.BOOKING), o.GetStringDefault("BOOKING_URL", ":8092")) oclib.AddPath(oclib.LibDataEnum(oclib.BOOKING), o.GetStringDefault("BOOKING_URL", ":8092"))
oclib.AddPath(oclib.LibDataEnum(oclib.PEER), o.GetStringDefault("PEER_URL", ":8093")) oclib.AddPath(oclib.LibDataEnum(oclib.PEER), o.GetStringDefault("PEER_URL", ":8093"))
oclib.AddPath(oclib.LibDataEnum(oclib.WORKFLOW), o.GetStringDefault("WORKFLOW_URL", ":"+o.GetStringDefault("PORT", "8088")))
// Normal beego init // Normal beego init
//if beego.BConfig.RunMode == "dev" { //if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.DirectoryIndex = true