Peer Manipulation
This commit is contained in:
@@ -62,6 +62,14 @@ const (
|
|||||||
NATIVE_TOOL = tools.NATIVE_TOOL
|
NATIVE_TOOL = tools.NATIVE_TOOL
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetMySelf() (string, error) {
|
||||||
|
return utils.GetMySelf((&peer.Peer{}).GetAccessor(&tools.APIRequest{Admin: true}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsMySelf(peerID string) (bool, string) {
|
||||||
|
return utils.IsMySelf(peerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{Admin: true}))
|
||||||
|
}
|
||||||
|
|
||||||
func GenerateNodeID() (string, error) {
|
func GenerateNodeID() (string, error) {
|
||||||
folderStatic := "/var/lib/opencloud-node"
|
folderStatic := "/var/lib/opencloud-node"
|
||||||
if _, err := os.Stat(folderStatic); err == nil {
|
if _, err := os.Stat(folderStatic); err == nil {
|
||||||
|
|||||||
@@ -63,7 +63,12 @@ func (a *collaborativeAreaMongoAccessor) UpdateOne(set utils.DBObject, id string
|
|||||||
|
|
||||||
// StoreOne stores a collaborative area in the database, it automatically share to peers if the workspace is shared
|
// StoreOne stores a collaborative area in the database, it automatically share to peers if the workspace is shared
|
||||||
func (a *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (a *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
_, id := (&peer.Peer{}).IsMySelf() // get the local peer
|
id, err := utils.GetMySelf((&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})) // get the local peer
|
||||||
|
if err != nil {
|
||||||
|
return data, 404, err
|
||||||
|
}
|
||||||
data.(*CollaborativeArea).Clear(id) // set the creator
|
data.(*CollaborativeArea).Clear(id) // set the creator
|
||||||
// retrieve or proper peer
|
// retrieve or proper peer
|
||||||
if data.(*CollaborativeArea).CollaborativeAreaRule != nil {
|
if data.(*CollaborativeArea).CollaborativeAreaRule != nil {
|
||||||
@@ -271,7 +276,9 @@ func (a *collaborativeAreaMongoAccessor) contactPeer(shared *CollaborativeArea,
|
|||||||
|
|
||||||
paccess := (&peer.Peer{})
|
paccess := (&peer.Peer{})
|
||||||
for k := range shared.AllowedPeersGroup {
|
for k := range shared.AllowedPeersGroup {
|
||||||
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: k}}).IsMySelf(); ok || (shared.IsSent && meth == tools.POST) || (!shared.IsSent && meth != tools.POST) {
|
if ok, _ := utils.IsMySelf(k, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})); ok || (shared.IsSent && meth == tools.POST) || (!shared.IsSent && meth != tools.POST) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
shared.IsSent = meth == tools.POST
|
shared.IsSent = meth == tools.POST
|
||||||
|
|||||||
@@ -57,24 +57,6 @@ func (m PeerRelation) EnumIndex() int {
|
|||||||
return int(m)
|
return int(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSelf() (utils.ShallowDBObject, string) {
|
|
||||||
d, code, err := NewAccessor(nil).Search(nil, SELF.String(), false)
|
|
||||||
if code != 200 || err != nil || len(d) == 0 {
|
|
||||||
return nil, ""
|
|
||||||
}
|
|
||||||
id := d[0].GetID()
|
|
||||||
return d[0], id
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsMySelf(peerID string) (bool, string) {
|
|
||||||
d, code, err := NewAccessor(nil).Search(nil, SELF.String(), false)
|
|
||||||
if code != 200 || err != nil || len(d) == 0 {
|
|
||||||
return false, ""
|
|
||||||
}
|
|
||||||
id := d[0].GetID()
|
|
||||||
return peerID == id, id
|
|
||||||
}
|
|
||||||
|
|
||||||
// Peer is a struct that represents a peer
|
// Peer is a struct that represents a peer
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
utils.AbstractObject
|
utils.AbstractObject
|
||||||
@@ -120,16 +102,6 @@ func (ao *Peer) RemoveExecution(exec PeerExecution) {
|
|||||||
ao.FailedExecution = new
|
ao.FailedExecution = new
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMySelf checks if the peer is the local peer
|
|
||||||
func (p *Peer) IsMySelf() (bool, string) {
|
|
||||||
d, code, err := NewAccessor(nil).Search(nil, SELF.String(), p.IsDraft)
|
|
||||||
if code != 200 || err != nil || len(d) == 0 {
|
|
||||||
return false, ""
|
|
||||||
}
|
|
||||||
id := d[0].GetID()
|
|
||||||
return p.UUID == id, id
|
|
||||||
}
|
|
||||||
|
|
||||||
// LaunchPeerExecution launches an execution on a peer
|
// LaunchPeerExecution launches an execution on a peer
|
||||||
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataType, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) (map[string]interface{}, error) {
|
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataType, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) (map[string]interface{}, error) {
|
||||||
p.UUID = peerID
|
p.UUID = peerID
|
||||||
|
|||||||
@@ -80,10 +80,14 @@ func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string, isDraf
|
|||||||
}
|
}
|
||||||
func (a *peerMongoAccessor) GetDefaultFilter(search string) *dbs.Filters {
|
func (a *peerMongoAccessor) GetDefaultFilter(search string) *dbs.Filters {
|
||||||
if i, err := strconv.Atoi(search); err == nil {
|
if i, err := strconv.Atoi(search); err == nil {
|
||||||
|
m := map[string][]dbs.Filter{ // search by name if no filters are provided
|
||||||
|
"relation": {{Operator: dbs.EQUAL.String(), Value: i}},
|
||||||
|
}
|
||||||
|
if i == PARTNER.EnumIndex() {
|
||||||
|
m["verify"] = []dbs.Filter{{Operator: dbs.EQUAL.String(), Value: false}}
|
||||||
|
}
|
||||||
return &dbs.Filters{
|
return &dbs.Filters{
|
||||||
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
Or: m,
|
||||||
"relation": {{Operator: dbs.EQUAL.String(), Value: i}},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if search == "*" {
|
if search == "*" {
|
||||||
|
|||||||
@@ -86,22 +86,6 @@ func (abs *AbstractInstanciatedResource[T]) RefineResourceByPartnership(peerID s
|
|||||||
return abs
|
return abs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (abs *AbstractInstanciatedResource[T]) FilterPeer(peerID string) *dbs.Filters {
|
|
||||||
return &dbs.Filters{
|
|
||||||
And: map[string][]dbs.Filter{
|
|
||||||
"instances": {{Operator: dbs.ELEMMATCH.String(), Value: &dbs.Filters{
|
|
||||||
And: map[string][]dbs.Filter{
|
|
||||||
"partnerships": {{Operator: dbs.ELEMMATCH.String(), Value: &dbs.Filters{
|
|
||||||
And: map[string][]dbs.Filter{
|
|
||||||
"peer_groups." + peerID: {{Operator: dbs.EXISTS.String(), Value: true}},
|
|
||||||
},
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
}}},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (abs *AbstractInstanciatedResource[T]) AddInstances(instance ResourceInstanceITF) {
|
func (abs *AbstractInstanciatedResource[T]) AddInstances(instance ResourceInstanceITF) {
|
||||||
abs.Instances = append(abs.Instances, instance.(T))
|
abs.Instances = append(abs.Instances, instance.(T))
|
||||||
}
|
}
|
||||||
@@ -121,7 +105,9 @@ func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource(t tools.Data
|
|||||||
if len(profiles) > 0 {
|
if len(profiles) > 0 {
|
||||||
profile = profiles[0]
|
profile = profiles[0]
|
||||||
} else {
|
} else {
|
||||||
if ok, _ := peer.IsMySelf(request.PeerID); ok {
|
if ok, _ := utils.IsMySelf(request.PeerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})); ok {
|
||||||
profile = pricing.GetDefaultPricingProfile()
|
profile = pricing.GetDefaultPricingProfile()
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("no pricing profile found")
|
return nil, errors.New("no pricing profile found")
|
||||||
@@ -171,7 +157,9 @@ func (abs *AbstractInstanciatedResource[T]) SetAllowedInstances(request *tools.A
|
|||||||
|
|
||||||
func (d *AbstractInstanciatedResource[T]) Trim() {
|
func (d *AbstractInstanciatedResource[T]) Trim() {
|
||||||
d.Type = d.GetType()
|
d.Type = d.GetType()
|
||||||
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: d.CreatorID}}).IsMySelf(); !ok {
|
if ok, _ := utils.IsMySelf(d.CreatorID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})); !ok {
|
||||||
for _, instance := range d.Instances {
|
for _, instance := range d.Instances {
|
||||||
instance.ClearPeerGroups()
|
instance.ClearPeerGroups()
|
||||||
}
|
}
|
||||||
@@ -259,7 +247,9 @@ func (ri *ResourceInstance[T]) GetProfile(peerID string, partnershipIndex *int,
|
|||||||
prts := ri.Partnerships[*partnershipIndex]
|
prts := ri.Partnerships[*partnershipIndex]
|
||||||
return prts.GetProfile(buyingIndex, strategyIndex)
|
return prts.GetProfile(buyingIndex, strategyIndex)
|
||||||
}
|
}
|
||||||
if ok, _ := peer.IsMySelf(peerID); ok {
|
if ok, _ := utils.IsMySelf(peerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})); ok {
|
||||||
return pricing.GetDefaultPricingProfile()
|
return pricing.GetDefaultPricingProfile()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -271,7 +261,9 @@ func (ri *ResourceInstance[T]) GetPricingsProfiles(peerID string, groups []strin
|
|||||||
pricings = append(pricings, p.GetPricingsProfiles(peerID, groups)...)
|
pricings = append(pricings, p.GetPricingsProfiles(peerID, groups)...)
|
||||||
}
|
}
|
||||||
if len(pricings) == 0 {
|
if len(pricings) == 0 {
|
||||||
if ok, _ := peer.IsMySelf(peerID); ok {
|
if ok, _ := utils.IsMySelf(peerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})); ok {
|
||||||
pricings = append(pricings, pricing.GetDefaultPricingProfile())
|
pricings = append(pricings, pricing.GetDefaultPricingProfile())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -286,7 +278,12 @@ func (ri *ResourceInstance[T]) GetPeerGroups() ([]ResourcePartnerITF, []map[stri
|
|||||||
groups = append(groups, p.GetPeerGroups())
|
groups = append(groups, p.GetPeerGroups())
|
||||||
}
|
}
|
||||||
if len(groups) == 0 {
|
if len(groups) == 0 {
|
||||||
_, id := peer.GetSelf()
|
id, err := utils.GetMySelf((&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
return partners, groups
|
||||||
|
}
|
||||||
groups = []map[string][]string{
|
groups = []map[string][]string{
|
||||||
{
|
{
|
||||||
id: {"*"},
|
id: {"*"},
|
||||||
@@ -358,7 +355,9 @@ func (ri *ResourcePartnerShip[T]) GetPricingsProfiles(peerID string, groups []st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(profiles) == 0 {
|
if len(profiles) == 0 {
|
||||||
if ok, _ := peer.IsMySelf(peerID); ok {
|
if ok, _ := utils.IsMySelf(peerID, (&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})); ok {
|
||||||
profiles = append(profiles, pricing.GetDefaultPricingProfile())
|
profiles = append(profiles, pricing.GetDefaultPricingProfile())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -367,7 +366,12 @@ func (ri *ResourcePartnerShip[T]) GetPricingsProfiles(peerID string, groups []st
|
|||||||
|
|
||||||
func (rp *ResourcePartnerShip[T]) GetPeerGroups() map[string][]string {
|
func (rp *ResourcePartnerShip[T]) GetPeerGroups() map[string][]string {
|
||||||
if len(rp.PeerGroups) == 0 {
|
if len(rp.PeerGroups) == 0 {
|
||||||
_, id := peer.GetSelf()
|
id, err := utils.GetMySelf((&peer.Peer{}).GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
return rp.PeerGroups
|
||||||
|
}
|
||||||
return map[string][]string{
|
return map[string][]string{
|
||||||
id: {"*"},
|
id: {"*"},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||||
|
"github.com/google/uuid"
|
||||||
mgb "go.mongodb.org/mongo-driver/mongo"
|
mgb "go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -164,3 +166,42 @@ func GenericRawUpdateOne(set DBObject, id string, a Accessor) (DBObject, int, er
|
|||||||
}
|
}
|
||||||
return a.LoadOne(id)
|
return a.LoadOne(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetMySelf(wfa Accessor) (string, error) {
|
||||||
|
id, err := GenerateNodeID()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
datas, _, _ := wfa.Search(nil, id, false)
|
||||||
|
if len(datas) > 0 {
|
||||||
|
return datas[0].GetID(), nil
|
||||||
|
}
|
||||||
|
return "", errors.New("peer not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsMySelf(peerID string, wfa Accessor) (bool, string) {
|
||||||
|
id, err := GetMySelf(wfa)
|
||||||
|
if err != nil {
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
return peerID == id, id
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateNodeID() (string, error) {
|
||||||
|
folderStatic := "/var/lib/opencloud-node"
|
||||||
|
if _, err := os.Stat(folderStatic); err == nil {
|
||||||
|
os.MkdirAll(folderStatic, 0644)
|
||||||
|
}
|
||||||
|
folderStatic += "/node_id"
|
||||||
|
if _, err := os.Stat(folderStatic); os.IsNotExist(err) {
|
||||||
|
hostname, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
id := uuid.NewSHA1(uuid.NameSpaceOID, []byte("oc-"+hostname))
|
||||||
|
err = os.WriteFile(folderStatic, []byte(id.String()), 0644)
|
||||||
|
return id.String(), err
|
||||||
|
}
|
||||||
|
data, err := os.ReadFile(folderStatic)
|
||||||
|
return string(data), err
|
||||||
|
}
|
||||||
|
|||||||
@@ -68,7 +68,9 @@ func (a *workflowMongoAccessor) share(realData *Workflow, delete bool, caller *t
|
|||||||
paccess := &peer.Peer{}
|
paccess := &peer.Peer{}
|
||||||
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
||||||
paccess.UUID = p
|
paccess.UUID = p
|
||||||
if ok, _ := paccess.IsMySelf(); ok { // if the peer is the current peer, never share because it will create a loop
|
if ok, _ := utils.IsMySelf(p, paccess.GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})); ok { // if the peer is the current peer, never share because it will create a loop
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if delete { // if the workflow is deleted, share the deletion orderResourceAccessor utils.Accessor
|
if delete { // if the workflow is deleted, share the deletion orderResourceAccessor utils.Accessor
|
||||||
|
|||||||
@@ -130,7 +130,9 @@ func (a *workspaceMongoAccessor) share(realData *Workspace, method tools.METHOD,
|
|||||||
paccess := &peer.Peer{}
|
paccess := &peer.Peer{}
|
||||||
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
||||||
paccess.UUID = p
|
paccess.UUID = p
|
||||||
if ok, _ := paccess.IsMySelf(); ok { // If the peer is the current peer, never share because it will create a loop
|
if ok, _ := utils.IsMySelf(p, paccess.GetAccessor(&tools.APIRequest{
|
||||||
|
Admin: true,
|
||||||
|
})); ok { // If the peer is the current peer, never share because it will create a loop
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if method == tools.DELETE { // If the workspace is deleted, share the deletion
|
if method == tools.DELETE { // If the workspace is deleted, share the deletion
|
||||||
|
|||||||
Reference in New Issue
Block a user