2024-11-28 16:49:41 +01:00
package resources
2024-07-18 11:51:12 +02:00
import (
2024-12-12 16:25:47 +01:00
"errors"
"time"
"cloud.o-forge.io/core/oc-lib/models/common"
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
2024-07-18 13:35:14 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-08-12 16:11:25 +02:00
"cloud.o-forge.io/core/oc-lib/tools"
2024-07-18 11:51:12 +02:00
)
2024-12-12 16:25:47 +01:00
/ *
* StorageResource is a struct that represents a storage resource
* it defines the resource storage
* /
type StorageResource struct {
AbstractResource [ * StorageResourceInstance ] // AbstractResource contains the basic fields of an object (id, name)
Type common . StorageType ` bson:"type,omitempty" json:"type,omitempty" ` // Type is the type of the storage
Acronym string ` bson:"acronym,omitempty" json:"acronym,omitempty" ` // Acronym is the acronym of the storage
}
2024-10-02 16:39:13 +02:00
2024-12-12 16:25:47 +01:00
func ( d * StorageResource ) GetAccessor ( request * tools . APIRequest ) utils . Accessor {
return NewAccessor [ * StorageResource ] ( tools . STORAGE_RESOURCE , request , func ( ) utils . DBObject { return & StorageResource { } } ) // Create a new instance of the accessor
}
2024-10-02 16:39:13 +02:00
2024-12-12 16:25:47 +01:00
type StorageResourceInstance struct {
ResourceInstance [ * StorageResourcePartnership ]
Local bool ` bson:"local" json:"local" `
SecurityLevel string ` bson:"security_level,omitempty" json:"security_level,omitempty" `
SizeType common . StorageSize ` bson:"size_type" json:"size_type" default:"0" ` // SizeType is the type of the storage size
SizeGB uint ` 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
2024-10-02 16:39:13 +02:00
}
2024-12-12 16:25:47 +01:00
func ( i * StorageResourceInstance ) GetID ( ) string {
return i . UUID
2024-10-02 16:39:13 +02:00
}
2024-12-12 16:25:47 +01:00
type StorageResourcePartnership struct {
ResourcePartnerShip [ * StorageResourcePricingProfile ]
MaxSizeGBAllowed float64 ` json:"allowed_gb,omitempty" bson:"allowed_gb,omitempty" `
OnlyEncryptedAllowed bool ` json:"personal_data_allowed,omitempty" bson:"personal_data_allowed,omitempty" `
}
type PrivilegeStoragePricingStrategy int
2024-11-07 12:39:28 +01:00
2024-11-07 11:05:24 +01:00
const (
2024-12-12 16:25:47 +01:00
BASIC_STORAGE PrivilegeStoragePricingStrategy = iota
GARANTED_ON_DELAY_STORAGE
GARANTED_STORAGE
2024-11-07 11:05:24 +01:00
)
2024-12-12 16:25:47 +01:00
func ( t PrivilegeStoragePricingStrategy ) String ( ) string {
return [ ... ] string { "BASIC_STORAGE" , "GARANTED_ON_DELAY_STORAGE" , "GARANTED_STORAGE" } [ t ]
}
type StorageResourcePricingStrategy int
const (
PER_DATA_STORED StorageResourcePricingStrategy = iota
PER_TB_STORED
PER_GB_STORED
PER_MB_STORED
PER_KB_STORED
)
func ( t StorageResourcePricingStrategy ) GetStrategy ( ) string {
return [ ... ] string { "PER_DATA_STORED" , "PER_GB_STORED" , "PER_MB_STORED" , "PER_KB_STORED" } [ t ]
}
func ( t StorageResourcePricingStrategy ) GetStrategyValue ( ) int {
return int ( t )
}
func ToStorageResourcePricingStrategy ( i int ) StorageResourcePricingStrategy {
return StorageResourcePricingStrategy ( i )
}
func ( t StorageResourcePricingStrategy ) GetQuantity ( amountOfDataGB float64 ) ( float64 , error ) {
switch t {
case PER_DATA_STORED :
return amountOfDataGB , nil
case PER_TB_STORED :
return amountOfDataGB * 1000 , nil
case PER_GB_STORED :
return amountOfDataGB , nil
case PER_MB_STORED :
return ( amountOfDataGB * 1000 ) , nil
case PER_KB_STORED :
return amountOfDataGB * 1000000 , nil
}
return 0 , errors . New ( "Pricing strategy not found" )
}
type StorageResourcePricingProfile struct {
2024-12-16 12:17:20 +01:00
pricing . ExploitPricingProfile [ StorageResourcePricingStrategy ] // ExploitPricingProfile is the pricing profile of a storage it means that we exploit the resource for an amount of continuous time
2024-12-12 16:25:47 +01:00
}
2024-12-17 10:42:00 +01:00
func ( p * StorageResourcePricingProfile ) IsPurchased ( ) bool {
2024-12-12 16:25:47 +01:00
return p . Pricing . BuyingStrategy != pricing . PAY_PER_USE
}
func ( p * StorageResourcePricingProfile ) GetPrice ( amountOfData float64 , val float64 , start time . Time , end time . Time , request * tools . APIRequest , params ... string ) ( float64 , error ) {
return p . Pricing . GetPrice ( amountOfData , val , start , & end )
}
type CustomizedStorageResource struct {
AbstractCustomizedResource [ * StorageResourceInstance ]
StorageGB float64 ` json:"storage_gb,omitempty" bson:"storage_gb,omitempty" `
}
func ( r * CustomizedStorageResource ) GetType ( ) tools . DataType {
return tools . STORAGE_RESOURCE
}
func ( r * CustomizedStorageResource ) GetPrice ( request * tools . APIRequest ) ( float64 , error ) {
if r . UsageStart == nil || r . UsageEnd == nil {
return 0 , errors . New ( "Usage start and end must be set" )
}
partner := r . GetPartnership ( request )
if partner != nil && partner . GetPricing ( r . SelectedPricing ) != nil {
return 0 , errors . New ( "Pricing strategy not found" )
}
pricing := partner . GetPricing ( r . SelectedPricing )
var err error
amountOfData := float64 ( 1 )
if pricing . GetOverrideStrategyValue ( ) >= 0 {
amountOfData , err = ToStorageResourcePricingStrategy ( pricing . GetOverrideStrategyValue ( ) ) . GetQuantity ( r . StorageGB )
if err != nil {
return 0 , err
}
}
return pricing . GetPrice ( amountOfData , r . ExplicitBookingDurationS , * r . UsageStart , * r . UsageEnd , request )
2024-07-18 11:51:12 +02:00
}