From dcdc6ff1d965d85cc0e77f3160a77614b0309a12 Mon Sep 17 00:00:00 2001 From: pb Date: Tue, 8 Jul 2025 12:02:18 +0200 Subject: [PATCH 1/3] added more info in GenericStoreOne error generated by validate() --- models/utils/common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/utils/common.go b/models/utils/common.go index 4e30b23..7259cc5 100755 --- a/models/utils/common.go +++ b/models/utils/common.go @@ -49,7 +49,7 @@ func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) { } err := validate.Struct(data) if err != nil { - return nil, 422, err + return nil, 422, errors.New("error when validating the received struct: " + err.Error()) } id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), a.GetType().String()) if err != nil { From 2748b59221ac124efa996c0943d17e6d65806296 Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 8 Jul 2025 13:42:13 +0200 Subject: [PATCH 2/3] test booking --- models/booking/booking.go | 7 +++---- models/workflow_execution/workflow_execution.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/models/booking/booking.go b/models/booking/booking.go index 831f443..4136625 100644 --- a/models/booking/booking.go +++ b/models/booking/booking.go @@ -6,7 +6,6 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/models/common/enum" "cloud.o-forge.io/core/oc-lib/models/common/models" - "cloud.o-forge.io/core/oc-lib/models/common/pricing" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" "go.mongodb.org/mongo-driver/bson/primitive" @@ -16,10 +15,10 @@ import ( * Booking is a struct that represents a booking */ type Booking struct { - utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name) - PricedItem pricing.PricedItemITF `json:"priced_item,omitempty" bson:"priced_item,omitempty"` // We need to add the validate:"required" tag once the pricing feature is implemented, removed to avoid handling the error + utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name) + PricedItem map[string]interface{} `json:"priced_item,omitempty" bson:"priced_item,omitempty"` // We need to add the validate:"required" tag once the pricing feature is implemented, removed to avoid handling the error - ResumeMetrics map[string]map[string]models.MetricResume `json:"resume_metrics,omitempty" bson:"resume_metrics,omitempty"` + ResumeMetrics map[string]map[string]models.MetricResume `json:"resume_metrics,omitempty" bson:"resume_metrics,omitempty"` ExecutionMetrics map[string][]models.MetricsSnapshot `json:"metrics,omitempty" bson:"metrics,omitempty"` ExecutionsID string `json:"executions_id,omitempty" bson:"executions_id,omitempty" validate:"required"` // ExecutionsID is the ID of the executions diff --git a/models/workflow_execution/workflow_execution.go b/models/workflow_execution/workflow_execution.go index 39ef327..6bb2818 100755 --- a/models/workflow_execution/workflow_execution.go +++ b/models/workflow_execution/workflow_execution.go @@ -1,6 +1,7 @@ package workflow_execution import ( + "encoding/json" "strings" "time" @@ -141,13 +142,16 @@ func (d *WorkflowExecution) buyEach(bs pricing.BillingStrategy, executionsID str if s := priced.GetLocationStart(); s != nil { start = *s } + var m map[string]interface{} + b, _ := json.Marshal(priced) + json.Unmarshal(b, &m) end := start.Add(time.Duration(priced.GetExplicitDurationInS()) * time.Second) bookingItem := &purchase_resource.PurchaseResource{ AbstractObject: utils.AbstractObject{ UUID: uuid.New().String(), Name: d.GetName() + "_" + executionsID + "_" + wfID, }, - PricedItem: priced, + PricedItem: m, ExecutionsID: executionsID, DestPeerID: priced.GetCreatorID(), ResourceID: priced.GetID(), @@ -189,12 +193,15 @@ func (d *WorkflowExecution) bookEach(executionsID string, wfID string, dt tools. start = *s } end := start.Add(time.Duration(priced.GetExplicitDurationInS()) * time.Second) + var m map[string]interface{} + b, _ := json.Marshal(priced) + json.Unmarshal(b, &m) bookingItem := &booking.Booking{ AbstractObject: utils.AbstractObject{ UUID: uuid.New().String(), Name: d.GetName() + "_" + executionsID + "_" + wfID, }, - PricedItem: priced, + PricedItem: m, ExecutionsID: executionsID, State: enum.SCHEDULED, ResourceID: priced.GetID(), From 346275e12cb4880874abf4357cfb5f3fcb02183e Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 8 Jul 2025 13:59:55 +0200 Subject: [PATCH 3/3] test --- models/bill/bill.go | 25 +++++++++++++------ .../purchase_resource/purchase_resource.go | 11 ++++---- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/models/bill/bill.go b/models/bill/bill.go index 5df1751..aa31062 100644 --- a/models/bill/bill.go +++ b/models/bill/bill.go @@ -1,14 +1,15 @@ package bill import ( + "encoding/json" "sync" "time" "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/models/common/enum" - "cloud.o-forge.io/core/oc-lib/models/common/pricing" "cloud.o-forge.io/core/oc-lib/models/order" "cloud.o-forge.io/core/oc-lib/models/peer" + "cloud.o-forge.io/core/oc-lib/models/resources" "cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" @@ -135,20 +136,25 @@ type PeerOrder struct { } func (d *PeerOrder) Pay(request *tools.APIRequest, response chan *PeerOrder, wg *sync.WaitGroup) { + d.Status = enum.PENDING go func() { + // DO SOMETHING TO PAY ON BLOCKCHAIN OR WHATEVER ON RETURN UPDATE STATUS d.Status = enum.PAID // TO REMOVE LATER IT'S A MOCK if d.Status == enum.PAID { for _, b := range d.Items { - if !b.Item.IsPurchasable() { + var priced *resources.PricedResource + bb, _ := json.Marshal(b.Item) + json.Unmarshal(bb, priced) + if !priced.IsPurchasable() { continue } accessor := purchase_resource.NewAccessor(request) accessor.StoreOne(&purchase_resource.PurchaseResource{ - ResourceID: b.Item.GetID(), - ResourceType: b.Item.GetType(), - EndDate: b.Item.GetLocationEnd(), + ResourceID: priced.GetID(), + ResourceType: priced.GetType(), + EndDate: priced.GetLocationEnd(), }) } } @@ -174,14 +180,17 @@ func (d *PeerOrder) SumUpBill(request *tools.APIRequest) error { type PeerItemOrder struct { Quantity int `json:"quantity,omitempty" bson:"quantity,omitempty"` Purchase *purchase_resource.PurchaseResource `json:"purchase,omitempty" bson:"purchase,omitempty"` - Item pricing.PricedItemITF `json:"item,omitempty" bson:"item,omitempty"` + Item map[string]interface{} `json:"item,omitempty" bson:"item,omitempty"` } func (d *PeerItemOrder) GetPrice(request *tools.APIRequest) (float64, error) { + var priced *resources.PricedResource + b, _ := json.Marshal(d.Item) + json.Unmarshal(b, priced) accessor := purchase_resource.NewAccessor(request) search, code, _ := accessor.Search(&dbs.Filters{ And: map[string][]dbs.Filter{ - "resource_id": {{Operator: dbs.EQUAL.String(), Value: d.Item.GetID()}}, + "resource_id": {{Operator: dbs.EQUAL.String(), Value: priced.GetID()}}, }, }, "", d.Purchase.IsDraft) if code == 200 && len(search) > 0 { @@ -191,7 +200,7 @@ func (d *PeerItemOrder) GetPrice(request *tools.APIRequest) (float64, error) { } } } - p, err := d.Item.GetPrice() + p, err := priced.GetPrice() if err != nil { return 0, err } diff --git a/models/resources/purchase_resource/purchase_resource.go b/models/resources/purchase_resource/purchase_resource.go index ca528ae..0767981 100644 --- a/models/resources/purchase_resource/purchase_resource.go +++ b/models/resources/purchase_resource/purchase_resource.go @@ -3,7 +3,6 @@ package purchase_resource import ( "time" - "cloud.o-forge.io/core/oc-lib/models/common/pricing" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" ) @@ -11,11 +10,11 @@ import ( type PurchaseResource struct { utils.AbstractObject DestPeerID string - PricedItem pricing.PricedItemITF `json:"priced_item,omitempty" bson:"priced_item,omitempty" validate:"required"` - ExecutionsID string `json:"executions_id,omitempty" bson:"executions_id,omitempty" validate:"required"` // ExecutionsID is the ID of the executions - EndDate *time.Time `json:"end_buying_date,omitempty" bson:"end_buying_date,omitempty"` - ResourceID string `json:"resource_id" bson:"resource_id" validate:"required"` - ResourceType tools.DataType `json:"resource_type" bson:"resource_type" validate:"required"` + PricedItem map[string]interface{} `json:"priced_item,omitempty" bson:"priced_item,omitempty" validate:"required"` + ExecutionsID string `json:"executions_id,omitempty" bson:"executions_id,omitempty" validate:"required"` // ExecutionsID is the ID of the executions + EndDate *time.Time `json:"end_buying_date,omitempty" bson:"end_buying_date,omitempty"` + ResourceID string `json:"resource_id" bson:"resource_id" validate:"required"` + ResourceType tools.DataType `json:"resource_type" bson:"resource_type" validate:"required"` } func (d *PurchaseResource) GetAccessor(request *tools.APIRequest) utils.Accessor {