packaged every model

This commit is contained in:
pb
2024-07-17 18:02:30 +02:00
parent 2684703da5
commit 2eb74da9d2
16 changed files with 731 additions and 0 deletions

24
storage/storage.go Normal file
View File

@@ -0,0 +1,24 @@
package storage
import oclib "oc-lib"
type URL struct {
Protocol string `json:"protocol"`
Path string `json:"path"`
}
type Storage struct {
oclib.AbstractResource `json:"resource" required:"true" bson:"resource"`
Capacity uint `json:"capacity,omitempty"`
Url URL `json:"url,omitempty"` // Will allow to select between several protocols
Encryption bool `json:"encryption,omitempty"`
Redundancy string `json:"redundancy,omitempty"`
Throughput string `json:"throughput,omitempty"`
BookingPrice uint `json:"booking_price,omitempty"`
}
func (s *Storage) GetType() oclib.ResourceType {
return oclib.STORAGE
}

View File

@@ -0,0 +1,38 @@
package storage
import (
"oc-lib/logs"
"oc-lib/mongo"
)
type StorageMongoAccessor struct{
}
func (schedulema *StorageMongoAccessor) StoreOne(storage Storage) string {
id, err := mongo.StoreOne(storage,"storage")
if err != nil{
l := logs.CreateLogger("oclib","")
l.Error().Msg("Could not store " + storage.Name + " to db. Error: " + err.Error())
return ""
}
return id
}
func (schedulema *StorageMongoAccessor) LoadOne(id string) Storage {
var storage Storage
res_mongo, err := mongo.LoadOne(id,"storage")
if err != nil{
l := logs.CreateLogger("oclib","")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return Storage{}
}
res_mongo.Decode(&storage)
return storage
}

48
storage/storage_test.go Normal file
View File

@@ -0,0 +1,48 @@
package storage
import (
oclib "oc-lib"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStoreOneStorage(t *testing.T){
s := Storage{ Capacity: 123, Url: URL{Protocol: "http",Path: "azerty.fr"} ,
AbstractResource: oclib.AbstractResource{
Uuid: "123",
Name: "testData",
Description: "Lorem Ipsum",
Logo : "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := StorageMongoAccessor{}
id := sma.StoreOne(s)
assert.NotEmpty(t, id)
}
func TestLoadOneStorage(t *testing.T){
s := Storage{ Capacity: 123, Url: URL{Protocol: "http",Path: "azerty.fr"} ,
AbstractResource: oclib.AbstractResource{
Uuid: "123",
Name: "testData",
Description: "Lorem Ipsum",
Logo : "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := StorageMongoAccessor{}
id := sma.StoreOne(s)
new_s := sma.LoadOne(id)
assert.Equal(t,s, new_s)
}