oc-lib/models/resources/resource_model/resource_model.go

64 lines
1.8 KiB
Go

package resource_model
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
type Model struct {
Type string `json:"type,omitempty" bson:"type,omitempty"` // Type is the type of the model
ReadOnly bool `json:"readonly,omitempty" bson:"readonly,omitempty"` // ReadOnly is the readonly of the model
}
/*
* ResourceModel is a struct that represents a resource model
* it defines the resource metadata and specificity
* Warning: This struct is not user available, it is only used by the system
*/
type ResourceModel struct {
utils.AbstractObject
ResourceType string `json:"resource_type,omitempty" bson:"resource_type,omitempty" validate:"required"`
Model map[string]map[string]Model `json:"model,omitempty" bson:"model,omitempty"`
Inputs []string `json:"inputs,omitempty" bson:"inputs,omitempty"`
Outputs []string `json:"outputs,omitempty" bson:"outputs,omitempty"`
}
func (d *ResourceModel) StoreDraftDefault() {
d.Name = d.ResourceType + " Resource Model"
d.IsDraft = false
}
func (abs *ResourceModel) VerifyAuth(request *tools.APIRequest) bool {
return true
}
func (d *ResourceModel) GetAccessor(request *tools.APIRequest) utils.Accessor {
return &ResourceModelMongoAccessor{
utils.AbstractAccessor{
Type: tools.RESOURCE_MODEL,
Request: request,
},
}
}
func (dma *ResourceModel) Deserialize(j map[string]interface{}, obj utils.DBObject) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, obj)
return obj
}
func (dma *ResourceModel) Serialize(obj utils.DBObject) map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(obj)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}