add event base intelligency
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
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"
|
||||
@@ -15,17 +17,30 @@ import (
|
||||
|
||||
// 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"`
|
||||
SelectedInstanceIndex *int `json:"selected_instance_index,omitempty" bson:"selected_instance_index,omitempty"` // SelectedInstance is the selected instance
|
||||
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"`
|
||||
}
|
||||
|
||||
func (r *AbstractResource) GetSelectedInstance() ResourceInstanceITF {
|
||||
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 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -57,29 +72,43 @@ func (abs *AbstractInstanciatedResource[T]) AddInstances(instance ResourceInstan
|
||||
abs.Instances = append(abs.Instances, instance.(T))
|
||||
}
|
||||
|
||||
func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource(t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||
func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||
instances := map[string]string{}
|
||||
profiles := []pricing.PricingProfileITF{}
|
||||
for _, instance := range abs.Instances {
|
||||
for _, instance := range abs.Instances { // TODO why it crush before ?
|
||||
instances[instance.GetID()] = instance.GetName()
|
||||
profiles = instance.GetPricingsProfiles(request.PeerID, request.Groups)
|
||||
}
|
||||
var profile pricing.PricingProfileITF
|
||||
if t := abs.GetSelectedInstance(); t != nil {
|
||||
profile = t.GetProfile()
|
||||
if t := abs.GetSelectedInstance(selectedInstance); t != nil {
|
||||
profile = t.GetProfile(request.PeerID, selectedPartnership, selectedBuyingStrategy, selectedStrategy)
|
||||
}
|
||||
if profile == nil && len(profiles) > 0 {
|
||||
profile = profiles[0]
|
||||
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,
|
||||
CreatorID: abs.CreatorID,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (abs *AbstractInstanciatedResource[T]) ClearEnv() utils.DBObject {
|
||||
@@ -89,9 +118,9 @@ func (abs *AbstractInstanciatedResource[T]) ClearEnv() utils.DBObject {
|
||||
return abs
|
||||
}
|
||||
|
||||
func (r *AbstractInstanciatedResource[T]) GetSelectedInstance() ResourceInstanceITF {
|
||||
if r.SelectedInstanceIndex != nil && len(r.Instances) > *r.SelectedInstanceIndex {
|
||||
return r.Instances[*r.SelectedInstanceIndex]
|
||||
func (r *AbstractInstanciatedResource[T]) GetSelectedInstance(selected *int) ResourceInstanceITF {
|
||||
if selected != nil && len(r.Instances) > *selected {
|
||||
return r.Instances[*selected]
|
||||
}
|
||||
if len(r.Instances) > 0 {
|
||||
return r.Instances[0]
|
||||
@@ -115,8 +144,8 @@ func (d *AbstractInstanciatedResource[T]) Trim() {
|
||||
}
|
||||
}
|
||||
|
||||
func (abs *AbstractInstanciatedResource[T]) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return len(VerifyAuthAction[T](abs.Instances, request)) > 0 || abs.AbstractObject.VerifyAuth(request)
|
||||
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 VerifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest) []T {
|
||||
@@ -150,18 +179,22 @@ type GeoPoint struct {
|
||||
|
||||
type ResourceInstance[T ResourcePartnerITF] struct {
|
||||
utils.AbstractObject
|
||||
Location 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"`
|
||||
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"`
|
||||
Location 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"`
|
||||
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{
|
||||
@@ -178,40 +211,27 @@ func (ri *ResourceInstance[T]) ClearEnv() {
|
||||
ri.Outputs = []models.Param{}
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) GetProfile() pricing.PricingProfileITF {
|
||||
if len(ri.Partnerships) > ri.SelectedPartnershipIndex {
|
||||
prts := ri.Partnerships[ri.SelectedPartnershipIndex]
|
||||
return prts.GetProfile(ri.SelectedBuyingStrategy, ri.SelectedBuyingStrategy)
|
||||
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
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) GetSelectedPartnership(peerID string, groups []string) ResourcePartnerITF {
|
||||
if len(ri.Partnerships) > ri.SelectedPartnershipIndex {
|
||||
return ri.Partnerships[ri.SelectedPartnershipIndex]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) GetPartnerships(peerID string, groups []string) []ResourcePartnerITF {
|
||||
partners := []ResourcePartnerITF{}
|
||||
for _, p := range ri.Partnerships {
|
||||
if p.GetPeerGroups()[peerID] != nil {
|
||||
for _, g := range p.GetPeerGroups()[peerID] {
|
||||
if slices.Contains(groups, g) {
|
||||
partners = append(partners, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return partners
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF {
|
||||
pricings := []pricing.PricingProfileITF{}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -222,6 +242,15 @@ 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
|
||||
}
|
||||
|
||||
@@ -238,12 +267,15 @@ type ResourcePartnerShip[T pricing.PricingProfileITF] struct {
|
||||
// to upgrade pricing profiles. to be a map BuyingStrategy, map of Strategy
|
||||
}
|
||||
|
||||
func (ri *ResourcePartnerShip[T]) GetProfile(buying int, strategy int) pricing.PricingProfileITF {
|
||||
if strat, ok := ri.PricingProfiles[buying]; ok {
|
||||
if profile, ok := strat[strategy]; ok {
|
||||
return profile
|
||||
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
|
||||
}
|
||||
|
||||
@@ -269,10 +301,21 @@ func (ri *ResourcePartnerShip[T]) GetPricingsProfiles(peerID string, groups []st
|
||||
return profiles
|
||||
}
|
||||
}
|
||||
if len(profiles) == 0 {
|
||||
if ok, _ := peer.IsMySelf(peerID); ok {
|
||||
profiles = append(profiles, pricing.GetDefaultPricingProfile())
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user