draft of compute units catalog
This commit is contained in:
parent
9fe72ea96e
commit
2264d22c69
@ -51,6 +51,7 @@ const (
|
||||
RULE = tools.RULE
|
||||
BOOKING = tools.BOOKING
|
||||
ORDER = tools.ORDER
|
||||
COMPUTE_UNITS = tools.COMPUTE_UNITS
|
||||
)
|
||||
|
||||
// will turn into standards api hostnames
|
||||
|
44
models/compute_units/compute_units.go
Normal file
44
models/compute_units/compute_units.go
Normal file
@ -0,0 +1,44 @@
|
||||
package compute_units
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/biter777/countries"
|
||||
)
|
||||
|
||||
/*
|
||||
* ComputeUnits is a struct that represents a compute units in your datacenters
|
||||
*/
|
||||
|
||||
type ComputeUnits struct {
|
||||
utils.AbstractObject
|
||||
MonitorPath string `json:"monitor_path,omitempty" bson:"monitor_path,omitempty"`
|
||||
Location resources.GeoPoint `json:"location,omitempty" bson:"location,omitempty"`
|
||||
Country countries.CountryCode `json:"country,omitempty" bson:"country,omitempty"`
|
||||
AccessProtocol string `json:"access_protocol,omitempty" bson:"access_protocol,omitempty"`
|
||||
Architecture string `json:"architecture,omitempty" bson:"architecture,omitempty"` // Architecture is the architecture
|
||||
Infrastructure enum.InfrastructureType `json:"infrastructure" bson:"infrastructure" default:"-1"` // Infrastructure is the infrastructure
|
||||
Source string `json:"source,omitempty" bson:"source,omitempty"` // Source is the source of the resource
|
||||
SecurityLevel string `json:"security_level,omitempty" bson:"security_level,omitempty"`
|
||||
PowerSources []string `json:"power_sources,omitempty" bson:"power_sources,omitempty"`
|
||||
AnnualCO2Emissions float64 `json:"annual_co2_emissions,omitempty" bson:"co2_emissions,omitempty"`
|
||||
CPUs map[string]*models.CPU `bson:"cpus,omitempty" json:"cpus,omitempty"` // CPUs is the list of CPUs key is model
|
||||
GPUs map[string]*models.GPU `bson:"gpus,omitempty" json:"gpus,omitempty"` // GPUs is the list of GPUs key is model
|
||||
Nodes []*resources.ComputeNode `json:"nodes,omitempty" bson:"nodes,omitempty"`
|
||||
ResourceID string `json:"-" bson:"-"`
|
||||
}
|
||||
|
||||
func (r *ComputeUnits) StoreDraftDefault() {
|
||||
r.IsDraft = true
|
||||
}
|
||||
|
||||
func (r *ComputeUnits) CanDelete() bool {
|
||||
return r.IsDraft // only draft ComputeUnits can be deleted
|
||||
}
|
||||
|
||||
func (d *ComputeUnits) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
94
models/compute_units/compute_units_mongo_accessor.go
Normal file
94
models/compute_units/compute_units_mongo_accessor.go
Normal file
@ -0,0 +1,94 @@
|
||||
package compute_units
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
type computeUnitsMongoAccessor struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the computeUnitsMongoAccessor
|
||||
func NewAccessor(request *tools.APIRequest) *computeUnitsMongoAccessor {
|
||||
return &computeUnitsMongoAccessor{
|
||||
AbstractAccessor: utils.AbstractAccessor{
|
||||
Logger: logs.CreateLogger(tools.COMPUTE_UNITS.String()), // Create a logger with the data type
|
||||
Request: request,
|
||||
Type: tools.COMPUTE_UNITS,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *computeUnitsMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericUpdateOne(set, id, a, &ComputeUnits{})
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data.(*ComputeUnits), a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
// is a publisher... that become a resources.
|
||||
if data.IsDrafted() {
|
||||
return nil, 422, errors.New("can't publish a drafted compute units")
|
||||
}
|
||||
computeUnits := data.(*ComputeUnits)
|
||||
resAccess := resources.NewAccessor[*resources.ComputeResource](tools.COMPUTE_RESOURCE, a.Request, func() utils.DBObject { return &resources.ComputeResource{} })
|
||||
var instance *resources.ComputeResourceInstance
|
||||
b, _ := json.Marshal(computeUnits)
|
||||
json.Unmarshal(b, instance)
|
||||
|
||||
if computeUnits.ResourceID != "" {
|
||||
// TODO dependent of a existing resource
|
||||
res, code, err := resAccess.LoadOne(computeUnits.ResourceID)
|
||||
if err == nil {
|
||||
return nil, code, err
|
||||
}
|
||||
existingComputeResource := res.(*resources.ComputeResource)
|
||||
if existingComputeResource.Architecture != computeUnits.Architecture || existingComputeResource.Infrastructure != computeUnits.Infrastructure {
|
||||
return nil, 422, errors.New("should be same architecture & infrastructure from the resource")
|
||||
}
|
||||
existingComputeResource.Instances = append(existingComputeResource.Instances, instance)
|
||||
return resAccess.UpdateOne(existingComputeResource, existingComputeResource.UUID)
|
||||
} else {
|
||||
var resource *resources.ComputeResource
|
||||
b, _ := json.Marshal(computeUnits)
|
||||
json.Unmarshal(b, resource)
|
||||
resource.Instances = append(resource.Instances, instance)
|
||||
return resAccess.StoreOne(resource)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*ComputeUnits](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
return d, 200, nil
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*ComputeUnits](a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*ComputeUnits](filters, search, (&ComputeUnits{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||||
}
|
||||
|
||||
func (a *computeUnitsMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||||
return func(d utils.DBObject) utils.ShallowDBObject {
|
||||
return d
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/compute_units"
|
||||
"cloud.o-forge.io/core/oc-lib/models/order"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
@ -38,6 +39,7 @@ var ModelsCatalog = map[string]func() utils.DBObject{
|
||||
tools.WORKSPACE_HISTORY.String(): func() utils.DBObject { return &w3.WorkspaceHistory{} },
|
||||
tools.ORDER.String(): func() utils.DBObject { return &order.Order{} },
|
||||
tools.PURCHASE_RESOURCE.String(): func() utils.DBObject { return &purchase_resource.PurchaseResource{} },
|
||||
tools.COMPUTE_UNITS.String(): func() utils.DBObject { return &compute_units.ComputeUnits{} },
|
||||
}
|
||||
|
||||
// Model returns the model object based on the model type
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/purchase_resource"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
@ -26,6 +26,7 @@ const (
|
||||
ADMIRALTY_SECRET
|
||||
ADMIRALTY_KUBECONFIG
|
||||
ADMIRALTY_NODES
|
||||
COMPUTE_UNITS
|
||||
)
|
||||
|
||||
var NOAPI = ""
|
||||
@ -65,6 +66,7 @@ var DefaultAPI = [...]string{
|
||||
ADMIRALTY_SECRETAPI,
|
||||
ADMIRALTY_KUBECONFIGAPI,
|
||||
ADMIRALTY_NODESAPI,
|
||||
DATACENTERAPI,
|
||||
}
|
||||
|
||||
// Bind the standard data name to the data type
|
||||
@ -91,6 +93,7 @@ var Str = [...]string{
|
||||
"admiralty_secret",
|
||||
"admiralty_kubeconfig",
|
||||
"admiralty_node",
|
||||
"compute_units",
|
||||
}
|
||||
|
||||
func FromInt(i int) string {
|
||||
|
Loading…
Reference in New Issue
Block a user