Skip to content

Commit

Permalink
Merge pull request #560 from MUzairS15/MUzairS15/fixes/2
Browse files Browse the repository at this point in the history
Fixes bugs related to registry logic.
  • Loading branch information
MUzairS15 authored Aug 14, 2024
2 parents 8996f15 + a702069 commit ba1fa94
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
23 changes: 12 additions & 11 deletions models/meshmodel/core/policies/rego_policy_relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ func NewRegoInstance(policyDir string, regManager *registry.RegistryManager) (*R
}

// RegoPolicyHandler takes the required inputs and run the query against all the policy files provided
func (r *Rego) RegoPolicyHandler(designFile pattern.PatternFile, regoQueryString string, relationshipsToEvalaute ...string) (interface{}, error) {
func (r *Rego) RegoPolicyHandler(designFile pattern.PatternFile, regoQueryString string, relationshipsToEvalaute ...string) (pattern.EvaluationResponse, error) {
var evaluationResponse pattern.EvaluationResponse
if r == nil {
return nil, ErrEval(fmt.Errorf("policy engine is not yet ready"))
return evaluationResponse, ErrEval(fmt.Errorf("policy engine is not yet ready"))
}
regoEngine, err := rego.New(
rego.Query(regoQueryString),
Expand All @@ -61,37 +62,37 @@ func (r *Rego) RegoPolicyHandler(designFile pattern.PatternFile, regoQueryString
).PrepareForEval(r.ctx)
if err != nil {
logrus.Error("error preparing for evaluation", err)
return nil, ErrPrepareForEval(err)
return evaluationResponse, ErrPrepareForEval(err)
}

eval_result, err := regoEngine.Eval(r.ctx, rego.EvalInput(designFile))
if err != nil {
return nil, ErrEval(err)
return evaluationResponse, ErrEval(err)
}

if len(eval_result) == 0 {
return nil, ErrEval(fmt.Errorf("evaluation results are empty"))
return evaluationResponse, ErrEval(fmt.Errorf("evaluation results are empty"))
}

if len(eval_result[0].Expressions) == 0 {
return nil, ErrEval(fmt.Errorf("evaluation results are empty"))
return evaluationResponse, ErrEval(fmt.Errorf("evaluation results are empty"))
}

result, err := utils.Cast[map[string]interface{}](eval_result[0].Expressions[0].Value)
if err != nil {
return nil, ErrEval(err)
return evaluationResponse, ErrEval(err)
}

evalResults, ok := result["evaluate"]
if !ok {
return nil, ErrEval(fmt.Errorf("evaluation results are empty"))
return evaluationResponse, ErrEval(fmt.Errorf("evaluation results are empty"))
}

evaluatedPatternFile, err := utils.MarshalAndUnmarshal[interface{}, pattern.PatternFile](evalResults)
if err != nil {
return nil, err
return evaluationResponse, err
}

return evaluatedPatternFile, nil
evaluationResponse.Design = evaluatedPatternFile
return evaluationResponse, nil

}
2 changes: 1 addition & 1 deletion models/meshmodel/core/v1beta1/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewDependencyHandler(connectionKind string) (DependencyHandler, error) {

type ArtifactHub struct{}

const MesheryAnnotationPrefix = "design.meshmodel.io"
const MesheryAnnotationPrefix = "design.meshery.io"

func (ah ArtifactHub) HandleDependents(comp component.ComponentDefinition, kc *kubernetes.Client, isDeploy, performUpgrade bool) (summary string, err error) {
sourceURI, err := utils.Cast[string](comp.Metadata.AdditionalProperties["source_uri"]) // should be part of registrant data(?)
Expand Down
4 changes: 2 additions & 2 deletions models/meshmodel/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ func (rm *RegistryManager) GetRegistrants(f *models.HostFilter) ([]models.MeshMo
"COUNT(CASE WHEN r.type = 'model' THEN 1 END) AS models," +
"COUNT(CASE WHEN r.type = 'relationship' THEN 1 END) AS relationships, " +
"COUNT(CASE WHEN r.type = 'policy' THEN 1 END) AS policies").
Joins("LEFT JOIN registries r ON c.id = r.registrant_id")
// Group("c.id, c.hostname, h.port") // required
Joins("LEFT JOIN registries r ON c.id = r.registrant_id").
Group("c.id")

if f.DisplayName != "" {
query = query.Where("kind LIKE ?", "%"+f.DisplayName+"%")
Expand Down
1 change: 1 addition & 0 deletions models/registration/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (d Dir) PkgUnit(regErrStore RegistrationErrorStore) (_ packagingUnit, err e
regErrStore.AddInvalidDefinition(path, err)
return nil
}

// set it to pkgunit
switch e.Type() {
case entity.Model:
Expand Down
8 changes: 5 additions & 3 deletions models/registration/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ func (rh *RegistrationHelper) register(pkg packagingUnit) {
}

}

model.Registrant.Status = connection.Registered
_, _, err := rh.regManager.RegisterEntity(
connection.Connection{Kind: model.Registrant.Kind},
model.Registrant,
&model,
)

Expand Down Expand Up @@ -107,7 +109,7 @@ func (rh *RegistrationHelper) register(pkg packagingUnit) {
}

_, _, err := rh.regManager.RegisterEntity(
connection.Connection{Kind: model.Registrant.Kind},
model.Registrant,
&comp,
)
if err != nil {
Expand All @@ -119,7 +121,7 @@ func (rh *RegistrationHelper) register(pkg packagingUnit) {
// 3. Register relationships
for _, rel := range pkg.relationships {
rel.Model = model
_, _, err := rh.regManager.RegisterEntity(connection.Connection{Kind: model.Registrant.Kind}, &rel)
_, _, err := rh.regManager.RegisterEntity(model.Registrant, &rel)
if err != nil {
err = ErrRegisterEntity(err, string(rel.Type()), string(rel.Kind))
rh.regErrStore.InsertEntityRegError(hostname, modelName, entity.RelationshipDefinition, rel.Id.String(), err)
Expand Down
6 changes: 3 additions & 3 deletions models/registration/svg_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string
return
}
svgColorPath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "color", filename+"-color.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, svgColor)
writeHashCheckSVG(hashString, svgColorPath)

}
White:
Expand Down Expand Up @@ -86,7 +86,7 @@ White:
return
}
svgWhitePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "white", filename+"-white.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, svgWhite)
writeHashCheckSVG(hashString, svgWhitePath)

}
Complete:
Expand Down Expand Up @@ -117,7 +117,7 @@ Complete:
return
}
svgCompletePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "complete", filename+"-complete.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, svgComplete)
writeHashCheckSVG(hashString, svgCompletePath)

}
return
Expand Down

0 comments on commit ba1fa94

Please sign in to comment.