Compare commits
15 Commits
feature/ev
...
payment
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c53e25e69a | ||
|
|
4aebb48e73 | ||
|
|
c9690fc1ba | ||
|
|
cd10104089 | ||
|
|
f6bc19123c | ||
|
|
305e5919f9 | ||
|
|
2ee2320a03 | ||
|
|
9a4cf686ca | ||
|
|
5376cf7681 | ||
|
|
2f0eb9ca35 | ||
|
|
c1d0932301 | ||
|
|
82cb2aa76a | ||
|
|
2cbbb49ce2 | ||
|
|
3c615fd3bc | ||
| 91d3e5c3cd |
3
.gitattributes
vendored
3
.gitattributes
vendored
@@ -1,3 +0,0 @@
|
||||
# Force Go as the main language
|
||||
*.go linguist-detectable=true
|
||||
* linguist-language=Go
|
||||
@@ -26,12 +26,12 @@ import (
|
||||
func GetConfLoader() *onion.Onion {
|
||||
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
|
||||
AppName := GetAppName()
|
||||
EnvPrefix := "OC_"
|
||||
EnvPrefix := strings.ToUpper(AppName[0:2]+AppName[3:]) + "_"
|
||||
defaultConfigFile := "/etc/oc/" + AppName[3:] + ".json"
|
||||
localConfigFile := "./" + AppName[3:] + ".json"
|
||||
var configFile string
|
||||
var o *onion.Onion
|
||||
l3 := GetEnvVarLayer(EnvPrefix)
|
||||
l3 := onion.NewEnvLayerPrefix("_", EnvPrefix)
|
||||
l2, err := onion.NewFileLayer(localConfigFile, nil)
|
||||
if err == nil {
|
||||
logger.Info().Msg("Local config file found " + localConfigFile + ", overriding default file")
|
||||
@@ -54,17 +54,3 @@ func GetConfLoader() *onion.Onion {
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func GetEnvVarLayer(prefix string) onion.Layer {
|
||||
envVars := make(map[string]interface{})
|
||||
|
||||
for _, e := range os.Environ() {
|
||||
pair := strings.SplitN(e, "=", 2)
|
||||
key := pair[0]
|
||||
if strings.HasPrefix(key, prefix) {
|
||||
envVars[strings.TrimPrefix(key, prefix)] = pair[1]
|
||||
}
|
||||
}
|
||||
|
||||
return onion.NewMapLayer(envVars)
|
||||
}
|
||||
|
||||
@@ -287,7 +287,7 @@ func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.C
|
||||
return nil, 503, err
|
||||
}
|
||||
opts := options.Find()
|
||||
opts.SetLimit(1000)
|
||||
opts.SetLimit(100)
|
||||
targetDBCollection := CollectionMap[collection_name]
|
||||
orList := bson.A{}
|
||||
andList := bson.A{}
|
||||
|
||||
156
entrypoint.go
156
entrypoint.go
@@ -15,14 +15,11 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models"
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area"
|
||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
|
||||
"cloud.o-forge.io/core/oc-lib/models/live"
|
||||
"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"
|
||||
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
@@ -54,10 +51,6 @@ const (
|
||||
RULE = tools.RULE
|
||||
BOOKING = tools.BOOKING
|
||||
ORDER = tools.ORDER
|
||||
LIVE_DATACENTER = tools.LIVE_DATACENTER
|
||||
LIVE_STORAGE = tools.LIVE_STORAGE
|
||||
PURCHASE_RESOURCE = tools.PURCHASE_RESOURCE
|
||||
NATIVE_TOOL = tools.NATIVE_TOOL
|
||||
)
|
||||
|
||||
// will turn into standards api hostnames
|
||||
@@ -109,7 +102,7 @@ func InitDaemon(appName string) {
|
||||
logs.SetLogger(logs.CreateLogger("main"))
|
||||
// Load the right config file
|
||||
o := GetConfLoader()
|
||||
resources.InitNative()
|
||||
|
||||
// feed the library with the loaded config
|
||||
SetConfig(
|
||||
o.GetStringDefault("MONGO_URL", "mongodb://127.0.0.1:27017"),
|
||||
@@ -244,7 +237,56 @@ func NewRequest(collection LibDataEnum, user string, peerID string, groups []str
|
||||
return &Request{collection: collection, user: user, peerID: peerID, groups: groups, caller: caller}
|
||||
}
|
||||
|
||||
/*
|
||||
func ToScheduler(m interface{}) (n *workflow_execution.WorkflowSchedule) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
return m.(*workflow_execution.WorkflowSchedule)
|
||||
}
|
||||
|
||||
func (r *Request) Schedule(wfID string, scheduler *workflow_execution.WorkflowSchedule) (*workflow_execution.WorkflowSchedule, error) {
|
||||
ws, _, _, err := scheduler.Schedules(wfID, &tools.APIRequest{
|
||||
Caller: r.caller,
|
||||
Username: r.user,
|
||||
PeerID: r.peerID,
|
||||
Groups: r.groups,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("BAM", ws)
|
||||
return ws, nil
|
||||
}
|
||||
|
||||
func (r *Request) CheckBooking(wfID string, start string, end string, durationInS float64, cron string) bool {
|
||||
ok, _, _, err := workflow_execution.NewScheduler(start, end, durationInS, cron).CheckBooking(wfID, &tools.APIRequest{
|
||||
Caller: r.caller,
|
||||
Username: r.user,
|
||||
PeerID: r.peerID,
|
||||
Groups: r.groups,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return false
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func (r *Request) DraftOrder(scheduler *workflow_execution.WorkflowSchedule) (*order.Order, error) {
|
||||
o := &order.Order{}
|
||||
if err := o.DraftOrder(scheduler, &tools.APIRequest{
|
||||
Caller: r.caller,
|
||||
Username: r.user,
|
||||
PeerID: r.peerID,
|
||||
Groups: r.groups,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
|
||||
func (r *Request) PaymentTunnel(o *order.Order, scheduler *workflow_execution.WorkflowSchedule) error {
|
||||
return o.Pay(scheduler, &tools.APIRequest{
|
||||
Caller: r.caller,
|
||||
@@ -252,9 +294,8 @@ func (r *Request) PaymentTunnel(o *order.Order, scheduler *workflow_execution.Wo
|
||||
PeerID: r.peerID,
|
||||
Groups: r.groups,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
* Search will search for the data in the database
|
||||
* @param filters *dbs.Filters
|
||||
@@ -522,9 +563,9 @@ func (l *LibData) ToRule() *rule.Rule {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecution {
|
||||
func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecutions {
|
||||
if l.Data.GetAccessor(nil).GetType() == tools.WORKFLOW_EXECUTION {
|
||||
return l.Data.(*workflow_execution.WorkflowExecution)
|
||||
return l.Data.(*workflow_execution.WorkflowExecutions)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -535,92 +576,3 @@ func (l *LibData) ToOrder() *order.Order {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LibData) ToLiveDatacenter() *live.LiveDatacenter {
|
||||
if l.Data.GetAccessor(nil).GetType() == tools.LIVE_DATACENTER {
|
||||
return l.Data.(*live.LiveDatacenter)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LibData) ToLiveStorage() *live.LiveStorage {
|
||||
if l.Data.GetAccessor(nil).GetType() == tools.LIVE_STORAGE {
|
||||
return l.Data.(*live.LiveStorage)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LibData) ToBookings() *booking.Booking {
|
||||
if l.Data.GetAccessor(nil).GetType() == tools.BOOKING {
|
||||
return l.Data.(*booking.Booking)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LibData) ToPurchasedResource() *purchase_resource.PurchaseResource {
|
||||
if l.Data.GetAccessor(nil).GetType() == tools.PURCHASE_RESOURCE {
|
||||
return l.Data.(*purchase_resource.PurchaseResource)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ============== ADMIRALTY ==============
|
||||
// Returns a concatenation of the peerId and namespace in order for
|
||||
// kubernetes ressources to have a unique name, under 63 characters
|
||||
// and yet identify which peer they are created for
|
||||
func GetConcatenatedName(peerId string, namespace string) string {
|
||||
s := strings.Split(namespace, "-")[:2]
|
||||
n := s[0] + "-" + s[1]
|
||||
|
||||
return peerId + "-" + n
|
||||
}
|
||||
|
||||
// ------------- Loading resources ----------
|
||||
|
||||
func LoadOneStorage(storageId string, user string, peerID string, groups []string) (*resources.StorageResource, error) {
|
||||
|
||||
res := NewRequest(LibDataEnum(STORAGE_RESOURCE), user, peerID, groups, nil).LoadOne(storageId)
|
||||
if res.Code != 200 {
|
||||
l := GetLogger()
|
||||
l.Error().Msg("Error while loading storage ressource " + storageId)
|
||||
return nil, fmt.Errorf(res.Err)
|
||||
}
|
||||
|
||||
return res.ToStorageResource(), nil
|
||||
}
|
||||
|
||||
func LoadOneComputing(computingId string, user string, peerID string, groups []string) (*resources.ComputeResource, error) {
|
||||
|
||||
res := NewRequest(LibDataEnum(COMPUTE_RESOURCE), user, peerID, groups, nil).LoadOne(computingId)
|
||||
if res.Code != 200 {
|
||||
l := GetLogger()
|
||||
l.Error().Msg("Error while loading computing ressource " + computingId)
|
||||
return nil, fmt.Errorf(res.Err)
|
||||
}
|
||||
|
||||
return res.ToComputeResource(), nil
|
||||
}
|
||||
|
||||
func LoadOneProcessing(processingId string, user string, peerID string, groups []string) (*resources.ProcessingResource, error) {
|
||||
|
||||
res := NewRequest(LibDataEnum(PROCESSING_RESOURCE), user, peerID, groups, nil).LoadOne(processingId)
|
||||
if res.Code != 200 {
|
||||
l := GetLogger()
|
||||
l.Error().Msg("Error while loading processing ressource " + processingId)
|
||||
return nil, fmt.Errorf(res.Err)
|
||||
}
|
||||
|
||||
return res.ToProcessingResource(), nil
|
||||
}
|
||||
|
||||
func LoadOneData(dataId string, user string, peerID string, groups []string) (*resources.DataResource, error) {
|
||||
|
||||
res := NewRequest(LibDataEnum(DATA_RESOURCE), user, peerID, groups, nil).LoadOne(dataId)
|
||||
if res.Code != 200 {
|
||||
l := GetLogger()
|
||||
l.Error().Msg("Error while loading data ressource " + dataId)
|
||||
return nil, fmt.Errorf(res.Err)
|
||||
}
|
||||
return res.ToDataResource(), nil
|
||||
|
||||
}
|
||||
|
||||
3
go.mod
Executable file → Normal file
3
go.mod
Executable file → Normal file
@@ -10,13 +10,12 @@ require (
|
||||
github.com/nats-io/nats.go v1.37.0
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/rs/zerolog v1.33.0
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/nats-io/nkeys v0.4.7 // indirect
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/stretchr/objx v0.5.2 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
4
go.sum
Executable file → Normal file
4
go.sum
Executable file → Normal file
@@ -106,13 +106,9 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8=
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
|
||||
@@ -1,221 +0,0 @@
|
||||
package bill
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"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/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"
|
||||
)
|
||||
|
||||
/*
|
||||
* Booking is a struct that represents a booking
|
||||
*/
|
||||
|
||||
type Bill struct {
|
||||
utils.AbstractObject
|
||||
OrderID string `json:"order_id" bson:"order_id" validate:"required"`
|
||||
Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
|
||||
SubOrders map[string]*PeerOrder `json:"sub_orders" bson:"sub_orders"`
|
||||
Total float64 `json:"total" bson:"total" validate:"required"`
|
||||
}
|
||||
|
||||
func GenerateBill(order *order.Order, request *tools.APIRequest) (*Bill, error) {
|
||||
// hhmmm : should get... the loop.
|
||||
return &Bill{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
Name: "bill_" + request.PeerID + "_" + time.Now().UTC().Format("2006-01-02T15:04:05"),
|
||||
IsDraft: false,
|
||||
},
|
||||
OrderID: order.UUID,
|
||||
Status: enum.PENDING,
|
||||
// SubOrders: peerOrders,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func DraftFirstBill(order *order.Order, request *tools.APIRequest) (*Bill, error) {
|
||||
peers := map[string][]*PeerItemOrder{}
|
||||
for _, p := range order.Purchases {
|
||||
// TODO : if once
|
||||
if _, ok := peers[p.DestPeerID]; !ok {
|
||||
peers[p.DestPeerID] = []*PeerItemOrder{}
|
||||
}
|
||||
peers[p.DestPeerID] = append(peers[p.DestPeerID], &PeerItemOrder{
|
||||
Purchase: p,
|
||||
Item: p.PricedItem,
|
||||
Quantity: 1,
|
||||
})
|
||||
}
|
||||
for _, b := range order.Bookings {
|
||||
// TODO : if once
|
||||
isPurchased := false
|
||||
for _, p := range order.Purchases {
|
||||
if p.ResourceID == b.ResourceID {
|
||||
isPurchased = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if isPurchased {
|
||||
continue
|
||||
}
|
||||
if _, ok := peers[b.DestPeerID]; !ok {
|
||||
peers[b.DestPeerID] = []*PeerItemOrder{}
|
||||
}
|
||||
peers[b.DestPeerID] = append(peers[b.DestPeerID], &PeerItemOrder{
|
||||
Item: b.PricedItem,
|
||||
})
|
||||
}
|
||||
peerOrders := map[string]*PeerOrder{}
|
||||
for peerID, items := range peers {
|
||||
pr, _, err := peer.NewAccessor(request).LoadOne(peerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
peerOrders[peerID] = &PeerOrder{
|
||||
PeerID: peerID,
|
||||
BillingAddress: pr.(*peer.Peer).WalletAddress,
|
||||
Items: items,
|
||||
}
|
||||
}
|
||||
bill := &Bill{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
Name: "bill_" + request.PeerID + "_" + time.Now().UTC().Format("2006-01-02T15:04:05"),
|
||||
IsDraft: true,
|
||||
},
|
||||
OrderID: order.UUID,
|
||||
Status: enum.PENDING,
|
||||
SubOrders: peerOrders,
|
||||
}
|
||||
return bill.SumUpBill(request)
|
||||
}
|
||||
|
||||
func (d *Bill) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
func (r *Bill) StoreDraftDefault() {
|
||||
r.IsDraft = true
|
||||
}
|
||||
|
||||
func (r *Bill) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||
if !r.IsDraft && r.Status != set.(*Bill).Status {
|
||||
return true, &Bill{Status: set.(*Bill).Status} // only state can be updated
|
||||
}
|
||||
return r.IsDraft, set
|
||||
}
|
||||
|
||||
func (r *Bill) CanDelete() bool {
|
||||
return r.IsDraft // only draft order can be deleted
|
||||
}
|
||||
|
||||
func (d *Bill) SumUpBill(request *tools.APIRequest) (*Bill, error) {
|
||||
for _, b := range d.SubOrders {
|
||||
err := b.SumUpBill(request)
|
||||
if err != nil {
|
||||
return d, err
|
||||
}
|
||||
d.Total += b.Total
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
type PeerOrder struct {
|
||||
Error string `json:"error,omitempty" bson:"error,omitempty"`
|
||||
PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"`
|
||||
Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
|
||||
BillingAddress string `json:"billing_address,omitempty" bson:"billing_address,omitempty"`
|
||||
Items []*PeerItemOrder `json:"items,omitempty" bson:"items,omitempty"`
|
||||
Total float64 `json:"total,omitempty" bson:"total,omitempty"`
|
||||
}
|
||||
|
||||
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 {
|
||||
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: priced.GetID(),
|
||||
ResourceType: priced.GetType(),
|
||||
EndDate: priced.GetLocationEnd(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if d.Status != enum.PENDING {
|
||||
response <- d
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
func (d *PeerOrder) SumUpBill(request *tools.APIRequest) error {
|
||||
for _, b := range d.Items {
|
||||
tot, err := b.GetPriceHT(request) // missing something
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.Total += tot
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PeerItemOrder struct {
|
||||
Quantity int `json:"quantity,omitempty" bson:"quantity,omitempty"`
|
||||
Purchase *purchase_resource.PurchaseResource `json:"purchase,omitempty" bson:"purchase,omitempty"`
|
||||
Item map[string]interface{} `json:"item,omitempty" bson:"item,omitempty"`
|
||||
}
|
||||
|
||||
func (d *PeerItemOrder) GetPriceHT(request *tools.APIRequest) (float64, error) {
|
||||
/////////// Temporary in order to allow GenerateOrder to complete while billing is still WIP
|
||||
if d.Purchase == nil {
|
||||
return 0, nil
|
||||
}
|
||||
///////////
|
||||
var priced *resources.PricedResource
|
||||
b, _ := json.Marshal(d.Item)
|
||||
err := json.Unmarshal(b, priced)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return 0, err
|
||||
}
|
||||
accessor := purchase_resource.NewAccessor(request)
|
||||
search, code, _ := accessor.Search(&dbs.Filters{
|
||||
And: map[string][]dbs.Filter{
|
||||
"resource_id": {{Operator: dbs.EQUAL.String(), Value: priced.GetID()}},
|
||||
},
|
||||
}, "", d.Purchase.IsDraft)
|
||||
if code == 200 && len(search) > 0 {
|
||||
for _, s := range search {
|
||||
if s.(*purchase_resource.PurchaseResource).EndDate == nil || time.Now().UTC().After(*s.(*purchase_resource.PurchaseResource).EndDate) {
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
p, err := priced.GetPriceHT()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return p * float64(d.Quantity), nil
|
||||
}
|
||||
|
||||
// WTF HOW TO SELECT THE RIGHT PRICE ???
|
||||
// SHOULD SET A BUYING STATUS WHEN PAYMENT IS VALIDATED
|
||||
@@ -1,63 +0,0 @@
|
||||
package bill
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type billMongoAccessor struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the billMongoAccessor
|
||||
func NewAccessor(request *tools.APIRequest) *billMongoAccessor {
|
||||
return &billMongoAccessor{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.LIVE_DATACENTER.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
Type: tools.LIVE_DATACENTER,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *billMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *billMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
// should verify if a source is existing...
|
||||
return utils.GenericUpdateOne(set, id, a, &Bill{})
|
||||
}
|
||||
|
||||
func (a *billMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data.(*Bill), a)
|
||||
}
|
||||
|
||||
func (a *billMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data.(*Bill), a)
|
||||
}
|
||||
|
||||
func (a *billMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*Bill](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
return d, 200, nil
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *billMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*Bill](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *billMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*Bill](filters, search, (&Bill{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *billMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
return d
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
# Billing process
|
||||
Scheduler process a drafted order + a first bill corresponding to every once buying.
|
||||
@@ -5,7 +5,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/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
@@ -15,19 +14,13 @@ 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 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"`
|
||||
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
|
||||
DestPeerID string `json:"dest_peer_id,omitempty" bson:"dest_peer_id,omitempty"` // DestPeerID is the ID of the destination peer
|
||||
WorkflowID string `json:"workflow_id,omitempty" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
|
||||
ExecutionID string `json:"execution_id,omitempty" bson:"execution_id,omitempty" validate:"required"`
|
||||
State enum.BookingStatus `json:"state,omitempty" bson:"state,omitempty" validate:"required"` // State is the state of the booking
|
||||
ExpectedStartDate time.Time `json:"expected_start_date,omitempty" bson:"expected_start_date,omitempty" validate:"required"` // ExpectedStartDate is the expected start date of the booking
|
||||
ExpectedEndDate *time.Time `json:"expected_end_date,omitempty" bson:"expected_end_date,omitempty" validate:"required"` // ExpectedEndDate is the expected end date of the booking
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
DestPeerID string `json:"dest_peer_id,omitempty"` // DestPeerID is the ID of the destination peer
|
||||
WorkflowID string `json:"workflow_id,omitempty" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
|
||||
ExecutionID string `json:"execution_id,omitempty" bson:"execution_id,omitempty" validate:"required"`
|
||||
State enum.BookingStatus `json:"state,omitempty" bson:"state,omitempty" validate:"required"` // State is the state of the booking
|
||||
ExpectedStartDate time.Time `json:"expected_start_date,omitempty" bson:"expected_start_date,omitempty" validate:"required"` // ExpectedStartDate is the expected start date of the booking
|
||||
ExpectedEndDate *time.Time `json:"expected_end_date,omitempty" bson:"expected_end_date,omitempty" validate:"required"` // ExpectedEndDate is the expected end date of the booking
|
||||
|
||||
RealStartDate *time.Time `json:"real_start_date,omitempty" bson:"real_start_date,omitempty"` // RealStartDate is the real start date of the booking
|
||||
RealEndDate *time.Time `json:"real_end_date,omitempty" bson:"real_end_date,omitempty"` // RealEndDate is the real end date of the booking
|
||||
@@ -36,33 +29,6 @@ type Booking struct {
|
||||
ResourceID string `json:"resource_id,omitempty" bson:"resource_id,omitempty" validate:"required"` // could be a Compute or a Storage
|
||||
}
|
||||
|
||||
func (b *Booking) CalcDeltaOfExecution() map[string]map[string]models.MetricResume {
|
||||
m := map[string]map[string]models.MetricResume{}
|
||||
for instance, snapshot := range b.ExecutionMetrics {
|
||||
m[instance] = map[string]models.MetricResume{}
|
||||
for _, metric := range snapshot {
|
||||
for _, mm := range metric.Metrics {
|
||||
if resume, ok := m[instance][mm.Name]; !ok {
|
||||
m[instance][mm.Name] = models.MetricResume{
|
||||
Delta: 0,
|
||||
LastValue: mm.Value,
|
||||
}
|
||||
} else {
|
||||
delta := resume.LastValue - mm.Value
|
||||
if delta == 0 {
|
||||
resume.Delta = delta
|
||||
} else {
|
||||
resume.Delta = (resume.Delta + delta) / 2
|
||||
}
|
||||
resume.LastValue = mm.Value
|
||||
m[instance][mm.Name] = resume
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CheckBooking checks if a booking is possible on a specific compute resource
|
||||
func (wfa *Booking) Check(id string, start time.Time, end *time.Time, parrallelAllowed int) (bool, error) {
|
||||
// check if
|
||||
@@ -114,16 +80,20 @@ func (d *Booking) GetDelayOnDuration() time.Duration {
|
||||
return d.GetRealDuration() - d.GetUsualDuration()
|
||||
}
|
||||
|
||||
func (d *Booking) GetName() string {
|
||||
return d.GetID() + "_" + d.ExpectedStartDate.String()
|
||||
}
|
||||
|
||||
func (d *Booking) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
func (d *Booking) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
func (d *Booking) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *Booking) StoreDraftDefault() {
|
||||
r.IsDraft = false
|
||||
r.IsDraft = true
|
||||
}
|
||||
|
||||
func (r *Booking) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package booking
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
@@ -11,13 +10,13 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type BookingMongoAccessor struct {
|
||||
type bookingMongoAccessor struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the BookingMongoAccessor
|
||||
func NewAccessor(request *tools.APIRequest) *BookingMongoAccessor {
|
||||
return &BookingMongoAccessor{
|
||||
// New creates a new instance of the bookingMongoAccessor
|
||||
func NewAccessor(request *tools.APIRequest) *bookingMongoAccessor {
|
||||
return &bookingMongoAccessor{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.BOOKING.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
@@ -29,37 +28,31 @@ func NewAccessor(request *tools.APIRequest) *BookingMongoAccessor {
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *BookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
func (a *bookingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *BookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
if set.(*Booking).State == 0 {
|
||||
return nil, 400, errors.New("state is required")
|
||||
}
|
||||
realSet := &Booking{State: set.(*Booking).State}
|
||||
return utils.GenericUpdateOne(realSet, id, a, &Booking{})
|
||||
func (a *bookingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericUpdateOne(set, id, a, &Booking{})
|
||||
}
|
||||
|
||||
func (a *BookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
func (a *bookingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
func (a *BookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
func (a *bookingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
func (a *BookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
func (a *bookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*Booking](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
now := time.Now()
|
||||
now = now.Add(time.Second * -60)
|
||||
if d.(*Booking).State == enum.DRAFT && now.UTC().After(d.(*Booking).ExpectedStartDate) {
|
||||
if d.(*Booking).State == enum.DRAFT && time.Now().UTC().After(d.(*Booking).ExpectedStartDate) {
|
||||
return utils.GenericDeleteOne(d.GetID(), a)
|
||||
}
|
||||
if (d.(*Booking).ExpectedEndDate) == nil {
|
||||
d.(*Booking).State = enum.FORGOTTEN
|
||||
utils.GenericRawUpdateOne(d, id, a)
|
||||
} else if d.(*Booking).State == enum.SCHEDULED && now.UTC().After(d.(*Booking).ExpectedStartDate) {
|
||||
} else if d.(*Booking).State == enum.SCHEDULED && time.Now().UTC().After(*&d.(*Booking).ExpectedStartDate) {
|
||||
d.(*Booking).State = enum.DELAYED
|
||||
utils.GenericRawUpdateOne(d, id, a)
|
||||
}
|
||||
@@ -67,23 +60,21 @@ func (a *BookingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *BookingMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
func (a *bookingMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*Booking](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *BookingMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
func (a *bookingMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*Booking](filters, search, (&Booking{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *BookingMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
func (a *bookingMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
now := time.Now()
|
||||
now = now.Add(time.Second * -60)
|
||||
if d.(*Booking).State == enum.DRAFT && now.UTC().After(d.(*Booking).ExpectedStartDate) {
|
||||
if d.(*Booking).State == enum.DRAFT && time.Now().UTC().After(d.(*Booking).ExpectedStartDate) {
|
||||
utils.GenericDeleteOne(d.GetID(), a)
|
||||
return nil
|
||||
}
|
||||
if d.(*Booking).State == enum.SCHEDULED && now.UTC().After(d.(*Booking).ExpectedStartDate) {
|
||||
if d.(*Booking).State == enum.SCHEDULED && time.Now().UTC().After(*&d.(*Booking).ExpectedStartDate) {
|
||||
d.(*Booking).State = enum.DELAYED
|
||||
utils.GenericRawUpdateOne(d, d.GetID(), a)
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package booking
|
||||
|
||||
type BookingMode int
|
||||
|
||||
const (
|
||||
PLANNED BookingMode = iota // predictible
|
||||
PREEMPTED // can be both predictible or unpredictible, first one asking for a quick exec, second on event, but we pay to preempt in any case.
|
||||
WHEN_POSSIBLE // unpredictable, two mode of payment can be available on that case: fixed, or per USE
|
||||
)
|
||||
|
||||
/*
|
||||
Ok make a point there:
|
||||
There is 3 notions about booking & payment :
|
||||
Booking mode : WHEN is executed
|
||||
Buying mode : Duration of payment
|
||||
Pricing Mode : How Many time we pay
|
||||
|
||||
|
||||
We can simplify Buying Mode and Pricing Mode, some Buying Mode implied limited pricing mode
|
||||
Such as Rules. Just like PERMANENT BUYING can be paid only once.
|
||||
|
||||
Booking Mode on WHEN POSSIBLE make an exception, because we can't know when executed.
|
||||
*/
|
||||
@@ -1,87 +0,0 @@
|
||||
package booking_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
func TestBooking_GetDurations(t *testing.T) {
|
||||
start := time.Now().Add(-2 * time.Hour)
|
||||
end := start.Add(1 * time.Hour)
|
||||
realStart := start.Add(30 * time.Minute)
|
||||
realEnd := realStart.Add(90 * time.Minute)
|
||||
|
||||
b := &booking.Booking{
|
||||
ExpectedStartDate: start,
|
||||
ExpectedEndDate: &end,
|
||||
RealStartDate: &realStart,
|
||||
RealEndDate: &realEnd,
|
||||
}
|
||||
|
||||
assert.Equal(t, 30*time.Minute, b.GetDelayForLaunch())
|
||||
assert.Equal(t, 90*time.Minute, b.GetRealDuration())
|
||||
assert.Equal(t, end.Sub(start), b.GetUsualDuration())
|
||||
assert.Equal(t, b.GetRealDuration()-b.GetUsualDuration(), b.GetDelayOnDuration())
|
||||
assert.Equal(t, realEnd.Sub(start), b.GetDelayForFinishing())
|
||||
}
|
||||
|
||||
func TestBooking_GetAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
b := &booking.Booking{}
|
||||
accessor := b.GetAccessor(req)
|
||||
|
||||
assert.NotNil(t, accessor)
|
||||
assert.Equal(t, tools.BOOKING, accessor.(*booking.BookingMongoAccessor).Type)
|
||||
}
|
||||
|
||||
func TestBooking_VerifyAuth(t *testing.T) {
|
||||
assert.True(t, (&booking.Booking{}).VerifyAuth("get", nil))
|
||||
}
|
||||
|
||||
func TestBooking_StoreDraftDefault(t *testing.T) {
|
||||
b := &booking.Booking{}
|
||||
b.StoreDraftDefault()
|
||||
assert.False(t, b.IsDraft)
|
||||
}
|
||||
|
||||
func TestBooking_CanUpdate(t *testing.T) {
|
||||
now := time.Now()
|
||||
b := &booking.Booking{
|
||||
State: enum.SCHEDULED,
|
||||
AbstractObject: utils.AbstractObject{IsDraft: false},
|
||||
RealStartDate: &now,
|
||||
}
|
||||
|
||||
set := &booking.Booking{
|
||||
State: enum.DELAYED,
|
||||
RealStartDate: &now,
|
||||
}
|
||||
|
||||
ok, result := b.CanUpdate(set)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, enum.DELAYED, result.(*booking.Booking).State)
|
||||
}
|
||||
|
||||
func TestBooking_CanDelete(t *testing.T) {
|
||||
b := &booking.Booking{AbstractObject: utils.AbstractObject{IsDraft: true}}
|
||||
assert.True(t, b.CanDelete())
|
||||
|
||||
b.IsDraft = false
|
||||
assert.False(t, b.CanDelete())
|
||||
}
|
||||
|
||||
func TestNewAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
accessor := booking.NewAccessor(req)
|
||||
|
||||
assert.NotNil(t, accessor)
|
||||
assert.Equal(t, tools.BOOKING, accessor.Type)
|
||||
assert.Equal(t, req, accessor.Request)
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func (ao *CollaborativeArea) Clear(peerID string) {
|
||||
ao.CollaborativeAreaRule.CreatedAt = time.Now().UTC()
|
||||
}
|
||||
|
||||
func (ao *CollaborativeArea) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
func (ao *CollaborativeArea) VerifyAuth(request *tools.APIRequest) bool {
|
||||
if (ao.AllowedPeersGroup != nil || config.GetConfig().Whitelist) && request != nil {
|
||||
if grps, ok := ao.AllowedPeersGroup[request.PeerID]; ok || config.GetConfig().Whitelist {
|
||||
if slices.Contains(grps, "*") || (!ok && config.GetConfig().Whitelist) {
|
||||
@@ -84,7 +84,7 @@ func (ao *CollaborativeArea) VerifyAuth(callName string, request *tools.APIReque
|
||||
}
|
||||
}
|
||||
}
|
||||
return ao.AbstractObject.VerifyAuth(callName, request)
|
||||
return ao.AbstractObject.VerifyAuth(request)
|
||||
}
|
||||
|
||||
func (d *CollaborativeArea) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
@@ -97,7 +97,7 @@ func (d *CollaborativeArea) Trim() *CollaborativeArea {
|
||||
|
||||
func (d *CollaborativeArea) StoreDraftDefault() {
|
||||
d.AllowedPeersGroup = map[string][]string{
|
||||
d.CreatorID: {"*"},
|
||||
d.CreatorID: []string{"*"},
|
||||
}
|
||||
d.IsDraft = false
|
||||
}
|
||||
|
||||
@@ -24,6 +24,6 @@ func (d *Rule) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request)
|
||||
}
|
||||
|
||||
func (d *Rule) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
func (d *Rule) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -42,15 +42,13 @@ const (
|
||||
S3
|
||||
MEMORY
|
||||
HARDWARE
|
||||
AZURE
|
||||
GCS
|
||||
)
|
||||
|
||||
// String() - Returns the string representation of the storage type
|
||||
func (t StorageType) String() string {
|
||||
return [...]string{"FILE", "STREAM", "API", "DATABASE", "S3", "MEMORY", "HARDWARE", "AZURE", "GCS"}[t]
|
||||
return [...]string{"FILE", "STREAM", "API", "DATABASE", "S3", "MEMORY", "HARDWARE"}[t]
|
||||
}
|
||||
|
||||
func TypeList() []StorageType {
|
||||
return []StorageType{FILE, STREAM, API, DATABASE, S3, MEMORY, HARDWARE, AZURE, GCS}
|
||||
return []StorageType{FILE, STREAM, API, DATABASE, S3, MEMORY, HARDWARE}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package models
|
||||
|
||||
type MetricsSnapshot struct {
|
||||
From string `json:"origin"`
|
||||
Metrics []Metric `json:"metrics"`
|
||||
}
|
||||
|
||||
type Metric struct {
|
||||
Name string `json:"name"`
|
||||
Value float64 `json:"value"`
|
||||
Error error `json:"error"`
|
||||
}
|
||||
|
||||
type MetricResume struct {
|
||||
Delta float64 `json:"delta"`
|
||||
LastValue float64 `json:"last_value"`
|
||||
}
|
||||
4
models/common/planner.go
Executable file → Normal file
4
models/common/planner.go
Executable file → Normal file
@@ -7,7 +7,7 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
func GetPlannerNearestStart(start time.Time, planned map[tools.DataType]map[string]pricing.PricedItemITF, request *tools.APIRequest) float64 {
|
||||
func GetPlannerNearestStart(start time.Time, planned map[tools.DataType][]pricing.PricedItemITF, request *tools.APIRequest) float64 {
|
||||
near := float64(10000000000) // set a high value
|
||||
for _, items := range planned { // loop through the planned items
|
||||
for _, priced := range items { // loop through the priced items
|
||||
@@ -23,7 +23,7 @@ func GetPlannerNearestStart(start time.Time, planned map[tools.DataType]map[stri
|
||||
return near
|
||||
}
|
||||
|
||||
func GetPlannerLongestTime(end *time.Time, planned map[tools.DataType]map[string]pricing.PricedItemITF, request *tools.APIRequest) float64 {
|
||||
func GetPlannerLongestTime(end *time.Time, planned map[tools.DataType][]pricing.PricedItemITF, request *tools.APIRequest) float64 {
|
||||
if end == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
10
models/common/pricing/interfaces.go
Executable file → Normal file
10
models/common/pricing/interfaces.go
Executable file → Normal file
@@ -3,24 +3,18 @@ package pricing
|
||||
import (
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type PricedItemITF interface {
|
||||
GetID() string
|
||||
GetType() tools.DataType
|
||||
IsPurchasable() bool
|
||||
IsBooked() bool
|
||||
GetQuantity() int
|
||||
AddQuantity(amount int)
|
||||
GetBookingMode() booking.BookingMode
|
||||
IsPurchased() bool
|
||||
GetCreatorID() string
|
||||
SelectPricing() PricingProfileITF
|
||||
GetLocationStart() *time.Time
|
||||
SetLocationStart(start time.Time)
|
||||
SetLocationEnd(end time.Time)
|
||||
GetLocationEnd() *time.Time
|
||||
GetExplicitDurationInS() float64
|
||||
GetPriceHT() (float64, error)
|
||||
GetPrice() (float64, error)
|
||||
}
|
||||
|
||||
33
models/common/pricing/pricing_profile.go
Executable file → Normal file
33
models/common/pricing/pricing_profile.go
Executable file → Normal file
@@ -5,11 +5,9 @@ import (
|
||||
)
|
||||
|
||||
type PricingProfileITF interface {
|
||||
IsBooked() bool
|
||||
IsPurchasable() bool
|
||||
GetPurchase() BuyingStrategy
|
||||
GetPrice(quantity float64, val float64, start time.Time, end time.Time, params ...string) (float64, error)
|
||||
IsPurchased() bool
|
||||
GetOverrideStrategyValue() int
|
||||
GetPriceHT(quantity float64, val float64, start time.Time, end time.Time, variation []*PricingVariation, params ...string) (float64, error)
|
||||
}
|
||||
|
||||
type RefundType int
|
||||
@@ -34,37 +32,10 @@ type AccessPricingProfile[T Strategy] struct { // only use for acces such as : D
|
||||
RefundRatio int32 `json:"refund_ratio" bson:"refund_ratio" default:"0"` // RefundRatio is the refund ratio if missing
|
||||
}
|
||||
|
||||
func (a AccessPricingProfile[T]) IsBooked() bool {
|
||||
return a.Pricing.BuyingStrategy == SUBSCRIPTION
|
||||
}
|
||||
|
||||
func (a AccessPricingProfile[T]) IsPurchasable() bool {
|
||||
return a.Pricing.BuyingStrategy == PERMANENT
|
||||
}
|
||||
|
||||
func (a AccessPricingProfile[T]) GetPurchase() BuyingStrategy {
|
||||
return a.Pricing.BuyingStrategy
|
||||
}
|
||||
|
||||
func (a AccessPricingProfile[T]) GetPriceHT(quantity float64, val float64, start time.Time, end time.Time, variations []*PricingVariation, params ...string) (float64, error) {
|
||||
return a.Pricing.GetPriceHT(quantity, val, start, &end, variations)
|
||||
}
|
||||
|
||||
func (b *AccessPricingProfile[T]) GetOverrideStrategyValue() int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func GetDefaultPricingProfile() PricingProfileITF {
|
||||
return &AccessPricingProfile[TimePricingStrategy]{
|
||||
Pricing: PricingStrategy[TimePricingStrategy]{
|
||||
Price: 0,
|
||||
Currency: "EUR",
|
||||
BuyingStrategy: PERMANENT,
|
||||
TimePricingStrategy: ONCE,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type ExploitPrivilegeStrategy int
|
||||
|
||||
const (
|
||||
|
||||
111
models/common/pricing/pricing_strategy.go
Executable file → Normal file
111
models/common/pricing/pricing_strategy.go
Executable file → Normal file
@@ -7,65 +7,20 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type BillingStrategy int // BAM BAM
|
||||
|
||||
// should except... on
|
||||
const (
|
||||
BILL_ONCE BillingStrategy = iota // is a permanent buying ( predictible )
|
||||
BILL_PER_WEEK
|
||||
BILL_PER_MONTH
|
||||
BILL_PER_YEAR
|
||||
)
|
||||
|
||||
func (t BillingStrategy) IsBillingStrategyAllowed(bs int) (BillingStrategy, bool) {
|
||||
switch t {
|
||||
case BILL_ONCE:
|
||||
return BILL_ONCE, bs == 0
|
||||
case BILL_PER_WEEK:
|
||||
case BILL_PER_MONTH:
|
||||
case BILL_PER_YEAR:
|
||||
return t, bs != 0
|
||||
}
|
||||
return t, false
|
||||
}
|
||||
|
||||
func (t BillingStrategy) String() string {
|
||||
return [...]string{"BILL_ONCE", "BILL_PER_WEEK", "BILL_PER_MONTH", "BILL_PER_YEAR"}[t]
|
||||
}
|
||||
|
||||
func BillingStrategyList() []BillingStrategy {
|
||||
return []BillingStrategy{BILL_ONCE, BILL_PER_WEEK, BILL_PER_MONTH, BILL_PER_YEAR}
|
||||
}
|
||||
|
||||
type BuyingStrategy int
|
||||
|
||||
// should except... on
|
||||
const (
|
||||
PERMANENT BuyingStrategy = iota // is a permanent buying ( predictible )
|
||||
UNDEFINED_SUBSCRIPTION // a endless subscription ( unpredictible )
|
||||
SUBSCRIPTION // a defined subscription ( predictible )
|
||||
// PAY_PER_USE // per request. ( unpredictible )
|
||||
UNLIMITED BuyingStrategy = iota
|
||||
SUBSCRIPTION
|
||||
PAY_PER_USE
|
||||
)
|
||||
|
||||
func (t BuyingStrategy) String() string {
|
||||
return [...]string{"PERMANENT", "UNDEFINED_SUBSCRIPTION", "SUBSCRIPTION"}[t]
|
||||
}
|
||||
|
||||
func (t BuyingStrategy) IsBillingStrategyAllowed(bs BillingStrategy) (BillingStrategy, bool) {
|
||||
switch t {
|
||||
case PERMANENT:
|
||||
return BILL_ONCE, bs == BILL_ONCE
|
||||
case UNDEFINED_SUBSCRIPTION:
|
||||
return BILL_PER_MONTH, bs != BILL_ONCE
|
||||
case SUBSCRIPTION:
|
||||
/*case PAY_PER_USE:
|
||||
return bs, true*/
|
||||
}
|
||||
return bs, false
|
||||
return [...]string{"UNLIMITED", "SUBSCRIPTION", "PAY PER USE"}[t]
|
||||
}
|
||||
|
||||
func BuyingStrategyList() []BuyingStrategy {
|
||||
return []BuyingStrategy{PERMANENT, UNDEFINED_SUBSCRIPTION, SUBSCRIPTION}
|
||||
return []BuyingStrategy{UNLIMITED, SUBSCRIPTION, PAY_PER_USE}
|
||||
}
|
||||
|
||||
type Strategy interface {
|
||||
@@ -85,18 +40,10 @@ const (
|
||||
PER_MONTH
|
||||
)
|
||||
|
||||
func IsTimeStrategy(i int) bool {
|
||||
return len(TimePricingStrategyList()) < i
|
||||
}
|
||||
|
||||
func (t TimePricingStrategy) String() string {
|
||||
return [...]string{"ONCE", "PER SECOND", "PER MINUTE", "PER HOUR", "PER DAY", "PER WEEK", "PER MONTH"}[t]
|
||||
}
|
||||
|
||||
func TimePricingStrategyListStr() []string {
|
||||
return []string{"ONCE", "PER SECOND", "PER MINUTE", "PER HOUR", "PER DAY", "PER WEEK", "PER MONTH"}
|
||||
}
|
||||
|
||||
func TimePricingStrategyList() []TimePricingStrategy {
|
||||
return []TimePricingStrategy{ONCE, PER_SECOND, PER_MINUTE, PER_HOUR, PER_DAY, PER_WEEK, PER_MONTH}
|
||||
}
|
||||
@@ -154,7 +101,6 @@ func BookingEstimation(t TimePricingStrategy, price float64, locationDurationInS
|
||||
return 0, errors.New("pricing strategy not found")
|
||||
}
|
||||
|
||||
// may suppress in pricing strategy -> to set in map
|
||||
type PricingStrategy[T Strategy] struct {
|
||||
Price float64 `json:"price" bson:"price" default:"0"` // Price is the Price of the pricing
|
||||
Currency string `json:"currency" bson:"currency" default:"USD"` // Currency is the currency of the pricing
|
||||
@@ -163,37 +109,11 @@ type PricingStrategy[T Strategy] struct {
|
||||
OverrideStrategy T `json:"override_strategy" bson:"override_strategy" default:"-1"` // Modulation is the modulation of the pricing
|
||||
}
|
||||
|
||||
func (p PricingStrategy[T]) GetPriceHT(amountOfData float64, bookingTimeDuration float64, start time.Time, end *time.Time, variations []*PricingVariation) (float64, error) {
|
||||
func (p PricingStrategy[T]) GetPrice(amountOfData float64, bookingTimeDuration float64, start time.Time, end *time.Time) (float64, error) {
|
||||
if p.BuyingStrategy == SUBSCRIPTION {
|
||||
price, err := BookingEstimation(p.GetTimePricingStrategy(), p.Price*float64(amountOfData), bookingTimeDuration, start, end)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if variations != nil {
|
||||
for _, v := range variations {
|
||||
price = v.GetPriceHT(price)
|
||||
}
|
||||
return price, nil
|
||||
}
|
||||
|
||||
return BookingEstimation(p.GetTimePricingStrategy(), p.Price*float64(amountOfData), bookingTimeDuration, start, end)
|
||||
} else if p.BuyingStrategy == UNLIMITED {
|
||||
return p.Price, nil
|
||||
|
||||
} else if p.BuyingStrategy == PERMANENT {
|
||||
if variations != nil {
|
||||
price := p.Price
|
||||
for _, v := range variations {
|
||||
price = v.GetPriceHT(price)
|
||||
}
|
||||
return price, nil
|
||||
}
|
||||
return p.Price, nil
|
||||
}
|
||||
if variations != nil {
|
||||
price := p.Price
|
||||
for _, v := range variations {
|
||||
price = v.GetPriceHT(price)
|
||||
}
|
||||
return price, nil
|
||||
}
|
||||
return p.Price * float64(amountOfData), nil
|
||||
}
|
||||
@@ -209,18 +129,3 @@ func (p PricingStrategy[T]) GetTimePricingStrategy() TimePricingStrategy {
|
||||
func (p PricingStrategy[T]) GetOverrideStrategy() T {
|
||||
return p.OverrideStrategy
|
||||
}
|
||||
|
||||
type PricingVariation struct {
|
||||
Inflate bool `json:"inflate" bson:"price"` // Price is the Price of the pricing
|
||||
Percentage float64 `json:"percent" bson:"percent"` // Currency is the currency of the pricing // Modulation is the modulation of the pricing
|
||||
Priority int `json:"priority" bson:"priority"`
|
||||
}
|
||||
|
||||
func (pv *PricingVariation) GetPriceHT(priceHT float64) float64 {
|
||||
value := (priceHT * pv.Percentage) / 100
|
||||
if pv.Inflate {
|
||||
return priceHT + value
|
||||
} else {
|
||||
return priceHT - value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
package pricing_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
)
|
||||
|
||||
type DummyStrategy int
|
||||
|
||||
func (d DummyStrategy) GetStrategy() string { return "DUMMY" }
|
||||
func (d DummyStrategy) GetStrategyValue() int { return int(d) }
|
||||
|
||||
func TestBuyingStrategy_String(t *testing.T) {
|
||||
assert.Equal(t, "UNLIMITED", pricing.PERMANENT.String())
|
||||
assert.Equal(t, "SUBSCRIPTION", pricing.SUBSCRIPTION.String())
|
||||
//assert.Equal(t, "PAY PER USE", pricing.PAY_PER_USE.String())
|
||||
}
|
||||
|
||||
func TestBuyingStrategyList(t *testing.T) {
|
||||
list := pricing.BuyingStrategyList()
|
||||
assert.Equal(t, 3, len(list))
|
||||
assert.Contains(t, list, pricing.SUBSCRIPTION)
|
||||
}
|
||||
|
||||
func TestTimePricingStrategy_String(t *testing.T) {
|
||||
assert.Equal(t, "ONCE", pricing.ONCE.String())
|
||||
assert.Equal(t, "PER SECOND", pricing.PER_SECOND.String())
|
||||
assert.Equal(t, "PER MONTH", pricing.PER_MONTH.String())
|
||||
}
|
||||
|
||||
func TestTimePricingStrategyList(t *testing.T) {
|
||||
list := pricing.TimePricingStrategyList()
|
||||
assert.Equal(t, 7, len(list))
|
||||
assert.Contains(t, list, pricing.PER_DAY)
|
||||
}
|
||||
|
||||
func TestTimePricingStrategy_Methods(t *testing.T) {
|
||||
ts := pricing.PER_MINUTE
|
||||
assert.Equal(t, "PER_MINUTE", ts.GetStrategy())
|
||||
assert.Equal(t, 2, ts.GetStrategyValue())
|
||||
}
|
||||
|
||||
func Test_getAverageTimeInSecond_WithEnd(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(30 * time.Minute)
|
||||
|
||||
_, err := pricing.BookingEstimation(pricing.PER_MINUTE, 2.0, 1200, start, &end)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_getAverageTimeInSecond_WithoutEnd(t *testing.T) {
|
||||
start := time.Now()
|
||||
|
||||
// getAverageTimeInSecond is tested via BookingEstimation
|
||||
price, err := pricing.BookingEstimation(pricing.PER_HOUR, 10.0, 100, start, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, price > 0)
|
||||
}
|
||||
|
||||
func TestBookingEstimation(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(2 * time.Hour)
|
||||
strategies := map[pricing.TimePricingStrategy]float64{
|
||||
pricing.ONCE: 50,
|
||||
pricing.PER_HOUR: 10,
|
||||
pricing.PER_MINUTE: 1,
|
||||
pricing.PER_SECOND: 0.1,
|
||||
pricing.PER_DAY: 100,
|
||||
pricing.PER_WEEK: 500,
|
||||
pricing.PER_MONTH: 2000,
|
||||
}
|
||||
|
||||
for strategy, price := range strategies {
|
||||
t.Run(strategy.String(), func(t *testing.T) {
|
||||
cost, err := pricing.BookingEstimation(strategy, price, 3600, start, &end)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, cost >= 0)
|
||||
})
|
||||
}
|
||||
|
||||
_, err := pricing.BookingEstimation(999, 10, 3600, start, &end)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestPricingStrategy_Getters(t *testing.T) {
|
||||
ps := pricing.PricingStrategy[DummyStrategy]{
|
||||
Price: 20,
|
||||
Currency: "USD",
|
||||
BuyingStrategy: pricing.SUBSCRIPTION,
|
||||
TimePricingStrategy: pricing.PER_MINUTE,
|
||||
OverrideStrategy: DummyStrategy(1),
|
||||
}
|
||||
|
||||
assert.Equal(t, pricing.SUBSCRIPTION, ps.GetBuyingStrategy())
|
||||
assert.Equal(t, pricing.PER_MINUTE, ps.GetTimePricingStrategy())
|
||||
assert.Equal(t, DummyStrategy(1), ps.GetOverrideStrategy())
|
||||
}
|
||||
|
||||
func TestPricingStrategy_GetPriceHT(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
|
||||
// SUBSCRIPTION case
|
||||
ps := pricing.PricingStrategy[DummyStrategy]{
|
||||
Price: 5,
|
||||
BuyingStrategy: pricing.SUBSCRIPTION,
|
||||
TimePricingStrategy: pricing.PER_HOUR,
|
||||
}
|
||||
|
||||
p, err := ps.GetPriceHT(2, 3600, start, &end, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, p > 0)
|
||||
|
||||
// UNLIMITED case
|
||||
ps.BuyingStrategy = pricing.PERMANENT
|
||||
p, err = ps.GetPriceHT(10, 0, start, &end, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5.0, p)
|
||||
|
||||
// PAY_PER_USE case
|
||||
//ps.BuyingStrategy = pricing.PAY_PER_USE
|
||||
p, err = ps.GetPriceHT(3, 0, start, &end, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 15.0, p)
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type LiveInterface interface {
|
||||
utils.DBObject
|
||||
GetMonitorPath() string
|
||||
GetResourcesID() []string
|
||||
SetResourcesID(string)
|
||||
GetResourceAccessor(request *tools.APIRequest) utils.Accessor
|
||||
GetResource() resources.ResourceInterface
|
||||
GetResourceInstance() resources.ResourceInstanceITF
|
||||
SetResourceInstance(res resources.ResourceInterface, i resources.ResourceInstanceITF) resources.ResourceInterface
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/biter777/countries"
|
||||
)
|
||||
|
||||
/*
|
||||
* LiveDatacenter is a struct that represents a compute units in your datacenters
|
||||
*/
|
||||
type Credentials struct {
|
||||
Login string `json:"login,omitempty" bson:"login,omitempty"`
|
||||
Pass string `json:"password,omitempty" bson:"password,omitempty"`
|
||||
Token string `json:"token,omitempty" bson:"token,omitempty"`
|
||||
}
|
||||
|
||||
type Certs struct {
|
||||
AuthorityCertificate string `json:"authority_certificate,omitempty" bson:"authority_certificate,omitempty"`
|
||||
ClientCertificate string `json:"client_certificate,omitempty" bson:"client_certificate,omitempty"`
|
||||
}
|
||||
|
||||
type LiveCerts struct {
|
||||
Host string `json:"host,omitempty" bson:"host,omitempty"`
|
||||
Port string `json:"port,omitempty" bson:"port,omitempty"`
|
||||
|
||||
Certificates *Certs `json:"certs,omitempty" bson:"certs,omitempty"`
|
||||
Credentials *Credentials `json:"creds,omitempty" bson:"creds,omitempty"`
|
||||
}
|
||||
|
||||
// TODO in the future multiple type of certs depending of infra type
|
||||
|
||||
type AbstractLive struct {
|
||||
utils.AbstractObject
|
||||
Certs LiveCerts `json:"certs,omitempty" bson:"certs,omitempty"`
|
||||
|
||||
MonitorPath string `json:"monitor_path,omitempty" bson:"monitor_path,omitempty"`
|
||||
Location resources.GeoPoint `json:"location,omitempty" bson:"location,omitempty"`
|
||||
Country countries.CountryCode `json:"country,omitempty" bson:"country,omitempty"`
|
||||
AccessProtocol string `json:"access_protocol,omitempty" bson:"access_protocol,omitempty"`
|
||||
ResourcesID []string `json:"resources_id" bson:"resources_id"`
|
||||
}
|
||||
|
||||
func (d *AbstractLive) GetMonitorPath() string {
|
||||
return d.MonitorPath
|
||||
}
|
||||
|
||||
func (d *AbstractLive) GetResourcesID() []string {
|
||||
return d.ResourcesID
|
||||
}
|
||||
|
||||
func (d *AbstractLive) SetResourcesID(id string) {
|
||||
if !slices.Contains(d.ResourcesID, id) {
|
||||
d.ResourcesID = append(d.ResourcesID, id)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *AbstractLive) GetResourceType() tools.DataType {
|
||||
return tools.INVALID
|
||||
}
|
||||
|
||||
func (r *AbstractLive) StoreDraftDefault() {
|
||||
r.IsDraft = true
|
||||
}
|
||||
|
||||
func (r *AbstractLive) CanDelete() bool {
|
||||
return r.IsDraft // only draft ComputeUnits can be deleted
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"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/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
/*
|
||||
* LiveDatacenter is a struct that represents a compute units in your datacenters
|
||||
*/
|
||||
|
||||
type LiveDatacenter struct {
|
||||
AbstractLive
|
||||
|
||||
StorageType enum.StorageType `bson:"storage_type" json:"storage_type" default:"-1"` // Type is the type of the storage
|
||||
Acronym string `bson:"acronym,omitempty" json:"acronym,omitempty"` // Acronym is the acronym of the storage
|
||||
|
||||
Architecture string `json:"architecture,omitempty" bson:"architecture,omitempty"` // Architecture is the architecture
|
||||
Infrastructure enum.InfrastructureType `json:"infrastructure" bson:"infrastructure" default:"-1"` // Infrastructure is the infrastructure
|
||||
Source string `json:"source,omitempty" bson:"source,omitempty"` // Source is the source of the resource
|
||||
SecurityLevel string `json:"security_level,omitempty" bson:"security_level,omitempty"`
|
||||
PowerSources []string `json:"power_sources,omitempty" bson:"power_sources,omitempty"`
|
||||
AnnualCO2Emissions float64 `json:"annual_co2_emissions,omitempty" bson:"co2_emissions,omitempty"`
|
||||
CPUs map[string]*models.CPU `bson:"cpus,omitempty" json:"cpus,omitempty"` // CPUs is the list of CPUs key is model
|
||||
GPUs map[string]*models.GPU `bson:"gpus,omitempty" json:"gpus,omitempty"` // GPUs is the list of GPUs key is model
|
||||
Nodes []*resources.ComputeNode `json:"nodes,omitempty" bson:"nodes,omitempty"`
|
||||
}
|
||||
|
||||
func (d *LiveDatacenter) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor[*LiveDatacenter](tools.LIVE_DATACENTER, request) // Create a new instance of the accessor
|
||||
}
|
||||
func (d *LiveDatacenter) GetResourceAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return resources.NewAccessor[*resources.ComputeResource](tools.COMPUTE_RESOURCE, request, func() utils.DBObject { return &resources.ComputeResource{} })
|
||||
}
|
||||
|
||||
func (d *LiveDatacenter) GetResource() resources.ResourceInterface {
|
||||
return &resources.ComputeResource{}
|
||||
}
|
||||
func (d *LiveDatacenter) GetResourceInstance() resources.ResourceInstanceITF {
|
||||
return &resources.ComputeResourceInstance{}
|
||||
}
|
||||
|
||||
func (d *LiveDatacenter) SetResourceInstance(res resources.ResourceInterface, i resources.ResourceInstanceITF) resources.ResourceInterface {
|
||||
r := res.(*resources.ComputeResource)
|
||||
r.Instances = append(r.Instances, i.(*resources.ComputeResourceInstance))
|
||||
return r
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type computeUnitsMongoAccessor[T LiveInterface] struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the computeUnitsMongoAccessor
|
||||
func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *computeUnitsMongoAccessor[T] {
|
||||
return &computeUnitsMongoAccessor[T]{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
Type: t,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *computeUnitsMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
// should verify if a source is existing...
|
||||
return utils.GenericUpdateOne(set, id, a, &LiveDatacenter{})
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data.(*LiveDatacenter), a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
// is a publisher... that become a resources.
|
||||
if data.IsDrafted() {
|
||||
return nil, 422, errors.New("can't publish a drafted compute units")
|
||||
}
|
||||
live := data.(T)
|
||||
if live.GetMonitorPath() == "" || live.GetID() != "" {
|
||||
return nil, 422, errors.New("publishing is only allowed is it can be monitored and be accessible")
|
||||
}
|
||||
if res, code, err := a.LoadOne(live.GetID()); err != nil {
|
||||
return nil, code, err
|
||||
} else {
|
||||
live = res.(T)
|
||||
}
|
||||
resAccess := live.GetResourceAccessor(a.Request)
|
||||
instance := live.GetResourceInstance()
|
||||
b, _ := json.Marshal(live)
|
||||
json.Unmarshal(b, instance)
|
||||
|
||||
if len(live.GetResourcesID()) > 0 {
|
||||
for _, r := range live.GetResourcesID() {
|
||||
// TODO dependent of a existing resource
|
||||
res, code, err := resAccess.LoadOne(r)
|
||||
if err == nil {
|
||||
return nil, code, err
|
||||
}
|
||||
existingResource := live.GetResource()
|
||||
b, _ := json.Marshal(res)
|
||||
json.Unmarshal(b, existingResource)
|
||||
live.SetResourceInstance(existingResource, instance)
|
||||
resAccess.UpdateOne(existingResource, existingResource.GetID())
|
||||
}
|
||||
if live.GetID() != "" {
|
||||
return a.LoadOne(live.GetID())
|
||||
} else {
|
||||
return a.StoreOne(live)
|
||||
}
|
||||
} else {
|
||||
r := live.GetResource()
|
||||
b, _ := json.Marshal(live)
|
||||
json.Unmarshal(b, &r)
|
||||
live.SetResourceInstance(r, instance)
|
||||
res, code, err := resAccess.StoreOne(r)
|
||||
if err != nil {
|
||||
return nil, code, err
|
||||
}
|
||||
live.SetResourcesID(res.GetID())
|
||||
if live.GetID() != "" {
|
||||
return a.UpdateOne(live, live.GetID())
|
||||
} else {
|
||||
return a.StoreOne(live)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
return d, 200, nil
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[T](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*LiveDatacenter](filters, search, (&LiveDatacenter{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor[T]) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
return d
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
/*
|
||||
* LiveStorage is a struct that represents a compute units in your datacenters
|
||||
*/
|
||||
|
||||
type LiveStorage struct {
|
||||
AbstractLive
|
||||
|
||||
Source string `bson:"source,omitempty" json:"source,omitempty"` // Source is the source of the storage
|
||||
Path string `bson:"path,omitempty" json:"path,omitempty"` // Path is the store folders in the source
|
||||
Local bool `bson:"local" json:"local"`
|
||||
SecurityLevel string `bson:"security_level,omitempty" json:"security_level,omitempty"`
|
||||
SizeType enum.StorageSize `bson:"size_type" json:"size_type" default:"0"` // SizeType is the type of the storage size
|
||||
SizeGB int64 `bson:"size,omitempty" json:"size,omitempty"` // Size is the size of the storage
|
||||
Encryption bool `bson:"encryption,omitempty" json:"encryption,omitempty"` // Encryption is a flag that indicates if the storage is encrypted
|
||||
Redundancy string `bson:"redundancy,omitempty" json:"redundancy,omitempty"` // Redundancy is the redundancy of the storage
|
||||
Throughput string `bson:"throughput,omitempty" json:"throughput,omitempty"` // Throughput is the throughput of the storage
|
||||
}
|
||||
|
||||
func (d *LiveStorage) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor[*LiveStorage](tools.LIVE_STORAGE, request) // Create a new instance of the accessor
|
||||
}
|
||||
func (d *LiveStorage) GetResourceAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return resources.NewAccessor[*resources.ComputeResource](tools.STORAGE_RESOURCE, request, func() utils.DBObject { return &resources.StorageResource{} })
|
||||
}
|
||||
|
||||
func (d *LiveStorage) GetResource() resources.ResourceInterface {
|
||||
return &resources.StorageResource{}
|
||||
}
|
||||
func (d *LiveStorage) GetResourceInstance() resources.ResourceInstanceITF {
|
||||
return &resources.StorageResourceInstance{}
|
||||
}
|
||||
|
||||
func (d *LiveStorage) SetResourceInstance(res resources.ResourceInterface, i resources.ResourceInstanceITF) resources.ResourceInterface {
|
||||
r := res.(*resources.StorageResource)
|
||||
r.Instances = append(r.Instances, i.(*resources.StorageResourceInstance))
|
||||
return r
|
||||
}
|
||||
@@ -2,8 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/bill"
|
||||
"cloud.o-forge.io/core/oc-lib/models/live"
|
||||
"cloud.o-forge.io/core/oc-lib/models/order"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
@@ -23,15 +21,14 @@ import (
|
||||
This package contains the models used in the application
|
||||
It's used to create the models dynamically
|
||||
*/
|
||||
var ModelsCatalog = map[string]func() utils.DBObject{
|
||||
var models = map[string]func() utils.DBObject{
|
||||
tools.WORKFLOW_RESOURCE.String(): func() utils.DBObject { return &resource.WorkflowResource{} },
|
||||
tools.DATA_RESOURCE.String(): func() utils.DBObject { return &resource.DataResource{} },
|
||||
tools.COMPUTE_RESOURCE.String(): func() utils.DBObject { return &resource.ComputeResource{} },
|
||||
tools.STORAGE_RESOURCE.String(): func() utils.DBObject { return &resource.StorageResource{} },
|
||||
tools.PROCESSING_RESOURCE.String(): func() utils.DBObject { return &resource.ProcessingResource{} },
|
||||
tools.NATIVE_TOOL.String(): func() utils.DBObject { return &resource.NativeTool{} },
|
||||
tools.WORKFLOW.String(): func() utils.DBObject { return &w2.Workflow{} },
|
||||
tools.WORKFLOW_EXECUTION.String(): func() utils.DBObject { return &workflow_execution.WorkflowExecution{} },
|
||||
tools.WORKFLOW_EXECUTION.String(): func() utils.DBObject { return &workflow_execution.WorkflowExecutions{} },
|
||||
tools.WORKSPACE.String(): func() utils.DBObject { return &w3.Workspace{} },
|
||||
tools.PEER.String(): func() utils.DBObject { return &peer.Peer{} },
|
||||
tools.COLLABORATIVE_AREA.String(): func() utils.DBObject { return &collaborative_area.CollaborativeArea{} },
|
||||
@@ -41,16 +38,13 @@ var ModelsCatalog = map[string]func() utils.DBObject{
|
||||
tools.WORKSPACE_HISTORY.String(): func() utils.DBObject { return &w3.WorkspaceHistory{} },
|
||||
tools.ORDER.String(): func() utils.DBObject { return &order.Order{} },
|
||||
tools.PURCHASE_RESOURCE.String(): func() utils.DBObject { return &purchase_resource.PurchaseResource{} },
|
||||
tools.LIVE_DATACENTER.String(): func() utils.DBObject { return &live.LiveDatacenter{} },
|
||||
tools.LIVE_STORAGE.String(): func() utils.DBObject { return &live.LiveStorage{} },
|
||||
tools.BILL.String(): func() utils.DBObject { return &bill.Bill{} },
|
||||
}
|
||||
|
||||
// Model returns the model object based on the model type
|
||||
func Model(model int) utils.DBObject {
|
||||
log := logs.GetLogger()
|
||||
if _, ok := ModelsCatalog[tools.FromInt(model)]; ok {
|
||||
return ModelsCatalog[tools.FromInt(model)]()
|
||||
if _, ok := models[tools.FromInt(model)]; ok {
|
||||
return models[tools.FromInt(model)]()
|
||||
}
|
||||
log.Error().Msg("Can't find model " + tools.FromInt(model) + ".")
|
||||
return nil
|
||||
@@ -59,7 +53,7 @@ func Model(model int) utils.DBObject {
|
||||
// GetModelsNames returns the names of the models
|
||||
func GetModelsNames() []string {
|
||||
names := []string{}
|
||||
for name := range ModelsCatalog {
|
||||
for name := range models {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"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/peer"
|
||||
"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/models/workflow_execution"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
@@ -17,12 +23,12 @@ import (
|
||||
|
||||
type Order struct {
|
||||
utils.AbstractObject
|
||||
ExecutionsID string `json:"executions_id" bson:"executions_id" validate:"required"`
|
||||
Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
|
||||
Purchases []*purchase_resource.PurchaseResource `json:"purchases" bson:"purchases"`
|
||||
Bookings []*booking.Booking `json:"bookings" bson:"bookings"`
|
||||
|
||||
Billing map[pricing.BillingStrategy][]*booking.Booking `json:"billing" bson:"billing"`
|
||||
OrderBy string `json:"order_by" bson:"order_by" validate:"required"`
|
||||
WorkflowID string `json:"workflow_id" bson:"workflow_id" validate:"required"`
|
||||
WorkflowExecutionIDs []string `json:"workflow_execution_ids" bson:"workflow_execution_ids" validate:"required"`
|
||||
Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
|
||||
SubOrders map[string]*PeerOrder `json:"sub_orders" bson:"sub_orders"`
|
||||
Total float64 `json:"total" bson:"total" validate:"required"`
|
||||
}
|
||||
|
||||
func (r *Order) StoreDraftDefault() {
|
||||
@@ -40,14 +46,287 @@ func (r *Order) CanDelete() bool {
|
||||
return r.IsDraft // only draft order can be deleted
|
||||
}
|
||||
|
||||
func (o *Order) Quantity() int {
|
||||
return len(o.Purchases) + len(o.Purchases)
|
||||
func (o *Order) DraftOrder(scheduler *workflow_execution.WorkflowSchedule, request *tools.APIRequest) error {
|
||||
// set the draft order from the model
|
||||
if err := o.draftStoreFromModel(scheduler, request); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Order) SetName(_ string) {
|
||||
func (o *Order) Pay(scheduler *workflow_execution.WorkflowSchedule, request *tools.APIRequest) error {
|
||||
if _, err := o.draftBookOrder(scheduler, request); err != nil {
|
||||
return err
|
||||
}
|
||||
o.Status = enum.PENDING
|
||||
_, code, err := o.GetAccessor(request).UpdateOne(o, o.GetID())
|
||||
if code != 200 || err != nil {
|
||||
return errors.New("could not update the order" + fmt.Sprintf("%v", err))
|
||||
}
|
||||
if err := o.pay(request); err != nil { // pay the order
|
||||
return err
|
||||
} else {
|
||||
o.IsDraft = false
|
||||
}
|
||||
for _, exec := range scheduler.WorkflowExecutions {
|
||||
exec.IsDraft = false
|
||||
_, code, err := utils.GenericUpdateOne(exec, exec.GetID(),
|
||||
workflow_execution.NewAccessor(request), &workflow_execution.WorkflowExecutions{})
|
||||
if code != 200 || err != nil {
|
||||
return errors.New("could not update the workflow execution" + fmt.Sprintf("%v", err))
|
||||
}
|
||||
}
|
||||
_, code, err = o.GetAccessor(request).UpdateOne(o, o.GetID())
|
||||
if code != 200 || err != nil {
|
||||
return errors.New("could not update the order" + fmt.Sprintf("%v", err))
|
||||
}
|
||||
/*
|
||||
TODO : TEMPORARY SET BOOKINGS TO UNDRAFT TO AVOID DELETION
|
||||
BUT NEXT ONLY WHO IS PAYED WILL BE ALLOWED TO CHANGE IT
|
||||
*/
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *Order) draftStoreFromModel(scheduler *workflow_execution.WorkflowSchedule, request *tools.APIRequest) error {
|
||||
if request == nil {
|
||||
return errors.New("no request found")
|
||||
}
|
||||
fmt.Println("Drafting order", scheduler.Workflow)
|
||||
if scheduler.Workflow == nil || scheduler.Workflow.Graph == nil { // if the workflow has no graph, return an error
|
||||
return errors.New("no graph found")
|
||||
}
|
||||
o.SetName()
|
||||
o.WorkflowID = scheduler.Workflow.GetID()
|
||||
o.IsDraft = true
|
||||
o.OrderBy = request.PeerID
|
||||
o.WorkflowExecutionIDs = []string{} // create an array of ids
|
||||
for _, exec := range scheduler.WorkflowExecutions {
|
||||
o.WorkflowExecutionIDs = append(o.WorkflowExecutionIDs, exec.GetID())
|
||||
}
|
||||
// set the name of the order
|
||||
resourcesByPeer := map[string][]pricing.PricedItemITF{} // create a map of resources by peer
|
||||
|
||||
processings := scheduler.Workflow.GetPricedItem(scheduler.Workflow.Graph.IsProcessing, request) // get the processing items
|
||||
datas := scheduler.Workflow.GetPricedItem(scheduler.Workflow.Graph.IsData, request) // get the data items
|
||||
storages := scheduler.Workflow.GetPricedItem(scheduler.Workflow.Graph.IsStorage, request) // get the storage items
|
||||
workflows := scheduler.Workflow.GetPricedItem(scheduler.Workflow.Graph.IsWorkflow, request) // get the workflow items
|
||||
for _, items := range []map[string]pricing.PricedItemITF{processings, datas, storages, workflows} {
|
||||
for _, item := range items {
|
||||
if _, ok := resourcesByPeer[item.GetCreatorID()]; !ok {
|
||||
resourcesByPeer[item.GetCreatorID()] = []pricing.PricedItemITF{}
|
||||
}
|
||||
resourcesByPeer[item.GetCreatorID()] = append(resourcesByPeer[item.GetCreatorID()], item)
|
||||
}
|
||||
}
|
||||
for peerID, resources := range resourcesByPeer {
|
||||
peerOrder := &PeerOrder{
|
||||
Status: enum.DRAFTED,
|
||||
PeerID: peerID,
|
||||
}
|
||||
peerOrder.GenerateID()
|
||||
for _, resource := range resources {
|
||||
peerOrder.AddItem(resource, len(resources)) // TODO SPECIALS REF ADDITIONALS NOTES
|
||||
}
|
||||
if o.SubOrders == nil {
|
||||
o.SubOrders = map[string]*PeerOrder{}
|
||||
}
|
||||
o.SubOrders[peerOrder.GetID()] = peerOrder
|
||||
}
|
||||
// search an order with same user name and same session id
|
||||
err := o.sumUpBill(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// should store the order
|
||||
res, code, err := o.GetAccessor(request).Search(&dbs.Filters{
|
||||
And: map[string][]dbs.Filter{
|
||||
"workflow_id": {{Operator: dbs.EQUAL.String(), Value: o.WorkflowID}},
|
||||
"order_by": {{Operator: dbs.EQUAL.String(), Value: request.PeerID}},
|
||||
},
|
||||
}, "", o.IsDraft)
|
||||
if code != 200 || err != nil {
|
||||
return errors.New("could not search the order" + fmt.Sprintf("%v", err))
|
||||
}
|
||||
if len(res) > 0 {
|
||||
_, code, err := utils.GenericUpdateOne(o, res[0].GetID(), o.GetAccessor(request), o)
|
||||
if code != 200 || err != nil {
|
||||
return errors.New("could not update the order" + fmt.Sprintf("%v", err))
|
||||
}
|
||||
} else {
|
||||
_, code, err := utils.GenericStoreOne(o, o.GetAccessor(request))
|
||||
if code != 200 || err != nil {
|
||||
return errors.New("could not store the order" + fmt.Sprintf("%v", err))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *Order) draftBookOrder(scheduler *workflow_execution.WorkflowSchedule, request *tools.APIRequest) ([]*booking.Booking, error) {
|
||||
draftedBookings := []*booking.Booking{}
|
||||
if request == nil {
|
||||
return draftedBookings, errors.New("no request found")
|
||||
}
|
||||
for _, exec := range scheduler.WorkflowExecutions {
|
||||
_, priceds, _, err := scheduler.Workflow.Planify(exec.ExecDate, exec.EndDate, request)
|
||||
if err != nil {
|
||||
return draftedBookings, errors.New("could not planify the workflow" + fmt.Sprintf("%v", err))
|
||||
}
|
||||
bookings := exec.Book(scheduler.Workflow.UUID, priceds)
|
||||
for _, booking := range bookings {
|
||||
_, err := (&peer.Peer{}).LaunchPeerExecution(booking.DestPeerID, "",
|
||||
tools.BOOKING, tools.POST, booking.Serialize(booking), request.Caller)
|
||||
if err != nil {
|
||||
return draftedBookings, errors.New("could not launch the peer execution : " + fmt.Sprintf("%v", err))
|
||||
}
|
||||
draftedBookings = append(draftedBookings, booking)
|
||||
}
|
||||
}
|
||||
return draftedBookings, nil
|
||||
}
|
||||
|
||||
func (o *Order) Quantity() int {
|
||||
return len(o.WorkflowExecutionIDs)
|
||||
}
|
||||
|
||||
func (d *Order) SetName() {
|
||||
d.Name = d.UUID + "_order_" + "_" + time.Now().UTC().Format("2006-01-02T15:04:05")
|
||||
}
|
||||
|
||||
func (d *Order) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
func (d *Order) sumUpBill(request *tools.APIRequest) error {
|
||||
for _, b := range d.SubOrders {
|
||||
err := b.SumUpBill(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.Total += b.Total
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TO FINISH
|
||||
func (d *Order) pay(request *tools.APIRequest) error {
|
||||
responses := make(chan *PeerOrder, len(d.SubOrders))
|
||||
var wg *sync.WaitGroup
|
||||
wg.Add(len(d.SubOrders))
|
||||
for _, b := range d.SubOrders {
|
||||
go b.Pay(request, responses, wg)
|
||||
}
|
||||
wg.Wait()
|
||||
errs := ""
|
||||
gotAnUnpaid := false
|
||||
count := 0
|
||||
for range responses {
|
||||
res := <-responses
|
||||
count++
|
||||
if res != nil {
|
||||
if res.Error != "" {
|
||||
errs += res.Error
|
||||
}
|
||||
if res.Status != enum.PAID {
|
||||
gotAnUnpaid = true
|
||||
}
|
||||
d.Status = enum.PARTIAL
|
||||
d.SubOrders[res.GetID()] = res
|
||||
if count == len(d.SubOrders) && !gotAnUnpaid {
|
||||
d.Status = enum.PAID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if errs != "" {
|
||||
return errors.New(errs)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PeerOrder struct {
|
||||
utils.AbstractObject
|
||||
Error string `json:"error,omitempty" bson:"error,omitempty"`
|
||||
PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"`
|
||||
Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
|
||||
BillingAddress string `json:"billing_address,omitempty" bson:"billing_address,omitempty"`
|
||||
Items []*PeerItemOrder `json:"items,omitempty" bson:"items,omitempty"`
|
||||
Total float64 `json:"total,omitempty" bson:"total,omitempty"`
|
||||
}
|
||||
|
||||
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.IsPurchased() {
|
||||
continue
|
||||
}
|
||||
accessor := purchase_resource.NewAccessor(request)
|
||||
accessor.StoreOne(&purchase_resource.PurchaseResource{
|
||||
ResourceID: b.Item.GetID(),
|
||||
ResourceType: b.Item.GetType(),
|
||||
EndDate: b.Item.GetLocationEnd(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if d.Status != enum.PENDING {
|
||||
response <- d
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
func (d *PeerOrder) SumUpBill(request *tools.APIRequest) error {
|
||||
for _, b := range d.Items {
|
||||
tot, err := b.GetPrice(request) // missing something
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.Total += tot
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *PeerOrder) AddItem(item pricing.PricedItemITF, quantity int) {
|
||||
d.Items = append(d.Items, &PeerItemOrder{
|
||||
Quantity: quantity,
|
||||
Item: item,
|
||||
})
|
||||
}
|
||||
|
||||
func (d *PeerOrder) SetName() {
|
||||
d.Name = d.UUID + "_order_" + d.PeerID + "_" + time.Now().UTC().Format("2006-01-02T15:04:05")
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
func (d *PeerItemOrder) GetPrice(request *tools.APIRequest) (float64, error) {
|
||||
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()}},
|
||||
},
|
||||
}, "", d.Purchase.IsDraft)
|
||||
if code == 200 && len(search) > 0 {
|
||||
for _, s := range search {
|
||||
if s.(*purchase_resource.PurchaseResource).EndDate == nil || time.Now().UTC().After(*s.(*purchase_resource.PurchaseResource).EndDate) {
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
p, err := d.Item.GetPrice()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return p * float64(d.Quantity), nil
|
||||
}
|
||||
|
||||
// WTF HOW TO SELECT THE RIGHT PRICE ???
|
||||
// SHOULD SET A BUYING STATUS WHEN PAYMENT IS VALIDATED
|
||||
|
||||
@@ -36,7 +36,7 @@ func (a *orderMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBO
|
||||
}
|
||||
|
||||
func (a *orderMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data,a)
|
||||
return nil, 404, errors.New("Not implemented")
|
||||
}
|
||||
|
||||
func (a *orderMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package tests
|
||||
@@ -26,24 +26,6 @@ func (m PeerState) EnumIndex() int {
|
||||
return int(m)
|
||||
}
|
||||
|
||||
func GetSelf() (utils.ShallowDBObject, string) {
|
||||
d, code, err := NewAccessor(nil).Search(nil, SELF.String(), false)
|
||||
if code != 200 || err != nil || len(d) == 0 {
|
||||
return nil, ""
|
||||
}
|
||||
id := d[0].GetID()
|
||||
return d[0], id
|
||||
}
|
||||
|
||||
func IsMySelf(peerID string) (bool, string) {
|
||||
d, code, err := NewAccessor(nil).Search(nil, SELF.String(), false)
|
||||
if code != 200 || err != nil || len(d) == 0 {
|
||||
return false, ""
|
||||
}
|
||||
id := d[0].GetID()
|
||||
return peerID == id, id
|
||||
}
|
||||
|
||||
// Peer is a struct that represents a peer
|
||||
type Peer struct {
|
||||
utils.AbstractObject
|
||||
@@ -55,7 +37,7 @@ type Peer struct {
|
||||
FailedExecution []PeerExecution `json:"failed_execution" bson:"failed_execution"` // FailedExecution is the list of failed executions, to be retried
|
||||
}
|
||||
|
||||
func (ao *Peer) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
func (ao *Peer) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -95,7 +77,7 @@ func (p *Peer) IsMySelf() (bool, string) {
|
||||
}
|
||||
|
||||
// LaunchPeerExecution launches an execution on a peer
|
||||
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataType, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) (map[string]interface{}, error) {
|
||||
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataType, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
|
||||
p.UUID = peerID
|
||||
return cache.LaunchPeerExecution(peerID, dataID, dt, method, body, caller) // Launch the execution on the peer through the cache
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
@@ -28,19 +29,47 @@ type PeerCache struct {
|
||||
}
|
||||
|
||||
// urlFormat formats the URL of the peer with the data type API function
|
||||
func (p *PeerCache) urlFormat(hostUrl string, dt tools.DataType) string {
|
||||
return hostUrl + "/" + strings.ReplaceAll(dt.API(), "oc-", "")
|
||||
func (p *PeerCache) urlFormat(url string, dt tools.DataType) string {
|
||||
// localhost is replaced by the local peer URL
|
||||
// because localhost must collide on a web request security protocol
|
||||
localhost := ""
|
||||
if strings.Contains(url, "localhost") {
|
||||
localhost = "localhost"
|
||||
}
|
||||
if strings.Contains(url, "127.0.0.1") {
|
||||
localhost = "127.0.0.1"
|
||||
}
|
||||
if localhost != "" {
|
||||
r := regexp.MustCompile("(" + localhost + ":[0-9]+)")
|
||||
t := r.FindString(url)
|
||||
if t != "" {
|
||||
url = strings.Replace(url, t, dt.API()+":8080/oc", -1)
|
||||
} else {
|
||||
url = strings.ReplaceAll(url, localhost, dt.API()+":8080/oc")
|
||||
}
|
||||
} else {
|
||||
url = url + "/" + dt.API()
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
// checkPeerStatus checks the status of a peer
|
||||
func (p *PeerCache) checkPeerStatus(peerID string, appName string) (*Peer, bool) {
|
||||
func (p *PeerCache) checkPeerStatus(peerID string, appName string, caller *tools.HTTPCaller) (*Peer, bool) {
|
||||
api := tools.API{}
|
||||
access := NewShallowAccessor()
|
||||
res, code, _ := access.LoadOne(peerID) // Load the peer from db
|
||||
if code != 200 { // no peer no party
|
||||
return nil, false
|
||||
}
|
||||
url := p.urlFormat(res.(*Peer).Url, tools.PEER) + "/status" // Format the URL
|
||||
methods := caller.URLS[tools.PEER] // Get the methods url of the peer
|
||||
if methods == nil {
|
||||
return res.(*Peer), false
|
||||
}
|
||||
meth := methods[tools.POST] // Get the POST method to check status
|
||||
if meth == "" {
|
||||
return res.(*Peer), false
|
||||
}
|
||||
url := p.urlFormat(res.(*Peer).Url, tools.PEER) + meth // Format the URL
|
||||
state, services := api.CheckRemotePeer(url)
|
||||
res.(*Peer).ServicesState = services // Update the services states of the peer
|
||||
access.UpdateOne(res, peerID) // Update the peer in the db
|
||||
@@ -48,49 +77,48 @@ func (p *PeerCache) checkPeerStatus(peerID string, appName string) (*Peer, bool)
|
||||
}
|
||||
|
||||
// LaunchPeerExecution launches an execution on a peer
|
||||
// The method contacts the path described by : peer.Url + datatype path (from enums) + replacement of id by dataID
|
||||
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
||||
dt tools.DataType, method tools.METHOD, body interface{}, caller tools.HTTPCallerITF) (map[string]interface{}, error) {
|
||||
fmt.Println("Launching peer execution on", caller.GetUrls(), dt, method)
|
||||
methods := caller.GetUrls()[dt] // Get the methods url of the data type
|
||||
dt tools.DataType, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
|
||||
fmt.Println("Launching peer execution on", caller.URLS, dt, method)
|
||||
methods := caller.URLS[dt] // Get the methods url of the data type
|
||||
if m, ok := methods[method]; !ok || m == "" {
|
||||
return map[string]interface{}{}, errors.New("Requested method " + method.String() + " not declared in HTTPCaller")
|
||||
return nil, errors.New("no path found")
|
||||
}
|
||||
path := methods[method] // Get the path corresponding to the action we want to execute
|
||||
path = strings.ReplaceAll(path, ":id", dataID) // Replace the id in the path in case of a DELETE / UPDATE method (it's a standard naming in OC)
|
||||
meth := methods[method] // Get the method url to execute
|
||||
meth = strings.ReplaceAll(meth, ":id", dataID) // Replace the id in the url in case of a DELETE / UPDATE method (it's a standard naming in OC)
|
||||
url := ""
|
||||
|
||||
// Check the status of the peer
|
||||
if mypeer, ok := p.checkPeerStatus(peerID, dt.API()); !ok && mypeer != nil {
|
||||
if mypeer, ok := p.checkPeerStatus(peerID, dt.API(), caller); !ok && mypeer != nil {
|
||||
// If the peer is not reachable, add the execution to the failed executions list
|
||||
pexec := &PeerExecution{
|
||||
Method: method.String(),
|
||||
Url: p.urlFormat((mypeer.Url), dt) + path, // the url is constitued of : host URL + resource path + action path (ex : mypeer.com/datacenter/resourcetype/path/to/action)
|
||||
Url: p.urlFormat((mypeer.Url)+meth, dt),
|
||||
Body: body,
|
||||
DataType: dt.EnumIndex(),
|
||||
DataID: dataID,
|
||||
}
|
||||
mypeer.AddExecution(*pexec)
|
||||
NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
|
||||
return map[string]interface{}{}, errors.New("peer is " + peerID + " not reachable")
|
||||
return nil, errors.New("peer is not reachable")
|
||||
} else {
|
||||
if mypeer == nil {
|
||||
return map[string]interface{}{}, errors.New("peer " + peerID + " not found")
|
||||
return nil, errors.New("peer not found")
|
||||
}
|
||||
// If the peer is reachable, launch the execution
|
||||
url = p.urlFormat((mypeer.Url), dt) + path // Format the URL
|
||||
url = p.urlFormat((mypeer.Url)+meth, dt) // Format the URL
|
||||
tmp := mypeer.FailedExecution // Get the failed executions list
|
||||
mypeer.FailedExecution = []PeerExecution{} // Reset the failed executions list
|
||||
NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
|
||||
for _, v := range tmp { // Retry the failed executions
|
||||
go p.Exec(v.Url, tools.ToMethod(v.Method), v.Body, caller)
|
||||
go p.exec(v.Url, tools.ToMethod(v.Method), v.Body, caller)
|
||||
}
|
||||
}
|
||||
return p.Exec(url, method, body, caller) // Execute the method
|
||||
return nil, p.exec(url, method, body, caller) // Execute the method
|
||||
}
|
||||
|
||||
// exec executes the method on the peer
|
||||
func (p *PeerCache) Exec(url string, method tools.METHOD, body interface{}, caller tools.HTTPCallerITF) (map[string]interface{}, error) {
|
||||
func (p *PeerCache) exec(url string, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) error {
|
||||
var b []byte
|
||||
var err error
|
||||
if method == tools.POST { // Execute the POST method if it's a POST method
|
||||
@@ -102,16 +130,16 @@ func (p *PeerCache) Exec(url string, method tools.METHOD, body interface{}, call
|
||||
if method == tools.DELETE { // Execute the DELETE method if it's a DELETE method
|
||||
b, err = caller.CallDelete(url, "")
|
||||
}
|
||||
var m map[string]interface{}
|
||||
if err != nil {
|
||||
return m, err
|
||||
return err
|
||||
}
|
||||
var m map[string]interface{}
|
||||
err = json.Unmarshal(b, &m)
|
||||
if err != nil {
|
||||
return m, err
|
||||
return err
|
||||
}
|
||||
if e, ok := m["error"]; ok && e != "<nil>" && e != "" { // Check if there is an error in the response
|
||||
return m, errors.New(fmt.Sprintf("%v", m["error"]))
|
||||
return errors.New(fmt.Sprintf("%v", m["error"]))
|
||||
}
|
||||
return m, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ import (
|
||||
|
||||
type peerMongoAccessor struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
OverrideAuth bool
|
||||
overrideAuth bool
|
||||
}
|
||||
|
||||
// New creates a new instance of the peerMongoAccessor
|
||||
func NewShallowAccessor() *peerMongoAccessor {
|
||||
return &peerMongoAccessor{
|
||||
OverrideAuth: true,
|
||||
overrideAuth: true,
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
||||
Type: tools.PEER,
|
||||
@@ -27,7 +27,7 @@ func NewShallowAccessor() *peerMongoAccessor {
|
||||
|
||||
func NewAccessor(request *tools.APIRequest) *peerMongoAccessor {
|
||||
return &peerMongoAccessor{
|
||||
OverrideAuth: false,
|
||||
overrideAuth: false,
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
@@ -37,7 +37,7 @@ func NewAccessor(request *tools.APIRequest) *peerMongoAccessor {
|
||||
}
|
||||
|
||||
func (wfa *peerMongoAccessor) ShouldVerifyAuth() bool {
|
||||
return !wfa.OverrideAuth
|
||||
return !wfa.overrideAuth
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -73,12 +73,12 @@ func (wfa *peerMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, in
|
||||
}
|
||||
|
||||
func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*Peer](filters, search, wfa.GetDefaultFilter(search),
|
||||
return utils.GenericSearch[*Peer](filters, search, wfa.getDefaultFilter(search),
|
||||
func(d utils.DBObject) utils.ShallowDBObject {
|
||||
return d
|
||||
}, isDraft, wfa)
|
||||
}
|
||||
func (a *peerMongoAccessor) GetDefaultFilter(search string) *dbs.Filters {
|
||||
func (a *peerMongoAccessor) getDefaultFilter(search string) *dbs.Filters {
|
||||
if i, err := strconv.Atoi(search); err == nil {
|
||||
return &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
package peer_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// MockHTTPCaller mocks tools.HTTPCaller
|
||||
type MockHTTPCaller struct {
|
||||
mock.Mock
|
||||
URLS map[tools.DataType]map[tools.METHOD]string
|
||||
}
|
||||
|
||||
func (c *MockHTTPCaller) GetUrls() map[tools.DataType]map[tools.METHOD]string {
|
||||
return c.URLS
|
||||
}
|
||||
|
||||
func (m *MockHTTPCaller) CallPost(url, token string, body interface{}, types ...string) ([]byte, error) {
|
||||
args := m.Called(url, token, body)
|
||||
return args.Get(0).([]byte), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockHTTPCaller) CallGet(url, token string, types ...string) ([]byte, error) {
|
||||
args := m.Called(url, token)
|
||||
return args.Get(0).([]byte), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockHTTPCaller) CallDelete(url, token string) ([]byte, error) {
|
||||
args := m.Called(url, token)
|
||||
return args.Get(0).([]byte), args.Error(1)
|
||||
}
|
||||
|
||||
func TestLaunchPeerExecution_PeerNotReachable(t *testing.T) {
|
||||
cache := &peer.PeerCache{}
|
||||
caller := &MockHTTPCaller{
|
||||
URLS: map[tools.DataType]map[tools.METHOD]string{
|
||||
tools.PEER: {
|
||||
tools.POST: "/execute/:id",
|
||||
},
|
||||
},
|
||||
}
|
||||
exec, err := cache.LaunchPeerExecution("peer-id", "data-id", tools.PEER, tools.POST, map[string]string{"a": "b"}, caller)
|
||||
assert.Nil(t, exec)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "not reachable")
|
||||
}
|
||||
|
||||
func TestExecSuccess(t *testing.T) {
|
||||
cache := &peer.PeerCache{}
|
||||
caller := &MockHTTPCaller{}
|
||||
url := "http://mockpeer/resource"
|
||||
response := map[string]interface{}{"result": "ok"}
|
||||
data, _ := json.Marshal(response)
|
||||
|
||||
caller.On("CallPost", url, "", mock.Anything).Return(data, nil)
|
||||
_, err := cache.Exec(url, tools.POST, map[string]string{"key": "value"}, caller)
|
||||
assert.NoError(t, err)
|
||||
caller.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestExecReturnsErrorField(t *testing.T) {
|
||||
cache := &peer.PeerCache{}
|
||||
caller := &MockHTTPCaller{}
|
||||
url := "http://mockpeer/resource"
|
||||
response := map[string]interface{}{"error": "something failed"}
|
||||
data, _ := json.Marshal(response)
|
||||
|
||||
caller.On("CallPost", url, "", mock.Anything).Return(data, nil)
|
||||
_, err := cache.Exec(url, tools.POST, map[string]string{"key": "value"}, caller)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "something failed", err.Error())
|
||||
}
|
||||
|
||||
func TestExecInvalidJSON(t *testing.T) {
|
||||
cache := &peer.PeerCache{}
|
||||
caller := &MockHTTPCaller{}
|
||||
url := "http://mockpeer/resource"
|
||||
caller.On("CallPost", url, "", mock.Anything).Return([]byte("{invalid json}"), nil)
|
||||
_, err := cache.Exec(url, tools.POST, map[string]string{"key": "value"}, caller)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid character")
|
||||
}
|
||||
|
||||
type mockAccessor struct {
|
||||
loadOne func(string) (interface{}, int, error)
|
||||
updateOne func(interface{}, string) error
|
||||
}
|
||||
|
||||
func (m *mockAccessor) LoadOne(id string) (interface{}, int, error) {
|
||||
return m.loadOne(id)
|
||||
}
|
||||
|
||||
func (m *mockAccessor) UpdateOne(i interface{}, id string) error {
|
||||
return m.updateOne(i, id)
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
package peer_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
)
|
||||
|
||||
type MockAccessor struct {
|
||||
mock.Mock
|
||||
utils.AbstractAccessor
|
||||
}
|
||||
|
||||
func (m *MockAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(set, id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
args := m.Called(data)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(filters, search, isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func newTestPeer() *peer.Peer {
|
||||
return &peer.Peer{
|
||||
Url: "http://localhost",
|
||||
WalletAddress: "0x123",
|
||||
PublicKey: "pubkey",
|
||||
State: peer.SELF,
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteOne_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
mockAcc.On("DeleteOne", "id").Return(newTestPeer(), 200, nil)
|
||||
|
||||
obj, code, err := mockAcc.DeleteOne("id")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.NotNil(t, obj)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestUpdateOne_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
peerObj := newTestPeer()
|
||||
mockAcc.On("UpdateOne", peerObj, "id").Return(peerObj, 200, nil)
|
||||
|
||||
obj, code, err := mockAcc.UpdateOne(peerObj, "id")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, peerObj, obj)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestStoreOne_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
peerObj := newTestPeer()
|
||||
mockAcc.On("StoreOne", peerObj).Return(peerObj, 200, nil)
|
||||
|
||||
obj, code, err := mockAcc.StoreOne(peerObj)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, peerObj, obj)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestLoadOne_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
mockAcc.On("LoadOne", "test-id").Return(newTestPeer(), 200, nil)
|
||||
|
||||
obj, code, err := mockAcc.LoadOne("test-id")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.NotNil(t, obj)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestLoadAll_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
expected := []utils.ShallowDBObject{newTestPeer()}
|
||||
mockAcc.On("LoadAll", false).Return(expected, 200, nil)
|
||||
|
||||
objs, code, err := mockAcc.LoadAll(false)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, expected, objs)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestSearch_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
filters := &dbs.Filters{}
|
||||
expected := []utils.ShallowDBObject{newTestPeer()}
|
||||
mockAcc.On("Search", filters, "test", false).Return(expected, 200, nil)
|
||||
|
||||
objs, code, err := mockAcc.Search(filters, "test", false)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, expected, objs)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
85
models/resources/compute.go
Executable file → Normal file
85
models/resources/compute.go
Executable file → Normal file
@@ -10,7 +10,6 @@ import (
|
||||
"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"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -18,7 +17,7 @@ import (
|
||||
* it defines the resource compute
|
||||
*/
|
||||
type ComputeResource struct {
|
||||
AbstractInstanciatedResource[*ComputeResourceInstance]
|
||||
AbstractIntanciatedResource[*ComputeResourceInstance]
|
||||
Architecture string `json:"architecture,omitempty" bson:"architecture,omitempty"` // Architecture is the architecture
|
||||
Infrastructure enum.InfrastructureType `json:"infrastructure" bson:"infrastructure" default:"-1"` // Infrastructure is the infrastructure
|
||||
}
|
||||
@@ -31,18 +30,16 @@ func (r *ComputeResource) GetType() string {
|
||||
return tools.COMPUTE_RESOURCE.String()
|
||||
}
|
||||
|
||||
func (abs *ComputeResource) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||
func (abs *ComputeResource) ConvertToPricedResource(
|
||||
t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||
if t != tools.COMPUTE_RESOURCE {
|
||||
return nil, errors.New("not the proper type expected : cannot convert to priced resource : have " + t.String() + " wait Compute")
|
||||
}
|
||||
p, err := abs.AbstractInstanciatedResource.ConvertToPricedResource(t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil
|
||||
}
|
||||
p := abs.AbstractIntanciatedResource.ConvertToPricedResource(t, request)
|
||||
priced := p.(*PricedResource)
|
||||
return &PricedComputeResource{
|
||||
PricedResource: *priced,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type ComputeNode struct {
|
||||
@@ -55,7 +52,6 @@ type ComputeNode struct {
|
||||
|
||||
type ComputeResourceInstance struct {
|
||||
ResourceInstance[*ComputeResourcePartnership]
|
||||
Source string `json:"source,omitempty" bson:"source,omitempty"` // Source is the source of the resource
|
||||
SecurityLevel string `json:"security_level,omitempty" bson:"security_level,omitempty"`
|
||||
PowerSources []string `json:"power_sources,omitempty" bson:"power_sources,omitempty"`
|
||||
AnnualCO2Emissions float64 `json:"annual_co2_emissions,omitempty" bson:"co2_emissions,omitempty"`
|
||||
@@ -64,24 +60,9 @@ type ComputeResourceInstance struct {
|
||||
Nodes []*ComputeNode `json:"nodes,omitempty" bson:"nodes,omitempty"`
|
||||
}
|
||||
|
||||
func NewComputeResourceInstance(name string, peerID string) ResourceInstanceITF {
|
||||
return &ComputeResourceInstance{
|
||||
ResourceInstance: ResourceInstance[*ComputeResourcePartnership]{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
UUID: uuid.New().String(),
|
||||
Name: name,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type ComputeResourcePartnership struct {
|
||||
ResourcePartnerShip[*ComputeResourcePricingProfile]
|
||||
MinGaranteedCPUsCores map[string]float64 `json:"garanteed_cpus,omitempty" bson:"garanteed_cpus,omitempty"`
|
||||
MinGaranteedGPUsMemoryGB map[string]float64 `json:"garanteed_gpus,omitempty" bson:"garanteed_gpus,omitempty"`
|
||||
MinGaranteedRAMSize float64 `json:"garanteed_ram,omitempty" bson:"garanteed_ram,omitempty"`
|
||||
|
||||
MaxAllowedCPUsCores map[string]float64 `json:"allowed_cpus,omitempty" bson:"allowed_cpus,omitempty"`
|
||||
MaxAllowedCPUsCores map[string]int `json:"allowed_cpus,omitempty" bson:"allowed_cpus,omitempty"`
|
||||
MaxAllowedGPUsMemoryGB map[string]float64 `json:"allowed_gpus,omitempty" bson:"allowed_gpus,omitempty"`
|
||||
MaxAllowedRAMSize float64 `json:"allowed_ram,omitempty" bson:"allowed_ram,omitempty"`
|
||||
}
|
||||
@@ -94,19 +75,8 @@ type ComputeResourcePricingProfile struct {
|
||||
RAMPrice float64 `json:"ram_price" bson:"ram_price" default:"-1"` // RAMPrice is the price of the RAM
|
||||
}
|
||||
|
||||
func (p *ComputeResourcePricingProfile) IsPurchasable() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.UNDEFINED_SUBSCRIPTION
|
||||
}
|
||||
|
||||
func (p *ComputeResourcePricingProfile) GetPurchase() pricing.BuyingStrategy {
|
||||
return p.Pricing.BuyingStrategy
|
||||
}
|
||||
|
||||
func (p *ComputeResourcePricingProfile) IsBooked() bool {
|
||||
if p.Pricing.BuyingStrategy == pricing.PERMANENT {
|
||||
p.Pricing.BuyingStrategy = pricing.SUBSCRIPTION
|
||||
}
|
||||
return true
|
||||
func (p *ComputeResourcePricingProfile) IsPurchased() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
|
||||
}
|
||||
|
||||
func (p *ComputeResourcePricingProfile) GetOverrideStrategyValue() int {
|
||||
@@ -115,7 +85,7 @@ func (p *ComputeResourcePricingProfile) GetOverrideStrategyValue() int {
|
||||
|
||||
// NOT A PROPER QUANTITY
|
||||
// amountOfData is the number of CPUs, GPUs or RAM dependings on the params
|
||||
func (p *ComputeResourcePricingProfile) GetPriceHT(amountOfData float64, explicitDuration float64, start time.Time, end time.Time, variation []*pricing.PricingVariation, params ...string) (float64, error) {
|
||||
func (p *ComputeResourcePricingProfile) GetPrice(amountOfData float64, explicitDuration float64, start time.Time, end time.Time, params ...string) (float64, error) {
|
||||
if len(params) < 1 {
|
||||
return 0, errors.New("params must be set")
|
||||
}
|
||||
@@ -125,7 +95,7 @@ func (p *ComputeResourcePricingProfile) GetPriceHT(amountOfData float64, explici
|
||||
if _, ok := p.CPUsPrices[model]; ok {
|
||||
p.Pricing.Price = p.CPUsPrices[model]
|
||||
}
|
||||
r, err := p.Pricing.GetPriceHT(amountOfData, explicitDuration, start, &end, variation)
|
||||
r, err := p.Pricing.GetPrice(amountOfData, explicitDuration, start, &end)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -136,7 +106,7 @@ func (p *ComputeResourcePricingProfile) GetPriceHT(amountOfData float64, explici
|
||||
if _, ok := p.GPUsPrices[model]; ok {
|
||||
p.Pricing.Price = p.GPUsPrices[model]
|
||||
}
|
||||
r, err := p.Pricing.GetPriceHT(amountOfData, explicitDuration, start, &end, variation)
|
||||
r, err := p.Pricing.GetPrice(amountOfData, explicitDuration, start, &end)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -146,7 +116,7 @@ func (p *ComputeResourcePricingProfile) GetPriceHT(amountOfData float64, explici
|
||||
if p.RAMPrice >= 0 {
|
||||
p.Pricing.Price = p.RAMPrice
|
||||
}
|
||||
r, err := p.Pricing.GetPriceHT(float64(amountOfData), explicitDuration, start, &end, variation)
|
||||
r, err := p.Pricing.GetPrice(float64(amountOfData), explicitDuration, start, &end)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -167,36 +137,33 @@ func (r *PricedComputeResource) GetType() tools.DataType {
|
||||
return tools.COMPUTE_RESOURCE
|
||||
}
|
||||
|
||||
func (r *PricedComputeResource) GetPriceHT() (float64, error) {
|
||||
if r.BookingConfiguration == nil {
|
||||
r.BookingConfiguration = &BookingConfiguration{}
|
||||
}
|
||||
func (r *PricedComputeResource) GetPrice() (float64, error) {
|
||||
now := time.Now()
|
||||
if r.BookingConfiguration.UsageStart == nil {
|
||||
r.BookingConfiguration.UsageStart = &now
|
||||
if r.UsageStart == nil {
|
||||
r.UsageStart = &now
|
||||
}
|
||||
if r.BookingConfiguration.UsageEnd == nil {
|
||||
add := r.BookingConfiguration.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
r.BookingConfiguration.UsageEnd = &add
|
||||
if r.UsageEnd == nil {
|
||||
add := r.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
r.UsageEnd = &add
|
||||
}
|
||||
if r.SelectedPricing == nil {
|
||||
return 0, errors.New("pricing profile must be set on Priced Compute" + r.ResourceID)
|
||||
if len(r.PricingProfiles) == 0 {
|
||||
return 0, errors.New("pricing profile must be set on Priced Compute" + r.ResourceID)
|
||||
}
|
||||
r.SelectedPricing = &r.PricingProfiles[0]
|
||||
}
|
||||
pricing := r.SelectedPricing
|
||||
pricing := *r.SelectedPricing
|
||||
price := float64(0)
|
||||
for _, l := range []map[string]float64{r.CPUsLocated, r.GPUsLocated} {
|
||||
for model, amountOfData := range l {
|
||||
cpus, err := pricing.GetPriceHT(float64(amountOfData),
|
||||
r.BookingConfiguration.ExplicitBookingDurationS, *r.BookingConfiguration.UsageStart,
|
||||
*r.BookingConfiguration.UsageEnd, r.Variations, "cpus", model)
|
||||
cpus, err := pricing.GetPrice(float64(amountOfData), r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd, "cpus", model)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
price += cpus
|
||||
}
|
||||
}
|
||||
ram, err := pricing.GetPriceHT(r.RAMLocated, r.BookingConfiguration.ExplicitBookingDurationS,
|
||||
*r.BookingConfiguration.UsageStart, *r.BookingConfiguration.UsageEnd, r.Variations, "ram")
|
||||
ram, err := pricing.GetPrice(r.RAMLocated, r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd, "ram")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
73
models/resources/data.go
Executable file → Normal file
73
models/resources/data.go
Executable file → Normal file
@@ -9,7 +9,6 @@ import (
|
||||
"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"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -17,7 +16,7 @@ import (
|
||||
* it defines the resource data
|
||||
*/
|
||||
type DataResource struct {
|
||||
AbstractInstanciatedResource[*DataInstance]
|
||||
AbstractIntanciatedResource[*DataInstance]
|
||||
Type string `bson:"type,omitempty" json:"type,omitempty"`
|
||||
Quality string `bson:"quality,omitempty" json:"quality,omitempty"`
|
||||
OpenData bool `bson:"open_data" json:"open_data" default:"false"` // Type is the type of the storage
|
||||
@@ -38,18 +37,16 @@ func (r *DataResource) GetType() string {
|
||||
return tools.DATA_RESOURCE.String()
|
||||
}
|
||||
|
||||
func (abs *DataResource) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||
func (abs *DataResource) ConvertToPricedResource(
|
||||
t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||
if t != tools.DATA_RESOURCE {
|
||||
return nil, errors.New("not the proper type expected : cannot convert to priced resource : have " + t.String() + " wait Data")
|
||||
}
|
||||
p, err := abs.AbstractInstanciatedResource.ConvertToPricedResource(t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil
|
||||
}
|
||||
p := abs.AbstractIntanciatedResource.ConvertToPricedResource(t, request)
|
||||
priced := p.(*PricedResource)
|
||||
return &PricedDataResource{
|
||||
PricedResource: *priced,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type DataInstance struct {
|
||||
@@ -57,17 +54,6 @@ type DataInstance struct {
|
||||
Source string `json:"source,omitempty" bson:"source,omitempty"` // Source is the source of the data
|
||||
}
|
||||
|
||||
func NewDataInstance(name string, peerID string) ResourceInstanceITF {
|
||||
return &DataInstance{
|
||||
ResourceInstance: ResourceInstance[*DataResourcePartnership]{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
UUID: uuid.New().String(),
|
||||
Name: name,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (ri *DataInstance) StoreDraftDefault() {
|
||||
found := false
|
||||
for _, p := range ri.ResourceInstance.Env {
|
||||
@@ -96,7 +82,7 @@ type DataResourcePartnership struct {
|
||||
type DataResourcePricingStrategy int
|
||||
|
||||
const (
|
||||
PER_DOWNLOAD DataResourcePricingStrategy = iota + 6
|
||||
PER_DOWNLOAD DataResourcePricingStrategy = iota
|
||||
PER_TB_DOWNLOADED
|
||||
PER_GB_DOWNLOADED
|
||||
PER_MB_DOWNLOADED
|
||||
@@ -104,9 +90,7 @@ const (
|
||||
)
|
||||
|
||||
func (t DataResourcePricingStrategy) String() string {
|
||||
l := pricing.TimePricingStrategyListStr()
|
||||
l = append(l, []string{"PER DOWNLOAD", "PER TB DOWNLOADED", "PER GB DOWNLOADED", "PER MB DOWNLOADED", "PER KB DOWNLOADED"}...)
|
||||
return l[t]
|
||||
return [...]string{"PER DOWNLOAD", "PER TB DOWNLOADED", "PER GB DOWNLOADED", "PER MB DOWNLOADED", "PER KB DOWNLOADED"}[t]
|
||||
}
|
||||
|
||||
func DataResourcePricingStrategyList() []DataResourcePricingStrategy {
|
||||
@@ -118,9 +102,7 @@ func ToDataResourcePricingStrategy(i int) DataResourcePricingStrategy {
|
||||
}
|
||||
|
||||
func (t DataResourcePricingStrategy) GetStrategy() string {
|
||||
l := pricing.TimePricingStrategyListStr()
|
||||
l = append(l, []string{"PER DATA STORED", "PER TB STORED", "PER GB STORED", "PER MB STORED", "PER KB STORED"}...)
|
||||
return l[t]
|
||||
return [...]string{"PER_DOWNLOAD", "PER_GB", "PER_MB", "PER_KB"}[t]
|
||||
}
|
||||
|
||||
func (t DataResourcePricingStrategy) GetStrategyValue() int {
|
||||
@@ -151,13 +133,12 @@ func (p *DataResourcePricingProfile) GetOverrideStrategyValue() int {
|
||||
return p.Pricing.OverrideStrategy.GetStrategyValue()
|
||||
}
|
||||
|
||||
func (p *DataResourcePricingProfile) IsPurchasable() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.UNDEFINED_SUBSCRIPTION
|
||||
func (p *DataResourcePricingProfile) GetPrice(amountOfData float64, explicitDuration float64, start time.Time, end time.Time, params ...string) (float64, error) {
|
||||
return p.Pricing.GetPrice(amountOfData, explicitDuration, start, &end)
|
||||
}
|
||||
|
||||
func (p *DataResourcePricingProfile) IsBooked() bool {
|
||||
// TODO WHAT ABOUT PAY PER USE... it's a complicate CASE
|
||||
return p.Pricing.BuyingStrategy != pricing.PERMANENT
|
||||
func (p *DataResourcePricingProfile) IsPurchased() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
|
||||
}
|
||||
|
||||
type PricedDataResource struct {
|
||||
@@ -169,23 +150,23 @@ func (r *PricedDataResource) GetType() tools.DataType {
|
||||
return tools.DATA_RESOURCE
|
||||
}
|
||||
|
||||
func (r *PricedDataResource) GetPriceHT() (float64, error) {
|
||||
if r.BookingConfiguration == nil {
|
||||
r.BookingConfiguration = &BookingConfiguration{}
|
||||
}
|
||||
fmt.Println("GetPriceHT", r.BookingConfiguration.UsageStart, r.BookingConfiguration.UsageEnd)
|
||||
func (r *PricedDataResource) GetPrice() (float64, error) {
|
||||
fmt.Println("GetPrice", r.UsageStart, r.UsageEnd)
|
||||
now := time.Now()
|
||||
if r.BookingConfiguration.UsageStart == nil {
|
||||
r.BookingConfiguration.UsageStart = &now
|
||||
if r.UsageStart == nil {
|
||||
r.UsageStart = &now
|
||||
}
|
||||
if r.BookingConfiguration.UsageEnd == nil {
|
||||
add := r.BookingConfiguration.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
r.BookingConfiguration.UsageEnd = &add
|
||||
if r.UsageEnd == nil {
|
||||
add := r.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
r.UsageEnd = &add
|
||||
}
|
||||
if r.SelectedPricing == nil {
|
||||
return 0, errors.New("pricing profile must be set on Priced Data" + r.ResourceID)
|
||||
if len(r.PricingProfiles) == 0 {
|
||||
return 0, errors.New("pricing profile must be set on Priced Data" + r.ResourceID)
|
||||
}
|
||||
r.SelectedPricing = &r.PricingProfiles[0]
|
||||
}
|
||||
pricing := r.SelectedPricing
|
||||
pricing := *r.SelectedPricing
|
||||
var err error
|
||||
amountOfData := float64(1)
|
||||
if pricing.GetOverrideStrategyValue() >= 0 {
|
||||
@@ -194,7 +175,5 @@ func (r *PricedDataResource) GetPriceHT() (float64, error) {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return pricing.GetPriceHT(amountOfData, r.BookingConfiguration.ExplicitBookingDurationS,
|
||||
*r.BookingConfiguration.UsageStart, *r.BookingConfiguration.UsageEnd, r.Variations)
|
||||
return pricing.GetPrice(amountOfData, r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd)
|
||||
}
|
||||
|
||||
11
models/resources/interfaces.go
Executable file → Normal file
11
models/resources/interfaces.go
Executable file → Normal file
@@ -1,7 +1,6 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"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"
|
||||
@@ -10,13 +9,10 @@ import (
|
||||
type ResourceInterface interface {
|
||||
utils.DBObject
|
||||
Trim()
|
||||
GetBookingModes() map[booking.BookingMode]*pricing.PricingVariation
|
||||
ConvertToPricedResource(t tools.DataType, a *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, b *int, request *tools.APIRequest) (pricing.PricedItemITF, error)
|
||||
ConvertToPricedResource(t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF
|
||||
GetType() string
|
||||
GetSelectedInstance(selected *int) ResourceInstanceITF
|
||||
ClearEnv() utils.DBObject
|
||||
GetSelectedInstance() utils.DBObject
|
||||
SetAllowedInstances(request *tools.APIRequest)
|
||||
AddInstances(instance ResourceInstanceITF)
|
||||
}
|
||||
|
||||
type ResourceInstanceITF interface {
|
||||
@@ -24,8 +20,6 @@ type ResourceInstanceITF interface {
|
||||
GetID() string
|
||||
GetName() string
|
||||
StoreDraftDefault()
|
||||
ClearEnv()
|
||||
GetProfile(peerID string, partnershipIndex *int, buying *int, strategy *int) pricing.PricingProfileITF
|
||||
GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF
|
||||
GetPeerGroups() ([]ResourcePartnerITF, []map[string][]string)
|
||||
ClearPeerGroups()
|
||||
@@ -35,5 +29,4 @@ type ResourcePartnerITF interface {
|
||||
GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF
|
||||
GetPeerGroups() map[string][]string
|
||||
ClearPeerGroups()
|
||||
GetProfile(buying *int, strategy *int) pricing.PricingProfileITF
|
||||
}
|
||||
|
||||
3
models/resources/models.go
Executable file → Normal file
3
models/resources/models.go
Executable file → Normal file
@@ -11,14 +11,12 @@ type ResourceSet struct {
|
||||
Processings []string `bson:"processings,omitempty" json:"processings,omitempty"`
|
||||
Computes []string `bson:"computes,omitempty" json:"computes,omitempty"`
|
||||
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
|
||||
NativeTool []string `bson:"native,omitempty" json:"native,omitempty"`
|
||||
|
||||
DataResources []*DataResource `bson:"-" json:"data_resources,omitempty"`
|
||||
StorageResources []*StorageResource `bson:"-" json:"storage_resources,omitempty"`
|
||||
ProcessingResources []*ProcessingResource `bson:"-" json:"processing_resources,omitempty"`
|
||||
ComputeResources []*ComputeResource `bson:"-" json:"compute_resources,omitempty"`
|
||||
WorkflowResources []*WorkflowResource `bson:"-" json:"workflow_resources,omitempty"`
|
||||
NativeTools []*NativeTool `bson:"-" json:"native_tools,omitempty"`
|
||||
}
|
||||
|
||||
func (r *ResourceSet) Clear() {
|
||||
@@ -64,5 +62,4 @@ type ItemResource struct {
|
||||
Storage *StorageResource `bson:"storage,omitempty" json:"storage,omitempty"`
|
||||
Compute *ComputeResource `bson:"compute,omitempty" json:"compute,omitempty"`
|
||||
Workflow *WorkflowResource `bson:"workflow,omitempty" json:"workflow,omitempty"`
|
||||
NativeTool *NativeTool `bson:"native_tools,omitempty" json:"native_tools,omitempty"`
|
||||
}
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/native_tools"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
/*
|
||||
* NativeT ools is a struct that represents Native Functionnality of OPENCLOUD
|
||||
*/
|
||||
type NativeTool struct {
|
||||
AbstractResource
|
||||
Kind int `json:"kind" bson:"kind" validate:"required"`
|
||||
Params map[string]interface{}
|
||||
}
|
||||
|
||||
func (d *NativeTool) SetName(name string) {
|
||||
d.Name = name
|
||||
}
|
||||
|
||||
func (d *NativeTool) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor[*NativeTool](tools.NATIVE_TOOL, request, func() utils.DBObject { return &NativeTool{} })
|
||||
}
|
||||
|
||||
func (r *NativeTool) AddInstances(instance ResourceInstanceITF) {
|
||||
}
|
||||
|
||||
func (r *NativeTool) GetType() string {
|
||||
return tools.NATIVE_TOOL.String()
|
||||
}
|
||||
|
||||
func (d *NativeTool) ClearEnv() utils.DBObject {
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *NativeTool) Trim() {
|
||||
/* EMPTY */
|
||||
}
|
||||
func (w *NativeTool) SetAllowedInstances(request *tools.APIRequest) {
|
||||
/* EMPTY */
|
||||
}
|
||||
|
||||
func (w *NativeTool) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||
return &PricedResource{
|
||||
Name: w.Name,
|
||||
Logo: w.Logo,
|
||||
ResourceID: w.UUID,
|
||||
ResourceType: t,
|
||||
Quantity: 1,
|
||||
CreatorID: w.CreatorID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func InitNative() {
|
||||
for _, kind := range []native_tools.NativeToolsEnum{native_tools.WORKFLOW_EVENT} {
|
||||
newNative := &NativeTool{}
|
||||
access := newNative.GetAccessor(&tools.APIRequest{Admin: true})
|
||||
l, _, err := access.Search(nil, kind.String(), false)
|
||||
if err != nil || len(l) == 0 {
|
||||
newNative.Name = kind.String()
|
||||
newNative.Kind = int(kind)
|
||||
b, _ := json.Marshal(kind.Params())
|
||||
var m map[string]interface{}
|
||||
json.Unmarshal(b, &m)
|
||||
newNative.Params = m
|
||||
access.StoreOne(newNative)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package native_tools
|
||||
|
||||
type NativeToolsEnum int
|
||||
|
||||
const (
|
||||
WORKFLOW_EVENT NativeToolsEnum = iota
|
||||
)
|
||||
|
||||
var Params = [...]interface{}{
|
||||
WorkflowEventParams{},
|
||||
}
|
||||
|
||||
var Str = [...]string{
|
||||
"WORKFLOW_EVENT",
|
||||
}
|
||||
|
||||
func (d NativeToolsEnum) Params() interface{} {
|
||||
return Str[d]
|
||||
}
|
||||
|
||||
func (d NativeToolsEnum) String() string {
|
||||
return Str[d]
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package native_tools
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
)
|
||||
|
||||
/*
|
||||
* Workflow Event is a struct that represents a native functiunality.
|
||||
*/
|
||||
|
||||
type WorkflowEventParams struct {
|
||||
WorkflowResourceID string `json:"workflow_execution_id" bson:"workflow_execution_id" validate:"required"`
|
||||
BookingMode *booking.BookingMode `json:"booking_mode" bson:"booking_mode"`
|
||||
}
|
||||
|
||||
func (wep *WorkflowEventParams) GetBuyingStrategy() pricing.BillingStrategy {
|
||||
return pricing.BILL_ONCE
|
||||
}
|
||||
123
models/resources/priced_resource.go
Executable file → Normal file
123
models/resources/priced_resource.go
Executable file → Normal file
@@ -5,41 +5,22 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type BookingConfiguration struct {
|
||||
ExplicitBookingDurationS float64 `json:"explicit_location_duration_s,omitempty" bson:"explicit_location_duration_s,omitempty"`
|
||||
UsageStart *time.Time `json:"start,omitempty" bson:"start,omitempty"`
|
||||
UsageEnd *time.Time `json:"end,omitempty" bson:"end,omitempty"`
|
||||
Mode booking.BookingMode `json:"mode,omitempty" bson:"mode,omitempty"`
|
||||
}
|
||||
|
||||
type PricedResource struct {
|
||||
Name string `json:"name,omitempty" bson:"name,omitempty"`
|
||||
Logo string `json:"logo,omitempty" bson:"logo,omitempty"`
|
||||
InstancesRefs map[string]string `json:"instances_refs,omitempty" bson:"instances_refs,omitempty"`
|
||||
SelectedPricing pricing.PricingProfileITF `json:"selected_pricing,omitempty" bson:"selected_pricing,omitempty"`
|
||||
Quantity int `json:"quantity,omitempty" bson:"quantity,omitempty"`
|
||||
BookingConfiguration *BookingConfiguration `json:"booking_configuration,omitempty" bson:"booking_configuration,omitempty"`
|
||||
Variations []*pricing.PricingVariation `json:"pricing_variations" bson:"pricing_variations"`
|
||||
CreatorID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"`
|
||||
ResourceID string `json:"resource_id,omitempty" bson:"resource_id,omitempty"`
|
||||
ResourceType tools.DataType `json:"resource_type,omitempty" bson:"resource_type,omitempty"`
|
||||
}
|
||||
|
||||
func (abs *PricedResource) GetQuantity() int {
|
||||
return abs.Quantity
|
||||
}
|
||||
|
||||
func (abs *PricedResource) AddQuantity(amount int) {
|
||||
abs.Quantity += amount
|
||||
}
|
||||
|
||||
func (abs *PricedResource) SelectPricing() pricing.PricingProfileITF {
|
||||
return abs.SelectedPricing
|
||||
Name string `json:"name,omitempty" bson:"name,omitempty"`
|
||||
Logo string `json:"logo,omitempty" bson:"logo,omitempty"`
|
||||
InstancesRefs map[string]string `json:"instances_refs,omitempty" bson:"instances_refs,omitempty"`
|
||||
PricingProfiles []pricing.PricingProfileITF `json:"pricing_profiles,omitempty" bson:"pricing_profiles,omitempty"`
|
||||
SelectedPricing *pricing.PricingProfileITF `json:"selected_pricing,omitempty" bson:"selected_pricing,omitempty"`
|
||||
ExplicitBookingDurationS float64 `json:"explicit_location_duration_s,omitempty" bson:"explicit_location_duration_s,omitempty"`
|
||||
UsageStart *time.Time `json:"start,omitempty" bson:"start,omitempty"`
|
||||
UsageEnd *time.Time `json:"end,omitempty" bson:"end,omitempty"`
|
||||
CreatorID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"`
|
||||
ResourceID string `json:"resource_id,omitempty" bson:"resource_id,omitempty"`
|
||||
ResourceType tools.DataType `json:"resource_type,omitempty" bson:"resource_type,omitempty"`
|
||||
}
|
||||
|
||||
func (abs *PricedResource) GetID() string {
|
||||
@@ -54,89 +35,59 @@ func (abs *PricedResource) GetCreatorID() string {
|
||||
return abs.CreatorID
|
||||
}
|
||||
|
||||
func (abs *PricedResource) IsPurchasable() bool {
|
||||
func (abs *PricedResource) IsPurchased() bool {
|
||||
if abs.SelectedPricing == nil {
|
||||
return false
|
||||
}
|
||||
return (abs.SelectedPricing).IsPurchasable()
|
||||
}
|
||||
|
||||
func (abs *PricedResource) IsBooked() bool {
|
||||
return true // For dev purposes, prevent that DB objects that don't have a Pricing are considered as not booked
|
||||
if abs.SelectedPricing == nil {
|
||||
return false
|
||||
}
|
||||
return (abs.SelectedPricing).IsBooked()
|
||||
return (*abs.SelectedPricing).IsPurchased()
|
||||
}
|
||||
|
||||
func (abs *PricedResource) GetLocationEnd() *time.Time {
|
||||
if abs.BookingConfiguration == nil {
|
||||
return nil
|
||||
}
|
||||
return abs.BookingConfiguration.UsageEnd
|
||||
return abs.UsageEnd
|
||||
}
|
||||
|
||||
func (abs *PricedResource) GetLocationStart() *time.Time {
|
||||
if abs.BookingConfiguration == nil {
|
||||
return nil
|
||||
}
|
||||
return abs.BookingConfiguration.UsageStart
|
||||
return abs.UsageStart
|
||||
}
|
||||
|
||||
func (abs *PricedResource) SetLocationStart(start time.Time) {
|
||||
if abs.BookingConfiguration == nil {
|
||||
abs.BookingConfiguration = &BookingConfiguration{}
|
||||
}
|
||||
abs.BookingConfiguration.UsageStart = &start
|
||||
abs.UsageStart = &start
|
||||
}
|
||||
|
||||
func (abs *PricedResource) SetLocationEnd(end time.Time) {
|
||||
if abs.BookingConfiguration == nil {
|
||||
abs.BookingConfiguration = &BookingConfiguration{}
|
||||
}
|
||||
abs.BookingConfiguration.UsageEnd = &end
|
||||
}
|
||||
|
||||
func (abs *PricedResource) GetBookingMode() booking.BookingMode {
|
||||
if abs.BookingConfiguration == nil {
|
||||
return booking.WHEN_POSSIBLE
|
||||
}
|
||||
return abs.BookingConfiguration.Mode
|
||||
abs.UsageEnd = &end
|
||||
}
|
||||
|
||||
func (abs *PricedResource) GetExplicitDurationInS() float64 {
|
||||
if abs.BookingConfiguration == nil {
|
||||
abs.BookingConfiguration = &BookingConfiguration{}
|
||||
}
|
||||
if abs.BookingConfiguration.ExplicitBookingDurationS == 0 {
|
||||
if abs.BookingConfiguration.UsageEnd == nil && abs.BookingConfiguration.UsageStart == nil {
|
||||
if abs.ExplicitBookingDurationS == 0 {
|
||||
if abs.UsageEnd == nil && abs.UsageStart == nil {
|
||||
return time.Duration(1 * time.Hour).Seconds()
|
||||
}
|
||||
if abs.BookingConfiguration.UsageEnd == nil {
|
||||
add := abs.BookingConfiguration.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
abs.BookingConfiguration.UsageEnd = &add
|
||||
if abs.UsageEnd == nil {
|
||||
add := abs.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
abs.UsageEnd = &add
|
||||
}
|
||||
return abs.BookingConfiguration.UsageEnd.Sub(*abs.BookingConfiguration.UsageStart).Seconds()
|
||||
return abs.UsageEnd.Sub(*abs.UsageStart).Seconds()
|
||||
}
|
||||
return abs.BookingConfiguration.ExplicitBookingDurationS
|
||||
return abs.ExplicitBookingDurationS
|
||||
}
|
||||
|
||||
func (r *PricedResource) GetPriceHT() (float64, error) {
|
||||
func (r *PricedResource) GetPrice() (float64, error) {
|
||||
fmt.Println("GetPrice", r.UsageStart, r.UsageEnd)
|
||||
now := time.Now()
|
||||
if r.BookingConfiguration == nil {
|
||||
r.BookingConfiguration = &BookingConfiguration{}
|
||||
if r.UsageStart == nil {
|
||||
r.UsageStart = &now
|
||||
}
|
||||
fmt.Println("GetPriceHT", r.BookingConfiguration.UsageStart, r.BookingConfiguration.UsageEnd)
|
||||
if r.BookingConfiguration.UsageStart == nil {
|
||||
r.BookingConfiguration.UsageStart = &now
|
||||
}
|
||||
if r.BookingConfiguration.UsageEnd == nil {
|
||||
add := r.BookingConfiguration.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
r.BookingConfiguration.UsageEnd = &add
|
||||
if r.UsageEnd == nil {
|
||||
add := r.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
r.UsageEnd = &add
|
||||
}
|
||||
if r.SelectedPricing == nil {
|
||||
return 0, errors.New("pricing profile must be set on Priced Resource " + r.ResourceID)
|
||||
if len(r.PricingProfiles) == 0 {
|
||||
return 0, errors.New("pricing profile must be set on Priced Resource " + r.ResourceID)
|
||||
}
|
||||
r.SelectedPricing = &r.PricingProfiles[0]
|
||||
}
|
||||
pricing := r.SelectedPricing
|
||||
return pricing.GetPriceHT(1, 0, *r.BookingConfiguration.UsageStart, *r.BookingConfiguration.UsageEnd, r.Variations)
|
||||
pricing := *r.SelectedPricing
|
||||
return pricing.GetPrice(1, 0, *r.UsageStart, *r.UsageEnd)
|
||||
}
|
||||
|
||||
34
models/resources/processing.go
Executable file → Normal file
34
models/resources/processing.go
Executable file → Normal file
@@ -8,7 +8,6 @@ import (
|
||||
"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"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ProcessingUsage struct {
|
||||
@@ -26,8 +25,7 @@ type ProcessingUsage struct {
|
||||
* it defines the resource processing
|
||||
*/
|
||||
type ProcessingResource struct {
|
||||
AbstractInstanciatedResource[*ProcessingInstance]
|
||||
IsEvent bool `json:"is_event,omitempty" bson:"is_event,omitempty"`
|
||||
AbstractIntanciatedResource[*ProcessingInstance]
|
||||
Infrastructure enum.InfrastructureType `json:"infrastructure" bson:"infrastructure" default:"-1"` // Infrastructure is the infrastructure
|
||||
IsService bool `json:"is_service,omitempty" bson:"is_service,omitempty"` // IsService is a flag that indicates if the processing is a service
|
||||
Usage *ProcessingUsage `bson:"usage,omitempty" json:"usage,omitempty"` // Usage is the usage of the processing
|
||||
@@ -49,17 +47,6 @@ type ProcessingInstance struct {
|
||||
Access *ProcessingResourceAccess `json:"access,omitempty" bson:"access,omitempty"` // Access is the access
|
||||
}
|
||||
|
||||
func NewProcessingInstance(name string, peerID string) ResourceInstanceITF {
|
||||
return &ProcessingInstance{
|
||||
ResourceInstance: ResourceInstance[*ResourcePartnerShip[*ProcessingResourcePricingProfile]]{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
UUID: uuid.New().String(),
|
||||
Name: name,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type PricedProcessingResource struct {
|
||||
PricedResource
|
||||
IsService bool
|
||||
@@ -70,19 +57,16 @@ func (r *PricedProcessingResource) GetType() tools.DataType {
|
||||
}
|
||||
|
||||
func (a *PricedProcessingResource) GetExplicitDurationInS() float64 {
|
||||
if a.BookingConfiguration == nil {
|
||||
a.BookingConfiguration = &BookingConfiguration{}
|
||||
}
|
||||
if a.BookingConfiguration.ExplicitBookingDurationS == 0 {
|
||||
if a.IsService || a.BookingConfiguration.UsageStart == nil {
|
||||
if a.ExplicitBookingDurationS == 0 {
|
||||
if a.IsService || a.UsageStart == nil {
|
||||
if a.IsService {
|
||||
return -1
|
||||
}
|
||||
return time.Duration(1 * time.Hour).Seconds()
|
||||
}
|
||||
return a.BookingConfiguration.UsageEnd.Sub(*a.BookingConfiguration.UsageStart).Seconds()
|
||||
return a.UsageEnd.Sub(*a.UsageStart).Seconds()
|
||||
}
|
||||
return a.BookingConfiguration.ExplicitBookingDurationS
|
||||
return a.ExplicitBookingDurationS
|
||||
}
|
||||
|
||||
func (d *ProcessingResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
@@ -93,10 +77,10 @@ type ProcessingResourcePricingProfile struct {
|
||||
pricing.AccessPricingProfile[pricing.TimePricingStrategy] // AccessPricingProfile is the pricing profile of a data it means that we can access the data for an amount of time
|
||||
}
|
||||
|
||||
func (p *ProcessingResourcePricingProfile) IsPurchasable() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.UNDEFINED_SUBSCRIPTION
|
||||
func (p *ProcessingResourcePricingProfile) IsPurchased() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
|
||||
}
|
||||
|
||||
func (p *ProcessingResourcePricingProfile) IsBooked() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.PERMANENT
|
||||
func (p *ProcessingResourcePricingProfile) GetPrice(amountOfData float64, val float64, start time.Time, end time.Time, params ...string) (float64, error) {
|
||||
return p.Pricing.GetPrice(amountOfData, val, start, &end)
|
||||
}
|
||||
|
||||
@@ -9,12 +9,9 @@ import (
|
||||
|
||||
type PurchaseResource struct {
|
||||
utils.AbstractObject
|
||||
DestPeerID string `json:"dest_peer_id" bson:"dest_peer_id"`
|
||||
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"`
|
||||
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 {
|
||||
|
||||
@@ -9,13 +9,13 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type PurchaseResourceMongoAccessor struct {
|
||||
type purchaseResourceMongoAccessor struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the bookingMongoAccessor
|
||||
func NewAccessor(request *tools.APIRequest) *PurchaseResourceMongoAccessor {
|
||||
return &PurchaseResourceMongoAccessor{
|
||||
func NewAccessor(request *tools.APIRequest) *purchaseResourceMongoAccessor {
|
||||
return &purchaseResourceMongoAccessor{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.PURCHASE_RESOURCE.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
@@ -27,23 +27,23 @@ func NewAccessor(request *tools.APIRequest) *PurchaseResourceMongoAccessor {
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *PurchaseResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
func (a *purchaseResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *PurchaseResourceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
func (a *purchaseResourceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericUpdateOne(set, id, a, &PurchaseResource{})
|
||||
}
|
||||
|
||||
func (a *PurchaseResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
func (a *purchaseResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
func (a *PurchaseResourceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
func (a *purchaseResourceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
func (a *purchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*PurchaseResource](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||
utils.GenericDeleteOne(id, a)
|
||||
@@ -53,15 +53,15 @@ func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int,
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *PurchaseResourceMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
func (a *purchaseResourceMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*PurchaseResource](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *PurchaseResourceMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
func (a *purchaseResourceMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*PurchaseResource](filters, search, (&PurchaseResource{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *PurchaseResourceMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
func (a *purchaseResourceMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
|
||||
utils.GenericDeleteOne(d.GetID(), a)
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
package purchase_resource_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestGetAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
res := &purchase_resource.PurchaseResource{}
|
||||
accessor := res.GetAccessor(req)
|
||||
|
||||
assert.NotNil(t, accessor)
|
||||
assert.Equal(t, tools.PURCHASE_RESOURCE, accessor.(*purchase_resource.PurchaseResourceMongoAccessor).Type)
|
||||
}
|
||||
|
||||
func TestCanUpdate(t *testing.T) {
|
||||
set := &purchase_resource.PurchaseResource{ResourceID: "id"}
|
||||
r := &purchase_resource.PurchaseResource{
|
||||
AbstractObject: utils.AbstractObject{IsDraft: true},
|
||||
}
|
||||
can, updated := r.CanUpdate(set)
|
||||
assert.True(t, can)
|
||||
assert.Equal(t, set, updated)
|
||||
|
||||
r.IsDraft = false
|
||||
can, _ = r.CanUpdate(set)
|
||||
assert.False(t, can)
|
||||
}
|
||||
|
||||
func TestCanDelete(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
past := now.Add(-1 * time.Hour)
|
||||
future := now.Add(1 * time.Hour)
|
||||
|
||||
t.Run("nil EndDate", func(t *testing.T) {
|
||||
r := &purchase_resource.PurchaseResource{}
|
||||
assert.False(t, r.CanDelete())
|
||||
})
|
||||
|
||||
t.Run("EndDate in past", func(t *testing.T) {
|
||||
r := &purchase_resource.PurchaseResource{EndDate: &past}
|
||||
assert.True(t, r.CanDelete())
|
||||
})
|
||||
|
||||
t.Run("EndDate in future", func(t *testing.T) {
|
||||
r := &purchase_resource.PurchaseResource{EndDate: &future}
|
||||
assert.False(t, r.CanDelete())
|
||||
})
|
||||
}
|
||||
199
models/resources/resource.go
Executable file → Normal file
199
models/resources/resource.go
Executable file → Normal file
@@ -1,46 +1,30 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"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/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/biter777/countries"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// AbstractResource is the struct containing all of the attributes commons to all ressources
|
||||
type AbstractResource struct {
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type of the resource
|
||||
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"` // Logo is the logo of the resource
|
||||
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
|
||||
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"` // ShortDescription is the short description of the resource
|
||||
Owners []utils.Owner `json:"owners,omitempty" bson:"owners,omitempty"` // Owners is the list of owners of the resource
|
||||
UsageRestrictions string `bson:"usage_restrictions,omitempty" json:"usage_restrictions,omitempty"`
|
||||
AllowedBookingModes map[booking.BookingMode]*pricing.PricingVariation `bson:"allowed_booking_modes" json:"allowed_booking_modes"`
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type of the resource
|
||||
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"` // Logo is the logo of the resource
|
||||
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
|
||||
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"` // ShortDescription is the short description of the resource
|
||||
Owners []utils.Owner `json:"owners,omitempty" bson:"owners,omitempty"` // Owners is the list of owners of the resource
|
||||
UsageRestrictions string `bson:"usage_restrictions,omitempty" json:"usage_restrictions,omitempty"`
|
||||
SelectedInstanceIndex *int `json:"selected_instance_index,omitempty" bson:"selected_instance_index,omitempty"` // SelectedInstance is the selected instance
|
||||
}
|
||||
|
||||
func (r *AbstractResource) GetBookingModes() map[booking.BookingMode]*pricing.PricingVariation {
|
||||
if len(r.AllowedBookingModes) == 0 {
|
||||
return map[booking.BookingMode]*pricing.PricingVariation{
|
||||
booking.PLANNED: {
|
||||
Percentage: 0,
|
||||
}, booking.WHEN_POSSIBLE: {
|
||||
Percentage: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
return r.AllowedBookingModes
|
||||
}
|
||||
|
||||
func (r *AbstractResource) GetSelectedInstance(selected *int) ResourceInstanceITF {
|
||||
func (r *AbstractResource) GetSelectedInstance() utils.DBObject {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -63,64 +47,33 @@ func (r *AbstractResource) CanDelete() bool {
|
||||
return r.IsDraft // only draft bookings can be deleted
|
||||
}
|
||||
|
||||
type AbstractInstanciatedResource[T ResourceInstanceITF] struct {
|
||||
type AbstractIntanciatedResource[T ResourceInstanceITF] struct {
|
||||
AbstractResource // AbstractResource contains the basic fields of an object (id, name)
|
||||
Instances []T `json:"instances,omitempty" bson:"instances,omitempty"` // Bill is the bill of the resource // Bill is the bill of the resource
|
||||
}
|
||||
|
||||
func (abs *AbstractInstanciatedResource[T]) AddInstances(instance ResourceInstanceITF) {
|
||||
abs.Instances = append(abs.Instances, instance.(T))
|
||||
}
|
||||
|
||||
func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||
func (abs *AbstractIntanciatedResource[T]) ConvertToPricedResource(
|
||||
t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||
instances := map[string]string{}
|
||||
profiles := []pricing.PricingProfileITF{}
|
||||
for _, instance := range abs.Instances { // TODO why it crush before ?
|
||||
for _, instance := range abs.Instances {
|
||||
instances[instance.GetID()] = instance.GetName()
|
||||
profiles = instance.GetPricingsProfiles(request.PeerID, request.Groups)
|
||||
}
|
||||
var profile pricing.PricingProfileITF
|
||||
if t := abs.GetSelectedInstance(selectedInstance); t != nil {
|
||||
profile = t.GetProfile(request.PeerID, selectedPartnership, selectedBuyingStrategy, selectedStrategy)
|
||||
}
|
||||
if profile == nil {
|
||||
if len(profiles) > 0 {
|
||||
profile = profiles[0]
|
||||
} else {
|
||||
if ok, _ := peer.IsMySelf(request.PeerID); ok {
|
||||
profile = pricing.GetDefaultPricingProfile()
|
||||
} else {
|
||||
return nil, errors.New("no pricing profile found")
|
||||
}
|
||||
}
|
||||
}
|
||||
variations := []*pricing.PricingVariation{}
|
||||
if selectedBookingModeIndex != nil && abs.AllowedBookingModes[booking.BookingMode(*selectedBookingModeIndex)] != nil {
|
||||
variations = append(variations, abs.AllowedBookingModes[booking.BookingMode(*selectedBookingModeIndex)])
|
||||
}
|
||||
return &PricedResource{
|
||||
Name: abs.Name,
|
||||
Logo: abs.Logo,
|
||||
ResourceID: abs.UUID,
|
||||
ResourceType: t,
|
||||
Quantity: 1,
|
||||
InstancesRefs: instances,
|
||||
SelectedPricing: profile,
|
||||
Variations: variations,
|
||||
PricingProfiles: profiles,
|
||||
CreatorID: abs.CreatorID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (abs *AbstractInstanciatedResource[T]) ClearEnv() utils.DBObject {
|
||||
for _, instance := range abs.Instances {
|
||||
instance.ClearEnv()
|
||||
}
|
||||
return abs
|
||||
}
|
||||
|
||||
func (r *AbstractInstanciatedResource[T]) GetSelectedInstance(selected *int) ResourceInstanceITF {
|
||||
if selected != nil && len(r.Instances) > *selected {
|
||||
return r.Instances[*selected]
|
||||
func (r *AbstractIntanciatedResource[T]) GetSelectedInstance() utils.DBObject {
|
||||
if r.SelectedInstanceIndex != nil && len(r.Instances) > *r.SelectedInstanceIndex {
|
||||
return r.Instances[*r.SelectedInstanceIndex]
|
||||
}
|
||||
if len(r.Instances) > 0 {
|
||||
return r.Instances[0]
|
||||
@@ -128,14 +81,11 @@ func (r *AbstractInstanciatedResource[T]) GetSelectedInstance(selected *int) Res
|
||||
return nil
|
||||
}
|
||||
|
||||
func (abs *AbstractInstanciatedResource[T]) SetAllowedInstances(request *tools.APIRequest) {
|
||||
if request != nil && request.PeerID == abs.CreatorID && request.PeerID != "" {
|
||||
return
|
||||
}
|
||||
abs.Instances = VerifyAuthAction[T](abs.Instances, request)
|
||||
func (abs *AbstractIntanciatedResource[T]) SetAllowedInstances(request *tools.APIRequest) {
|
||||
abs.Instances = verifyAuthAction[T](abs.Instances, request)
|
||||
}
|
||||
|
||||
func (d *AbstractInstanciatedResource[T]) Trim() {
|
||||
func (d *AbstractIntanciatedResource[T]) Trim() {
|
||||
d.Type = d.GetType()
|
||||
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: d.CreatorID}}).IsMySelf(); !ok {
|
||||
for _, instance := range d.Instances {
|
||||
@@ -144,11 +94,11 @@ func (d *AbstractInstanciatedResource[T]) Trim() {
|
||||
}
|
||||
}
|
||||
|
||||
func (abs *AbstractInstanciatedResource[T]) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
return len(VerifyAuthAction[T](abs.Instances, request)) > 0 || abs.AbstractObject.VerifyAuth(callName, request)
|
||||
func (abs *AbstractIntanciatedResource[T]) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return len(verifyAuthAction[T](abs.Instances, request)) > 0 || abs.AbstractObject.VerifyAuth(request)
|
||||
}
|
||||
|
||||
func VerifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest) []T {
|
||||
func verifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest) []T {
|
||||
instances := []T{}
|
||||
for _, instance := range baseInstance {
|
||||
_, peerGroups := instance.GetPeerGroups()
|
||||
@@ -185,41 +135,7 @@ type ResourceInstance[T ResourcePartnerITF] struct {
|
||||
Env []models.Param `json:"env,omitempty" bson:"env,omitempty"`
|
||||
Inputs []models.Param `json:"inputs,omitempty" bson:"inputs,omitempty"`
|
||||
Outputs []models.Param `json:"outputs,omitempty" bson:"outputs,omitempty"`
|
||||
|
||||
// SelectedPartnershipIndex int `json:"selected_partnership_index,omitempty" bson:"selected_partnership_index,omitempty"`
|
||||
// SelectedBuyingStrategy int `json:"selected_buying_strategy,omitempty" bson:"selected_buying_strategy,omitempty"`
|
||||
// SelectedStrategy int `json:"selected_strategy,omitempty" bson:"selected_strategy,omitempty"`
|
||||
|
||||
Partnerships []T `json:"partnerships,omitempty" bson:"partnerships,omitempty"`
|
||||
}
|
||||
|
||||
// TODO should kicks all selection
|
||||
|
||||
func NewInstance[T ResourcePartnerITF](name string) *ResourceInstance[T] {
|
||||
return &ResourceInstance[T]{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
UUID: uuid.New().String(),
|
||||
Name: name,
|
||||
},
|
||||
Partnerships: []T{},
|
||||
}
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) ClearEnv() {
|
||||
ri.Env = []models.Param{}
|
||||
ri.Inputs = []models.Param{}
|
||||
ri.Outputs = []models.Param{}
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) GetProfile(peerID string, partnershipIndex *int, buyingIndex *int, strategyIndex *int) pricing.PricingProfileITF {
|
||||
if partnershipIndex != nil && len(ri.Partnerships) > *partnershipIndex {
|
||||
prts := ri.Partnerships[*partnershipIndex]
|
||||
return prts.GetProfile(buyingIndex, strategyIndex)
|
||||
}
|
||||
if ok, _ := peer.IsMySelf(peerID); ok {
|
||||
return pricing.GetDefaultPricingProfile()
|
||||
}
|
||||
return nil
|
||||
Partnerships []T `json:"partnerships,omitempty" bson:"partnerships,omitempty"`
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF {
|
||||
@@ -227,11 +143,6 @@ func (ri *ResourceInstance[T]) GetPricingsProfiles(peerID string, groups []strin
|
||||
for _, p := range ri.Partnerships {
|
||||
pricings = append(pricings, p.GetPricingsProfiles(peerID, groups)...)
|
||||
}
|
||||
if len(pricings) == 0 {
|
||||
if ok, _ := peer.IsMySelf(peerID); ok {
|
||||
pricings = append(pricings, pricing.GetDefaultPricingProfile())
|
||||
}
|
||||
}
|
||||
return pricings
|
||||
}
|
||||
|
||||
@@ -242,15 +153,6 @@ func (ri *ResourceInstance[T]) GetPeerGroups() ([]ResourcePartnerITF, []map[stri
|
||||
partners = append(partners, p)
|
||||
groups = append(groups, p.GetPeerGroups())
|
||||
}
|
||||
if len(groups) == 0 {
|
||||
_, id := peer.GetSelf()
|
||||
groups = []map[string][]string{
|
||||
{
|
||||
id: {"*"},
|
||||
},
|
||||
}
|
||||
// TODO make allow all only for self.
|
||||
}
|
||||
return partners, groups
|
||||
}
|
||||
|
||||
@@ -263,59 +165,34 @@ func (ri *ResourceInstance[T]) ClearPeerGroups() {
|
||||
type ResourcePartnerShip[T pricing.PricingProfileITF] struct {
|
||||
Namespace string `json:"namespace" bson:"namespace" default:"default-namespace"`
|
||||
PeerGroups map[string][]string `json:"peer_groups,omitempty" bson:"peer_groups,omitempty"`
|
||||
PricingProfiles map[int]map[int]T `json:"pricing_profiles,omitempty" bson:"pricing_profiles,omitempty"`
|
||||
// to upgrade pricing profiles. to be a map BuyingStrategy, map of Strategy
|
||||
PricingProfiles []T `json:"pricing_profiles,omitempty" bson:"pricing_profiles,omitempty"`
|
||||
}
|
||||
|
||||
func (ri *ResourcePartnerShip[T]) GetProfile(buying *int, strategy *int) pricing.PricingProfileITF {
|
||||
if buying != nil && strategy != nil {
|
||||
if strat, ok := ri.PricingProfiles[*buying]; ok {
|
||||
if profile, ok := strat[*strategy]; ok {
|
||||
return profile
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
Le pricing doit être selectionné lors d'un scheduling...
|
||||
le type de paiement défini le type de stratégie de paiement
|
||||
note : il faut rajouté - une notion de facturation
|
||||
Une order est l'ensemble de la commande... un booking une réservation, une purchase un acte d'achat.
|
||||
Une bill (facture) représente alors... l'emission d'une facture à un instant T en but d'être honorée, envoyée ... etc.
|
||||
*/
|
||||
func (ri *ResourcePartnerShip[T]) GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF {
|
||||
profiles := []pricing.PricingProfileITF{}
|
||||
if ri.PeerGroups[peerID] == nil {
|
||||
return profiles
|
||||
}
|
||||
for _, p := range ri.PeerGroups[peerID] {
|
||||
if slices.Contains(groups, p) || slices.Contains(groups, "*") {
|
||||
if ri.PeerGroups[peerID] != nil {
|
||||
for _, ri := range ri.PricingProfiles {
|
||||
profiles = append(profiles, ri)
|
||||
}
|
||||
if slices.Contains(groups, "*") {
|
||||
for _, ri := range ri.PricingProfiles {
|
||||
for _, i := range ri {
|
||||
profiles = append(profiles, i)
|
||||
}
|
||||
profiles = append(profiles, ri)
|
||||
}
|
||||
return profiles
|
||||
}
|
||||
}
|
||||
if len(profiles) == 0 {
|
||||
if ok, _ := peer.IsMySelf(peerID); ok {
|
||||
profiles = append(profiles, pricing.GetDefaultPricingProfile())
|
||||
for _, p := range ri.PeerGroups[peerID] {
|
||||
if slices.Contains(groups, p) {
|
||||
for _, ri := range ri.PricingProfiles {
|
||||
profiles = append(profiles, ri)
|
||||
}
|
||||
return profiles
|
||||
}
|
||||
}
|
||||
}
|
||||
return profiles
|
||||
}
|
||||
|
||||
func (rp *ResourcePartnerShip[T]) GetPeerGroups() map[string][]string {
|
||||
if len(rp.PeerGroups) == 0 {
|
||||
_, id := peer.GetSelf()
|
||||
return map[string][]string{
|
||||
id: {"*"},
|
||||
}
|
||||
}
|
||||
return rp.PeerGroups
|
||||
}
|
||||
|
||||
|
||||
47
models/resources/resource_accessor.go
Executable file → Normal file
47
models/resources/resource_accessor.go
Executable file → Normal file
@@ -1,7 +1,6 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
@@ -10,21 +9,17 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type ResourceMongoAccessor[T ResourceInterface] struct {
|
||||
type resourceMongoAccessor[T ResourceInterface] struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
generateData func() utils.DBObject
|
||||
}
|
||||
|
||||
// New creates a new instance of the computeMongoAccessor
|
||||
func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIRequest, g func() utils.DBObject) *ResourceMongoAccessor[T] {
|
||||
if !slices.Contains([]tools.DataType{
|
||||
tools.COMPUTE_RESOURCE, tools.STORAGE_RESOURCE,
|
||||
tools.PROCESSING_RESOURCE, tools.WORKFLOW_RESOURCE,
|
||||
tools.DATA_RESOURCE, tools.NATIVE_TOOL,
|
||||
}, t) {
|
||||
func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIRequest, g func() utils.DBObject) *resourceMongoAccessor[T] {
|
||||
if !slices.Contains([]tools.DataType{tools.COMPUTE_RESOURCE, tools.STORAGE_RESOURCE, tools.PROCESSING_RESOURCE, tools.WORKFLOW_RESOURCE, tools.DATA_RESOURCE}, t) {
|
||||
return nil
|
||||
}
|
||||
return &ResourceMongoAccessor[T]{
|
||||
return &resourceMongoAccessor[T]{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
@@ -37,54 +32,39 @@ func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIReques
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (dca *ResourceMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
func (dca *resourceMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, dca)
|
||||
}
|
||||
|
||||
func (dca *ResourceMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
||||
return nil, 404, errors.New("can't update a non existing computing units resource not reported onto compute units catalog")
|
||||
}
|
||||
func (dca *resourceMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
set.(T).Trim()
|
||||
return utils.GenericUpdateOne(set, id, dca, dca.generateData())
|
||||
}
|
||||
|
||||
func (dca *ResourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
||||
return nil, 404, errors.New("can't create a non existing computing units resource not reported onto compute units catalog")
|
||||
}
|
||||
func (dca *resourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
data.(T).Trim()
|
||||
return utils.GenericStoreOne(data, dca)
|
||||
}
|
||||
|
||||
func (dca *ResourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
||||
return nil, 404, errors.New("can't copy/publish a non existing computing units resource not reported onto compute units catalog")
|
||||
}
|
||||
func (dca *resourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return dca.StoreOne(data)
|
||||
}
|
||||
|
||||
func (dca *ResourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
func (dca *resourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
d.(T).SetAllowedInstances(dca.Request)
|
||||
return d, 200, nil
|
||||
}, dca)
|
||||
}
|
||||
|
||||
func (wfa *ResourceMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
func (wfa *resourceMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
||||
d.(T).SetAllowedInstances(wfa.Request)
|
||||
return d
|
||||
}, isDraft, wfa)
|
||||
}
|
||||
|
||||
func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
if filters == nil && search == "*" {
|
||||
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
||||
d.(T).SetAllowedInstances(wfa.Request)
|
||||
return d
|
||||
}, isDraft, wfa)
|
||||
}
|
||||
func (wfa *resourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[T](filters, search, wfa.getResourceFilter(search),
|
||||
func(d utils.DBObject) utils.ShallowDBObject {
|
||||
d.(T).SetAllowedInstances(wfa.Request)
|
||||
@@ -92,7 +72,10 @@ func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string,
|
||||
}, isDraft, wfa)
|
||||
}
|
||||
|
||||
func (abs *ResourceMongoAccessor[T]) getResourceFilter(search string) *dbs.Filters {
|
||||
func (abs *resourceMongoAccessor[T]) getResourceFilter(search string) *dbs.Filters {
|
||||
if search == "*" {
|
||||
search = ""
|
||||
}
|
||||
return &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
|
||||
"abstractintanciatedresource.abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||
|
||||
85
models/resources/storage.go
Executable file → Normal file
85
models/resources/storage.go
Executable file → Normal file
@@ -10,7 +10,6 @@ import (
|
||||
"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"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -18,9 +17,9 @@ import (
|
||||
* it defines the resource storage
|
||||
*/
|
||||
type StorageResource struct {
|
||||
AbstractInstanciatedResource[*StorageResourceInstance] // AbstractResource contains the basic fields of an object (id, name)
|
||||
StorageType enum.StorageType `bson:"storage_type" json:"storage_type" default:"-1"` // Type is the type of the storage
|
||||
Acronym string `bson:"acronym,omitempty" json:"acronym,omitempty"` // Acronym is the acronym of the storage
|
||||
AbstractIntanciatedResource[*StorageResourceInstance] // AbstractResource contains the basic fields of an object (id, name)
|
||||
StorageType enum.StorageType `bson:"storage_type" json:"storage_type" default:"-1"` // Type is the type of the storage
|
||||
Acronym string `bson:"acronym,omitempty" json:"acronym,omitempty"` // Acronym is the acronym of the storage
|
||||
}
|
||||
|
||||
func (d *StorageResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
@@ -31,24 +30,21 @@ func (r *StorageResource) GetType() string {
|
||||
return tools.STORAGE_RESOURCE.String()
|
||||
}
|
||||
|
||||
func (abs *StorageResource) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||
func (abs *StorageResource) ConvertToPricedResource(
|
||||
t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||
if t != tools.STORAGE_RESOURCE {
|
||||
return nil, errors.New("not the proper type expected : cannot convert to priced resource : have " + t.String() + " wait Storage")
|
||||
}
|
||||
p, err := abs.AbstractInstanciatedResource.ConvertToPricedResource(t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil
|
||||
}
|
||||
p := abs.AbstractIntanciatedResource.ConvertToPricedResource(t, request)
|
||||
priced := p.(*PricedResource)
|
||||
return &PricedStorageResource{
|
||||
PricedResource: *priced,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type StorageResourceInstance struct {
|
||||
ResourceInstance[*StorageResourcePartnership]
|
||||
Source string `bson:"source,omitempty" json:"source,omitempty"` // Source is the source of the storage
|
||||
Path string `bson:"path,omitempty" json:"path,omitempty"` // Path is the store folders in the source
|
||||
Local bool `bson:"local" json:"local"`
|
||||
SecurityLevel string `bson:"security_level,omitempty" json:"security_level,omitempty"`
|
||||
SizeType enum.StorageSize `bson:"size_type" json:"size_type" default:"0"` // SizeType is the type of the storage size
|
||||
@@ -58,23 +54,6 @@ type StorageResourceInstance struct {
|
||||
Throughput string `bson:"throughput,omitempty" json:"throughput,omitempty"` // Throughput is the throughput of the storage
|
||||
}
|
||||
|
||||
func NewStorageResourceInstance(name string, peerID string) ResourceInstanceITF {
|
||||
return &StorageResourceInstance{
|
||||
ResourceInstance: ResourceInstance[*StorageResourcePartnership]{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
UUID: uuid.New().String(),
|
||||
Name: name,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (ri *StorageResourceInstance) ClearEnv() {
|
||||
ri.Env = []models.Param{}
|
||||
ri.Inputs = []models.Param{}
|
||||
ri.Outputs = []models.Param{}
|
||||
}
|
||||
|
||||
func (ri *StorageResourceInstance) StoreDraftDefault() {
|
||||
found := false
|
||||
for _, p := range ri.ResourceInstance.Env {
|
||||
@@ -118,7 +97,7 @@ func (t PrivilegeStoragePricingStrategy) String() string {
|
||||
type StorageResourcePricingStrategy int
|
||||
|
||||
const (
|
||||
PER_DATA_STORED StorageResourcePricingStrategy = iota + 6
|
||||
PER_DATA_STORED StorageResourcePricingStrategy = iota
|
||||
PER_TB_STORED
|
||||
PER_GB_STORED
|
||||
PER_MB_STORED
|
||||
@@ -130,15 +109,11 @@ func StorageResourcePricingStrategyList() []StorageResourcePricingStrategy {
|
||||
}
|
||||
|
||||
func (t StorageResourcePricingStrategy) String() string {
|
||||
l := pricing.TimePricingStrategyListStr()
|
||||
l = append(l, []string{"PER DATA STORED", "PER TB STORED", "PER GB STORED", "PER MB STORED", "PER KB STORED"}...)
|
||||
return l[t]
|
||||
return [...]string{"PER DATA STORED", "PER TB STORED", "PER GB STORED", "PER MB STORED", "PER KB STORED"}[t]
|
||||
}
|
||||
|
||||
func (t StorageResourcePricingStrategy) GetStrategy() string {
|
||||
l := pricing.TimePricingStrategyListStr()
|
||||
l = append(l, []string{"PER DATA STORED", "PER TB STORED", "PER GB STORED", "PER MB STORED", "PER KB STORED"}...)
|
||||
return l[t]
|
||||
return [...]string{"PER_DATA_STORED", "PER_GB_STORED", "PER_MB_STORED", "PER_KB_STORED"}[t]
|
||||
}
|
||||
|
||||
func (t StorageResourcePricingStrategy) GetStrategyValue() int {
|
||||
@@ -169,15 +144,12 @@ type StorageResourcePricingProfile struct {
|
||||
pricing.ExploitPricingProfile[StorageResourcePricingStrategy] // ExploitPricingProfile is the pricing profile of a storage it means that we exploit the resource for an amount of continuous time
|
||||
}
|
||||
|
||||
func (p *StorageResourcePricingProfile) IsPurchasable() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.UNDEFINED_SUBSCRIPTION
|
||||
func (p *StorageResourcePricingProfile) IsPurchased() bool {
|
||||
return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
|
||||
}
|
||||
|
||||
func (p *StorageResourcePricingProfile) IsBooked() bool {
|
||||
if p.Pricing.BuyingStrategy == pricing.PERMANENT {
|
||||
p.Pricing.BuyingStrategy = pricing.SUBSCRIPTION
|
||||
}
|
||||
return true
|
||||
func (p *StorageResourcePricingProfile) GetPrice(amountOfData float64, val float64, start time.Time, end time.Time, params ...string) (float64, error) {
|
||||
return p.Pricing.GetPrice(amountOfData, val, start, &end)
|
||||
}
|
||||
|
||||
type PricedStorageResource struct {
|
||||
@@ -189,23 +161,23 @@ func (r *PricedStorageResource) GetType() tools.DataType {
|
||||
return tools.STORAGE_RESOURCE
|
||||
}
|
||||
|
||||
func (r *PricedStorageResource) GetPriceHT() (float64, error) {
|
||||
if r.BookingConfiguration == nil {
|
||||
r.BookingConfiguration = &BookingConfiguration{}
|
||||
}
|
||||
fmt.Println("GetPriceHT", r.BookingConfiguration.UsageStart, r.BookingConfiguration.UsageEnd)
|
||||
func (r *PricedStorageResource) GetPrice() (float64, error) {
|
||||
fmt.Println("GetPrice", r.UsageStart, r.UsageEnd)
|
||||
now := time.Now()
|
||||
if r.BookingConfiguration.UsageStart == nil {
|
||||
r.BookingConfiguration.UsageStart = &now
|
||||
if r.UsageStart == nil {
|
||||
r.UsageStart = &now
|
||||
}
|
||||
if r.BookingConfiguration.UsageEnd == nil {
|
||||
add := r.BookingConfiguration.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
r.BookingConfiguration.UsageEnd = &add
|
||||
if r.UsageEnd == nil {
|
||||
add := r.UsageStart.Add(time.Duration(1 * time.Hour))
|
||||
r.UsageEnd = &add
|
||||
}
|
||||
if r.SelectedPricing == nil {
|
||||
return 0, errors.New("pricing profile must be set on Priced Storage" + r.ResourceID)
|
||||
if len(r.PricingProfiles) == 0 {
|
||||
return 0, errors.New("pricing profile must be set on Priced Storage" + r.ResourceID)
|
||||
}
|
||||
r.SelectedPricing = &r.PricingProfiles[0]
|
||||
}
|
||||
pricing := r.SelectedPricing
|
||||
pricing := *r.SelectedPricing
|
||||
var err error
|
||||
amountOfData := float64(1)
|
||||
if pricing.GetOverrideStrategyValue() >= 0 {
|
||||
@@ -214,6 +186,5 @@ func (r *PricedStorageResource) GetPriceHT() (float64, error) {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return pricing.GetPriceHT(amountOfData, r.BookingConfiguration.ExplicitBookingDurationS,
|
||||
*r.BookingConfiguration.UsageStart, *r.BookingConfiguration.UsageEnd, r.Variations)
|
||||
return pricing.GetPrice(amountOfData, r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd)
|
||||
}
|
||||
|
||||
@@ -1,110 +0,0 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestComputeResource_GetType(t *testing.T) {
|
||||
r := &resources.ComputeResource{}
|
||||
assert.Equal(t, tools.COMPUTE_RESOURCE.String(), r.GetType())
|
||||
}
|
||||
|
||||
func TestComputeResource_GetAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
cr := &resources.ComputeResource{}
|
||||
accessor := cr.GetAccessor(req)
|
||||
assert.NotNil(t, accessor)
|
||||
}
|
||||
|
||||
func TestComputeResource_ConvertToPricedResource(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
cr := &resources.ComputeResource{}
|
||||
cr.UUID = "comp123"
|
||||
cr.AbstractInstanciatedResource.UUID = cr.UUID
|
||||
result, _ := cr.ConvertToPricedResource(tools.COMPUTE_RESOURCE, nil, nil, nil, nil, nil, req)
|
||||
assert.NotNil(t, result)
|
||||
assert.IsType(t, &resources.PricedComputeResource{}, result)
|
||||
}
|
||||
|
||||
func TestComputeResourcePricingProfile_GetPriceHT_CPUs(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
profile := resources.ComputeResourcePricingProfile{
|
||||
CPUsPrices: map[string]float64{"Xeon": 2.0},
|
||||
ExploitPricingProfile: pricing.ExploitPricingProfile[pricing.TimePricingStrategy]{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{Price: 1.0},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
price, err := profile.GetPriceHT(2, 3600, start, end, []*pricing.PricingVariation{}, "cpus", "Xeon")
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, price, float64(0))
|
||||
}
|
||||
|
||||
func TestComputeResourcePricingProfile_GetPriceHT_InvalidParams(t *testing.T) {
|
||||
profile := resources.ComputeResourcePricingProfile{}
|
||||
_, err := profile.GetPriceHT(1, 3600, time.Now(), time.Now(), []*pricing.PricingVariation{})
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "params must be set", err.Error())
|
||||
}
|
||||
|
||||
func TestPricedComputeResource_GetPriceHT(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
r := resources.PricedComputeResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "comp456",
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
ExplicitBookingDurationS: 3600,
|
||||
},
|
||||
},
|
||||
CPUsLocated: map[string]float64{"Xeon": 2},
|
||||
GPUsLocated: map[string]float64{"Tesla": 1},
|
||||
RAMLocated: 4,
|
||||
}
|
||||
|
||||
price, err := r.GetPriceHT()
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, price, float64(0))
|
||||
}
|
||||
|
||||
func TestPricedComputeResource_GetPriceHT_MissingProfile(t *testing.T) {
|
||||
r := resources.PricedComputeResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "comp789",
|
||||
},
|
||||
}
|
||||
_, err := r.GetPriceHT()
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "pricing profile must be set")
|
||||
}
|
||||
|
||||
func TestPricedComputeResource_FillWithDefaultProcessingUsage(t *testing.T) {
|
||||
usage := &resources.ProcessingUsage{
|
||||
CPUs: map[string]*models.CPU{"t": {Model: "Xeon", Cores: 4}},
|
||||
GPUs: map[string]*models.GPU{"t1": {Model: "Tesla"}},
|
||||
RAM: &models.RAM{SizeGb: 16},
|
||||
}
|
||||
r := &resources.PricedComputeResource{
|
||||
CPUsLocated: make(map[string]float64),
|
||||
GPUsLocated: make(map[string]float64),
|
||||
RAMLocated: 0,
|
||||
}
|
||||
r.FillWithDefaultProcessingUsage(usage)
|
||||
assert.Equal(t, float64(4), r.CPUsLocated["Xeon"])
|
||||
assert.Equal(t, float64(1), r.GPUsLocated["Tesla"])
|
||||
assert.Equal(t, float64(16), r.RAMLocated)
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDataResource_GetType(t *testing.T) {
|
||||
d := &resources.DataResource{}
|
||||
assert.Equal(t, tools.DATA_RESOURCE.String(), d.GetType())
|
||||
}
|
||||
|
||||
func TestDataResource_GetAccessor(t *testing.T) {
|
||||
req := &tools.APIRequest{}
|
||||
acc := (&resources.DataResource{}).GetAccessor(req)
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
|
||||
func TestDataResource_ConvertToPricedResource(t *testing.T) {
|
||||
d := &resources.DataResource{}
|
||||
d.UUID = "123"
|
||||
res, _ := d.ConvertToPricedResource(tools.DATA_RESOURCE, nil, nil, nil, nil, nil, &tools.APIRequest{})
|
||||
assert.IsType(t, &resources.PricedDataResource{}, res)
|
||||
|
||||
nilRes, _ := d.ConvertToPricedResource(tools.PROCESSING_RESOURCE, nil, nil, nil, nil, nil, &tools.APIRequest{})
|
||||
assert.Nil(t, nilRes)
|
||||
}
|
||||
|
||||
func TestDataInstance_StoreDraftDefault(t *testing.T) {
|
||||
di := &resources.DataInstance{
|
||||
Source: "test-src",
|
||||
ResourceInstance: resources.ResourceInstance[*resources.DataResourcePartnership]{
|
||||
Env: []models.Param{},
|
||||
},
|
||||
}
|
||||
di.StoreDraftDefault()
|
||||
assert.Len(t, di.ResourceInstance.Env, 1)
|
||||
assert.Equal(t, "source", di.ResourceInstance.Env[0].Attr)
|
||||
assert.Equal(t, "test-src", di.ResourceInstance.Env[0].Value)
|
||||
|
||||
// Call again, should not duplicate
|
||||
di.StoreDraftDefault()
|
||||
assert.Len(t, di.ResourceInstance.Env, 1)
|
||||
}
|
||||
|
||||
func TestDataResourcePricingStrategy_GetQuantity(t *testing.T) {
|
||||
tests := []struct {
|
||||
strategy resources.DataResourcePricingStrategy
|
||||
input float64
|
||||
expected float64
|
||||
}{
|
||||
{resources.PER_DOWNLOAD, 1, 1},
|
||||
{resources.PER_TB_DOWNLOADED, 1, 1000},
|
||||
{resources.PER_GB_DOWNLOADED, 2.5, 2.5},
|
||||
{resources.PER_MB_DOWNLOADED, 1, 0.001},
|
||||
{resources.PER_KB_DOWNLOADED, 1, 0.000001},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
q, err := tt.strategy.GetQuantity(tt.input)
|
||||
require.NoError(t, err)
|
||||
assert.InDelta(t, tt.expected, q, 1e-9)
|
||||
}
|
||||
|
||||
_, err := resources.DataResourcePricingStrategy(999).GetQuantity(1)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDataResourcePricingProfile_IsPurchased(t *testing.T) {
|
||||
profile := &resources.DataResourcePricingProfile{}
|
||||
profile.Pricing.BuyingStrategy = pricing.SUBSCRIPTION
|
||||
assert.True(t, profile.IsPurchasable())
|
||||
}
|
||||
|
||||
func TestPricedDataResource_GetPriceHT(t *testing.T) {
|
||||
now := time.Now()
|
||||
later := now.Add(1 * time.Hour)
|
||||
mockPrice := 42.0
|
||||
|
||||
pricingProfile := &resources.DataResourcePricingProfile{AccessPricingProfile: pricing.AccessPricingProfile[resources.DataResourcePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[resources.DataResourcePricingStrategy]{Price: 42.0}},
|
||||
}
|
||||
pricingProfile.Pricing.OverrideStrategy = resources.PER_GB_DOWNLOADED
|
||||
|
||||
r := &resources.PricedDataResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &now,
|
||||
UsageEnd: &later,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
price, err := r.GetPriceHT()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, mockPrice, price)
|
||||
}
|
||||
|
||||
func TestPricedDataResource_GetPriceHT_NoProfiles(t *testing.T) {
|
||||
r := &resources.PricedDataResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "test-resource",
|
||||
},
|
||||
}
|
||||
_, err := r.GetPriceHT()
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "pricing profile must be set")
|
||||
}
|
||||
|
||||
func TestPricedDataResource_GetType(t *testing.T) {
|
||||
r := &resources.PricedDataResource{}
|
||||
assert.Equal(t, tools.DATA_RESOURCE, r.GetType())
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
// ---- Mock PricingProfile ----
|
||||
|
||||
type MockPricingProfile struct {
|
||||
pricing.PricingProfileITF
|
||||
Purchased bool
|
||||
ReturnErr bool
|
||||
ReturnCost float64
|
||||
}
|
||||
|
||||
func (m *MockPricingProfile) IsPurchasable() bool {
|
||||
return m.Purchased
|
||||
}
|
||||
|
||||
func (m *MockPricingProfile) GetPriceHT(amount float64, explicitDuration float64, start time.Time, end time.Time, variations []*pricing.PricingVariation, _ ...string) (float64, error) {
|
||||
if m.ReturnErr {
|
||||
return 0, errors.New("mock error")
|
||||
}
|
||||
return m.ReturnCost, nil
|
||||
}
|
||||
|
||||
// ---- Tests ----
|
||||
|
||||
func TestGetIDAndCreatorAndType(t *testing.T) {
|
||||
r := resources.PricedResource{
|
||||
ResourceID: "res-123",
|
||||
CreatorID: "user-abc",
|
||||
ResourceType: tools.DATA_RESOURCE,
|
||||
}
|
||||
assert.Equal(t, "res-123", r.GetID())
|
||||
assert.Equal(t, "user-abc", r.GetCreatorID())
|
||||
assert.Equal(t, tools.DATA_RESOURCE, r.GetType())
|
||||
}
|
||||
|
||||
func TestIsPurchased(t *testing.T) {
|
||||
t.Run("nil selected pricing returns false", func(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
assert.False(t, r.IsPurchasable())
|
||||
})
|
||||
|
||||
t.Run("returns true if pricing profile is purchased", func(t *testing.T) {
|
||||
mock := &MockPricingProfile{Purchased: true}
|
||||
r := &resources.PricedResource{SelectedPricing: mock}
|
||||
assert.True(t, r.IsPurchasable())
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAndSetLocationStartEnd(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
|
||||
now := time.Now()
|
||||
r.SetLocationStart(now)
|
||||
r.SetLocationEnd(now.Add(2 * time.Hour))
|
||||
|
||||
assert.Equal(t, now, *r.GetLocationStart())
|
||||
assert.Equal(t, now.Add(2*time.Hour), *r.GetLocationEnd())
|
||||
}
|
||||
|
||||
func TestGetExplicitDurationInS(t *testing.T) {
|
||||
t.Run("uses explicit duration if set", func(t *testing.T) {
|
||||
r := &resources.PricedResource{BookingConfiguration: &resources.BookingConfiguration{
|
||||
ExplicitBookingDurationS: 3600,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, 3600.0, r.GetExplicitDurationInS())
|
||||
})
|
||||
|
||||
t.Run("computes duration from start and end", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(2 * time.Hour)
|
||||
r := &resources.PricedResource{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start, UsageEnd: &end,
|
||||
},
|
||||
}
|
||||
assert.InDelta(t, 7200.0, r.GetExplicitDurationInS(), 0.1)
|
||||
})
|
||||
|
||||
t.Run("defaults to 1 hour when times not set", func(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
assert.InDelta(t, 3600.0, r.GetExplicitDurationInS(), 0.1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetPriceHT(t *testing.T) {
|
||||
t.Run("returns error if no pricing profile", func(t *testing.T) {
|
||||
r := &resources.PricedResource{ResourceID: "no-profile"}
|
||||
price, err := r.GetPriceHT()
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "pricing profile must be set")
|
||||
assert.Equal(t, 0.0, price)
|
||||
})
|
||||
|
||||
t.Run("uses first profile if selected is nil", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(30 * time.Minute)
|
||||
r := &resources.PricedResource{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
},
|
||||
}
|
||||
price, err := r.GetPriceHT()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 42.0, price)
|
||||
})
|
||||
|
||||
t.Run("returns error if profile GetPriceHT fails", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
mock := &MockPricingProfile{ReturnErr: true}
|
||||
r := &resources.PricedResource{
|
||||
SelectedPricing: mock,
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
},
|
||||
}
|
||||
price, err := r.GetPriceHT()
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, 0.0, price)
|
||||
})
|
||||
|
||||
t.Run("uses SelectedPricing if set", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
mock := &MockPricingProfile{ReturnCost: 10.0}
|
||||
r := &resources.PricedResource{
|
||||
SelectedPricing: mock,
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
},
|
||||
}
|
||||
price, err := r.GetPriceHT()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 10.0, price)
|
||||
})
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
. "cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestProcessingResource_GetType(t *testing.T) {
|
||||
r := &ProcessingResource{}
|
||||
assert.Equal(t, tools.PROCESSING_RESOURCE.String(), r.GetType())
|
||||
}
|
||||
|
||||
func TestPricedProcessingResource_GetType(t *testing.T) {
|
||||
r := &PricedProcessingResource{}
|
||||
assert.Equal(t, tools.PROCESSING_RESOURCE, r.GetType())
|
||||
}
|
||||
|
||||
func TestPricedProcessingResource_GetExplicitDurationInS(t *testing.T) {
|
||||
now := time.Now()
|
||||
after := now.Add(2 * time.Hour)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input PricedProcessingResource
|
||||
expected float64
|
||||
}{
|
||||
{
|
||||
name: "Service without explicit duration",
|
||||
input: PricedProcessingResource{
|
||||
IsService: true,
|
||||
},
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "Nil start time, non-service",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: nil,
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: float64((1 * time.Hour).Seconds()),
|
||||
},
|
||||
{
|
||||
name: "Duration computed from start and end",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &now,
|
||||
UsageEnd: &after,
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: float64((2 * time.Hour).Seconds()),
|
||||
},
|
||||
{
|
||||
name: "Explicit duration takes precedence",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
ExplicitBookingDurationS: 1337,
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: 1337,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
assert.Equal(t, test.expected, test.input.GetExplicitDurationInS())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessingResource_GetAccessor(t *testing.T) {
|
||||
request := &tools.APIRequest{}
|
||||
r := &ProcessingResource{}
|
||||
acc := r.GetAccessor(request)
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
|
||||
func TestProcessingResourcePricingProfile_GetPriceHT(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(2 * time.Hour)
|
||||
mockPricing := pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{
|
||||
Price: 100.0,
|
||||
},
|
||||
}
|
||||
profile := &ProcessingResourcePricingProfile{AccessPricingProfile: mockPricing}
|
||||
price, err := profile.GetPriceHT(0, 0, start, end, []*pricing.PricingVariation{})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 100.0, price)
|
||||
}
|
||||
|
||||
func TestProcessingResourcePricingProfile_IsPurchased(t *testing.T) {
|
||||
purchased := &ProcessingResourcePricingProfile{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{
|
||||
BuyingStrategy: pricing.PERMANENT,
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.True(t, purchased.IsPurchasable())
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type MockInstance struct {
|
||||
ID string
|
||||
Name string
|
||||
resources.ResourceInstance[*MockPartner]
|
||||
}
|
||||
|
||||
func (m *MockInstance) GetID() string { return m.ID }
|
||||
func (m *MockInstance) GetName() string { return m.Name }
|
||||
func (m *MockInstance) ClearEnv() {}
|
||||
func (m *MockInstance) ClearPeerGroups() {}
|
||||
func (m *MockInstance) GetProfile(peerID string, a *int, b *int, c *int) pricing.PricingProfileITF {
|
||||
return nil
|
||||
}
|
||||
func (m *MockInstance) GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF {
|
||||
return nil
|
||||
}
|
||||
func (m *MockInstance) GetPeerGroups() ([]resources.ResourcePartnerITF, []map[string][]string) {
|
||||
return nil, []map[string][]string{
|
||||
{"peer1": {"group1"}},
|
||||
}
|
||||
}
|
||||
|
||||
type MockPartner struct {
|
||||
groups map[string][]string
|
||||
}
|
||||
|
||||
func (m *MockPartner) GetProfile(buying *int, strategy *int) pricing.PricingProfileITF {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockPartner) GetPeerGroups() map[string][]string {
|
||||
return m.groups
|
||||
}
|
||||
func (m *MockPartner) ClearPeerGroups() {}
|
||||
func (m *MockPartner) GetPricingsProfiles(string, []string) []pricing.PricingProfileITF {
|
||||
return nil
|
||||
}
|
||||
|
||||
type MockDBObject struct {
|
||||
utils.AbstractObject
|
||||
isDraft bool
|
||||
}
|
||||
|
||||
func (m *MockDBObject) IsDrafted() bool {
|
||||
return m.isDraft
|
||||
}
|
||||
|
||||
func TestGetSelectedInstance_WithValidIndex(t *testing.T) {
|
||||
index := 1
|
||||
inst1 := &MockInstance{ID: "1"}
|
||||
inst2 := &MockInstance{ID: "2"}
|
||||
resource := &resources.AbstractInstanciatedResource[*MockInstance]{
|
||||
AbstractResource: resources.AbstractResource{},
|
||||
Instances: []*MockInstance{inst1, inst2},
|
||||
}
|
||||
result := resource.GetSelectedInstance(&index)
|
||||
assert.Equal(t, inst2, result)
|
||||
}
|
||||
|
||||
func TestGetSelectedInstance_NoIndex(t *testing.T) {
|
||||
inst := &MockInstance{ID: "1"}
|
||||
resource := &resources.AbstractInstanciatedResource[*MockInstance]{
|
||||
Instances: []*MockInstance{inst},
|
||||
}
|
||||
result := resource.GetSelectedInstance(nil)
|
||||
assert.Equal(t, inst, result)
|
||||
}
|
||||
|
||||
func TestCanUpdate_WhenOnlyStateDiffers(t *testing.T) {
|
||||
resource := &resources.AbstractResource{AbstractObject: utils.AbstractObject{IsDraft: false}}
|
||||
set := &MockDBObject{isDraft: true}
|
||||
canUpdate, updated := resource.CanUpdate(set)
|
||||
assert.True(t, canUpdate)
|
||||
assert.Equal(t, set, updated)
|
||||
}
|
||||
|
||||
func TestVerifyAuthAction_WithMatchingGroup(t *testing.T) {
|
||||
inst := &MockInstance{
|
||||
ResourceInstance: resources.ResourceInstance[*MockPartner]{
|
||||
Partnerships: []*MockPartner{
|
||||
{groups: map[string][]string{"peer1": {"group1"}}},
|
||||
},
|
||||
},
|
||||
}
|
||||
req := &tools.APIRequest{PeerID: "peer1", Groups: []string{"group1"}}
|
||||
result := resources.VerifyAuthAction([]*MockInstance{inst}, req)
|
||||
assert.Len(t, result, 1)
|
||||
}
|
||||
|
||||
type FakeResource struct {
|
||||
resources.AbstractInstanciatedResource[*MockInstance]
|
||||
}
|
||||
|
||||
func (f *FakeResource) Trim() {}
|
||||
func (f *FakeResource) SetAllowedInstances(*tools.APIRequest) {}
|
||||
func (f *FakeResource) VerifyAuth(string, *tools.APIRequest) bool { return true }
|
||||
|
||||
func TestNewAccessor_ReturnsValid(t *testing.T) {
|
||||
acc := resources.NewAccessor[*FakeResource](tools.COMPUTE_RESOURCE, &tools.APIRequest{}, func() utils.DBObject {
|
||||
return &FakeResource{}
|
||||
})
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
)
|
||||
|
||||
func TestStorageResource_GetType(t *testing.T) {
|
||||
res := &resources.StorageResource{}
|
||||
assert.Equal(t, tools.STORAGE_RESOURCE.String(), res.GetType())
|
||||
}
|
||||
|
||||
func TestStorageResource_GetAccessor(t *testing.T) {
|
||||
res := &resources.StorageResource{}
|
||||
req := &tools.APIRequest{}
|
||||
accessor := res.GetAccessor(req)
|
||||
assert.NotNil(t, accessor)
|
||||
}
|
||||
|
||||
func TestStorageResource_ConvertToPricedResource_ValidType(t *testing.T) {
|
||||
res := &resources.StorageResource{}
|
||||
res.AbstractInstanciatedResource.CreatorID = "creator"
|
||||
res.AbstractInstanciatedResource.UUID = "res-id"
|
||||
priced, _ := res.ConvertToPricedResource(tools.STORAGE_RESOURCE, nil, nil, nil, nil, nil, &tools.APIRequest{})
|
||||
assert.NotNil(t, priced)
|
||||
assert.IsType(t, &resources.PricedStorageResource{}, priced)
|
||||
}
|
||||
|
||||
func TestStorageResource_ConvertToPricedResource_InvalidType(t *testing.T) {
|
||||
res := &resources.StorageResource{}
|
||||
priced, _ := res.ConvertToPricedResource(tools.COMPUTE_RESOURCE, nil, nil, nil, nil, nil, &tools.APIRequest{})
|
||||
assert.Nil(t, priced)
|
||||
}
|
||||
|
||||
func TestStorageResourceInstance_ClearEnv(t *testing.T) {
|
||||
inst := &resources.StorageResourceInstance{
|
||||
ResourceInstance: resources.ResourceInstance[*resources.StorageResourcePartnership]{
|
||||
Env: []models.Param{{Attr: "A"}},
|
||||
Inputs: []models.Param{{Attr: "B"}},
|
||||
Outputs: []models.Param{{Attr: "C"}},
|
||||
},
|
||||
}
|
||||
|
||||
inst.ClearEnv()
|
||||
assert.Empty(t, inst.Env)
|
||||
assert.Empty(t, inst.Inputs)
|
||||
assert.Empty(t, inst.Outputs)
|
||||
}
|
||||
|
||||
func TestStorageResourceInstance_StoreDraftDefault(t *testing.T) {
|
||||
inst := &resources.StorageResourceInstance{
|
||||
Source: "my-source",
|
||||
ResourceInstance: resources.ResourceInstance[*resources.StorageResourcePartnership]{
|
||||
Env: []models.Param{},
|
||||
},
|
||||
}
|
||||
|
||||
inst.StoreDraftDefault()
|
||||
assert.Len(t, inst.Env, 1)
|
||||
assert.Equal(t, "source", inst.Env[0].Attr)
|
||||
assert.Equal(t, "my-source", inst.Env[0].Value)
|
||||
assert.True(t, inst.Env[0].Readonly)
|
||||
}
|
||||
|
||||
func TestStorageResourcePricingStrategy_GetQuantity(t *testing.T) {
|
||||
tests := []struct {
|
||||
strategy resources.StorageResourcePricingStrategy
|
||||
dataGB float64
|
||||
expect float64
|
||||
}{
|
||||
{resources.PER_DATA_STORED, 1.2, 1.2},
|
||||
{resources.PER_TB_STORED, 1.2, 1200},
|
||||
{resources.PER_GB_STORED, 2.5, 2.5},
|
||||
{resources.PER_MB_STORED, 1.0, 1000},
|
||||
{resources.PER_KB_STORED, 0.1, 100000},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
q, err := tt.strategy.GetQuantity(tt.dataGB)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.expect, q)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageResourcePricingStrategy_GetQuantity_Invalid(t *testing.T) {
|
||||
invalid := resources.StorageResourcePricingStrategy(99)
|
||||
q, err := invalid.GetQuantity(1.0)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 0.0, q)
|
||||
}
|
||||
|
||||
func TestPricedStorageResource_GetPriceHT_NoProfiles(t *testing.T) {
|
||||
res := &resources.PricedStorageResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "res-id",
|
||||
},
|
||||
}
|
||||
_, err := res.GetPriceHT()
|
||||
assert.Error(t, err)
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package resources_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
)
|
||||
|
||||
func TestWorkflowResource_GetType(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
assert.Equal(t, tools.WORKFLOW_RESOURCE.String(), w.GetType())
|
||||
}
|
||||
|
||||
func TestWorkflowResource_ConvertToPricedResource(t *testing.T) {
|
||||
w := &resources.WorkflowResource{
|
||||
AbstractResource: resources.AbstractResource{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
Name: "Test Workflow",
|
||||
UUID: "workflow-uuid",
|
||||
CreatorID: "creator-id",
|
||||
},
|
||||
Logo: "logo.png",
|
||||
},
|
||||
}
|
||||
|
||||
req := &tools.APIRequest{
|
||||
PeerID: "peer-1",
|
||||
Groups: []string{"group1"},
|
||||
}
|
||||
|
||||
pr, _ := w.ConvertToPricedResource(tools.WORKFLOW_RESOURCE, nil, nil, nil, nil, nil, req)
|
||||
assert.Equal(t, "creator-id", pr.GetCreatorID())
|
||||
assert.Equal(t, tools.WORKFLOW_RESOURCE, pr.GetType())
|
||||
}
|
||||
|
||||
func TestWorkflowResource_ClearEnv(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
assert.Equal(t, w, w.ClearEnv())
|
||||
}
|
||||
|
||||
func TestWorkflowResource_Trim(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
w.Trim()
|
||||
// nothing to assert; just test that it doesn't panic
|
||||
}
|
||||
|
||||
func TestWorkflowResource_SetAllowedInstances(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
w.SetAllowedInstances(&tools.APIRequest{})
|
||||
// no-op; just confirm no crash
|
||||
}
|
||||
|
||||
func TestWorkflowResource_GetAccessor(t *testing.T) {
|
||||
w := &resources.WorkflowResource{}
|
||||
request := &tools.APIRequest{}
|
||||
accessor := w.GetAccessor(request)
|
||||
assert.NotNil(t, accessor)
|
||||
}
|
||||
15
models/resources/workflow.go
Executable file → Normal file
15
models/resources/workflow.go
Executable file → Normal file
@@ -16,20 +16,13 @@ type WorkflowResource struct {
|
||||
}
|
||||
|
||||
func (d *WorkflowResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor[*WorkflowResource](tools.WORKFLOW_RESOURCE, request, func() utils.DBObject { return &WorkflowResource{} })
|
||||
}
|
||||
|
||||
func (r *WorkflowResource) AddInstances(instance ResourceInstanceITF) {
|
||||
return NewAccessor[*ComputeResource](tools.WORKFLOW_RESOURCE, request, func() utils.DBObject { return &WorkflowResource{} })
|
||||
}
|
||||
|
||||
func (r *WorkflowResource) GetType() string {
|
||||
return tools.WORKFLOW_RESOURCE.String()
|
||||
}
|
||||
|
||||
func (d *WorkflowResource) ClearEnv() utils.DBObject {
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *WorkflowResource) Trim() {
|
||||
/* EMPTY */
|
||||
}
|
||||
@@ -37,13 +30,13 @@ func (w *WorkflowResource) SetAllowedInstances(request *tools.APIRequest) {
|
||||
/* EMPTY */
|
||||
}
|
||||
|
||||
func (w *WorkflowResource) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||
func (w *WorkflowResource) ConvertToPricedResource(
|
||||
t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||
return &PricedResource{
|
||||
Name: w.Name,
|
||||
Logo: w.Logo,
|
||||
ResourceID: w.UUID,
|
||||
ResourceType: t,
|
||||
Quantity: 1,
|
||||
CreatorID: w.CreatorID,
|
||||
}, nil // TODO ???
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestModel_ReturnsValidInstances(t *testing.T) {
|
||||
for name, _ := range models.ModelsCatalog {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
modelInt, _ := strconv.Atoi(name)
|
||||
obj := models.Model(modelInt)
|
||||
assert.NotNil(t, obj, "Model() returned nil for valid model name %s", name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestModel_UnknownModelReturnsNil(t *testing.T) {
|
||||
invalidModelInt := -9999 // unlikely to be valid
|
||||
obj := models.Model(invalidModelInt)
|
||||
assert.Nil(t, obj)
|
||||
}
|
||||
|
||||
func TestGetModelsNames_ReturnsAllKeys(t *testing.T) {
|
||||
names := models.GetModelsNames()
|
||||
assert.Len(t, names, len(models.ModelsCatalog))
|
||||
|
||||
seen := make(map[string]bool)
|
||||
for _, name := range names {
|
||||
seen[name] = true
|
||||
}
|
||||
for key := range models.ModelsCatalog {
|
||||
assert.Contains(t, seen, key)
|
||||
}
|
||||
}
|
||||
12
models/utils/abstracts.go
Executable file → Normal file
12
models/utils/abstracts.go
Executable file → Normal file
@@ -43,14 +43,6 @@ func (ri *AbstractObject) GetAccessor(request *tools.APIRequest) Accessor {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AbstractObject) SetID(id string) {
|
||||
r.UUID = id
|
||||
}
|
||||
|
||||
func (r *AbstractObject) SetName(name string) {
|
||||
r.Name = name
|
||||
}
|
||||
|
||||
func (r *AbstractObject) GenerateID() {
|
||||
if r.UUID == "" {
|
||||
r.UUID = uuid.New().String()
|
||||
@@ -98,8 +90,8 @@ func (ao *AbstractObject) UpToDate(user string, peer string, create bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func (ao *AbstractObject) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
return (ao.AccessMode == Public && callName == "get") || request.Admin || (request != nil && ao.CreatorID == request.PeerID && request.PeerID != "")
|
||||
func (ao *AbstractObject) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return ao.AccessMode == Public || (request != nil && ao.CreatorID == request.PeerID)
|
||||
}
|
||||
|
||||
func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
|
||||
|
||||
17
models/utils/common.go
Executable file → Normal file
17
models/utils/common.go
Executable file → Normal file
@@ -18,7 +18,7 @@ func VerifyAccess(a Accessor, id string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if a.ShouldVerifyAuth() && !data.VerifyAuth("get", a.GetRequest()) {
|
||||
if a.ShouldVerifyAuth() && !data.VerifyAuth(a.GetRequest()) {
|
||||
return errors.New("you are not allowed to access :" + a.GetType().String())
|
||||
}
|
||||
return nil
|
||||
@@ -41,7 +41,7 @@ func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
|
||||
}},
|
||||
},
|
||||
}
|
||||
if a.ShouldVerifyAuth() && !data.VerifyAuth("store", a.GetRequest()) {
|
||||
if a.ShouldVerifyAuth() && !data.VerifyAuth(a.GetRequest()) {
|
||||
return nil, 403, errors.New("you are not allowed to access : " + a.GetType().String())
|
||||
}
|
||||
if cursor, _, _ := a.Search(&f, "", data.IsDrafted()); len(cursor) > 0 {
|
||||
@@ -49,7 +49,7 @@ func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
|
||||
}
|
||||
err := validate.Struct(data)
|
||||
if err != nil {
|
||||
return nil, 422, errors.New("error when validating the received struct: " + err.Error())
|
||||
return nil, 422, err
|
||||
}
|
||||
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), a.GetType().String())
|
||||
if err != nil {
|
||||
@@ -66,9 +66,10 @@ func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
|
||||
return nil, 403, errors.New("you are not allowed to delete :" + a.GetType().String())
|
||||
}
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
if a.ShouldVerifyAuth() && !res.VerifyAuth("delete", a.GetRequest()) {
|
||||
if a.ShouldVerifyAuth() && !res.VerifyAuth(a.GetRequest()) {
|
||||
return nil, 403, errors.New("you are not allowed to access " + a.GetType().String())
|
||||
}
|
||||
_, code, err = mongo.MONGOService.DeleteOne(id, a.GetType().String())
|
||||
@@ -92,7 +93,7 @@ func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObje
|
||||
}
|
||||
set = newSet
|
||||
r.UpToDate(a.GetUser(), a.GetPeerID(), false)
|
||||
if a.ShouldVerifyAuth() && !r.VerifyAuth("update", a.GetRequest()) {
|
||||
if a.ShouldVerifyAuth() && !r.VerifyAuth(a.GetRequest()) {
|
||||
return nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
|
||||
}
|
||||
change := set.Serialize(set) // get the changes
|
||||
@@ -113,10 +114,11 @@ func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, erro
|
||||
var data T
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
res_mongo.Decode(&data)
|
||||
if a.ShouldVerifyAuth() && !data.VerifyAuth("get", a.GetRequest()) {
|
||||
if a.ShouldVerifyAuth() && !data.VerifyAuth(a.GetRequest()) {
|
||||
return nil, 403, errors.New("you are not allowed to access :" + a.GetType().String())
|
||||
}
|
||||
return f(data)
|
||||
@@ -126,13 +128,14 @@ func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, onlyDraft
|
||||
objs := []ShallowDBObject{}
|
||||
var results []T
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
if err = res.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
if (a.ShouldVerifyAuth() && !r.VerifyAuth("get", a.GetRequest())) || f(r) == nil || (onlyDraft && !r.IsDrafted()) || (!onlyDraft && r.IsDrafted()) {
|
||||
if (a.ShouldVerifyAuth() && !r.VerifyAuth(a.GetRequest())) || f(r) == nil || (onlyDraft && !r.IsDrafted()) || (!onlyDraft && r.IsDrafted()) {
|
||||
continue
|
||||
}
|
||||
objs = append(objs, f(r))
|
||||
|
||||
4
models/utils/interfaces.go
Executable file → Normal file
4
models/utils/interfaces.go
Executable file → Normal file
@@ -18,17 +18,15 @@ type ShallowDBObject interface {
|
||||
// DBObject is an interface that defines the basic methods for a DBObject
|
||||
type DBObject interface {
|
||||
GenerateID()
|
||||
SetID(id string)
|
||||
GetID() string
|
||||
GetName() string
|
||||
SetName(name string)
|
||||
IsDrafted() bool
|
||||
CanDelete() bool
|
||||
StoreDraftDefault()
|
||||
GetCreatorID() string
|
||||
UpToDate(user string, peer string, create bool)
|
||||
CanUpdate(set DBObject) (bool, DBObject)
|
||||
VerifyAuth(callName string, request *tools.APIRequest) bool
|
||||
VerifyAuth(request *tools.APIRequest) bool
|
||||
Serialize(obj DBObject) map[string]interface{}
|
||||
GetAccessor(request *tools.APIRequest) Accessor
|
||||
Deserialize(j map[string]interface{}, obj DBObject) DBObject
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGenerateID(t *testing.T) {
|
||||
ao := &utils.AbstractObject{}
|
||||
ao.GenerateID()
|
||||
assert.NotEmpty(t, ao.UUID)
|
||||
_, err := uuid.Parse(ao.UUID)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestStoreDraftDefault(t *testing.T) {
|
||||
ao := &utils.AbstractObject{IsDraft: true}
|
||||
ao.StoreDraftDefault()
|
||||
assert.False(t, ao.IsDraft)
|
||||
}
|
||||
|
||||
func TestCanUpdate(t *testing.T) {
|
||||
ao := &utils.AbstractObject{}
|
||||
res, set := ao.CanUpdate(nil)
|
||||
assert.True(t, res)
|
||||
assert.Nil(t, set)
|
||||
}
|
||||
|
||||
func TestCanDelete(t *testing.T) {
|
||||
ao := &utils.AbstractObject{}
|
||||
assert.True(t, ao.CanDelete())
|
||||
}
|
||||
|
||||
func TestIsDrafted(t *testing.T) {
|
||||
ao := &utils.AbstractObject{IsDraft: true}
|
||||
assert.True(t, ao.IsDrafted())
|
||||
}
|
||||
|
||||
func TestGetID(t *testing.T) {
|
||||
u := uuid.New().String()
|
||||
ao := &utils.AbstractObject{UUID: u}
|
||||
assert.Equal(t, u, ao.GetID())
|
||||
}
|
||||
|
||||
func TestGetName(t *testing.T) {
|
||||
name := "MyObject"
|
||||
ao := &utils.AbstractObject{Name: name}
|
||||
assert.Equal(t, name, ao.GetName())
|
||||
}
|
||||
|
||||
func TestGetCreatorID(t *testing.T) {
|
||||
id := "creator-123"
|
||||
ao := &utils.AbstractObject{CreatorID: id}
|
||||
assert.Equal(t, id, ao.GetCreatorID())
|
||||
}
|
||||
|
||||
func TestUpToDate_CreateFalse(t *testing.T) {
|
||||
ao := &utils.AbstractObject{}
|
||||
now := time.Now()
|
||||
time.Sleep(time.Millisecond) // ensure time difference
|
||||
ao.UpToDate("user123", "peer456", false)
|
||||
assert.WithinDuration(t, now, ao.UpdateDate, time.Second)
|
||||
assert.Equal(t, "peer456", ao.UpdaterID)
|
||||
assert.Equal(t, "user123", ao.UserUpdaterID)
|
||||
assert.True(t, ao.CreationDate.IsZero())
|
||||
}
|
||||
|
||||
func TestUpToDate_CreateTrue(t *testing.T) {
|
||||
ao := &utils.AbstractObject{}
|
||||
now := time.Now()
|
||||
time.Sleep(time.Millisecond)
|
||||
ao.UpToDate("user123", "peer456", true)
|
||||
assert.WithinDuration(t, now, ao.UpdateDate, time.Second)
|
||||
assert.WithinDuration(t, now, ao.CreationDate, time.Second)
|
||||
assert.Equal(t, "peer456", ao.UpdaterID)
|
||||
assert.Equal(t, "peer456", ao.CreatorID)
|
||||
assert.Equal(t, "user123", ao.UserUpdaterID)
|
||||
assert.Equal(t, "user123", ao.UserCreatorID)
|
||||
}
|
||||
|
||||
func TestVerifyAuth(t *testing.T) {
|
||||
request := &tools.APIRequest{PeerID: "peer123"}
|
||||
ao := &utils.AbstractObject{CreatorID: "peer123"}
|
||||
assert.True(t, ao.VerifyAuth("get", request))
|
||||
|
||||
ao = &utils.AbstractObject{AccessMode: utils.Public}
|
||||
assert.True(t, ao.VerifyAuth("get", nil))
|
||||
|
||||
ao = &utils.AbstractObject{AccessMode: utils.Private, CreatorID: "peer123"}
|
||||
request = &tools.APIRequest{PeerID: "wrong"}
|
||||
assert.False(t, ao.VerifyAuth("get", request))
|
||||
}
|
||||
|
||||
func TestGetObjectFilters(t *testing.T) {
|
||||
ao := &utils.AbstractObject{}
|
||||
f := ao.GetObjectFilters("*")
|
||||
assert.NotNil(t, f)
|
||||
assert.Contains(t, f.Or, "abstractobject.name")
|
||||
assert.Equal(t, dbs.LIKE.String(), f.Or["abstractobject.name"][0].Operator)
|
||||
}
|
||||
|
||||
func TestDeserialize(t *testing.T) {
|
||||
ao := &utils.AbstractObject{}
|
||||
input := map[string]interface{}{"name": "test", "id": uuid.New().String()}
|
||||
res := ao.Deserialize(input, &utils.AbstractObject{})
|
||||
assert.NotNil(t, res)
|
||||
}
|
||||
|
||||
func TestSerialize(t *testing.T) {
|
||||
ao := &utils.AbstractObject{Name: "test", UUID: uuid.New().String()}
|
||||
m := ao.Serialize(ao)
|
||||
assert.Equal(t, "test", m["name"])
|
||||
}
|
||||
|
||||
func TestAbstractAccessorMethods(t *testing.T) {
|
||||
r := &utils.AbstractAccessor{Request: &tools.APIRequest{Username: "alice", PeerID: "peer1", Groups: []string{"dev"}}}
|
||||
assert.True(t, r.ShouldVerifyAuth())
|
||||
assert.Equal(t, "alice", r.GetUser())
|
||||
assert.Equal(t, "peer1", r.GetPeerID())
|
||||
assert.Equal(t, []string{"dev"}, r.GetGroups())
|
||||
assert.Equal(t, r.Request.Caller, r.GetCaller())
|
||||
}
|
||||
@@ -1,168 +0,0 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// --- Mock Definitions ---
|
||||
|
||||
type MockDBObject struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockAccessor) GetLogger() *zerolog.Logger {
|
||||
return nil
|
||||
}
|
||||
func (m *MockAccessor) GetGroups() []string {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func (m *MockAccessor) GetCaller() *tools.HTTPCaller {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockDBObject) GenerateID() { m.Called() }
|
||||
func (m *MockDBObject) StoreDraftDefault() { m.Called() }
|
||||
func (m *MockDBObject) UpToDate(user, peer string, create bool) {
|
||||
m.Called(user, peer, create)
|
||||
}
|
||||
func (m *MockDBObject) VerifyAuth(req *tools.APIRequest) bool {
|
||||
args := m.Called(req)
|
||||
return args.Bool(0)
|
||||
}
|
||||
func (m *MockDBObject) CanDelete() bool {
|
||||
args := m.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
func (m *MockDBObject) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||
args := m.Called(set)
|
||||
return args.Bool(0), args.Get(1).(utils.DBObject)
|
||||
}
|
||||
func (m *MockDBObject) IsDrafted() bool {
|
||||
args := m.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
func (m *MockDBObject) Serialize(obj utils.DBObject) map[string]interface{} {
|
||||
args := m.Called(obj)
|
||||
return args.Get(0).(map[string]interface{})
|
||||
}
|
||||
func (m *MockDBObject) Deserialize(mdata map[string]interface{}, obj utils.DBObject) utils.DBObject {
|
||||
args := m.Called(mdata, obj)
|
||||
return args.Get(0).(utils.DBObject)
|
||||
}
|
||||
func (m *MockDBObject) GetID() string {
|
||||
args := m.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
func (m *MockDBObject) GetName() string {
|
||||
args := m.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
type MockAccessor struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
func (m *MockAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(set, id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
args := m.Called(data)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
args := m.Called(data)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) ShouldVerifyAuth() bool {
|
||||
args := m.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
func (m *MockAccessor) GetRequest() *tools.APIRequest {
|
||||
args := m.Called()
|
||||
return args.Get(0).(*tools.APIRequest)
|
||||
}
|
||||
func (m *MockAccessor) GetType() tools.DataType {
|
||||
args := m.Called()
|
||||
return args.Get(0).(tools.DataType)
|
||||
}
|
||||
func (m *MockAccessor) Search(filters *dbs.Filters, s string, d bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(filters, s, d)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) GetUser() string {
|
||||
args := m.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
func (m *MockAccessor) GetPeerID() string {
|
||||
args := m.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
// --- Test Cases ---
|
||||
|
||||
func TestVerifyAccess_Authorized(t *testing.T) {
|
||||
mockObj := new(MockDBObject)
|
||||
mockAcc := new(MockAccessor)
|
||||
|
||||
req := &tools.APIRequest{PeerID: "peer"}
|
||||
mockAcc.On("LoadOne", "123").Return(mockObj, 200, nil)
|
||||
mockAcc.On("ShouldVerifyAuth").Return(true)
|
||||
mockObj.On("VerifyAuth", req).Return(true)
|
||||
mockAcc.On("GetRequest").Return(req)
|
||||
|
||||
err := utils.VerifyAccess(mockAcc, "123")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestVerifyAccess_Unauthorized(t *testing.T) {
|
||||
mockObj := new(MockDBObject)
|
||||
mockAcc := new(MockAccessor)
|
||||
|
||||
req := &tools.APIRequest{PeerID: "peer"}
|
||||
mockAcc.On("LoadOne", "123").Return(mockObj, 200, nil)
|
||||
mockAcc.On("ShouldVerifyAuth").Return(true)
|
||||
mockObj.On("VerifyAuth", req).Return(false)
|
||||
mockAcc.On("GetRequest").Return(req)
|
||||
|
||||
err := utils.VerifyAccess(mockAcc, "123")
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "not allowed")
|
||||
}
|
||||
|
||||
func TestVerifyAccess_LoadError(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
|
||||
mockAcc.On("LoadOne", "bad-id").Return(nil, 404, errors.New("not found"))
|
||||
|
||||
err := utils.VerifyAccess(mockAcc, "bad-id")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "not found", err.Error())
|
||||
}
|
||||
@@ -15,15 +15,6 @@ type Graph struct {
|
||||
Links []GraphLink `bson:"links" json:"links" default:"{}" validate:"required"` // Links is the list of links between elements in the graph
|
||||
}
|
||||
|
||||
func NewGraph() *Graph {
|
||||
return &Graph{
|
||||
Partial: false,
|
||||
Zoom: 1,
|
||||
Items: map[string]GraphItem{},
|
||||
Links: []GraphLink{},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Graph) Clear(id string) {
|
||||
realItems := map[string]GraphItem{}
|
||||
for k, it := range g.Items {
|
||||
@@ -47,10 +38,6 @@ func (wf *Graph) IsProcessing(item GraphItem) bool {
|
||||
return item.Processing != nil
|
||||
}
|
||||
|
||||
func (wf *Graph) IsNativeTool(item GraphItem) bool {
|
||||
return item.NativeTool != nil
|
||||
}
|
||||
|
||||
func (wf *Graph) IsCompute(item GraphItem) bool {
|
||||
return item.Compute != nil
|
||||
}
|
||||
@@ -68,7 +55,7 @@ func (wf *Graph) IsWorkflow(item GraphItem) bool {
|
||||
}
|
||||
|
||||
func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, processings []*resources.ProcessingResource, resource resources.ResourceInterface,
|
||||
f func(GraphItem) resources.ResourceInterface, instance int, bookingMode int, request *tools.APIRequest) (float64, float64, error) {
|
||||
f func(GraphItem) resources.ResourceInterface, request *tools.APIRequest) (float64, float64) {
|
||||
nearestStart := float64(10000000000)
|
||||
oneIsInfinite := false
|
||||
longestDuration := float64(0)
|
||||
@@ -80,10 +67,7 @@ func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, proce
|
||||
} else if link.Source.ID == processing.GetID() && f(g.Items[link.Source.ID]) != nil && f(g.Items[link.Source.ID]).GetID() == resource.GetID() { // if the source is the processing and the destination is not a compute
|
||||
source = link.Destination.ID
|
||||
}
|
||||
priced, err := processing.ConvertToPricedResource(tools.PROCESSING_RESOURCE, &instance, &bookingMode, request)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
priced := processing.ConvertToPricedResource(tools.PROCESSING_RESOURCE, request)
|
||||
if source != "" {
|
||||
if priced.GetLocationStart() != nil {
|
||||
near := float64(priced.GetLocationStart().Sub(start).Seconds())
|
||||
@@ -104,15 +88,15 @@ func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, proce
|
||||
}
|
||||
}
|
||||
if oneIsInfinite {
|
||||
return nearestStart, -1, nil
|
||||
return nearestStart, -1
|
||||
}
|
||||
return nearestStart, longestDuration, nil
|
||||
return nearestStart, longestDuration
|
||||
}
|
||||
|
||||
/*
|
||||
* GetAverageTimeBeforeStart is a function that returns the average time before the start of a processing
|
||||
*/
|
||||
func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingID string, instance int, bookingMode int, request *tools.APIRequest) (float64, error) {
|
||||
func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingID string, request *tools.APIRequest) float64 {
|
||||
currents := []float64{} // list of current time
|
||||
for _, link := range g.Links { // for each link
|
||||
var source string // source is the source of the link
|
||||
@@ -128,20 +112,13 @@ func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingI
|
||||
if r == nil { // if item is nil, continue
|
||||
continue
|
||||
}
|
||||
priced, err := r.ConvertToPricedResource(dt, &instance, &bookingMode, request)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
priced := r.ConvertToPricedResource(dt, request)
|
||||
current := priced.GetExplicitDurationInS() // get the explicit duration of the item
|
||||
if current < 0 { // if current is negative, its means that duration of a before could be infinite continue
|
||||
return current, nil
|
||||
return current
|
||||
}
|
||||
add, err := g.GetAverageTimeProcessingBeforeStart(current, source, instance, bookingMode, request)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
current += add // get the average time before start of the source
|
||||
currents = append(currents, current) // append the current to the currents
|
||||
current += g.GetAverageTimeProcessingBeforeStart(current, source, request) // get the average time before start of the source
|
||||
currents = append(currents, current) // append the current to the currents
|
||||
}
|
||||
var max float64 // get the max time to wait dependancies to finish
|
||||
for _, current := range currents {
|
||||
@@ -149,14 +126,12 @@ func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingI
|
||||
max = current
|
||||
}
|
||||
}
|
||||
return max, nil
|
||||
return max
|
||||
}
|
||||
|
||||
func (g *Graph) GetResource(id string) (tools.DataType, resources.ResourceInterface) {
|
||||
if item, ok := g.Items[id]; ok {
|
||||
if item.Data != nil {
|
||||
return tools.NATIVE_TOOL, item.NativeTool
|
||||
} else if item.Data != nil {
|
||||
return tools.DATA_RESOURCE, item.Data
|
||||
} else if item.Compute != nil {
|
||||
return tools.COMPUTE_RESOURCE, item.Compute
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
@@ -18,19 +12,8 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow/graph"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ConfigItem map[string]int
|
||||
|
||||
func (c ConfigItem) Get(key string) *int {
|
||||
i := 0
|
||||
if ins, ok := c[key]; ok {
|
||||
i = ins
|
||||
}
|
||||
return &i
|
||||
}
|
||||
|
||||
/*
|
||||
* Workflow is a struct that represents a workflow
|
||||
* it defines the native workflow
|
||||
@@ -48,326 +31,6 @@ func (d *Workflow) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
func (d *Workflow) GetResources(dt tools.DataType) []resources.ResourceInterface {
|
||||
itf := []resources.ResourceInterface{}
|
||||
switch dt {
|
||||
case tools.NATIVE_TOOL:
|
||||
for _, d := range d.NativeTools {
|
||||
itf = append(itf, d)
|
||||
}
|
||||
return itf
|
||||
case tools.DATA_RESOURCE:
|
||||
for _, d := range d.DataResources {
|
||||
itf = append(itf, d)
|
||||
}
|
||||
return itf
|
||||
case tools.PROCESSING_RESOURCE:
|
||||
for _, d := range d.ProcessingResources {
|
||||
itf = append(itf, d)
|
||||
}
|
||||
return itf
|
||||
case tools.COMPUTE_RESOURCE:
|
||||
for _, d := range d.ComputeResources {
|
||||
itf = append(itf, d)
|
||||
}
|
||||
return itf
|
||||
case tools.WORKFLOW_RESOURCE:
|
||||
for _, d := range d.WorkflowResources {
|
||||
itf = append(itf, d)
|
||||
}
|
||||
return itf
|
||||
case tools.STORAGE_RESOURCE:
|
||||
for _, d := range d.StorageResources {
|
||||
itf = append(itf, d)
|
||||
}
|
||||
return itf
|
||||
}
|
||||
return itf
|
||||
}
|
||||
|
||||
func (d *Workflow) ExtractFromPlantUML(plantUML multipart.File, request *tools.APIRequest) (*Workflow, error) {
|
||||
if plantUML == nil {
|
||||
return d, errors.New("no file available to export")
|
||||
}
|
||||
|
||||
defer plantUML.Close()
|
||||
|
||||
d.Datas = []string{}
|
||||
d.Storages = []string{}
|
||||
d.Processings = []string{}
|
||||
d.Computes = []string{}
|
||||
d.Workflows = []string{}
|
||||
|
||||
d.DataResources = []*resources.DataResource{}
|
||||
d.StorageResources = []*resources.StorageResource{}
|
||||
d.ProcessingResources = []*resources.ProcessingResource{}
|
||||
d.ComputeResources = []*resources.ComputeResource{}
|
||||
d.WorkflowResources = []*resources.WorkflowResource{}
|
||||
|
||||
d.Graph = graph.NewGraph()
|
||||
resourceCatalog := map[string]func() resources.ResourceInterface{
|
||||
"Processing": func() resources.ResourceInterface {
|
||||
return &resources.ProcessingResource{
|
||||
AbstractInstanciatedResource: resources.AbstractInstanciatedResource[*resources.ProcessingInstance]{
|
||||
Instances: []*resources.ProcessingInstance{},
|
||||
},
|
||||
}
|
||||
},
|
||||
"Storage": func() resources.ResourceInterface {
|
||||
return &resources.StorageResource{
|
||||
AbstractInstanciatedResource: resources.AbstractInstanciatedResource[*resources.StorageResourceInstance]{
|
||||
Instances: []*resources.StorageResourceInstance{},
|
||||
},
|
||||
}
|
||||
},
|
||||
"Data": func() resources.ResourceInterface {
|
||||
return &resources.DataResource{
|
||||
AbstractInstanciatedResource: resources.AbstractInstanciatedResource[*resources.DataInstance]{
|
||||
Instances: []*resources.DataInstance{},
|
||||
},
|
||||
}
|
||||
},
|
||||
"ComputeUnit": func() resources.ResourceInterface {
|
||||
return &resources.ComputeResource{
|
||||
AbstractInstanciatedResource: resources.AbstractInstanciatedResource[*resources.ComputeResourceInstance]{
|
||||
Instances: []*resources.ComputeResourceInstance{},
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
graphVarName := map[string]*graph.GraphItem{}
|
||||
scanner := bufio.NewScanner(plantUML)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
for n, new := range resourceCatalog {
|
||||
if strings.Contains(line, n+"(") && !strings.Contains(line, "!procedure") { // should exclude declaration of type.
|
||||
newRes := new()
|
||||
varName, graphItem, err := d.extractResourcePlantUML(line, newRes, n, request.PeerID)
|
||||
if err != nil {
|
||||
return d, err
|
||||
}
|
||||
graphVarName[varName] = graphItem
|
||||
continue
|
||||
} else if strings.Contains(line, n+"-->") {
|
||||
err := d.extractLink(line, graphVarName, "-->", false)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
} else if strings.Contains(line, n+"<--") {
|
||||
err := d.extractLink(line, graphVarName, "<--", true)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
} else if strings.Contains(line, n+"--") {
|
||||
err := d.extractLink(line, graphVarName, "--", false)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
} else if strings.Contains(line, n+"-") {
|
||||
err := d.extractLink(line, graphVarName, "-", false)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return d, err
|
||||
}
|
||||
d.generateResource(d.GetResources(tools.DATA_RESOURCE), request)
|
||||
d.generateResource(d.GetResources(tools.PROCESSING_RESOURCE), request)
|
||||
d.generateResource(d.GetResources(tools.STORAGE_RESOURCE), request)
|
||||
d.generateResource(d.GetResources(tools.COMPUTE_RESOURCE), request)
|
||||
d.generateResource(d.GetResources(tools.WORKFLOW_RESOURCE), request)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *Workflow) generateResource(datas []resources.ResourceInterface, request *tools.APIRequest) error {
|
||||
for _, d := range datas {
|
||||
access := d.GetAccessor(request)
|
||||
if _, code, err := access.LoadOne(d.GetID()); err != nil && code == 200 {
|
||||
continue
|
||||
}
|
||||
access.StoreOne(d)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Workflow) extractLink(line string, graphVarName map[string]*graph.GraphItem, pattern string, reverse bool) error {
|
||||
splitted := strings.Split(line, pattern)
|
||||
if len(splitted) < 2 {
|
||||
return errors.New("links elements not found")
|
||||
}
|
||||
if graphVarName[splitted[0]] != nil {
|
||||
return errors.New("links elements not found -> " + strings.Trim(splitted[0], " "))
|
||||
}
|
||||
if graphVarName[splitted[1]] != nil {
|
||||
return errors.New("links elements not found -> " + strings.Trim(splitted[1], " "))
|
||||
}
|
||||
link := &graph.GraphLink{
|
||||
Source: graph.Position{
|
||||
ID: graphVarName[splitted[0]].ID,
|
||||
X: 0,
|
||||
Y: 0,
|
||||
},
|
||||
Destination: graph.Position{
|
||||
ID: graphVarName[splitted[1]].ID,
|
||||
X: 0,
|
||||
Y: 0,
|
||||
},
|
||||
}
|
||||
if reverse {
|
||||
tmp := link.Destination
|
||||
link.Destination = link.Source
|
||||
link.Source = tmp
|
||||
}
|
||||
splittedComments := strings.Split(line, "'")
|
||||
if len(splittedComments) <= 1 {
|
||||
return errors.New("Can't deserialize Object, there's no commentary")
|
||||
}
|
||||
comment := strings.ReplaceAll(splittedComments[1], "'", "") // for now it's a json.
|
||||
json.Unmarshal([]byte(comment), link)
|
||||
d.Graph.Links = append(d.Graph.Links, *link)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Workflow) extractResourcePlantUML(line string, resource resources.ResourceInterface, dataName string, peerID string) (string, *graph.GraphItem, error) {
|
||||
splittedFunc := strings.Split(line, "(")
|
||||
if len(splittedFunc) <= 1 {
|
||||
return "", nil, errors.New("Can't deserialize Object, there's no func")
|
||||
}
|
||||
splittedParams := strings.Split(splittedFunc[1], ",")
|
||||
if len(splittedFunc) <= 1 {
|
||||
return "", nil, errors.New("Can't deserialize Object, there's no params")
|
||||
}
|
||||
|
||||
varName := splittedParams[0]
|
||||
splitted := strings.Split(splittedParams[1], "\"")
|
||||
|
||||
if len(splitted) <= 1 {
|
||||
return "", nil, errors.New("Can't deserialize Object, there's no name")
|
||||
}
|
||||
resource.SetName(splitted[1])
|
||||
|
||||
splittedComments := strings.Split(line, "'")
|
||||
if len(splittedComments) <= 1 {
|
||||
return "", nil, errors.New("Can't deserialize Object, there's no commentary")
|
||||
}
|
||||
comment := strings.ReplaceAll(splittedComments[1], "'", "") // for now it's a json.
|
||||
instance := d.getNewInstance(dataName, splitted[1], peerID)
|
||||
if instance == nil {
|
||||
return "", nil, errors.New("No instance found.")
|
||||
}
|
||||
resource.AddInstances(instance)
|
||||
|
||||
json.Unmarshal([]byte(comment), instance)
|
||||
// deserializer les instances... une instance doit par défaut avoir certaines valeurs d'accès.
|
||||
graphID := uuid.New()
|
||||
graphItem := &graph.GraphItem{
|
||||
ID: graphID.String(),
|
||||
}
|
||||
graphItem = d.getNewGraphItem(dataName, graphItem, resource)
|
||||
|
||||
d.Graph.Items[graphID.String()] = *graphItem
|
||||
return varName, graphItem, nil
|
||||
}
|
||||
|
||||
func (d *Workflow) getNewGraphItem(dataName string, graphItem *graph.GraphItem, resource resources.ResourceInterface) *graph.GraphItem {
|
||||
switch dataName {
|
||||
case "Data":
|
||||
d.Datas = append(d.Datas, resource.GetID())
|
||||
d.DataResources = append(d.DataResources, resource.(*resources.DataResource))
|
||||
graphItem.Data = resource.(*resources.DataResource)
|
||||
case "Processing":
|
||||
d.Processings = append(d.Processings, resource.GetID())
|
||||
d.ProcessingResources = append(d.ProcessingResources, resource.(*resources.ProcessingResource))
|
||||
graphItem.Processing = resource.(*resources.ProcessingResource)
|
||||
case "Event":
|
||||
access := resources.NewAccessor[*resources.NativeTool](tools.NATIVE_TOOL, &tools.APIRequest{
|
||||
Admin: true,
|
||||
}, func() utils.DBObject { return &resources.NativeTool{} })
|
||||
t, _, err := access.Search(nil, "WORKFLOW_EVENT", false)
|
||||
if err == nil && len(t) > 0 {
|
||||
d.NativeTool = append(d.NativeTool, t[0].GetID())
|
||||
graphItem.NativeTool = t[0].(*resources.NativeTool)
|
||||
}
|
||||
case "Storage":
|
||||
d.Storages = append(d.Storages, resource.GetID())
|
||||
d.StorageResources = append(d.StorageResources, resource.(*resources.StorageResource))
|
||||
graphItem.Storage = resource.(*resources.StorageResource)
|
||||
case "ComputeUnit":
|
||||
d.Computes = append(d.Computes, resource.GetID())
|
||||
d.ComputeResources = append(d.ComputeResources, resource.(*resources.ComputeResource))
|
||||
graphItem.Compute = resource.(*resources.ComputeResource)
|
||||
default:
|
||||
return graphItem
|
||||
}
|
||||
return graphItem
|
||||
}
|
||||
|
||||
func (d *Workflow) getNewInstance(dataName string, name string, peerID string) resources.ResourceInstanceITF {
|
||||
switch dataName {
|
||||
case "Data":
|
||||
return resources.NewDataInstance(name, peerID)
|
||||
case "Processing":
|
||||
return resources.NewProcessingInstance(name, peerID)
|
||||
case "Storage":
|
||||
return resources.NewStorageResourceInstance(name, peerID)
|
||||
case "ComputeUnit":
|
||||
return resources.NewComputeResourceInstance(name, peerID)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type Deps struct {
|
||||
Source string
|
||||
Dest string
|
||||
}
|
||||
|
||||
func (w *Workflow) IsDependancy(id string) []Deps {
|
||||
dependancyOfIDs := []Deps{}
|
||||
for _, link := range w.Graph.Links {
|
||||
if _, ok := w.Graph.Items[link.Destination.ID]; !ok {
|
||||
continue
|
||||
}
|
||||
source := w.Graph.Items[link.Destination.ID].Processing
|
||||
if id == link.Source.ID && source != nil {
|
||||
dependancyOfIDs = append(dependancyOfIDs, Deps{Source: source.GetName(), Dest: link.Destination.ID})
|
||||
}
|
||||
sourceWF := w.Graph.Items[link.Destination.ID].Workflow
|
||||
if id == link.Source.ID && sourceWF != nil {
|
||||
dependancyOfIDs = append(dependancyOfIDs, Deps{Source: sourceWF.GetName(), Dest: link.Destination.ID})
|
||||
}
|
||||
}
|
||||
return dependancyOfIDs
|
||||
}
|
||||
|
||||
func (w *Workflow) GetFirstItems() []graph.GraphItem {
|
||||
return w.GetGraphItems(func(item graph.GraphItem) bool {
|
||||
return len(w.GetDependencies(w.GetID())) == 0
|
||||
})
|
||||
}
|
||||
|
||||
func (w *Workflow) GetDependencies(id string) (dependencies []Deps) {
|
||||
for _, link := range w.Graph.Links {
|
||||
if _, ok := w.Graph.Items[link.Source.ID]; !ok {
|
||||
continue
|
||||
}
|
||||
source := w.Graph.Items[link.Source.ID].Processing
|
||||
if id == link.Destination.ID && source != nil {
|
||||
dependencies = append(dependencies, Deps{Source: source.GetName(), Dest: link.Source.ID})
|
||||
continue
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (w *Workflow) GetGraphItems(f func(item graph.GraphItem) bool) (list_datas []graph.GraphItem) {
|
||||
for _, item := range w.Graph.Items {
|
||||
if f(item) {
|
||||
@@ -377,27 +40,16 @@ func (w *Workflow) GetGraphItems(f func(item graph.GraphItem) bool) (list_datas
|
||||
return
|
||||
}
|
||||
|
||||
func (w *Workflow) GetPricedItem(
|
||||
f func(item graph.GraphItem) bool, request *tools.APIRequest,
|
||||
instance int,
|
||||
partnership int,
|
||||
buying int,
|
||||
strategy int,
|
||||
bookingMode int,
|
||||
buyingStrategy int,
|
||||
pricingStrategy int) (map[string]pricing.PricedItemITF, error) {
|
||||
func (w *Workflow) GetPricedItem(f func(item graph.GraphItem) bool, request *tools.APIRequest) map[string]pricing.PricedItemITF {
|
||||
list_datas := map[string]pricing.PricedItemITF{}
|
||||
for _, item := range w.Graph.Items {
|
||||
if f(item) {
|
||||
dt, res := item.GetResource()
|
||||
ord, err := res.ConvertToPricedResource(dt, &instance, &partnership, &buying, &strategy, &bookingMode, request)
|
||||
if err != nil {
|
||||
return list_datas, err
|
||||
}
|
||||
ord := res.ConvertToPricedResource(dt, request)
|
||||
list_datas[res.GetID()] = ord
|
||||
}
|
||||
}
|
||||
return list_datas, nil
|
||||
return list_datas
|
||||
}
|
||||
|
||||
type Related struct {
|
||||
@@ -420,17 +72,19 @@ func (w *Workflow) GetByRelatedProcessing(processingID string, g func(item graph
|
||||
_, node = item.GetResource() // we are looking for the storage as destination
|
||||
}
|
||||
if processingID == nodeID && node != nil { // if the storage is linked to the processing
|
||||
relID := node.GetID()
|
||||
rel := Related{}
|
||||
if _, ok := related[processingID]; !ok {
|
||||
related[processingID] = Related{}
|
||||
}
|
||||
rel := related[node.GetID()]
|
||||
rel.Node = node
|
||||
rel.Links = append(rel.Links, link)
|
||||
related[relID] = rel
|
||||
related[processingID] = rel
|
||||
}
|
||||
}
|
||||
return related
|
||||
}
|
||||
|
||||
func (ao *Workflow) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
func (ao *Workflow) VerifyAuth(request *tools.APIRequest) bool {
|
||||
isAuthorized := false
|
||||
if len(ao.Shared) > 0 {
|
||||
for _, shared := range ao.Shared {
|
||||
@@ -438,11 +92,11 @@ func (ao *Workflow) VerifyAuth(callName string, request *tools.APIRequest) bool
|
||||
if code != 200 || shared == nil {
|
||||
isAuthorized = false
|
||||
} else {
|
||||
isAuthorized = shared.VerifyAuth(callName, request)
|
||||
isAuthorized = shared.VerifyAuth(request)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ao.AbstractObject.VerifyAuth(callName, request) || isAuthorized
|
||||
return ao.AbstractObject.VerifyAuth(request) || isAuthorized
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -474,177 +128,91 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (wf *Workflow) Planify(start time.Time, end *time.Time, instances ConfigItem, partnerships ConfigItem, buyings ConfigItem, strategies ConfigItem, bookingMode int, request *tools.APIRequest) (bool, float64, map[tools.DataType]map[string]pricing.PricedItemITF, *Workflow, error) {
|
||||
priceds := map[tools.DataType]map[string]pricing.PricedItemITF{}
|
||||
ps, priceds, err := plan[*resources.ProcessingResource](tools.PROCESSING_RESOURCE, instances, partnerships, buyings, strategies, bookingMode, wf, priceds, request, wf.Graph.IsProcessing,
|
||||
func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64, error) {
|
||||
d, err := wf.Graph.GetAverageTimeProcessingBeforeStart(0, res.GetID(),
|
||||
*instances.Get(res.GetID()), *partnerships.Get(res.GetID()), *buyings.Get(res.GetID()), *strategies.Get(res.GetID()),
|
||||
bookingMode, request)
|
||||
if err != nil {
|
||||
return start, 0, err
|
||||
}
|
||||
return start.Add(time.Duration(d) * time.Second), priced.GetExplicitDurationInS(), nil
|
||||
}, func(started time.Time, duration float64) (*time.Time, error) {
|
||||
func (wf *Workflow) Planify(start time.Time, end *time.Time, request *tools.APIRequest) (float64, map[tools.DataType][]pricing.PricedItemITF, *Workflow, error) {
|
||||
priceds := map[tools.DataType][]pricing.PricedItemITF{}
|
||||
ps, priceds, err := plan[*resources.ProcessingResource](tools.PROCESSING_RESOURCE, wf, priceds, request, wf.Graph.IsProcessing,
|
||||
func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64) {
|
||||
return start.Add(time.Duration(wf.Graph.GetAverageTimeProcessingBeforeStart(0, res.GetID(), request)) * time.Second), priced.GetExplicitDurationInS()
|
||||
}, func(started time.Time, duration float64) *time.Time {
|
||||
s := started.Add(time.Duration(duration))
|
||||
return &s, nil
|
||||
return &s
|
||||
})
|
||||
if err != nil {
|
||||
return false, 0, priceds, nil, err
|
||||
return 0, priceds, nil, err
|
||||
}
|
||||
if _, priceds, err = plan[resources.ResourceInterface](tools.NATIVE_TOOL, instances, partnerships, buyings, strategies, bookingMode, wf, priceds, request,
|
||||
wf.Graph.IsNativeTool, func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64, error) {
|
||||
return start, 0, nil
|
||||
}, func(started time.Time, duration float64) (*time.Time, error) {
|
||||
return end, nil
|
||||
if _, priceds, err = plan[resources.ResourceInterface](tools.DATA_RESOURCE, wf, priceds, request, wf.Graph.IsData,
|
||||
func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64) {
|
||||
return start, 0
|
||||
}, func(started time.Time, duration float64) *time.Time {
|
||||
return end
|
||||
}); err != nil {
|
||||
return false, 0, priceds, nil, err
|
||||
return 0, priceds, nil, err
|
||||
}
|
||||
if _, priceds, err = plan[resources.ResourceInterface](tools.DATA_RESOURCE, instances, partnerships, buyings, strategies, bookingMode, wf, priceds, request,
|
||||
wf.Graph.IsData, func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64, error) {
|
||||
return start, 0, nil
|
||||
}, func(started time.Time, duration float64) (*time.Time, error) {
|
||||
return end, nil
|
||||
}); err != nil {
|
||||
return false, 0, priceds, nil, err
|
||||
}
|
||||
for k, f := range map[tools.DataType]func(graph.GraphItem) bool{tools.STORAGE_RESOURCE: wf.Graph.IsStorage,
|
||||
tools.COMPUTE_RESOURCE: wf.Graph.IsCompute} {
|
||||
if _, priceds, err = plan[resources.ResourceInterface](k, instances, partnerships, buyings, strategies, bookingMode, wf, priceds, request,
|
||||
f, func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64, error) {
|
||||
nearestStart, longestDuration, err := wf.Graph.GetAverageTimeRelatedToProcessingActivity(start, ps, res, func(i graph.GraphItem) (r resources.ResourceInterface) {
|
||||
for k, f := range map[tools.DataType]func(graph.GraphItem) bool{tools.STORAGE_RESOURCE: wf.Graph.IsStorage, tools.COMPUTE_RESOURCE: wf.Graph.IsCompute} {
|
||||
if _, priceds, err = plan[resources.ResourceInterface](k, wf, priceds, request, f,
|
||||
func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64) {
|
||||
nearestStart, longestDuration := wf.Graph.GetAverageTimeRelatedToProcessingActivity(start, ps, res, func(i graph.GraphItem) (r resources.ResourceInterface) {
|
||||
if f(i) {
|
||||
_, r = i.GetResource()
|
||||
}
|
||||
return r
|
||||
}, *instances.Get(res.GetID()), *partnerships.Get(res.GetID()),
|
||||
*buyings.Get(res.GetID()), *strategies.Get(res.GetID()), bookingMode, request)
|
||||
if err != nil {
|
||||
return start, longestDuration, err
|
||||
}
|
||||
return start.Add(time.Duration(nearestStart) * time.Second), longestDuration, nil
|
||||
}, func(started time.Time, duration float64) (*time.Time, error) {
|
||||
}, request)
|
||||
return start.Add(time.Duration(nearestStart) * time.Second), longestDuration
|
||||
}, func(started time.Time, duration float64) *time.Time {
|
||||
s := started.Add(time.Duration(duration))
|
||||
return &s, nil
|
||||
return &s
|
||||
}); err != nil {
|
||||
return false, 0, priceds, nil, err
|
||||
return 0, priceds, nil, err
|
||||
}
|
||||
}
|
||||
longest := common.GetPlannerLongestTime(end, priceds, request)
|
||||
if _, priceds, err = plan[resources.ResourceInterface](tools.WORKFLOW_RESOURCE, instances, partnerships, buyings, strategies,
|
||||
bookingMode, wf, priceds, request, wf.Graph.IsWorkflow,
|
||||
func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64, error) {
|
||||
if _, priceds, err = plan[resources.ResourceInterface](tools.WORKFLOW_RESOURCE, wf, priceds, request, wf.Graph.IsWorkflow,
|
||||
func(res resources.ResourceInterface, priced pricing.PricedItemITF) (time.Time, float64) {
|
||||
start := start.Add(time.Duration(common.GetPlannerNearestStart(start, priceds, request)) * time.Second)
|
||||
longest := float64(-1)
|
||||
r, code, err := res.GetAccessor(request).LoadOne(res.GetID())
|
||||
if code != 200 || err != nil {
|
||||
return start, longest, err
|
||||
return start, longest
|
||||
}
|
||||
_, neoLongest, priceds2, _, err := r.(*Workflow).Planify(start, end, instances, partnerships, buyings, strategies, bookingMode, request)
|
||||
// should ... import priced
|
||||
if err != nil {
|
||||
return start, longest, err
|
||||
if neoLongest, _, _, err := r.(*Workflow).Planify(start, end, request); err != nil {
|
||||
return start, longest
|
||||
} else if neoLongest > longest {
|
||||
longest = neoLongest
|
||||
}
|
||||
for k, v := range priceds2 {
|
||||
if priceds[k] == nil {
|
||||
priceds[k] = map[string]pricing.PricedItemITF{}
|
||||
}
|
||||
for k2, v2 := range v {
|
||||
if priceds[k][k2] != nil {
|
||||
v2.AddQuantity(priceds[k][k2].GetQuantity())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return start.Add(time.Duration(common.GetPlannerNearestStart(start, priceds, request)) * time.Second), longest, nil
|
||||
}, func(start time.Time, longest float64) (*time.Time, error) {
|
||||
return start.Add(time.Duration(common.GetPlannerNearestStart(start, priceds, request)) * time.Second), longest
|
||||
}, func(start time.Time, longest float64) *time.Time {
|
||||
s := start.Add(time.Duration(longest) * time.Second)
|
||||
return &s, nil
|
||||
return &s
|
||||
}); err != nil {
|
||||
return false, 0, priceds, nil, err
|
||||
return 0, priceds, nil, err
|
||||
}
|
||||
isPreemptible := true
|
||||
for _, first := range wf.GetFirstItems() {
|
||||
_, res := first.GetResource()
|
||||
if res.GetBookingModes()[booking.PREEMPTED] == nil {
|
||||
isPreemptible = false
|
||||
break
|
||||
}
|
||||
}
|
||||
return isPreemptible, longest, priceds, wf, nil
|
||||
return longest, priceds, wf, nil
|
||||
}
|
||||
|
||||
// Returns a map of DataType (processing,computing,data,storage,worfklow) where each resource (identified by its UUID)
|
||||
// is mapped to the list of its items (different appearance) in the graph
|
||||
// ex: if the same Minio storage is represented by several nodes in the graph, in [tools.STORAGE_RESSOURCE] its UUID will be mapped to
|
||||
// the list of GraphItem ID that correspond to the ID of each node
|
||||
func (w *Workflow) GetItemsByResources() map[tools.DataType]map[string][]string {
|
||||
res := make(map[tools.DataType]map[string][]string)
|
||||
dtMethodMap := map[tools.DataType]func() []graph.GraphItem{
|
||||
tools.STORAGE_RESOURCE: func() []graph.GraphItem { return w.GetGraphItems(w.Graph.IsStorage) },
|
||||
tools.DATA_RESOURCE: func() []graph.GraphItem { return w.GetGraphItems(w.Graph.IsData) },
|
||||
tools.COMPUTE_RESOURCE: func() []graph.GraphItem { return w.GetGraphItems(w.Graph.IsCompute) },
|
||||
tools.PROCESSING_RESOURCE: func() []graph.GraphItem { return w.GetGraphItems(w.Graph.IsProcessing) },
|
||||
tools.WORKFLOW_RESOURCE: func() []graph.GraphItem { return w.GetGraphItems(w.Graph.IsWorkflow) },
|
||||
}
|
||||
|
||||
for dt, meth := range dtMethodMap {
|
||||
res[dt] = make(map[string][]string)
|
||||
items := meth()
|
||||
for _, i := range items {
|
||||
_, r := i.GetResource()
|
||||
rId := r.GetID()
|
||||
res[dt][rId] = append(res[dt][rId], i.ID)
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func plan[T resources.ResourceInterface](
|
||||
dt tools.DataType, instances ConfigItem, partnerships ConfigItem, buyings ConfigItem, strategies ConfigItem, bookingMode int, wf *Workflow, priceds map[tools.DataType]map[string]pricing.PricedItemITF, request *tools.APIRequest,
|
||||
f func(graph.GraphItem) bool,
|
||||
start func(resources.ResourceInterface, pricing.PricedItemITF) (time.Time, float64, error),
|
||||
end func(time.Time, float64) (*time.Time, error)) ([]T, map[tools.DataType]map[string]pricing.PricedItemITF, error) {
|
||||
func plan[T resources.ResourceInterface](dt tools.DataType, wf *Workflow, priceds map[tools.DataType][]pricing.PricedItemITF, request *tools.APIRequest,
|
||||
f func(graph.GraphItem) bool, start func(resources.ResourceInterface, pricing.PricedItemITF) (time.Time, float64), end func(time.Time, float64) *time.Time) ([]T, map[tools.DataType][]pricing.PricedItemITF, error) {
|
||||
resources := []T{}
|
||||
for _, item := range wf.GetGraphItems(f) {
|
||||
if priceds[dt] == nil {
|
||||
priceds[dt] = map[string]pricing.PricedItemITF{}
|
||||
priceds[dt] = []pricing.PricedItemITF{}
|
||||
}
|
||||
dt, realItem := item.GetResource()
|
||||
if realItem == nil {
|
||||
return resources, priceds, errors.New("could not load the processing resource")
|
||||
}
|
||||
priced, err := realItem.ConvertToPricedResource(dt, instances.Get(realItem.GetID()),
|
||||
partnerships.Get(realItem.GetID()), buyings.Get(realItem.GetID()), strategies.Get(realItem.GetID()), &bookingMode, request)
|
||||
if err != nil {
|
||||
return resources, priceds, err
|
||||
}
|
||||
// Should be commented once the Pricing selection feature has been implemented, related to the commit d35ad440fa77763ec7f49ab34a85e47e75581b61
|
||||
// if priced.SelectPricing() == nil {
|
||||
// return resources, priceds, errors.New("no pricings are selected... can't proceed")
|
||||
// }
|
||||
started, duration, err := start(realItem, priced)
|
||||
if err != nil {
|
||||
return resources, priceds, err
|
||||
}
|
||||
priced := realItem.ConvertToPricedResource(dt, request)
|
||||
started, duration := start(realItem, priced)
|
||||
priced.SetLocationStart(started)
|
||||
if duration >= 0 {
|
||||
if e, err := end(started, duration); err == nil && e != nil {
|
||||
if e := end(started, duration); e != nil {
|
||||
priced.SetLocationEnd(*e)
|
||||
}
|
||||
}
|
||||
if e, err := end(started, priced.GetExplicitDurationInS()); err != nil && e != nil {
|
||||
if e := end(started, priced.GetExplicitDurationInS()); e != nil {
|
||||
priced.SetLocationEnd(*e)
|
||||
}
|
||||
resources = append(resources, realItem.(T))
|
||||
if priceds[dt][item.ID] != nil {
|
||||
priced.AddQuantity(priceds[dt][item.ID].GetQuantity())
|
||||
}
|
||||
priceds[dt][item.ID] = priced
|
||||
|
||||
priceds[dt] = append(priceds[dt], priced)
|
||||
}
|
||||
return resources, priceds, nil
|
||||
}
|
||||
|
||||
@@ -95,12 +95,12 @@ func (a *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.
|
||||
if set.(*Workflow).Graph != nil && set.(*Workflow).Graph.Partial {
|
||||
return nil, 403, errors.New("you are not allowed to update a partial workflow")
|
||||
}
|
||||
res, code, err := utils.GenericUpdateOne(set, id, a, &Workflow{})
|
||||
res, code, err := utils.GenericUpdateOne(a.verifyResource(set), id, a, &Workflow{})
|
||||
if code != 200 {
|
||||
return nil, code, err
|
||||
}
|
||||
workflow := res.(*Workflow)
|
||||
a.execute(workflow, false, true) // update the workspace for the workflow
|
||||
a.execute(workflow, false, false) // update the workspace for the workflow
|
||||
a.share(workflow, false, a.GetCaller()) // share the update to the peers
|
||||
return res, code, nil
|
||||
}
|
||||
@@ -119,19 +119,12 @@ func (a *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, i
|
||||
workflow := res.(*Workflow)
|
||||
|
||||
a.share(workflow, false, a.GetCaller()) // share the creation to the peers
|
||||
a.execute(workflow, false, true) // store the workspace for the workflow
|
||||
a.execute(workflow, false, false) // store the workspace for the workflow
|
||||
return res, code, nil
|
||||
}
|
||||
|
||||
// CopyOne copies a workflow in the database
|
||||
func (a *workflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
wf := data.(*Workflow)
|
||||
for _, item := range wf.Graph.Items {
|
||||
_, obj := item.GetResource()
|
||||
if obj != nil {
|
||||
obj.ClearEnv()
|
||||
}
|
||||
}
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
@@ -214,10 +207,10 @@ func (a *workflowMongoAccessor) verifyResource(obj utils.DBObject) utils.DBObjec
|
||||
} else if t == tools.DATA_RESOURCE {
|
||||
access = resources.NewAccessor[*resources.DataResource](t, a.GetRequest(), func() utils.DBObject { return &resources.DataResource{} })
|
||||
} else {
|
||||
wf.Graph.Clear(resource.GetID())
|
||||
wf.Graph.Clear(item.Data.GetID())
|
||||
}
|
||||
if error := utils.VerifyAccess(access, resource.GetID()); error != nil {
|
||||
wf.Graph.Clear(resource.GetID())
|
||||
wf.Graph.Clear(item.Data.GetID())
|
||||
}
|
||||
}
|
||||
return wf
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
package workflow_execution_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockAccessor struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockAccessor) LoadOne(id string) (interface{}, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return nil, args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) Search(filters interface{}, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(filters, search, isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func TestStoreDraftDefault(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
exec.StoreDraftDefault()
|
||||
assert.False(t, exec.IsDraft)
|
||||
assert.Equal(t, enum.SCHEDULED, exec.State)
|
||||
}
|
||||
|
||||
func TestCanUpdate_StateChange(t *testing.T) {
|
||||
existing := &workflow_execution.WorkflowExecution{State: enum.DRAFT}
|
||||
newExec := &workflow_execution.WorkflowExecution{State: enum.SCHEDULED}
|
||||
canUpdate, updated := existing.CanUpdate(newExec)
|
||||
assert.True(t, canUpdate)
|
||||
assert.Equal(t, enum.SCHEDULED, updated.(*workflow_execution.WorkflowExecution).State)
|
||||
}
|
||||
|
||||
func TestCanUpdate_SameState_Draft(t *testing.T) {
|
||||
existing := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}, State: enum.DRAFT}
|
||||
newExec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}, State: enum.DRAFT}
|
||||
canUpdate, _ := existing.CanUpdate(newExec)
|
||||
assert.False(t, canUpdate)
|
||||
}
|
||||
|
||||
func TestCanDelete_TrueIfDraft(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}}
|
||||
assert.True(t, exec.CanDelete())
|
||||
}
|
||||
|
||||
func TestCanDelete_FalseIfNotDraft(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: false}}
|
||||
assert.False(t, exec.CanDelete())
|
||||
}
|
||||
|
||||
func TestEquals_True(t *testing.T) {
|
||||
d := time.Now()
|
||||
exec1 := &workflow_execution.WorkflowExecution{ExecDate: d, WorkflowID: "123"}
|
||||
exec2 := &workflow_execution.WorkflowExecution{ExecDate: d, WorkflowID: "123"}
|
||||
assert.True(t, exec1.Equals(exec2))
|
||||
}
|
||||
|
||||
func TestEquals_False(t *testing.T) {
|
||||
exec1 := &workflow_execution.WorkflowExecution{ExecDate: time.Now(), WorkflowID: "abc"}
|
||||
exec2 := &workflow_execution.WorkflowExecution{ExecDate: time.Now().Add(time.Hour), WorkflowID: "def"}
|
||||
assert.False(t, exec1.Equals(exec2))
|
||||
}
|
||||
|
||||
func TestArgoStatusToState_Success(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
exec.ArgoStatusToState("succeeded")
|
||||
assert.Equal(t, enum.SUCCESS, exec.State)
|
||||
}
|
||||
|
||||
func TestArgoStatusToState_DefaultToFailure(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
exec.ArgoStatusToState("unknown")
|
||||
assert.Equal(t, enum.FAILURE, exec.State)
|
||||
}
|
||||
|
||||
func TestGenerateID_AssignsUUID(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
exec.GenerateID()
|
||||
assert.NotEmpty(t, exec.UUID)
|
||||
}
|
||||
|
||||
func TestGetName_ReturnsCorrectFormat(t *testing.T) {
|
||||
time := time.Now()
|
||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{UUID: "abc"}, ExecDate: time}
|
||||
assert.Contains(t, exec.GetName(), "abc")
|
||||
assert.Contains(t, exec.GetName(), time.String())
|
||||
}
|
||||
|
||||
func TestVerifyAuth_AlwaysTrue(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
assert.True(t, exec.VerifyAuth("get", nil))
|
||||
}
|
||||
|
||||
func TestUpdateOne_RejectsZeroState(t *testing.T) {
|
||||
accessor := &workflow_execution.WorkflowExecutionMongoAccessor{}
|
||||
set := &workflow_execution.WorkflowExecution{State: 0}
|
||||
_, code, err := accessor.UpdateOne(set, "someID")
|
||||
assert.Equal(t, 400, code)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestLoadOne_DraftExpired_ShouldDelete(t *testing.T) {
|
||||
// Normally would mock time.Now and delete call; for now we test structure
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
exec := &workflow_execution.WorkflowExecution{
|
||||
ExecDate: time.Now().Add(-2 * time.Minute),
|
||||
State: enum.DRAFT,
|
||||
AbstractObject: utils.AbstractObject{UUID: "to-delete"},
|
||||
}
|
||||
_, _, _ = accessor.LoadOne(exec.GetID())
|
||||
// No panic = good enough placeholder
|
||||
}
|
||||
|
||||
func TestLoadOne_ScheduledExpired_ShouldUpdateToForgotten(t *testing.T) {
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
exec := &workflow_execution.WorkflowExecution{
|
||||
ExecDate: time.Now().Add(-2 * time.Minute),
|
||||
State: enum.SCHEDULED,
|
||||
AbstractObject: utils.AbstractObject{UUID: "to-forget"},
|
||||
}
|
||||
_, _, _ = accessor.LoadOne(exec.GetID())
|
||||
}
|
||||
|
||||
func TestDeleteOne_NotImplemented(t *testing.T) {
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
_, code, err := accessor.DeleteOne("someID")
|
||||
assert.Equal(t, 404, code)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestStoreOne_NotImplemented(t *testing.T) {
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
_, code, err := accessor.StoreOne(nil)
|
||||
assert.Equal(t, 404, code)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestCopyOne_NotImplemented(t *testing.T) {
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
_, code, err := accessor.CopyOne(nil)
|
||||
assert.Equal(t, 404, code)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetExecFilters_BasicPattern(t *testing.T) {
|
||||
a := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
filters := a.GetExecFilters("foo")
|
||||
assert.Contains(t, filters.Or["abstractobject.name"][0].Value, "foo")
|
||||
}
|
||||
142
models/workflow_execution/workflow_execution.go
Executable file → Normal file
142
models/workflow_execution/workflow_execution.go
Executable file → Normal file
@@ -1,7 +1,6 @@
|
||||
package workflow_execution
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -9,7 +8,6 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"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/resources/purchase_resource"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/google/uuid"
|
||||
@@ -17,43 +15,39 @@ import (
|
||||
)
|
||||
|
||||
/*
|
||||
* WorkflowExecution is a struct that represents a list of workflow executions
|
||||
* WorkflowExecutions is a struct that represents a list of workflow executions
|
||||
* Warning: No user can write (del, post, put) a workflow execution, it is only used by the system
|
||||
* workflows generate their own executions
|
||||
*/
|
||||
type WorkflowExecution struct {
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
Priority int `json:"priority" bson:"priority"` // will best effort on default // Will Best Effort on priority
|
||||
PeerBuyByGraph map[string]map[string][]string `json:"peer_buy_by_graph,omitempty" bson:"peer_buy_by_graph,omitempty"` // BookByResource is a map of the resource id and the list of the booking id
|
||||
PeerBookByGraph map[string]map[string][]string `json:"peer_book_by_graph,omitempty" bson:"peer_book_by_graph,omitempty"` // BookByResource is a map of the resource id and the list of the booking id
|
||||
ExecutionsID string `json:"executions_id,omitempty" bson:"executions_id,omitempty"`
|
||||
ExecDate time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"` // ExecDate is the execution date of the workflow, is required
|
||||
EndDate *time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"` // EndDate is the end date of the workflow
|
||||
State enum.BookingStatus `json:"state" bson:"state" default:"0"` // TEMPORARY TODO DEFAULT 1 -> 0 State is the state of the workflow
|
||||
WorkflowID string `json:"workflow_id" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
|
||||
type WorkflowExecutions struct {
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
ExecDate time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"` // ExecDate is the execution date of the workflow, is required
|
||||
EndDate *time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"` // EndDate is the end date of the workflow
|
||||
State enum.BookingStatus `json:"state" bson:"state" default:"0"` // TEMPORARY TODO DEFAULT 1 -> 0 State is the state of the workflow
|
||||
WorkflowID string `json:"workflow_id" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
|
||||
}
|
||||
|
||||
func (r *WorkflowExecution) StoreDraftDefault() {
|
||||
r.IsDraft = false // TODO: TEMPORARY
|
||||
r.State = enum.SCHEDULED
|
||||
func (r *WorkflowExecutions) StoreDraftDefault() {
|
||||
r.IsDraft = true // TODO: TEMPORARY
|
||||
r.State = enum.DRAFT
|
||||
}
|
||||
|
||||
func (r *WorkflowExecution) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||
if r.State != set.(*WorkflowExecution).State {
|
||||
return true, &WorkflowExecution{State: set.(*WorkflowExecution).State} // only state can be updated
|
||||
func (r *WorkflowExecutions) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||
if r.State != set.(*WorkflowExecutions).State {
|
||||
return true, &WorkflowExecutions{State: set.(*WorkflowExecutions).State} // only state can be updated
|
||||
}
|
||||
return !r.IsDraft, set // only draft buying can be updated
|
||||
}
|
||||
|
||||
func (r *WorkflowExecution) CanDelete() bool {
|
||||
func (r *WorkflowExecutions) CanDelete() bool {
|
||||
return r.IsDraft // only draft bookings can be deleted
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecution) Equals(we *WorkflowExecution) bool {
|
||||
func (wfa *WorkflowExecutions) Equals(we *WorkflowExecutions) bool {
|
||||
return wfa.ExecDate.Equal(we.ExecDate) && wfa.WorkflowID == we.WorkflowID
|
||||
}
|
||||
|
||||
func (ws *WorkflowExecution) PurgeDraft(request *tools.APIRequest) error {
|
||||
func (ws *WorkflowExecutions) PurgeDraft(request *tools.APIRequest) error {
|
||||
if ws.EndDate == nil {
|
||||
// if no end... then Book like a savage
|
||||
e := ws.ExecDate.Add(time.Hour)
|
||||
@@ -80,7 +74,7 @@ func (ws *WorkflowExecution) PurgeDraft(request *tools.APIRequest) error {
|
||||
}
|
||||
|
||||
// tool to transform the argo status to a state
|
||||
func (wfa *WorkflowExecution) ArgoStatusToState(status string) *WorkflowExecution {
|
||||
func (wfa *WorkflowExecutions) ArgoStatusToState(status string) *WorkflowExecutions {
|
||||
status = strings.ToLower(status)
|
||||
switch status {
|
||||
case "succeeded": // Succeeded
|
||||
@@ -95,116 +89,38 @@ func (wfa *WorkflowExecution) ArgoStatusToState(status string) *WorkflowExecutio
|
||||
return wfa
|
||||
}
|
||||
|
||||
func (r *WorkflowExecution) GenerateID() {
|
||||
if r.UUID == "" {
|
||||
r.UUID = uuid.New().String()
|
||||
}
|
||||
func (r *WorkflowExecutions) GenerateID() {
|
||||
r.UUID = uuid.New().String()
|
||||
}
|
||||
|
||||
func (d *WorkflowExecution) GetName() string {
|
||||
func (d *WorkflowExecutions) GetName() string {
|
||||
return d.UUID + "_" + d.ExecDate.String()
|
||||
}
|
||||
|
||||
func (d *WorkflowExecution) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
func (d *WorkflowExecutions) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
func (d *WorkflowExecution) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
func (d *WorkflowExecutions) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
/*
|
||||
booking is an activity reserved for not a long time investment.
|
||||
... purchase is dependant of a one time buying.
|
||||
use of a datacenter or storage can't be buy for permanent access.
|
||||
*/
|
||||
func (d *WorkflowExecution) Buy(bs pricing.BillingStrategy, executionsID string, wfID string, priceds map[tools.DataType]map[string]pricing.PricedItemITF) []*purchase_resource.PurchaseResource {
|
||||
purchases := d.buyEach(bs, executionsID, wfID, tools.PROCESSING_RESOURCE, priceds[tools.PROCESSING_RESOURCE])
|
||||
purchases = append(purchases, d.buyEach(bs, executionsID, wfID, tools.DATA_RESOURCE, priceds[tools.DATA_RESOURCE])...)
|
||||
return purchases
|
||||
}
|
||||
|
||||
func (d *WorkflowExecution) buyEach(bs pricing.BillingStrategy, executionsID string, wfID string, dt tools.DataType, priceds map[string]pricing.PricedItemITF) []*purchase_resource.PurchaseResource {
|
||||
items := []*purchase_resource.PurchaseResource{}
|
||||
for itemID, priced := range priceds {
|
||||
if !priced.IsPurchasable() || bs != pricing.BILL_ONCE { // buy only that must be buy
|
||||
continue
|
||||
}
|
||||
if d.PeerBuyByGraph == nil {
|
||||
d.PeerBuyByGraph = map[string]map[string][]string{}
|
||||
}
|
||||
if d.PeerBuyByGraph[priced.GetCreatorID()] == nil {
|
||||
d.PeerBuyByGraph[priced.GetCreatorID()] = map[string][]string{}
|
||||
}
|
||||
if d.PeerBuyByGraph[priced.GetCreatorID()][itemID] == nil {
|
||||
d.PeerBuyByGraph[priced.GetCreatorID()][itemID] = []string{}
|
||||
}
|
||||
start := d.ExecDate
|
||||
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: m,
|
||||
ExecutionsID: executionsID,
|
||||
DestPeerID: priced.GetCreatorID(),
|
||||
ResourceID: priced.GetID(),
|
||||
ResourceType: dt,
|
||||
EndDate: &end,
|
||||
}
|
||||
items = append(items, bookingItem)
|
||||
d.PeerBuyByGraph[priced.GetCreatorID()][itemID] = append(
|
||||
d.PeerBuyByGraph[priced.GetCreatorID()][itemID], bookingItem.GetID())
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func (d *WorkflowExecution) Book(executionsID string, wfID string, priceds map[tools.DataType]map[string]pricing.PricedItemITF) []*booking.Booking {
|
||||
booking := d.bookEach(executionsID, wfID, tools.STORAGE_RESOURCE, priceds[tools.STORAGE_RESOURCE])
|
||||
booking = append(booking, d.bookEach(executionsID, wfID, tools.PROCESSING_RESOURCE, priceds[tools.PROCESSING_RESOURCE])...)
|
||||
booking = append(booking, d.bookEach(executionsID, wfID, tools.COMPUTE_RESOURCE, priceds[tools.COMPUTE_RESOURCE])...)
|
||||
booking = append(booking, d.bookEach(executionsID, wfID, tools.DATA_RESOURCE, priceds[tools.DATA_RESOURCE])...)
|
||||
func (d *WorkflowExecutions) Book(wfID string, priceds map[tools.DataType][]pricing.PricedItemITF) []*booking.Booking {
|
||||
booking := d.bookEach(wfID, tools.STORAGE_RESOURCE, priceds[tools.STORAGE_RESOURCE])
|
||||
booking = append(booking, d.bookEach(wfID, tools.PROCESSING_RESOURCE, priceds[tools.PROCESSING_RESOURCE])...)
|
||||
return booking
|
||||
}
|
||||
|
||||
func (d *WorkflowExecution) bookEach(executionsID string, wfID string, dt tools.DataType, priceds map[string]pricing.PricedItemITF) []*booking.Booking {
|
||||
func (d *WorkflowExecutions) bookEach(wfID string, dt tools.DataType, priceds []pricing.PricedItemITF) []*booking.Booking {
|
||||
items := []*booking.Booking{}
|
||||
for itemID, priced := range priceds {
|
||||
if !priced.IsBooked() { // book only that must be booked
|
||||
continue
|
||||
}
|
||||
if d.PeerBookByGraph == nil {
|
||||
d.PeerBookByGraph = map[string]map[string][]string{}
|
||||
}
|
||||
if d.PeerBookByGraph[priced.GetCreatorID()] == nil {
|
||||
d.PeerBookByGraph[priced.GetCreatorID()] = map[string][]string{}
|
||||
}
|
||||
if d.PeerBookByGraph[priced.GetCreatorID()][itemID] == nil {
|
||||
d.PeerBookByGraph[priced.GetCreatorID()][itemID] = []string{}
|
||||
}
|
||||
for _, priced := range priceds {
|
||||
start := d.ExecDate
|
||||
if s := priced.GetLocationStart(); s != nil {
|
||||
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: m,
|
||||
ExecutionsID: executionsID,
|
||||
State: enum.SCHEDULED,
|
||||
State: enum.DRAFT,
|
||||
ResourceID: priced.GetID(),
|
||||
ResourceType: dt,
|
||||
DestPeerID: priced.GetCreatorID(),
|
||||
@@ -214,8 +130,6 @@ func (d *WorkflowExecution) bookEach(executionsID string, wfID string, dt tools.
|
||||
ExpectedEndDate: &end,
|
||||
}
|
||||
items = append(items, bookingItem)
|
||||
d.PeerBookByGraph[priced.GetCreatorID()][itemID] = append(
|
||||
d.PeerBookByGraph[priced.GetCreatorID()][itemID], bookingItem.GetID())
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
56
models/workflow_execution/workflow_execution_mongo_accessor.go
Executable file → Normal file
56
models/workflow_execution/workflow_execution_mongo_accessor.go
Executable file → Normal file
@@ -11,13 +11,13 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type WorkflowExecutionMongoAccessor struct {
|
||||
type workflowExecutionMongoAccessor struct {
|
||||
utils.AbstractAccessor
|
||||
shallow bool
|
||||
}
|
||||
|
||||
func newShallowAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
|
||||
return &WorkflowExecutionMongoAccessor{
|
||||
func newShallowAccessor(request *tools.APIRequest) *workflowExecutionMongoAccessor {
|
||||
return &workflowExecutionMongoAccessor{
|
||||
shallow: true,
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
|
||||
@@ -27,8 +27,8 @@ func newShallowAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccess
|
||||
}
|
||||
}
|
||||
|
||||
func NewAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
|
||||
return &WorkflowExecutionMongoAccessor{
|
||||
func NewAccessor(request *tools.APIRequest) *workflowExecutionMongoAccessor {
|
||||
return &workflowExecutionMongoAccessor{
|
||||
shallow: false,
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
|
||||
@@ -38,60 +38,56 @@ func NewAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
|
||||
}
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
func (wfa *workflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return nil, 404, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
if set.(*WorkflowExecution).State == 0 {
|
||||
func (wfa *workflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
if set.(*WorkflowExecutions).State == 0 {
|
||||
return nil, 400, errors.New("state is required")
|
||||
}
|
||||
realSet := WorkflowExecution{State: set.(*WorkflowExecution).State}
|
||||
return utils.GenericUpdateOne(&realSet, id, wfa, &WorkflowExecution{})
|
||||
realSet := WorkflowExecutions{State: set.(*WorkflowExecutions).State}
|
||||
return utils.GenericUpdateOne(&realSet, id, wfa, &WorkflowExecutions{})
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
func (wfa *workflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return nil, 404, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
func (wfa *workflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return nil, 404, errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (a *WorkflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*WorkflowExecution](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
now := time.Now()
|
||||
now = now.Add(time.Second * -60)
|
||||
if d.(*WorkflowExecution).State == enum.DRAFT && !a.shallow && now.UTC().After(d.(*WorkflowExecution).ExecDate) {
|
||||
func (a *workflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*WorkflowExecutions](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
if d.(*WorkflowExecutions).State == enum.DRAFT && !a.shallow && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
|
||||
utils.GenericDeleteOne(d.GetID(), newShallowAccessor(a.Request))
|
||||
return nil, 404, errors.New("not found")
|
||||
}
|
||||
if d.(*WorkflowExecution).State == enum.SCHEDULED && !a.shallow && now.UTC().After(d.(*WorkflowExecution).ExecDate) {
|
||||
d.(*WorkflowExecution).State = enum.FORGOTTEN
|
||||
if d.(*WorkflowExecutions).State == enum.SCHEDULED && !a.shallow && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
|
||||
d.(*WorkflowExecutions).State = enum.FORGOTTEN
|
||||
utils.GenericRawUpdateOne(d, id, newShallowAccessor(a.Request))
|
||||
}
|
||||
return d, 200, nil
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *WorkflowExecutionMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*WorkflowExecution](a.getExec(), isDraft, a)
|
||||
func (a *workflowExecutionMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*WorkflowExecutions](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *WorkflowExecutionMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*WorkflowExecution](filters, search, a.GetExecFilters(search), a.getExec(), isDraft, a)
|
||||
func (a *workflowExecutionMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*WorkflowExecutions](filters, search, a.GetExecFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *WorkflowExecutionMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
func (a *workflowExecutionMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
now := time.Now()
|
||||
now = now.Add(time.Second * -60)
|
||||
if d.(*WorkflowExecution).State == enum.DRAFT && now.UTC().After(d.(*WorkflowExecution).ExecDate) {
|
||||
if d.(*WorkflowExecutions).State == enum.DRAFT && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
|
||||
utils.GenericDeleteOne(d.GetID(), newShallowAccessor(a.Request))
|
||||
return nil
|
||||
}
|
||||
if d.(*WorkflowExecution).State == enum.SCHEDULED && now.UTC().After(d.(*WorkflowExecution).ExecDate) {
|
||||
d.(*WorkflowExecution).State = enum.FORGOTTEN
|
||||
if d.(*WorkflowExecutions).State == enum.SCHEDULED && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
|
||||
d.(*WorkflowExecutions).State = enum.FORGOTTEN
|
||||
utils.GenericRawUpdateOne(d, d.GetID(), newShallowAccessor(a.Request))
|
||||
return d
|
||||
}
|
||||
@@ -99,7 +95,7 @@ func (a *WorkflowExecutionMongoAccessor) getExec() func(utils.DBObject) utils.Sh
|
||||
}
|
||||
}
|
||||
|
||||
func (a *WorkflowExecutionMongoAccessor) GetExecFilters(search string) *dbs.Filters {
|
||||
func (a *workflowExecutionMongoAccessor) GetExecFilters(search string) *dbs.Filters {
|
||||
return &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
|
||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search + "_execution"}},
|
||||
|
||||
201
models/workflow_execution/workflow_scheduler.go
Normal file
201
models/workflow_execution/workflow_scheduler.go
Normal file
@@ -0,0 +1,201 @@
|
||||
package workflow_execution
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/robfig/cron"
|
||||
)
|
||||
|
||||
/*
|
||||
* WorkflowSchedule is a struct that contains the scheduling information of a workflow
|
||||
* It contains the mode of the schedule (Task or Service), the name of the schedule, the start and end time of the schedule and the cron expression
|
||||
*/
|
||||
// it's a flying object only use in a session time. It's not stored in the database
|
||||
type WorkflowSchedule struct {
|
||||
Workflow *workflow.Workflow `json:"workflow,omitempty"` // Workflow is the workflow dependancy of the schedule
|
||||
WorkflowExecutions []*WorkflowExecutions `json:"workflow_executions,omitempty"` // WorkflowExecutions is the list of executions of the workflow
|
||||
Message string `json:"message,omitempty"` // Message is the message of the schedule
|
||||
Warning string `json:"warning,omitempty"` // Warning is the warning message of the schedule
|
||||
Start time.Time `json:"start" validate:"required,ltfield=End"` // Start is the start time of the schedule, is required and must be less than the End time
|
||||
End *time.Time `json:"end,omitempty"` // End is the end time of the schedule, is required and must be greater than the Start time
|
||||
DurationS float64 `json:"duration_s" default:"-1"` // End is the end time of the schedule
|
||||
Cron string `json:"cron,omitempty"` // here the cron format : ss mm hh dd MM dw task
|
||||
}
|
||||
|
||||
func NewScheduler(start string, end string, durationInS float64, cron string) *WorkflowSchedule {
|
||||
s, err := time.Parse("2006-01-02T15:04:05", start)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
ws := &WorkflowSchedule{
|
||||
Start: s,
|
||||
DurationS: durationInS,
|
||||
Cron: cron,
|
||||
}
|
||||
e, err := time.Parse("2006-01-02T15:04:05", end)
|
||||
if err == nil {
|
||||
ws.End = &e
|
||||
}
|
||||
return ws
|
||||
}
|
||||
|
||||
func (ws *WorkflowSchedule) CheckBooking(wfID string, request *tools.APIRequest) (bool, *workflow.Workflow, []*WorkflowExecutions, error) {
|
||||
if request.Caller == nil && request.Caller.URLS == nil && request.Caller.URLS[tools.BOOKING] == nil || request.Caller.URLS[tools.BOOKING][tools.GET] == "" {
|
||||
return false, nil, []*WorkflowExecutions{}, errors.New("no caller defined")
|
||||
}
|
||||
access := workflow.NewAccessor(request)
|
||||
res, code, err := access.LoadOne(wfID)
|
||||
if code != 200 {
|
||||
return false, nil, []*WorkflowExecutions{}, errors.New("could not load the workflow with id: " + err.Error())
|
||||
}
|
||||
wf := res.(*workflow.Workflow)
|
||||
longest, priceds, wf, err := wf.Planify(ws.Start, ws.End, request)
|
||||
if err != nil {
|
||||
return false, wf, []*WorkflowExecutions{}, err
|
||||
}
|
||||
ws.DurationS = longest
|
||||
ws.Message = "We estimate that the workflow will start at " + ws.Start.String() + " and last " + fmt.Sprintf("%v", ws.DurationS) + " seconds."
|
||||
if ws.End != nil && ws.Start.Add(time.Duration(longest)*time.Second).After(*ws.End) {
|
||||
ws.Warning = "The workflow may be too long to be executed in the given time frame, we will try to book it anyway\n"
|
||||
}
|
||||
execs, err := ws.getExecutions(wf)
|
||||
if err != nil {
|
||||
return false, wf, []*WorkflowExecutions{}, err
|
||||
}
|
||||
for _, exec := range execs {
|
||||
bookings := exec.Book(wfID, priceds)
|
||||
for _, b := range bookings {
|
||||
meth := request.Caller.URLS[tools.BOOKING][tools.GET]
|
||||
meth = strings.ReplaceAll(meth, ":id", b.ResourceID)
|
||||
meth = strings.ReplaceAll(meth, ":start_date", b.ExpectedStartDate.Format("2006-01-02T15:04:05"))
|
||||
meth = strings.ReplaceAll(meth, ":end_date", b.ExpectedEndDate.Format("2006-01-02T15:04:05"))
|
||||
request.Caller.URLS[tools.BOOKING][tools.GET] = meth
|
||||
_, err := (&peer.Peer{}).LaunchPeerExecution(b.DestPeerID, b.ResourceID, tools.BOOKING, tools.GET, nil, request.Caller)
|
||||
if err != nil {
|
||||
return false, wf, execs, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, wf, execs, nil
|
||||
}
|
||||
|
||||
func (ws *WorkflowSchedule) Schedules(wfID string, request *tools.APIRequest) (*WorkflowSchedule, *workflow.Workflow, []*WorkflowExecutions, error) {
|
||||
if request == nil {
|
||||
return ws, nil, []*WorkflowExecutions{}, errors.New("no request found")
|
||||
}
|
||||
c := request.Caller
|
||||
if c == nil || c.URLS == nil || c.URLS[tools.BOOKING] == nil {
|
||||
return ws, nil, []*WorkflowExecutions{}, errors.New("no caller defined")
|
||||
}
|
||||
methods := c.URLS[tools.BOOKING]
|
||||
if _, ok := methods[tools.GET]; !ok {
|
||||
return ws, nil, []*WorkflowExecutions{}, errors.New("no path found")
|
||||
}
|
||||
ok, wf, executions, err := ws.CheckBooking(wfID, request)
|
||||
if !ok || err != nil {
|
||||
return ws, nil, []*WorkflowExecutions{}, errors.New("could not book the workflow : " + fmt.Sprintf("%v", err))
|
||||
}
|
||||
|
||||
ws.Workflow = wf
|
||||
ws.WorkflowExecutions = executions
|
||||
|
||||
for _, exec := range executions {
|
||||
err := exec.PurgeDraft(request)
|
||||
if err != nil {
|
||||
return ws, nil, []*WorkflowExecutions{}, errors.New("purge draft" + fmt.Sprintf("%v", err))
|
||||
}
|
||||
exec.GenerateID()
|
||||
exec.StoreDraftDefault()
|
||||
// Should DELETE the previous execution2
|
||||
utils.GenericStoreOne(exec, NewAccessor(request))
|
||||
}
|
||||
return ws, wf, executions, nil
|
||||
}
|
||||
|
||||
/*
|
||||
BOOKING IMPLIED TIME, not of subscription but of execution
|
||||
so is processing time execution time applied on computes
|
||||
data can improve the processing time
|
||||
time should implied a security time border (10sec) if not from the same executions
|
||||
VERIFY THAT WE HANDLE DIFFERENCE BETWEEN LOCATION TIME && BOOKING
|
||||
*/
|
||||
|
||||
/*
|
||||
* getExecutions is a function that returns the executions of a workflow
|
||||
* it returns an array of workflow_execution.WorkflowExecution
|
||||
*/
|
||||
func (ws *WorkflowSchedule) getExecutions(workflow *workflow.Workflow) ([]*WorkflowExecutions, error) {
|
||||
workflows_executions := []*WorkflowExecutions{}
|
||||
dates, err := ws.getDates()
|
||||
if err != nil {
|
||||
return workflows_executions, err
|
||||
}
|
||||
for _, date := range dates {
|
||||
obj := &WorkflowExecutions{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
Name: workflow.Name + "_execution_" + date.Start.String(), // set the name of the execution
|
||||
},
|
||||
ExecDate: date.Start, // set the execution date
|
||||
EndDate: date.End, // set the end date
|
||||
State: enum.DRAFT, // set the state to 1 (scheduled)
|
||||
WorkflowID: workflow.GetID(), // set the workflow id dependancy of the execution
|
||||
}
|
||||
workflows_executions = append(workflows_executions, obj)
|
||||
}
|
||||
return workflows_executions, nil
|
||||
}
|
||||
|
||||
func (ws *WorkflowSchedule) getDates() ([]Schedule, error) {
|
||||
schedule := []Schedule{}
|
||||
if len(ws.Cron) > 0 { // if cron is set then end date should be set
|
||||
if ws.End == nil {
|
||||
return schedule, errors.New("a cron task should have an end date")
|
||||
}
|
||||
if ws.DurationS <= 0 {
|
||||
ws.DurationS = ws.End.Sub(ws.Start).Seconds()
|
||||
}
|
||||
cronStr := strings.Split(ws.Cron, " ") // split the cron string to treat it
|
||||
if len(cronStr) < 6 { // if the cron string is less than 6 fields, return an error because format is : ss mm hh dd MM dw (6 fields)
|
||||
return schedule, errors.New("Bad cron message: (" + ws.Cron + "). Should be at least ss mm hh dd MM dw")
|
||||
}
|
||||
subCron := strings.Join(cronStr[:6], " ")
|
||||
// cron should be parsed as ss mm hh dd MM dw t (min 6 fields)
|
||||
specParser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow) // create a new cron parser
|
||||
sched, err := specParser.Parse(subCron) // parse the cron string
|
||||
if err != nil {
|
||||
return schedule, errors.New("Bad cron message: " + err.Error())
|
||||
}
|
||||
// loop through the cron schedule to set the executions
|
||||
for s := sched.Next(ws.Start); !s.IsZero() && s.Before(*ws.End); s = sched.Next(s) {
|
||||
e := s.Add(time.Duration(ws.DurationS) * time.Second)
|
||||
schedule = append(schedule, Schedule{
|
||||
Start: s,
|
||||
End: &e,
|
||||
})
|
||||
}
|
||||
} else { // if no cron, set the execution to the start date
|
||||
schedule = append(schedule, Schedule{
|
||||
Start: ws.Start,
|
||||
End: ws.End,
|
||||
})
|
||||
}
|
||||
return schedule, nil
|
||||
}
|
||||
|
||||
type Schedule struct {
|
||||
Start time.Time
|
||||
End *time.Time
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO : LARGEST GRAIN PLANIFYING THE WORKFLOW WHEN OPTION IS SET
|
||||
* SET PROTECTION BORDER TIME
|
||||
*/
|
||||
@@ -1,215 +0,0 @@
|
||||
// File: workspace_accessor_test.go
|
||||
|
||||
package workspace_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workspace"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockWorkspaceAccessor struct {
|
||||
mock.Mock
|
||||
workspace.Workspace
|
||||
}
|
||||
|
||||
func (m *MockWorkspaceAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
args := m.Called(data)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockWorkspaceAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(set, id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockWorkspaceAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockWorkspaceAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockWorkspaceAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockWorkspaceAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(filters, search, isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func TestStoreOne_Success(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
ws := &workspace.Workspace{AbstractObject: utils.AbstractObject{Name: "test_ws"}}
|
||||
mockAcc.On("StoreOne", ws).Return(ws, 200, nil)
|
||||
|
||||
res, code, err := mockAcc.StoreOne(ws)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, ws, res)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestStoreOne_Conflict(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
ws := &workspace.Workspace{AbstractObject: utils.AbstractObject{Name: "duplicate"}}
|
||||
mockAcc.On("StoreOne", ws).Return(nil, 409, errors.New("a workspace with the same name already exists"))
|
||||
|
||||
res, code, err := mockAcc.StoreOne(ws)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 409, code)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func TestUpdateOne_Success(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
ws := &workspace.Workspace{AbstractObject: utils.AbstractObject{UUID: "123", IsDraft: false}}
|
||||
mockAcc.On("UpdateOne", ws, "123").Return(ws, 200, nil)
|
||||
|
||||
res, code, err := mockAcc.UpdateOne(ws, "123")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, ws, res)
|
||||
}
|
||||
|
||||
func TestUpdateOne_Error(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
ws := &workspace.Workspace{AbstractObject: utils.AbstractObject{UUID: "999"}}
|
||||
err := errors.New("update failed")
|
||||
mockAcc.On("UpdateOne", ws, "999").Return(nil, 500, err)
|
||||
|
||||
res, code, err := mockAcc.UpdateOne(ws, "999")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 500, code)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func TestDeleteOne_Success(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
ws := &workspace.Workspace{AbstractObject: utils.AbstractObject{UUID: "321"}}
|
||||
mockAcc.On("DeleteOne", "321").Return(ws, 200, nil)
|
||||
|
||||
res, code, err := mockAcc.DeleteOne("321")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, ws, res)
|
||||
}
|
||||
|
||||
func TestDeleteOne_NotFound(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
err := errors.New("not found")
|
||||
mockAcc.On("DeleteOne", "notfound").Return(nil, 404, err)
|
||||
|
||||
res, code, err := mockAcc.DeleteOne("notfound")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 404, code)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func TestLoadOne_Success(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
ws := &workspace.Workspace{AbstractObject: utils.AbstractObject{UUID: "loadid"}}
|
||||
mockAcc.On("LoadOne", "loadid").Return(ws, 200, nil)
|
||||
|
||||
res, code, err := mockAcc.LoadOne("loadid")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, ws, res)
|
||||
}
|
||||
|
||||
func TestLoadOne_Error(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
err := errors.New("db error")
|
||||
mockAcc.On("LoadOne", "badid").Return(nil, 500, err)
|
||||
|
||||
res, code, err := mockAcc.LoadOne("badid")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 500, code)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func TestLoadAll_Success(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
ws := &workspace.Workspace{AbstractObject: utils.AbstractObject{UUID: "all1"}}
|
||||
mockAcc.On("LoadAll", true).Return([]utils.ShallowDBObject{ws}, 200, nil)
|
||||
|
||||
res, code, err := mockAcc.LoadAll(true)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Len(t, res, 1)
|
||||
}
|
||||
|
||||
func TestLoadAll_Empty(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
mockAcc.On("LoadAll", false).Return([]utils.ShallowDBObject{}, 200, nil)
|
||||
|
||||
res, code, err := mockAcc.LoadAll(false)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Empty(t, res)
|
||||
}
|
||||
|
||||
func TestSearch_Success(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
filters := &dbs.Filters{}
|
||||
mockAcc.On("Search", filters, "keyword", true).Return([]utils.ShallowDBObject{}, 200, nil)
|
||||
|
||||
res, code, err := mockAcc.Search(filters, "keyword", true)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.NotNil(t, res)
|
||||
}
|
||||
|
||||
func TestSearch_Error(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
filters := &dbs.Filters{}
|
||||
err := errors.New("search failed")
|
||||
mockAcc.On("Search", filters, "fail", false).Return(nil, 500, err)
|
||||
|
||||
res, code, err := mockAcc.Search(filters, "fail", false)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 500, code)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
// Additional edge test cases
|
||||
|
||||
func TestStoreOne_InvalidType(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
mockAcc.On("StoreOne", mock.Anything).Return(nil, 400, errors.New("invalid type"))
|
||||
|
||||
res, code, err := mockAcc.StoreOne(&utils.AbstractObject{})
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 400, code)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func TestUpdateOne_NilData(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
mockAcc.On("UpdateOne", nil, "id").Return(nil, 400, errors.New("nil data"))
|
||||
|
||||
res, code, err := mockAcc.UpdateOne(nil, "id")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 400, code)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func TestDeleteOne_NilID(t *testing.T) {
|
||||
mockAcc := new(MockWorkspaceAccessor)
|
||||
mockAcc.On("DeleteOne", "").Return(nil, 400, errors.New("missing ID"))
|
||||
|
||||
res, code, err := mockAcc.DeleteOne("")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 400, code)
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
@@ -20,13 +20,13 @@ func (d *Workspace) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
func (ao *Workspace) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
func (ao *Workspace) VerifyAuth(request *tools.APIRequest) bool {
|
||||
if ao.Shared != "" {
|
||||
shared, code, _ := shallow_collaborative_area.NewAccessor(request).LoadOne(ao.Shared)
|
||||
if code != 200 || shared == nil {
|
||||
return false
|
||||
}
|
||||
return shared.VerifyAuth(callName, request)
|
||||
return shared.VerifyAuth(request)
|
||||
}
|
||||
return ao.AbstractObject.VerifyAuth(callName, request)
|
||||
return ao.AbstractObject.VerifyAuth(request)
|
||||
}
|
||||
|
||||
@@ -72,11 +72,9 @@ func (a *workspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils
|
||||
func (a *workspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
filters := &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: data.GetName() + "_workspace"}},
|
||||
"abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: a.GetPeerID()}},
|
||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: data.GetName() + "_workspace"}},
|
||||
},
|
||||
}
|
||||
// filters *dbs.Filters, word string, isDraft bool
|
||||
res, _, err := a.Search(filters, "", true) // Search for the workspace
|
||||
if err == nil && len(res) > 0 { // If the workspace already exists, return an error
|
||||
return nil, 409, errors.New("a workspace with the same name already exists")
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
@@ -16,7 +15,6 @@ type APIRequest struct {
|
||||
PeerID string
|
||||
Groups []string
|
||||
Caller *HTTPCaller
|
||||
Admin bool
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -111,14 +109,14 @@ func (a *API) SubscribeRouter(infos []*beego.ControllerInfo) {
|
||||
}
|
||||
}
|
||||
}
|
||||
go nats.SetNATSPub("api", DISCOVERY, discovery)
|
||||
nats.SetNATSPub("api", DISCOVERY, discovery)
|
||||
}
|
||||
|
||||
// CheckRemotePeer checks the state of a remote peer
|
||||
func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
|
||||
// Check if the database is up
|
||||
caller := NewHTTPCaller(map[DataType]map[METHOD]string{}) // Create a new http caller
|
||||
var resp APIStatusResponse
|
||||
caller := NewHTTPCaller(map[DataType]map[METHOD]string{}) // Create a new http caller
|
||||
b, err := caller.CallPost(url, "", map[string]interface{}{}) // Call the status endpoint of the peer
|
||||
if err != nil {
|
||||
return DEAD, map[string]int{} // If the peer is not reachable, return dead
|
||||
@@ -137,7 +135,6 @@ func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
|
||||
// CheckRemoteAPIs checks the state of remote APIs from your proper OC
|
||||
func (a *API) CheckRemoteAPIs(apis []DataType) (State, map[string]string, error) {
|
||||
// Check if the database is up
|
||||
l := logs.GetLogger()
|
||||
new := map[string]string{}
|
||||
caller := NewHTTPCaller(map[DataType]map[METHOD]string{}) // Create a new http caller
|
||||
code := 0
|
||||
@@ -148,7 +145,6 @@ func (a *API) CheckRemoteAPIs(apis []DataType) (State, map[string]string, error)
|
||||
var resp APIStatusResponse
|
||||
b, err := caller.CallGet("http://"+api.API()+":8080", "/oc/version/status") // Call the status endpoint of the remote API (standard OC status endpoint)
|
||||
if err != nil {
|
||||
l.Error().Msg(api.String() + " not reachable")
|
||||
state = REDUCED_SERVICE // If a remote API is not reachable, return reduced service
|
||||
continue
|
||||
}
|
||||
@@ -165,7 +161,6 @@ func (a *API) CheckRemoteAPIs(apis []DataType) (State, map[string]string, error)
|
||||
reachable = true // If the remote API is reachable, set reachable to true cause we are not dead
|
||||
}
|
||||
if !reachable {
|
||||
l.Error().Msg("Peer check returned no answers")
|
||||
state = DEAD // If no remote API is reachable, return dead, nobody is alive
|
||||
}
|
||||
if code > 0 {
|
||||
|
||||
@@ -21,17 +21,6 @@ const (
|
||||
WORKSPACE_HISTORY
|
||||
ORDER
|
||||
PURCHASE_RESOURCE
|
||||
ADMIRALTY_SOURCE
|
||||
ADMIRALTY_TARGET
|
||||
ADMIRALTY_SECRET
|
||||
ADMIRALTY_KUBECONFIG
|
||||
ADMIRALTY_NODES
|
||||
LIVE_DATACENTER
|
||||
LIVE_STORAGE
|
||||
BILL
|
||||
MINIO_SVCACC
|
||||
MINIO_SVCACC_SECRET
|
||||
NATIVE_TOOL
|
||||
)
|
||||
|
||||
var NOAPI = ""
|
||||
@@ -41,13 +30,6 @@ var WORKFLOWAPI = "oc-workflow"
|
||||
var WORKSPACEAPI = "oc-workspace"
|
||||
var PEERSAPI = "oc-peer"
|
||||
var DATACENTERAPI = "oc-datacenter"
|
||||
var PURCHASEAPI = "oc-catalog/purchase"
|
||||
var ADMIRALTY_SOURCEAPI = DATACENTERAPI + "/admiralty/source"
|
||||
var ADMIRALTY_TARGETAPI = DATACENTERAPI + "/admiralty/target"
|
||||
var ADMIRALTY_SECRETAPI = DATACENTERAPI + "/admiralty/secret"
|
||||
var ADMIRALTY_KUBECONFIGAPI = DATACENTERAPI + "/admiralty/kubeconfig"
|
||||
var ADMIRALTY_NODESAPI = DATACENTERAPI + "/admiralty/node"
|
||||
var MINIO = DATACENTERAPI + "/minio"
|
||||
|
||||
// Bind the standard API name to the data type
|
||||
var DefaultAPI = [...]string{
|
||||
@@ -67,18 +49,7 @@ var DefaultAPI = [...]string{
|
||||
NOAPI,
|
||||
NOAPI,
|
||||
NOAPI,
|
||||
PURCHASEAPI,
|
||||
ADMIRALTY_SOURCEAPI,
|
||||
ADMIRALTY_TARGETAPI,
|
||||
ADMIRALTY_SECRETAPI,
|
||||
ADMIRALTY_KUBECONFIGAPI,
|
||||
ADMIRALTY_NODESAPI,
|
||||
DATACENTERAPI,
|
||||
DATACENTERAPI,
|
||||
NOAPI,
|
||||
MINIO,
|
||||
MINIO,
|
||||
CATALOGAPI,
|
||||
}
|
||||
|
||||
// Bind the standard data name to the data type
|
||||
@@ -100,17 +71,6 @@ var Str = [...]string{
|
||||
"workspace_history",
|
||||
"order",
|
||||
"purchase_resource",
|
||||
"admiralty_source",
|
||||
"admiralty_target",
|
||||
"admiralty_secret",
|
||||
"admiralty_kubeconfig",
|
||||
"admiralty_node",
|
||||
"live_datacenter",
|
||||
"live_storage",
|
||||
"bill",
|
||||
"service_account",
|
||||
"secret",
|
||||
"native_tool",
|
||||
}
|
||||
|
||||
func FromInt(i int) string {
|
||||
@@ -131,8 +91,5 @@ func (d DataType) EnumIndex() int {
|
||||
}
|
||||
|
||||
func DataTypeList() []DataType {
|
||||
return []DataType{DATA_RESOURCE, PROCESSING_RESOURCE, STORAGE_RESOURCE, COMPUTE_RESOURCE, WORKFLOW_RESOURCE,
|
||||
WORKFLOW, WORKFLOW_EXECUTION, WORKSPACE, PEER, COLLABORATIVE_AREA, RULE, BOOKING, WORKFLOW_HISTORY, WORKSPACE_HISTORY,
|
||||
ORDER, PURCHASE_RESOURCE, ADMIRALTY_SOURCE, ADMIRALTY_TARGET, ADMIRALTY_SECRET, ADMIRALTY_KUBECONFIG, ADMIRALTY_NODES,
|
||||
LIVE_DATACENTER, LIVE_STORAGE, BILL, NATIVE_TOOL}
|
||||
return []DataType{DATA_RESOURCE, PROCESSING_RESOURCE, STORAGE_RESOURCE, COMPUTE_RESOURCE, WORKFLOW_RESOURCE, WORKFLOW, WORKFLOW_EXECUTION, WORKSPACE, PEER, COLLABORATIVE_AREA, RULE, BOOKING, WORKFLOW_HISTORY, WORKSPACE_HISTORY, ORDER, PURCHASE_RESOURCE}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package tools
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
@@ -17,7 +16,6 @@ const (
|
||||
REMOVE NATSMethod = iota
|
||||
CREATE
|
||||
DISCOVERY
|
||||
WORKFLOW_EVENT
|
||||
)
|
||||
|
||||
// NameToMethod returns the NATSMethod enum value from a string
|
||||
@@ -55,25 +53,22 @@ func (s *natsCaller) ListenNats(chanName string, exec func(msg map[string]interf
|
||||
log.Error().Msg(" -> NATS_SERVER is not set")
|
||||
return
|
||||
}
|
||||
for {
|
||||
nc, err := nats.Connect(config.GetConfig().NATSUrl)
|
||||
if err != nil {
|
||||
time.Sleep(1 * time.Minute)
|
||||
continue
|
||||
}
|
||||
ch := make(chan *nats.Msg, 64)
|
||||
subs, err := nc.ChanSubscribe(chanName, ch)
|
||||
if err != nil {
|
||||
log.Error().Msg("Error listening to NATS : " + err.Error())
|
||||
}
|
||||
defer subs.Unsubscribe()
|
||||
nc, err := nats.Connect(config.GetConfig().NATSUrl)
|
||||
if err != nil {
|
||||
log.Error().Msg(" -> Could not reach NATS server : " + err.Error())
|
||||
return
|
||||
}
|
||||
ch := make(chan *nats.Msg, 64)
|
||||
subs, err := nc.ChanSubscribe(chanName, ch)
|
||||
if err != nil {
|
||||
log.Error().Msg("Error listening to NATS : " + err.Error())
|
||||
}
|
||||
defer subs.Unsubscribe()
|
||||
|
||||
for msg := range ch {
|
||||
map_mess := map[string]interface{}{}
|
||||
json.Unmarshal(msg.Data, &map_mess)
|
||||
exec(map_mess)
|
||||
}
|
||||
break
|
||||
for msg := range ch {
|
||||
map_mess := map[string]interface{}{}
|
||||
json.Unmarshal(msg.Data, &map_mess)
|
||||
exec(map_mess)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,23 +77,18 @@ func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interfa
|
||||
if config.GetConfig().NATSUrl == "" {
|
||||
return " -> NATS_SERVER is not set"
|
||||
}
|
||||
for {
|
||||
nc, err := nats.Connect(config.GetConfig().NATSUrl)
|
||||
if err != nil {
|
||||
time.Sleep(1 * time.Minute)
|
||||
continue
|
||||
}
|
||||
defer nc.Close()
|
||||
js, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return " -> " + err.Error()
|
||||
}
|
||||
err = nc.Publish(method.GenerateKey(dataName), js) // Publish the message on the NATS server with a channel name based on the data name (or whatever start) and the method
|
||||
if err != nil {
|
||||
time.Sleep(1 * time.Minute)
|
||||
continue
|
||||
}
|
||||
break
|
||||
nc, err := nats.Connect(config.GetConfig().NATSUrl)
|
||||
if err != nil {
|
||||
return " -> Could not reach NATS server : " + err.Error()
|
||||
}
|
||||
defer nc.Close()
|
||||
js, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return " -> " + err.Error()
|
||||
}
|
||||
err = nc.Publish(method.GenerateKey(dataName), js) // Publish the message on the NATS server with a channel name based on the data name (or whatever start) and the method
|
||||
if err != nil {
|
||||
return " -> " + err.Error() // Return an error if the message could not be published
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package tools
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -47,19 +46,11 @@ func ToMethod(str string) METHOD {
|
||||
return GET
|
||||
}
|
||||
|
||||
type HTTPCallerITF interface {
|
||||
GetUrls() map[DataType]map[METHOD]string
|
||||
CallGet(url string, subpath string, types ...string) ([]byte, error)
|
||||
CallPost(url string, subpath string, body interface{}, types ...string) ([]byte, error)
|
||||
CallDelete(url string, subpath string) ([]byte, error)
|
||||
}
|
||||
|
||||
var HTTPCallerInstance = &HTTPCaller{} // Singleton instance of the HTTPCaller
|
||||
|
||||
type HTTPCaller struct {
|
||||
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
|
||||
Disabled bool // Disabled flag
|
||||
LastResults map[string]interface{} // Used to store information regarding the last execution of a given method on a given data type
|
||||
URLS map[DataType]map[METHOD]string // Map of the different methods and their urls
|
||||
Disabled bool // Disabled flag
|
||||
}
|
||||
|
||||
// NewHTTPCaller creates a new instance of the HTTP Caller
|
||||
@@ -70,20 +61,6 @@ func NewHTTPCaller(urls map[DataType]map[METHOD]string) *HTTPCaller {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *HTTPCaller) GetUrls() map[DataType]map[METHOD]string {
|
||||
return c.URLS
|
||||
}
|
||||
|
||||
// Creates a copy of the current caller, in order to have parallelized executions without race condition
|
||||
func (c *HTTPCaller) DeepCopy(dst HTTPCaller) error {
|
||||
bytes, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(bytes, &dst)
|
||||
}
|
||||
|
||||
// CallGet calls the GET method on the HTTP server
|
||||
func (caller *HTTPCaller) CallGet(url string, subpath string, types ...string) ([]byte, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, url+subpath, bytes.NewBuffer([]byte("")))
|
||||
@@ -99,33 +76,17 @@ func (caller *HTTPCaller) CallGet(url string, subpath string, types ...string) (
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
err = caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return caller.LastResults["body"].([]byte), nil
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// CallPut calls the DELETE method on the HTTP server
|
||||
func (caller *HTTPCaller) CallDelete(url string, subpath string) ([]byte, error) {
|
||||
req, err := http.NewRequest("DELETE", url+subpath, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil || req == nil || req.Body == nil {
|
||||
resp, err := http.NewRequest("DELETE", url+subpath, nil)
|
||||
if err != nil || resp == nil || resp.Body == nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
err = caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return caller.LastResults["body"].([]byte), nil
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// CallPost calls the POST method on the HTTP server
|
||||
@@ -144,12 +105,7 @@ func (caller *HTTPCaller) CallPost(url string, subpath string, body interface{},
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
err = caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return caller.LastResults["body"].([]byte), nil
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// CallPost calls the POST method on the HTTP server
|
||||
@@ -167,12 +123,7 @@ func (caller *HTTPCaller) CallPut(url string, subpath string, body map[string]in
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
err = caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return caller.LastResults["body"].([]byte), nil
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// CallRaw calls the Raw method on the HTTP server
|
||||
@@ -192,12 +143,7 @@ func (caller *HTTPCaller) CallRaw(method string, url string, subpath string,
|
||||
req.AddCookie(c)
|
||||
}
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
// CallRaw calls the Raw method on the HTTP server
|
||||
@@ -217,17 +163,3 @@ func (caller *HTTPCaller) CallForm(method string, url string, subpath string,
|
||||
client := &http.Client{}
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
func (caller *HTTPCaller) StoreResp(resp *http.Response) error {
|
||||
caller.LastResults = make(map[string]interface{})
|
||||
caller.LastResults["header"] = resp.Header
|
||||
caller.LastResults["code"] = resp.StatusCode
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading the body of the last request")
|
||||
return err
|
||||
}
|
||||
|
||||
caller.LastResults["body"] = data
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,227 +0,0 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
func TestMethodString(t *testing.T) {
|
||||
tests := []struct {
|
||||
method tools.METHOD
|
||||
expected string
|
||||
}{
|
||||
{tools.GET, "GET"},
|
||||
{tools.PUT, "PUT"},
|
||||
{tools.POST, "POST"},
|
||||
{tools.POSTCHECK, "POST"},
|
||||
{tools.DELETE, "DELETE"},
|
||||
{tools.STRICT_INTERNAL_GET, "INTERNALGET"},
|
||||
{tools.STRICT_INTERNAL_PUT, "INTERNALPUT"},
|
||||
{tools.STRICT_INTERNAL_POST, "INTERNALPOST"},
|
||||
{tools.STRICT_INTERNAL_DELETE, "INTERNALDELETE"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if test.method.String() != test.expected {
|
||||
t.Errorf("Expected %s, got %s", test.expected, test.method.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToMethod(t *testing.T) {
|
||||
method := tools.ToMethod("INTERNALPUT")
|
||||
if method != tools.STRICT_INTERNAL_PUT {
|
||||
t.Errorf("Expected STRICT_INTERNAL_PUT, got %v", method)
|
||||
}
|
||||
|
||||
defaultMethod := tools.ToMethod("INVALID")
|
||||
if defaultMethod != tools.GET {
|
||||
t.Errorf("Expected default GET, got %v", defaultMethod)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnumIndex(t *testing.T) {
|
||||
if tools.GET.EnumIndex() != 0 {
|
||||
t.Errorf("Expected index 0 for GET, got %d", tools.GET.EnumIndex())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallGet(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`"ok"`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
caller := &tools.HTTPCaller{}
|
||||
body, err := caller.CallGet(ts.URL, "/test", "application/json")
|
||||
if err != nil || string(body) != `"ok"` {
|
||||
t.Errorf("Expected body to be ok, got %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallPost(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(w, r.Body)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
caller := &tools.HTTPCaller{}
|
||||
body, err := caller.CallPost(ts.URL, "/post", map[string]string{"key": "val"})
|
||||
if err != nil || !strings.Contains(string(body), "key") {
|
||||
t.Errorf("POST failed, body: %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallPut(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(w, r.Body)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
caller := &tools.HTTPCaller{}
|
||||
body, err := caller.CallPut(ts.URL, "/put", map[string]interface{}{"foo": "bar"})
|
||||
if err != nil || !strings.Contains(string(body), "foo") {
|
||||
t.Errorf("PUT failed, body: %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallDelete(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`deleted`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
caller := &tools.HTTPCaller{}
|
||||
body, err := caller.CallDelete(ts.URL, "/delete")
|
||||
if err != nil || string(body) != "deleted" {
|
||||
t.Errorf("DELETE failed, body: %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallRaw(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
caller := &tools.HTTPCaller{}
|
||||
resp, err := caller.CallRaw("POST", ts.URL, "/", map[string]interface{}{"a": 1}, "application/json", true)
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("CallRaw failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallForm(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("Expected POST, got %s", r.Method)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
caller := &tools.HTTPCaller{}
|
||||
form := url.Values{}
|
||||
form.Set("foo", "bar")
|
||||
|
||||
_, err := caller.CallForm("POST", ts.URL, "/form", form, "application/x-www-form-urlencoded", true)
|
||||
if err != nil {
|
||||
t.Errorf("CallForm error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreResp(t *testing.T) {
|
||||
resp := &http.Response{
|
||||
Header: http.Header{},
|
||||
StatusCode: 200,
|
||||
Body: io.NopCloser(bytes.NewBuffer([]byte("body content"))),
|
||||
}
|
||||
|
||||
caller := &tools.HTTPCaller{}
|
||||
err := caller.StoreResp(resp)
|
||||
if err != nil {
|
||||
t.Errorf("StoreResp failed: %v", err)
|
||||
}
|
||||
|
||||
if string(caller.LastResults["body"].([]byte)) != "body content" {
|
||||
t.Errorf("Expected body content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewHTTPCaller(t *testing.T) {
|
||||
c := tools.NewHTTPCaller(nil)
|
||||
if c.Disabled != false {
|
||||
t.Errorf("Expected Disabled false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUrls(t *testing.T) {
|
||||
urls := map[tools.DataType]map[tools.METHOD]string{}
|
||||
c := tools.NewHTTPCaller(urls)
|
||||
if c.GetUrls() == nil {
|
||||
t.Errorf("GetUrls returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeepCopy(t *testing.T) {
|
||||
original := tools.NewHTTPCaller(nil)
|
||||
copy := tools.HTTPCaller{}
|
||||
err := original.DeepCopy(copy)
|
||||
if err != nil {
|
||||
t.Errorf("DeepCopy failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallPost_InvalidJSON(t *testing.T) {
|
||||
caller := &tools.HTTPCaller{}
|
||||
_, err := caller.CallPost("http://invalid", "/post", func() {})
|
||||
if err == nil {
|
||||
t.Error("Expected error when marshaling unsupported type")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallPut_ErrorOnNewRequest(t *testing.T) {
|
||||
caller := &tools.HTTPCaller{}
|
||||
_, err := caller.CallPut("http://[::1]:namedport", "/put", nil)
|
||||
if err == nil {
|
||||
t.Error("Expected error from invalid URL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallGet_Error(t *testing.T) {
|
||||
caller := &tools.HTTPCaller{}
|
||||
_, err := caller.CallGet("http://[::1]:namedport", "/bad", "application/json")
|
||||
if err == nil {
|
||||
t.Error("Expected error from invalid URL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallDelete_Error(t *testing.T) {
|
||||
caller := &tools.HTTPCaller{}
|
||||
_, err := caller.CallDelete("http://[::1]:namedport", "/bad")
|
||||
if err == nil {
|
||||
t.Error("Expected error from invalid URL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallRaw_Error(t *testing.T) {
|
||||
caller := &tools.HTTPCaller{}
|
||||
_, err := caller.CallRaw("POST", "http://[::1]:namedport", "/raw", nil, "application/json", false)
|
||||
if err == nil {
|
||||
t.Error("Expected error from invalid URL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallForm_Error(t *testing.T) {
|
||||
caller := &tools.HTTPCaller{}
|
||||
_, err := caller.CallForm("POST", "http://[::1]:namedport", "/form", url.Values{}, "application/json", false)
|
||||
if err == nil {
|
||||
t.Error("Expected error from invalid URL")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user