oc-lib/models/resources/storage.go

138 lines
5.2 KiB
Go

package resources
import (
"errors"
"time"
"cloud.o-forge.io/core/oc-lib/models/common"
"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"
)
/*
* 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
}
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
}
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
}
func (i *StorageResourceInstance) GetID() string {
return i.UUID
}
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
const (
BASIC_STORAGE PrivilegeStoragePricingStrategy = iota
GARANTED_ON_DELAY_STORAGE
GARANTED_STORAGE
)
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 {
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) IsPurchased() bool {
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)
}