generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from meshery/nithish/feat/meshmodel_relations…
…hips [Feat] Add support for `Relationships`
- Loading branch information
Showing
4 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/layer5io/meshkit/database" | ||
"github.com/layer5io/meshkit/models/meshmodel/core/types" | ||
) | ||
|
||
// https://docs.google.com/drawings/d/1_qzQ_YxvCWPYrOBcdqGMlMwfbsZx96SBuIkbn8TfKhU/edit?pli=1 | ||
// see RELATIONSHIPDEFINITIONS table in the diagram | ||
// TODO: Add support for Model | ||
type RelationshipDefinition struct { | ||
ID uuid.UUID `json:"-"` | ||
TypeMeta | ||
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"` | ||
SubType string `json:"subType" yaml:"subType" gorm:"subType"` | ||
Selectors map[string]interface{} `json:"selectors" yaml:"selectors"` | ||
CreatedAt time.Time `json:"-"` | ||
UpdatedAt time.Time `json:"-"` | ||
} | ||
|
||
type RelationshipDefinitionDB struct { | ||
ID uuid.UUID `json:"-"` | ||
TypeMeta | ||
Metadata []byte `json:"metadata" yaml:"metadata"` | ||
SubType string `json:"subType" yaml:"subType"` | ||
Selectors []byte `json:"selectors" yaml:"selectors"` | ||
CreatedAt time.Time `json:"-"` | ||
UpdatedAt time.Time `json:"-"` | ||
} | ||
|
||
// For now, only filtering by Kind and SubType are allowed. | ||
// In the future, we will add support to query using `selectors` (using CUE) | ||
// TODO: Add support for Model | ||
type RelationshipFilter struct { | ||
Kind string | ||
SubType string | ||
} | ||
|
||
// Create the filter from map[string]interface{} | ||
func (rf *RelationshipFilter) Create(m map[string]interface{}) { | ||
if m == nil { | ||
return | ||
} | ||
} | ||
|
||
func GetRelationships(db *database.Handler, f RelationshipFilter) (rs []RelationshipDefinition) { | ||
var rdb []RelationshipDefinitionDB | ||
// GORM takes care of drafting the correct SQL | ||
// https://gorm.io/docs/query.html#Struct-amp-Map-Conditions | ||
_ = db.Where(&RelationshipDefinitionDB{SubType: f.SubType, TypeMeta: TypeMeta{Kind: f.Kind}}).Find(&rdb) | ||
for _, reldb := range rdb { | ||
rel := reldb.GetRelationshipDefinition() | ||
rs = append(rs, rel) | ||
} | ||
return | ||
} | ||
|
||
func (rdb *RelationshipDefinitionDB) GetRelationshipDefinition() (r RelationshipDefinition) { | ||
r.ID = rdb.ID | ||
r.TypeMeta = rdb.TypeMeta | ||
if r.Metadata == nil { | ||
r.Metadata = make(map[string]interface{}) | ||
} | ||
_ = json.Unmarshal(rdb.Metadata, &r.Metadata) | ||
if r.Selectors == nil { | ||
r.Selectors = make(map[string]interface{}) | ||
} | ||
_ = json.Unmarshal(rdb.Selectors, &r.Selectors) | ||
r.SubType = rdb.SubType | ||
r.Kind = rdb.Kind | ||
return | ||
} | ||
|
||
func (r RelationshipDefinition) Type() types.CapabilityType { | ||
return types.RelationshipDefinition | ||
} | ||
|
||
func CreateRelationship(db *database.Handler, r RelationshipDefinition) (uuid.UUID, error) { | ||
r.ID = uuid.New() | ||
rdb := r.GetRelationshipDefinitionDB() | ||
err := db.Create(&rdb).Error | ||
if err != nil { | ||
return uuid.UUID{}, err | ||
} | ||
return r.ID, err | ||
} | ||
|
||
func (r *RelationshipDefinition) GetRelationshipDefinitionDB() (rdb RelationshipDefinitionDB) { | ||
rdb.ID = r.ID | ||
rdb.TypeMeta = r.TypeMeta | ||
rdb.Metadata, _ = json.Marshal(r.Metadata) | ||
rdb.Selectors, _ = json.Marshal(r.Selectors) | ||
rdb.Kind = r.Kind | ||
rdb.SubType = r.SubType | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters