Adjust + Test
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"slices"
|
||||
@@ -20,33 +19,15 @@ 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"`
|
||||
AllowedBookingModes map[booking.BookingMode]*pricing.PricingVariation `bson:"allowed_booking_modes" json:"allowed_booking_modes"`
|
||||
Signature []byte `bson:"signature,omitempty" json:"signature,omitempty"`
|
||||
}
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
|
||||
func (r *AbstractResource) Unsign() {
|
||||
r.Signature = nil
|
||||
}
|
||||
|
||||
func (r *AbstractResource) Sign() {
|
||||
priv, err := tools.LoadKeyFromFilePrivate() // your node private key
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
b, _ := json.Marshal(r)
|
||||
hash := sha256.Sum256(b)
|
||||
r.Signature, err = priv.Sign(hash[:])
|
||||
}
|
||||
|
||||
func (abs *AbstractResource) GetSignature() []byte {
|
||||
return abs.Signature
|
||||
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 (abs *AbstractResource) FilterPeer(peerID string) *dbs.Filters {
|
||||
@@ -66,10 +47,6 @@ func (r *AbstractResource) GetBookingModes() map[booking.BookingMode]*pricing.Pr
|
||||
return r.AllowedBookingModes
|
||||
}
|
||||
|
||||
func (r *AbstractResource) GetSelectedInstance(selected *int) ResourceInstanceITF {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AbstractResource) GetType() string {
|
||||
return tools.INVALID.String()
|
||||
}
|
||||
@@ -90,50 +67,41 @@ func (r *AbstractResource) CanDelete() bool {
|
||||
}
|
||||
|
||||
type AbstractInstanciatedResource[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
|
||||
}
|
||||
AbstractResource // AbstractResource contains the basic fields of an object (id, name)
|
||||
|
||||
// PEERID found
|
||||
func (abs *AbstractInstanciatedResource[T]) RefineResourceByPartnership(peerID string) ResourceInterface {
|
||||
instances := []T{}
|
||||
for _, i := range instances {
|
||||
i, ok := i.RefineResourceByPartnership(peerID)
|
||||
if ok {
|
||||
instances = append(instances, i.(T))
|
||||
}
|
||||
}
|
||||
abs.Instances = instances
|
||||
return abs
|
||||
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 *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 { // 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(selectedInstance); t != nil {
|
||||
instances[t.GetID()] = t.GetName()
|
||||
profile = t.GetProfile(request.PeerID, selectedPartnership, selectedBuyingStrategy, selectedStrategy)
|
||||
} else {
|
||||
for _, instance := range abs.Instances { // TODO why it crush before ?
|
||||
instances[instance.GetID()] = instance.GetName()
|
||||
profiles := instance.GetPricingsProfiles(request.PeerID, request.Groups)
|
||||
if len(profiles) > 0 {
|
||||
profile = profiles[0]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if profile == nil {
|
||||
if len(profiles) > 0 {
|
||||
profile = profiles[0]
|
||||
} else { // TODO : reset Self for Pricing Profile
|
||||
/*if ok, _ := utils.IsMySelf(request.PeerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||
Admin: true,
|
||||
})); ok {*/
|
||||
profile = pricing.GetDefaultPricingProfile()
|
||||
/*} else {
|
||||
return nil, errors.New("no pricing profile found")
|
||||
}*/
|
||||
}
|
||||
/*if ok, _ := utils.IsMySelf(request.PeerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||
Admin: true,
|
||||
})); 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 {
|
||||
@@ -169,11 +137,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 != "" {
|
||||
func (abs *AbstractInstanciatedResource[T]) SetAllowedInstances(request *tools.APIRequest, instanceID ...string) {
|
||||
if (request != nil && request.PeerID == abs.CreatorID && request.PeerID != "") || request.Admin {
|
||||
return
|
||||
}
|
||||
abs.Instances = VerifyAuthAction(abs.Instances, request)
|
||||
abs.Instances = VerifyAuthAction(abs.Instances, request, instanceID...)
|
||||
}
|
||||
|
||||
func (d *AbstractInstanciatedResource[T]) Trim() {
|
||||
@@ -191,9 +159,12 @@ func (abs *AbstractInstanciatedResource[T]) VerifyAuth(callName string, request
|
||||
return len(VerifyAuthAction(abs.Instances, request)) > 0 || abs.AbstractObject.VerifyAuth(callName, request)
|
||||
}
|
||||
|
||||
func VerifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest) []T {
|
||||
func VerifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest, instanceID ...string) []T {
|
||||
instances := []T{}
|
||||
for _, instance := range baseInstance {
|
||||
if len(instanceID) > 0 && !slices.Contains(instanceID, instance.GetID()) {
|
||||
continue
|
||||
}
|
||||
_, peerGroups := instance.GetPeerGroups()
|
||||
for _, peers := range peerGroups {
|
||||
if request == nil {
|
||||
@@ -201,11 +172,14 @@ func VerifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.AP
|
||||
}
|
||||
if grps, ok := peers[request.PeerID]; ok || config.GetConfig().Whitelist {
|
||||
if (ok && slices.Contains(grps, "*")) || (!ok && config.GetConfig().Whitelist) {
|
||||
instance.FilterInstance(request.PeerID)
|
||||
instances = append(instances, instance)
|
||||
// TODO filter Partners + Profiles...
|
||||
continue
|
||||
}
|
||||
for _, grp := range grps {
|
||||
if slices.Contains(request.Groups, grp) {
|
||||
instance.FilterInstance(request.PeerID)
|
||||
instances = append(instances, instance)
|
||||
}
|
||||
}
|
||||
@@ -244,17 +218,15 @@ func NewInstance[T ResourcePartnerITF](name string) *ResourceInstance[T] {
|
||||
}
|
||||
}
|
||||
|
||||
func (abs *ResourceInstance[T]) RefineResourceByPartnership(peerID string) (ResourceInstanceITF, bool) {
|
||||
okk := false
|
||||
partners := []T{}
|
||||
for _, p := range abs.Partnerships {
|
||||
partner, ok := p.RefineResourceByPartnership(peerID)
|
||||
if ok {
|
||||
partners = append(partners, partner.(T))
|
||||
okk = true
|
||||
func (ri *ResourceInstance[T]) FilterInstance(peerID string) {
|
||||
partnerships := []T{}
|
||||
for _, p := range ri.Partnerships {
|
||||
if p.GetPeerGroups()[peerID] != nil {
|
||||
p.FilterPartnership(peerID)
|
||||
partnerships = append(partnerships, p)
|
||||
}
|
||||
}
|
||||
return abs, okk
|
||||
ri.Partnerships = partnerships
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) ClearEnv() {
|
||||
@@ -328,17 +300,14 @@ type ResourcePartnerShip[T pricing.PricingProfileITF] struct {
|
||||
// to upgrade pricing profiles. to be a map BuyingStrategy, map of Strategy
|
||||
}
|
||||
|
||||
func (ri *ResourcePartnerShip[T]) RefineResourceByPartnership(peerID string) (ResourcePartnerITF, bool) {
|
||||
ok := false
|
||||
peerGrp := map[string][]string{}
|
||||
for k, v := range ri.PeerGroups {
|
||||
if k == peerID {
|
||||
peerGrp[k] = v
|
||||
ok = true
|
||||
func (ri *ResourcePartnerShip[T]) FilterPartnership(peerID string) {
|
||||
if ri.PeerGroups[peerID] == nil {
|
||||
ri.PeerGroups = map[string][]string{}
|
||||
} else {
|
||||
ri.PeerGroups = map[string][]string{
|
||||
peerID: ri.PeerGroups[peerID],
|
||||
}
|
||||
}
|
||||
ri.PeerGroups = peerGrp
|
||||
return ri, ok
|
||||
}
|
||||
|
||||
func (ri *ResourcePartnerShip[T]) GetProfile(buying *int, strategy *int) pricing.PricingProfileITF {
|
||||
|
||||
Reference in New Issue
Block a user