organize + graph
This commit is contained in:
39
models/resources/storage/storage.go
Normal file
39
models/resources/storage/storage.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
resources "oc-lib/models/resources"
|
||||
"oc-lib/models/utils"
|
||||
)
|
||||
|
||||
type URL struct {
|
||||
Protocol string `bson:"protocol" json:"protocol"`
|
||||
Path string `bson:"path" json:"path"`
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
resources.AbstractResource
|
||||
|
||||
Capacity uint `bson:"capacity,omitempty" json:"capacity,omitempty"`
|
||||
Url URL `bson:"url,omitempty" json:"url,omitempty"` // Will allow to select between several protocols
|
||||
|
||||
Encryption bool `bson:"encryption,omitempty" json:"encryption,omitempty"`
|
||||
Redundancy string `bson:"redundancy,omitempty" json:"redundancy,omitempty"`
|
||||
Throughput string `bson:"throughput,omitempty" json:"throughput,omitempty"`
|
||||
BookingPrice uint `bson:"booking_price,omitempty" json:"booking_price,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Storage) GetType() resources.ResourceType {
|
||||
return resources.STORAGE
|
||||
}
|
||||
|
||||
func (d *Storage) GetAccessor(driver utils.Driver) utils.Accessor {
|
||||
var data utils.Accessor
|
||||
switch driver {
|
||||
case utils.MONGO:
|
||||
data = &StorageMongoAccessor{}
|
||||
default:
|
||||
data = &StorageMongoAccessor{}
|
||||
}
|
||||
data.SetLogger()
|
||||
return data
|
||||
}
|
44
models/resources/storage/storage_mongo_accessor.go
Normal file
44
models/resources/storage/storage_mongo_accessor.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"oc-lib/dbs/mongo"
|
||||
"oc-lib/logs"
|
||||
"oc-lib/models/utils"
|
||||
)
|
||||
|
||||
type StorageMongoAccessor struct {
|
||||
utils.AbstractAccessor
|
||||
}
|
||||
|
||||
func (dma *StorageMongoAccessor) DeleteOne(id string) utils.DBObject {
|
||||
return dma.GenericDeleteOne(id, dma)
|
||||
}
|
||||
|
||||
func (dma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) utils.DBObject {
|
||||
return dma.GenericUpdateOne(set, id, dma)
|
||||
}
|
||||
|
||||
func (dma *StorageMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject {
|
||||
id, err := mongo.StoreOne(data.(*Storage), "data")
|
||||
if err != nil {
|
||||
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
||||
return &Storage{}
|
||||
}
|
||||
return dma.LoadOne(id)
|
||||
}
|
||||
|
||||
func (schedulema *StorageMongoAccessor) LoadOne(id string) utils.DBObject {
|
||||
|
||||
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
|
||||
}
|
46
models/resources/storage/storage_test.go
Normal file
46
models/resources/storage/storage_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
resources "oc-lib/models/resources"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStoreOneStorage(t *testing.T) {
|
||||
s := Storage{Capacity: 123, Url: URL{Protocol: "http", Path: "azerty.fr"},
|
||||
AbstractResource: resources.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: resources.AbstractResource{
|
||||
Uuid: "123",
|
||||
Name: "testData",
|
||||
Description: "Lorem Ipsum",
|
||||
Logo: "azerty.com",
|
||||
Owner: "toto",
|
||||
OwnerLogo: "totoLogo",
|
||||
SourceUrl: "azerty.fr",
|
||||
},
|
||||
}
|
||||
|
||||
sma := StorageMongoAccessor{}
|
||||
new_s := sma.StoreOne(&s)
|
||||
|
||||
assert.Equal(t, s, new_s)
|
||||
}
|
Reference in New Issue
Block a user