119 lines
2.6 KiB
Go
119 lines
2.6 KiB
Go
package models
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"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"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func getObjIDFromString(id string) interface{} {
|
|
objectID, err := primitive.ObjectIDFromHex(id)
|
|
if err == nil {
|
|
return objectID
|
|
}
|
|
|
|
return id
|
|
}
|
|
|
|
func getMultipleObjIDFromArray(ids []string) []interface{} {
|
|
|
|
var ret []interface{}
|
|
|
|
for _, val := range ids {
|
|
ret = append(ret, getObjIDFromString(val))
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
func getOneResourceByID(ID string, rType rtype.Rtype) (obj interface{}, err error) {
|
|
|
|
targetDBCollection := rType.MongoCollection()
|
|
var retObj interface{}
|
|
|
|
// asd := rType.
|
|
switch rType {
|
|
case rtype.DATA:
|
|
retObj = &DataModel{}
|
|
case rtype.COMPUTING:
|
|
retObj = &ComputingModel{}
|
|
case rtype.STORAGE:
|
|
retObj = &StorageModel{}
|
|
case rtype.DATACENTER:
|
|
retObj = &DatacenterModel{}
|
|
default:
|
|
message := "Rtype " + rType.String() + " is not implemented"
|
|
logs.Error(message)
|
|
return nil, errors.New(message)
|
|
}
|
|
|
|
filter := bson.M{"_id": getObjIDFromString(ID)}
|
|
|
|
res := targetDBCollection.FindOne(services.MngoCtx, filter)
|
|
res.Decode(retObj)
|
|
|
|
if res.Err() != nil {
|
|
logs.Warn("Couldn't find resource: " + res.Err().Error())
|
|
}
|
|
|
|
return retObj, res.Err()
|
|
}
|
|
|
|
func getMultipleResourceByIDs(IDs []string, rType rtype.Rtype) (interface{}, error) {
|
|
|
|
targetDBCollection := rType.MongoCollection()
|
|
var retObj interface{}
|
|
|
|
// asd := rType.
|
|
switch rType {
|
|
case rtype.DATA:
|
|
retObj = &[]DataModel{}
|
|
case rtype.COMPUTING:
|
|
retObj = &[]ComputingModel{}
|
|
case rtype.STORAGE:
|
|
retObj = &[]StorageModel{}
|
|
case rtype.DATACENTER:
|
|
retObj = &[]DatacenterModel{}
|
|
default:
|
|
message := "Rtype " + rType.String() + " is not implemented"
|
|
logs.Error(message)
|
|
return nil, errors.New(message)
|
|
}
|
|
|
|
filter := bson.M{"_id": bson.M{"$in": getMultipleObjIDFromArray(IDs)}}
|
|
|
|
//FIXME: Limit of find
|
|
res, err := targetDBCollection.Find(services.MngoCtx,
|
|
filter,
|
|
options.Find().SetLimit(100),
|
|
)
|
|
|
|
if err != nil {
|
|
logs.Warn("Couldn't find multiple data: " + err.Error())
|
|
return nil, err
|
|
}
|
|
res.All(services.MngoCtx, retObj)
|
|
|
|
return retObj, res.Err()
|
|
}
|
|
|
|
func postOneResource(retObj interface{}, rType rtype.Rtype) (ID string, err error) {
|
|
|
|
targetDBCollection := rType.MongoCollection()
|
|
|
|
result, err := targetDBCollection.InsertOne(services.MngoCtx, retObj)
|
|
if err != nil {
|
|
logs.Warn("Couldn't insert resource: " + err.Error())
|
|
return "", err
|
|
}
|
|
|
|
return result.InsertedID.(primitive.ObjectID).Hex(), nil
|
|
|
|
}
|