2024-07-30 12:08:13 +02:00
package resource_model
import (
"encoding/json"
2024-11-28 11:05:54 +01:00
"slices"
2024-07-30 12:08:13 +02:00
2024-11-28 11:05:54 +01:00
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/peer"
2024-07-30 12:08:13 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-08-12 16:11:25 +02:00
"cloud.o-forge.io/core/oc-lib/tools"
2024-07-30 12:08:13 +02:00
"github.com/google/uuid"
)
2024-10-09 09:05:49 +02:00
type WebResource struct {
Protocol string ` bson:"protocol,omitempty" json:"protocol,omitempty" ` // Protocol is the protocol of the URL
Path string ` bson:"path,omitempty" json:"path,omitempty" ` // Path is the path of the URL
}
2024-08-30 14:50:48 +02:00
/ *
* AbstractResource is a struct that represents a resource
* it defines the resource data
* /
2024-07-30 12:08:13 +02:00
type AbstractResource struct {
2024-08-30 14:50:48 +02:00
utils . AbstractObject // AbstractObject contains the basic fields of an object (id, name)
ShortDescription string ` json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required" ` // ShortDescription is the short description of the resource
Description string ` json:"description,omitempty" bson:"description,omitempty" ` // Description is the description of the resource
Logo string ` json:"logo,omitempty" bson:"logo,omitempty" validate:"required" ` // Logo is the logo of the resource
Owner string ` json:"owner,omitempty" bson:"owner,omitempty" validate:"required" ` // Owner is the owner of the resource
OwnerLogo string ` json:"owner_logo,omitempty" bson:"owner_logo,omitempty" ` // OwnerLogo is the owner logo of the resource
SourceUrl string ` json:"source_url,omitempty" bson:"source_url,omitempty" validate:"required" ` // SourceUrl is the source URL of the resource
PeerID string ` json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required" ` // PeerID is the ID of the peer getting this resource
License string ` json:"license,omitempty" bson:"license,omitempty" ` // License is the license of the resource
ResourceModel * ResourceModel ` json:"resource_model,omitempty" bson:"resource_model,omitempty" ` // ResourceModel is the model of the resource
2024-11-28 11:05:54 +01:00
AllowedPeersGroup map [ string ] [ ] string ` json:"allowed_peers_group,omitempty" bson:"allowed_peers_group,omitempty" ` // AllowedPeersGroup is the group of allowed peers
Price string ` json:"price,omitempty" bson:"price,omitempty" ` // Price is the price of access to the resource
Currency string ` json:"currency,omitempty" bson:"currency,omitempty" ` // Currency is the currency of the price
}
func ( abs * AbstractResource ) VerifyAuth ( peerID string , groups [ ] string ) bool {
if grps , ok := abs . AllowedPeersGroup [ peerID ] ; ok {
if slices . Contains ( grps , "*" ) {
return true
}
for _ , grp := range grps {
if slices . Contains ( groups , grp ) {
return true
}
}
}
return false
}
func ( abs * AbstractResource ) GetResourceFilter ( search string ) * dbs . Filters {
return & dbs . Filters {
Or : map [ string ] [ ] dbs . Filter { // filter by like name, short_description, description, owner, url if no filters are provided
"abstractresource.abstractobject.name" : { { Operator : dbs . LIKE . String ( ) , Value : search } } ,
"abstractresource.short_description" : { { Operator : dbs . LIKE . String ( ) , Value : search } } ,
"abstractresource.description" : { { Operator : dbs . LIKE . String ( ) , Value : search } } ,
"abstractresource.owner" : { { Operator : dbs . LIKE . String ( ) , Value : search } } ,
"abstractresource.source_url" : { { Operator : dbs . LIKE . String ( ) , Value : search } } ,
} ,
}
2024-07-30 12:08:13 +02:00
}
2024-08-30 14:50:48 +02:00
/ *
* GetModelType returns the type of the model key
* /
2024-09-27 11:14:46 +02:00
func ( abs * AbstractResource ) GetModelType ( cat string , key string ) interface { } {
2024-07-31 16:56:30 +02:00
if abs . ResourceModel == nil || abs . ResourceModel . Model == nil {
return nil
}
if _ , ok := abs . ResourceModel . Model [ key ] ; ! ok {
return nil
}
2024-09-27 11:14:46 +02:00
return abs . ResourceModel . Model [ cat ] [ key ] . Type
2024-07-31 16:56:30 +02:00
}
2024-08-30 14:50:48 +02:00
/ *
* GetModelKeys returns the keys of the model
* /
2024-07-31 16:56:30 +02:00
func ( abs * AbstractResource ) GetModelKeys ( ) [ ] string {
keys := make ( [ ] string , 0 )
for k := range abs . ResourceModel . Model {
keys = append ( keys , k )
}
return keys
}
2024-08-30 14:50:48 +02:00
/ *
* GetModelReadOnly returns the readonly of the model key
* /
2024-09-27 11:14:46 +02:00
func ( abs * AbstractResource ) GetModelReadOnly ( cat string , key string ) interface { } {
2024-07-31 16:56:30 +02:00
if abs . ResourceModel == nil || abs . ResourceModel . Model == nil {
return nil
}
if _ , ok := abs . ResourceModel . Model [ key ] ; ! ok {
return nil
}
2024-09-27 11:14:46 +02:00
return abs . ResourceModel . Model [ cat ] [ key ] . ReadOnly
2024-07-31 16:56:30 +02:00
}
2024-11-28 11:05:54 +01:00
func ( d * AbstractResource ) Trim ( ) * AbstractResource {
if ok , _ := ( & peer . Peer { AbstractObject : utils . AbstractObject { UUID : d . PeerID } } ) . IsMySelf ( ) ; ! ok {
d . AllowedPeersGroup = map [ string ] [ ] string { }
}
return d
}
2024-07-30 12:08:13 +02:00
type Model struct {
2024-10-01 09:16:56 +02:00
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
2024-07-30 12:08:13 +02:00
}
2024-08-30 14:50:48 +02:00
/ *
* 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
* /
2024-07-30 12:08:13 +02:00
type ResourceModel struct {
2024-09-27 11:14:46 +02:00
UUID string ` json:"id,omitempty" bson:"id,omitempty" validate:"required" `
ResourceType string ` json:"resource_type,omitempty" bson:"resource_type,omitempty" validate:"required" `
2024-10-09 09:29:16 +02:00
VarRefs map [ string ] string ` json:"var_refs,omitempty" bson:"var_refs,omitempty" ` // VarRefs is the variable references of the model
2024-09-27 11:14:46 +02:00
Model map [ string ] map [ string ] Model ` json:"model,omitempty" bson:"model,omitempty" `
2024-07-30 12:08:13 +02:00
}
func ( ao * ResourceModel ) GetID ( ) string {
return ao . UUID
}
2024-10-02 12:23:22 +02:00
func ( ao * ResourceModel ) UpToDate ( ) { }
2024-07-30 12:08:13 +02:00
func ( r * ResourceModel ) GenerateID ( ) {
r . UUID = uuid . New ( ) . String ( )
}
func ( d * ResourceModel ) GetName ( ) string {
return d . UUID
}
2024-11-28 11:05:54 +01:00
func ( abs * ResourceModel ) VerifyAuth ( peerID string , groups [ ] string ) bool {
return true
}
func ( d * ResourceModel ) GetAccessor ( peerID string , groups [ ] string , caller * tools . HTTPCaller ) utils . Accessor {
2024-07-30 12:08:13 +02:00
data := & ResourceModelMongoAccessor { }
2024-11-28 11:05:54 +01:00
data . Init ( tools . RESOURCE_MODEL , peerID , groups , caller )
2024-07-30 12:08:13 +02:00
return data
}
2024-11-28 11:05:54 +01:00
func ( dma * ResourceModel ) Deserialize ( j map [ string ] interface { } , obj utils . DBObject ) utils . DBObject {
2024-07-30 12:08:13 +02:00
b , err := json . Marshal ( j )
if err != nil {
return nil
}
2024-11-28 11:05:54 +01:00
json . Unmarshal ( b , obj )
return obj
2024-07-30 12:08:13 +02:00
}
2024-11-28 11:05:54 +01:00
func ( dma * ResourceModel ) Serialize ( obj utils . DBObject ) map [ string ] interface { } {
2024-07-30 12:08:13 +02:00
var m map [ string ] interface { }
2024-11-28 11:05:54 +01:00
b , err := json . Marshal ( obj )
2024-07-30 12:08:13 +02:00
if err != nil {
return nil
}
json . Unmarshal ( b , & m )
return m
}