Resource Buy & Limitation
This commit is contained in:
@@ -20,6 +20,7 @@ type ResourceInterface interface {
|
|||||||
ConvertToPricedResource(t tools.DataType, a *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, b *int, request *tools.APIRequest) (pricing.PricedItemITF, error)
|
ConvertToPricedResource(t tools.DataType, a *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, b *int, request *tools.APIRequest) (pricing.PricedItemITF, error)
|
||||||
GetType() string
|
GetType() string
|
||||||
ClearEnv() utils.DBObject
|
ClearEnv() utils.DBObject
|
||||||
|
VerifyBuy()
|
||||||
SetAllowedInstances(request *tools.APIRequest, instance_id ...string) []ResourceInstanceITF
|
SetAllowedInstances(request *tools.APIRequest, instance_id ...string) []ResourceInstanceITF
|
||||||
AddInstances(instance ResourceInstanceITF)
|
AddInstances(instance ResourceInstanceITF)
|
||||||
GetSelectedInstance(index *int) ResourceInstanceITF
|
GetSelectedInstance(index *int) ResourceInstanceITF
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
"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/common/pricing"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
"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/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/biter777/countries"
|
"github.com/biter777/countries"
|
||||||
@@ -21,7 +22,7 @@ import (
|
|||||||
// AbstractResource is the struct containing all of the attributes commons to all ressources
|
// AbstractResource is the struct containing all of the attributes commons to all ressources
|
||||||
type AbstractResource struct {
|
type AbstractResource struct {
|
||||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||||
|
IsBuy bool `json:"is_buy"` // is_buy precise if a resource is buy or not
|
||||||
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type of the resource
|
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type of the resource
|
||||||
Logo string `json:"logo,omitempty" bson:"logo,omitempty"` // Logo is the logo of the resource
|
Logo string `json:"logo,omitempty" bson:"logo,omitempty"` // Logo is the logo of the resource
|
||||||
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
|
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
|
||||||
@@ -35,6 +36,18 @@ type AbstractResource struct {
|
|||||||
Outputs []models.Param `json:"outputs,omitempty" bson:"outputs,omitempty"`
|
Outputs []models.Param `json:"outputs,omitempty" bson:"outputs,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (abs *AbstractResource) VerifyBuy() {
|
||||||
|
p := &purchase_resource.PurchaseResource{}
|
||||||
|
access := p.GetAccessor(&tools.APIRequest{Admin: true})
|
||||||
|
|
||||||
|
access.Search(&dbs.Filters{
|
||||||
|
And: map[string][]dbs.Filter{
|
||||||
|
"resource_id": {{Operator: dbs.EQUAL.String(), Value: abs.GetID()}},
|
||||||
|
},
|
||||||
|
}, "", false, 0, 1)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (abs *AbstractResource) GetEnv() []models.Param {
|
func (abs *AbstractResource) GetEnv() []models.Param {
|
||||||
return abs.Env
|
return abs.Env
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,16 @@ func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIReques
|
|||||||
* Nothing special here, just the basic CRUD operations
|
* Nothing special here, just the basic CRUD operations
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
func (dca *ResourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
||||||
|
data, code, err := dca.AbstractAccessor.LoadOne(id)
|
||||||
|
if err == nil {
|
||||||
|
data.(T).VerifyBuy()
|
||||||
|
data.(T).SetAllowedInstances(dca.Request)
|
||||||
|
return data, code, err
|
||||||
|
}
|
||||||
|
return data, code, err
|
||||||
|
}
|
||||||
|
|
||||||
func (dca *ResourceMongoAccessor[T]) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
func (dca *ResourceMongoAccessor[T]) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
||||||
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
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")
|
return nil, 404, errors.New("can't update a non existing computing units resource not reported onto compute units catalog")
|
||||||
@@ -87,12 +97,14 @@ func (wfa *ResourceMongoAccessor[T]) LoadAll(isDraft bool, offset int64, limit i
|
|||||||
func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) {
|
func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) {
|
||||||
if filters == nil && search == "*" {
|
if filters == nil && search == "*" {
|
||||||
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
d.(T).VerifyBuy()
|
||||||
d.(T).SetAllowedInstances(wfa.Request)
|
d.(T).SetAllowedInstances(wfa.Request)
|
||||||
return d
|
return d
|
||||||
}, isDraft, wfa, offset, limit)
|
}, isDraft, wfa, offset, limit)
|
||||||
}
|
}
|
||||||
return utils.GenericSearch[T](filters, search, wfa.GetObjectFilters(search),
|
return utils.GenericSearch[T](filters, search, wfa.GetObjectFilters(search),
|
||||||
func(d utils.DBObject) utils.ShallowDBObject {
|
func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
d.(T).VerifyBuy()
|
||||||
d.(T).SetAllowedInstances(wfa.Request)
|
d.(T).SetAllowedInstances(wfa.Request)
|
||||||
return d
|
return d
|
||||||
}, isDraft, wfa, offset, limit)
|
}, isDraft, wfa, offset, limit)
|
||||||
@@ -100,6 +112,7 @@ func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string,
|
|||||||
|
|
||||||
func (a *ResourceMongoAccessor[T]) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
func (a *ResourceMongoAccessor[T]) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
|
||||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||||
|
d.(T).VerifyBuy()
|
||||||
d.(T).SetAllowedInstances(a.Request)
|
d.(T).SetAllowedInstances(a.Request)
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user