139 lines
3.2 KiB
Go
139 lines
3.2 KiB
Go
|
package models
|
||
|
|
||
|
import (
|
||
|
"cloud.o-forge.io/core/oc-catalog/models/rtype"
|
||
|
"cloud.o-forge.io/core/oc-catalog/services"
|
||
|
"github.com/beego/beego/v2/core/logs"
|
||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||
|
)
|
||
|
|
||
|
type StorageNEWModel struct {
|
||
|
Name string `json:"name" required:"true"`
|
||
|
Description string `json:"description" required:"true"`
|
||
|
ShortDescription string `json:"short_description" required:"true" validate:"required"`
|
||
|
Logo string `json:"logo" required:"true" validate:"required"`
|
||
|
Type string `json:"type,omitempty" required:"true"`
|
||
|
|
||
|
DCacronym string `json:"DCacronym" required:"true" description:"Unique ID of the DC where it is the storage"`
|
||
|
|
||
|
Size uint `json:"size" required:"true"`
|
||
|
Encryption bool `json:"encryption" `
|
||
|
Redundancy string `json:"redundancy" `
|
||
|
Throughput string `json:"throughput" `
|
||
|
BookingPrice uint `json:"bookingPrice" `
|
||
|
}
|
||
|
|
||
|
type StorageModel struct {
|
||
|
ID string `json:"ID" bson:"_id" required:"true"`
|
||
|
StorageNEWModel `bson:",inline"`
|
||
|
}
|
||
|
|
||
|
func (obj StorageModel) getRtype() rtype.Rtype {
|
||
|
return rtype.STORAGE
|
||
|
}
|
||
|
|
||
|
func (model StorageModel) getName() string {
|
||
|
return model.Name
|
||
|
}
|
||
|
|
||
|
type StorageObject struct {
|
||
|
ReferenceID primitive.ObjectID `json:"referenceID" description:"Storage model ID"`
|
||
|
|
||
|
Inputs []string `json:"inputs" `
|
||
|
Outputs []string `json:"outputs" `
|
||
|
}
|
||
|
|
||
|
func (obj StorageObject) getHost() *string {
|
||
|
return nil // Host is DC only attribute
|
||
|
}
|
||
|
|
||
|
func (obj StorageObject) getModel() (ret ResourceModel, err error) {
|
||
|
var ret2 StorageModel
|
||
|
res := services.MngoCollStorage.FindOne(services.MngoCtx,
|
||
|
primitive.M{"_id": obj.ReferenceID},
|
||
|
)
|
||
|
|
||
|
if err = res.Err(); err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = res.Decode(&ret2)
|
||
|
return ret2, err
|
||
|
}
|
||
|
|
||
|
func (obj *StorageObject) setReference(rID primitive.ObjectID) {
|
||
|
obj.ReferenceID = rID
|
||
|
}
|
||
|
|
||
|
func (obj StorageObject) getReference() primitive.ObjectID {
|
||
|
return obj.ReferenceID
|
||
|
}
|
||
|
|
||
|
func (obj StorageObject) getRtype() rtype.Rtype {
|
||
|
return rtype.STORAGE
|
||
|
}
|
||
|
|
||
|
func (obj StorageObject) getName() (name *string) {
|
||
|
|
||
|
res := services.MngoCollStorage.FindOne(services.MngoCtx, primitive.M{"_id": obj.ReferenceID})
|
||
|
|
||
|
if res.Err() != nil {
|
||
|
logs.Error(res)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var ret StorageModel
|
||
|
res.Decode(&ret)
|
||
|
|
||
|
return &ret.Name
|
||
|
}
|
||
|
|
||
|
func (obj StorageObject) isLinked(rObjID string) LinkingState {
|
||
|
if contains(obj.Inputs, rObjID) {
|
||
|
return INPUT
|
||
|
}
|
||
|
|
||
|
if contains(obj.Outputs, rObjID) {
|
||
|
return OUTPUT
|
||
|
}
|
||
|
|
||
|
return NO_LINK
|
||
|
}
|
||
|
|
||
|
func (obj *StorageObject) addLink(direction LinkingState, rObjID string) {
|
||
|
switch direction {
|
||
|
case INPUT:
|
||
|
obj.Inputs = append(obj.Inputs, rObjID)
|
||
|
case OUTPUT:
|
||
|
obj.Outputs = append(obj.Outputs, rObjID)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func PostOneStorage(obj StorageNEWModel) (string, error) {
|
||
|
return postOneResource(obj, rtype.STORAGE)
|
||
|
}
|
||
|
|
||
|
func GetOneStorage(ID string) (object *StorageModel, err error) {
|
||
|
obj, err := getOneResourceByID(ID, rtype.STORAGE)
|
||
|
|
||
|
if err != nil {
|
||
|
return object, err
|
||
|
}
|
||
|
|
||
|
object = obj.(*StorageModel)
|
||
|
|
||
|
return object, err
|
||
|
}
|
||
|
|
||
|
func GetMultipleStorage(IDs []string) (object *[]StorageModel, err error) {
|
||
|
objArray, err := getMultipleResourceByIDs(IDs, rtype.STORAGE)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
object = objArray.(*[]StorageModel)
|
||
|
|
||
|
return object, err
|
||
|
}
|