package models import ( "net" "time" "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 DatacenterCpuModel struct { Cores uint `json:"cores" required:"true"` //TODO: validate Architecture string `json:"architecture"` //TOOD: enum Shared bool `json:"shared"` MinimumMemory uint `json:"minimum_memory"` Platform string `json:"platform"` } type DatacenterMemoryModel struct { Size uint `json:"size" description:"Units in MB"` Ecc bool `json:"ecc"` } type DatacenterGpuModel struct { CudaCores uint `json:"cuda_cores"` Model string `json:"model"` Memory uint `json:"memory" description:"Units in MB"` TensorCores uint `json:"tensor_cores"` } type DatacenterNEWModel struct { Name string `json:"name" required:"true"` Type string `json:"type,omitempty" required:"true"` Acronym string `json:"acronym" required:"true" description:"id of the DC"` Hosts []string `json:"hosts" required:"true" description:"list of host:port"` Description string `json:"description" required:"true"` ShortDescription string `json:"short_description" required:"true" validate:"required"` Logo string `json:"logo" required:"true" validate:"required"` CPU DatacenterCpuModel `json:"cpu" required:"true"` RAM DatacenterMemoryModel `json:"ram" required:"true"` GPU []DatacenterGpuModel `json:"gpu" required:"true"` Owner string `json:"owner" ` BookingPrice int `json:"bookingPrice" ` } type DatacenterModel struct { ID string `json:"ID" bson:"_id" required:"true"` DatacenterNEWModel `bson:",inline"` } func GetDatacenterFromAcronym(DC_name string) (retObj *DatacenterModel) { // TODO: This call should get the data from the peers, since it could be a different // host in the future res := services.MngoCollDatacenter.FindOne(services.MngoCtx, primitive.M{"acronym": DC_name}) if res.Err() != nil { logs.Error(res) return } var ret DatacenterModel res.Decode(&ret) return &ret } func (obj DatacenterModel) GetTotalCPUs() uint { return obj.CPU.Cores } func (obj DatacenterModel) GetTotalGPUs() uint { return uint(len(obj.GPU)) } func (obj DatacenterModel) GetTotalRAM() uint { return obj.RAM.Size } func (obj DatacenterModel) getRtype() rtype.Rtype { return rtype.DATACENTER } func (model DatacenterModel) getName() string { return model.Name } type DatacenterObject struct { ReferenceID primitive.ObjectID `json:"referenceID" description:"Data model ID"` } func (obj *DatacenterObject) setReference(rID primitive.ObjectID) { obj.ReferenceID = rID } func (obj DatacenterObject) getModel() (ret ResourceModel, err error) { var ret2 DatacenterModel res := services.MngoCollDatacenter.FindOne(services.MngoCtx, primitive.M{"_id": obj.ReferenceID}, ) if err = res.Err(); err != nil { return } err = res.Decode(&ret2) return ret2, err } func (obj DatacenterObject) getReference() primitive.ObjectID { return obj.ReferenceID } // Return a reachable host. If no one is reachable, return the first entry func (obj DatacenterObject) getHost() (host *string) { res := services.MngoCollDatacenter.FindOne(services.MngoCtx, primitive.M{"_id": obj.ReferenceID}) if res.Err() != nil { logs.Error(res) return nil } var ret DatacenterModel err := res.Decode(&ret) if err != nil { logs.Error(res) return nil } host = GetHost(ret.Hosts) return } func GetHost(hosts []string) (host *string) { // Return the first one if we can't reach any server host = &hosts[0] for _, singleHost := range hosts { conn, err := net.DialTimeout("tcp", singleHost, time.Duration(3)*time.Second) //FIXME: longer wait for connection in the future? if err != nil { continue } if conn != nil { //bingo host = &singleHost conn.Close() return } } return } func (obj DatacenterObject) getRtype() rtype.Rtype { return rtype.DATACENTER } func (obj DatacenterObject) getName() (name *string) { res := services.MngoCollDatacenter.FindOne(services.MngoCtx, primitive.M{"_id": obj.ReferenceID}) if res.Err() != nil { logs.Error(res) return } var ret DatacenterModel res.Decode(&ret) return &ret.Name } func (obj DatacenterObject) isLinked(rID string) LinkingState { return NO_LINK } func (obj *DatacenterObject) addLink(direction LinkingState, rObjID string) { } func PostOneDatacenter(obj DatacenterNEWModel) (string, error) { return postOneResource(obj, rtype.DATACENTER) } func GetMultipleDatacenter(IDs []string) (object *[]DatacenterModel, err error) { objArray, err := getMultipleResourceByIDs(IDs, rtype.DATACENTER) if err != nil { return nil, err } object = objArray.(*[]DatacenterModel) return object, err } func GetOneDatacenter(ID string) (object *DatacenterModel, err error) { obj, err := getOneResourceByID(ID, rtype.DATACENTER) if err != nil { return object, err } object = obj.(*DatacenterModel) //TODO: fix a possible segfault in this model and the others return object, err }