Extend for Human Readable
This commit is contained in:
@@ -196,6 +196,23 @@ type Claims struct {
|
||||
Session SessionClaims `json:"session"`
|
||||
}
|
||||
|
||||
func GetExtend(obj utils.DBObject, extends map[string][]tools.DataType) utils.DBObject {
|
||||
base := obj.Serialize(obj)
|
||||
for k, v := range extends {
|
||||
if base[k+"_id"] == nil || base[k+"_id"] == "" {
|
||||
continue
|
||||
}
|
||||
for _, vv := range v {
|
||||
if d, _, err := models.Model(vv.EnumIndex()).GetAccessor(&tools.APIRequest{
|
||||
Admin: true,
|
||||
}).LoadOne(fmt.Sprintf("%v", base[k+"_id"])); d != nil && err == nil {
|
||||
base[k] = d.Serialize(d)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj.Deserialize(base, obj)
|
||||
}
|
||||
func ExtractTokenInfo(request http.Request) (string, string, []string) {
|
||||
reqToken := request.Header.Get("Authorization")
|
||||
splitToken := strings.Split(reqToken, "Bearer ")
|
||||
@@ -419,7 +436,7 @@ func (r *Request) LoadAll(isDraft bool, offset int64, limit int64) (data LibData
|
||||
* @param c ...*tools.HTTPCaller
|
||||
* @return data LibData
|
||||
*/
|
||||
func (r *Request) LoadOne(id string) (data LibData) {
|
||||
func (r *Request) LoadOne(id string, extend ...string) (data LibData) {
|
||||
defer func() { // recover the panic
|
||||
if r := recover(); r != nil {
|
||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
|
||||
@@ -437,7 +454,7 @@ func (r *Request) LoadOne(id string) (data LibData) {
|
||||
data = LibData{Data: d, Code: code, Err: err.Error()}
|
||||
return
|
||||
}
|
||||
data = LibData{Data: d, Code: code}
|
||||
data = LibData{Data: GetExtend(d, d.Extend(extend...)), Code: code}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,20 @@ type Bill struct {
|
||||
Total float64 `json:"total" bson:"total" validate:"required"`
|
||||
}
|
||||
|
||||
func (ri *Bill) Extend(typ ...string) map[string][]tools.DataType {
|
||||
ext := ri.AbstractObject.Extend(typ...)
|
||||
for _, t := range typ {
|
||||
switch t {
|
||||
case "order":
|
||||
if _, ok := ext[t]; !ok {
|
||||
ext[t] = []tools.DataType{}
|
||||
}
|
||||
ext[t] = append(ext[t], tools.ORDER)
|
||||
}
|
||||
}
|
||||
return ext
|
||||
}
|
||||
|
||||
func GenerateBill(order *order.Order, request *tools.APIRequest) (*Bill, error) {
|
||||
// hhmmm : should get... the loop.
|
||||
return &Bill{
|
||||
|
||||
@@ -18,6 +18,20 @@ type ExecutionVerification struct {
|
||||
Validate bool `json:"validate" bson:"validate,omitempty"`
|
||||
}
|
||||
|
||||
func (ri *ExecutionVerification) Extend(typ ...string) map[string][]tools.DataType {
|
||||
ext := ri.AbstractObject.Extend(typ...)
|
||||
for _, t := range typ {
|
||||
switch t {
|
||||
case "wokflow":
|
||||
if _, ok := ext[t]; !ok {
|
||||
ext[t] = []tools.DataType{}
|
||||
}
|
||||
ext[t] = append(ext[t], tools.WORKFLOW)
|
||||
}
|
||||
}
|
||||
return ext
|
||||
}
|
||||
|
||||
func (r *ExecutionVerification) StoreDraftDefault() {
|
||||
r.IsDraft = false // TODO: TEMPORARY
|
||||
}
|
||||
|
||||
@@ -44,6 +44,24 @@ type AbstractLive struct {
|
||||
ResourcesID []string `json:"resources_id" bson:"resources_id"`
|
||||
}
|
||||
|
||||
func (ri *AbstractLive) Extend(typ ...string) map[string][]tools.DataType {
|
||||
ext := ri.AbstractObject.Extend(typ...)
|
||||
for _, t := range typ {
|
||||
switch t {
|
||||
case "resource":
|
||||
if _, ok := ext[t]; !ok {
|
||||
ext[t] = []tools.DataType{}
|
||||
}
|
||||
ext[t] = append(ext[t], tools.WORKFLOW_RESOURCE)
|
||||
ext[t] = append(ext[t], tools.DATA_RESOURCE)
|
||||
ext[t] = append(ext[t], tools.COMPUTE_RESOURCE)
|
||||
ext[t] = append(ext[t], tools.STORAGE_RESOURCE)
|
||||
ext[t] = append(ext[t], tools.PROCESSING_RESOURCE)
|
||||
}
|
||||
}
|
||||
return ext
|
||||
}
|
||||
|
||||
func (d *AbstractLive) GetMonitorPath() string {
|
||||
return d.MonitorPath
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/live"
|
||||
"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/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
@@ -15,7 +16,6 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
resource "cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
w3 "cloud.o-forge.io/core/oc-lib/models/workspace"
|
||||
|
||||
@@ -17,10 +17,10 @@ import (
|
||||
|
||||
type Order struct {
|
||||
utils.AbstractObject
|
||||
ExecutionsID string `json:"executions_id" bson:"executions_id" validate:"required"`
|
||||
Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
|
||||
Purchases []*purchase_resource.PurchaseResource `json:"purchases" bson:"purchases"`
|
||||
Bookings []*booking.Booking `json:"bookings" bson:"bookings"`
|
||||
ExecutionsID string `json:"executions_id" bson:"executions_id" validate:"required"`
|
||||
Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
|
||||
Purchases []*purchase_resource.PurchaseResource `json:"purchases" bson:"purchases"`
|
||||
Bookings []*booking.Booking `json:"bookings" bson:"bookings"`
|
||||
|
||||
Billing map[pricing.BillingStrategy][]*booking.Booking `json:"billing" bson:"billing"`
|
||||
}
|
||||
|
||||
@@ -100,6 +100,20 @@ type Peer struct {
|
||||
LastHeartbeat *time.Time `json:"last_heartbeat,omitempty" bson:"-"`
|
||||
}
|
||||
|
||||
func (ri *Peer) Extend(typ ...string) map[string][]tools.DataType {
|
||||
ext := ri.AbstractObject.Extend(typ...)
|
||||
for _, t := range typ {
|
||||
switch t {
|
||||
case "peer":
|
||||
if _, ok := ext[t]; !ok {
|
||||
ext[t] = []tools.DataType{}
|
||||
}
|
||||
ext[t] = append(ext[t], tools.PEER)
|
||||
}
|
||||
}
|
||||
return ext
|
||||
}
|
||||
|
||||
func (ao *Peer) VerifyAuth(callName string, request *tools.APIRequest) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -24,6 +24,33 @@ type PurchaseResource struct {
|
||||
SchedulerPeerID string `json:"scheduler_peer_id,omitempty" bson:"scheduler_peer_id,omitempty"`
|
||||
}
|
||||
|
||||
func (ri *PurchaseResource) Extend(typ ...string) map[string][]tools.DataType {
|
||||
ext := ri.AbstractObject.Extend(typ...)
|
||||
for _, t := range typ {
|
||||
switch t {
|
||||
case "dest_peer", "scheduler_peer":
|
||||
if _, ok := ext[t]; !ok {
|
||||
ext[t] = []tools.DataType{}
|
||||
}
|
||||
ext[t] = append(ext[t], tools.PEER)
|
||||
case "execution":
|
||||
if _, ok := ext[t]; !ok {
|
||||
ext[t] = []tools.DataType{}
|
||||
}
|
||||
ext[t] = append(ext[t], tools.WORKFLOW_EXECUTION)
|
||||
case "resource":
|
||||
if _, ok := ext[t]; !ok {
|
||||
ext[t] = []tools.DataType{}
|
||||
}
|
||||
ext[t] = append(ext[t], tools.WORKFLOW_RESOURCE)
|
||||
ext[t] = append(ext[t], tools.DATA_RESOURCE)
|
||||
ext[t] = append(ext[t], tools.COMPUTE_RESOURCE)
|
||||
ext[t] = append(ext[t], tools.STORAGE_RESOURCE)
|
||||
ext[t] = append(ext[t], tools.PROCESSING_RESOURCE)
|
||||
}
|
||||
}
|
||||
return ext
|
||||
}
|
||||
func (d *PurchaseResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
@@ -44,6 +44,20 @@ type AbstractObject struct {
|
||||
Signature []byte `bson:"signature,omitempty" json:"signature,omitempty"`
|
||||
}
|
||||
|
||||
func (ri *AbstractObject) Extend(typ ...string) map[string][]tools.DataType {
|
||||
dt := map[string][]tools.DataType{}
|
||||
for _, t := range typ {
|
||||
switch t {
|
||||
case "creator", "user_creator", "user_updater":
|
||||
if _, ok := dt[t]; !ok {
|
||||
dt[t] = []tools.DataType{}
|
||||
}
|
||||
dt[t] = append(dt[t], tools.PEER)
|
||||
}
|
||||
}
|
||||
return dt
|
||||
}
|
||||
|
||||
func (ri *AbstractObject) GetAccessor(request *tools.APIRequest) Accessor {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ type ShallowDBObject interface {
|
||||
// DBObject is an interface that defines the basic methods for a DBObject
|
||||
type DBObject interface {
|
||||
GenerateID()
|
||||
Extend(typ ...string) map[string][]tools.DataType
|
||||
SetNotInCatalog(bool)
|
||||
IsNotInCatalog() bool
|
||||
SetID(id string)
|
||||
|
||||
@@ -58,6 +58,20 @@ type WorkflowExecution struct {
|
||||
SelectedStrategies workflow.ConfigItem `json:"selected_strategies"`
|
||||
}
|
||||
|
||||
func (ri *WorkflowExecution) Extend(typ ...string) map[string][]tools.DataType {
|
||||
ext := ri.AbstractObject.Extend(typ...)
|
||||
for _, t := range typ {
|
||||
switch t {
|
||||
case "workflow":
|
||||
if _, ok := ext[t]; !ok {
|
||||
ext[t] = []tools.DataType{}
|
||||
}
|
||||
ext[t] = append(ext[t], tools.PEER)
|
||||
}
|
||||
}
|
||||
return ext
|
||||
}
|
||||
|
||||
func (r *WorkflowExecution) StoreDraftDefault() {
|
||||
r.IsDraft = true
|
||||
r.State = enum.DRAFT
|
||||
|
||||
Reference in New Issue
Block a user