deprecated-oc-catalog/models/storage.go

155 lines
3.6 KiB
Go

package models
import (
"fmt"
"strings"
"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"`
URL string `json:"URL"`
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
}
func (obj StorageModel) AddUserInput(inputs map[string]interface{} ){
// So far only a few input to handle so a switch with a case for each type of attribute
// is enough, to prevent too much complexity
for key, value := range inputs {
switch strings.ToLower(key) {
case "URL":
obj.URL = value.(string)
default:
logs.Alert(fmt.Printf("%s is not an attribute of storage componants", key))
}
}
}