Skip to content

Commit

Permalink
⚠️ Separate Target and RuleSet models/apis (#464)
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Lucidi <[email protected]>
  • Loading branch information
mansam authored Aug 3, 2023
1 parent 22bf2d6 commit 0d0355b
Show file tree
Hide file tree
Showing 20 changed files with 1,913 additions and 215 deletions.
4 changes: 2 additions & 2 deletions api/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ func (h AnalysisHandler) AppIssueReports(ctx *gin.Context) {
// @description - files
// @tags issueappreports
// @produce json
// @success 200 {object} []api.AppReport
// @success 200 {object} []api.IssueAppReport
// @router /analyses/report/applications [get]
func (h AnalysisHandler) IssueAppReports(ctx *gin.Context) {
resources := []IssueAppReport{}
Expand Down Expand Up @@ -1448,7 +1448,7 @@ func (h AnalysisHandler) DepReports(ctx *gin.Context) {
// @description - indirect
// @tags depappreports
// @produce json
// @success 200 {object} []api.AppReport
// @success 200 {object} []api.DepAppReport
// @router /analyses/report/applications [get]
func (h AnalysisHandler) DepAppReports(ctx *gin.Context) {
resources := []DepAppReport{}
Expand Down
1 change: 1 addition & 0 deletions api/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func All() []Handler {
&FileHandler{},
&MigrationWaveHandler{},
&BatchHandler{},
&TargetHandler{},
}
}

Expand Down
69 changes: 56 additions & 13 deletions api/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package api
import (
"encoding/json"
"github.com/gin-gonic/gin"
qf "github.com/konveyor/tackle2-hub/api/filter"
"github.com/konveyor/tackle2-hub/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"net/http"
)
Expand Down Expand Up @@ -61,6 +63,8 @@ func (h RuleSetHandler) Get(ctx *gin.Context) {
// List godoc
// @summary List all bindings.
// @description List all bindings.
// @description filters:
// @description - labels
// @tags rulesets
// @produce json
// @success 200 {object} []RuleSet
Expand All @@ -71,7 +75,17 @@ func (h RuleSetHandler) List(ctx *gin.Context) {
h.DB(ctx),
clause.Associations,
"Rules.File")
result := db.Find(&list)

filter, err := qf.New(ctx,
[]qf.Assert{
{Field: "name", Kind: qf.STRING},
{Field: "labels", Kind: qf.STRING},
})
if err != nil {
_ = ctx.Error(err)
return
}
result := db.Where("ID IN (?)", h.ruleSetIDs(ctx, filter)).Find(&list)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
Expand Down Expand Up @@ -137,6 +151,10 @@ func (h RuleSetHandler) Delete(ctx *gin.Context) {
_ = ctx.Error(result.Error)
return
}
if ruleset.Builtin() {
h.Status(ctx, http.StatusForbidden)
return
}
result = h.DB(ctx).Delete(ruleset, id)
if result.Error != nil {
_ = ctx.Error(result.Error)
Expand All @@ -163,15 +181,19 @@ func (h RuleSetHandler) Update(ctx *gin.Context) {
_ = ctx.Error(err)
return
}
//
// Delete unwanted ruleSets.
m := &model.RuleSet{}
db := h.preLoad(h.DB(ctx), clause.Associations)
result := db.First(m, id)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
if m.Builtin() {
h.Status(ctx, http.StatusForbidden)
return
}
//
// Delete unwanted rules.
for _, ruleset := range m.Rules {
if !r.HasRule(ruleset.ID) {
err := h.DB(ctx).Delete(ruleset).Error
Expand Down Expand Up @@ -218,16 +240,45 @@ func (h RuleSetHandler) Update(ctx *gin.Context) {
h.Status(ctx, http.StatusNoContent)
}

func (h *RuleSetHandler) ruleSetIDs(ctx *gin.Context, f qf.Filter) (q *gorm.DB) {
q = h.DB(ctx)
q = q.Model(&model.RuleSet{})
q = q.Select("ID")
q = f.Where(q, "-Labels")
filter := f
if f, found := filter.Field("labels"); found {
if f.Value.Operator(qf.AND) {
var qs []*gorm.DB
for _, f = range f.Expand() {
f = f.As("json_each.value")
iq := h.DB(ctx)
iq = iq.Table("Rule")
iq = iq.Joins("m ,json_each(Labels)")
iq = iq.Select("m.RuleSetID")
qs = append(qs, iq)
}
q = q.Where("ID IN (?)", model.Intersect(qs...))
} else {
f = f.As("json_each.value")
iq := h.DB(ctx)
iq = iq.Table("Rule")
iq = iq.Joins("m ,json_each(Labels)")
iq = iq.Select("m.RuleSetID")
iq = f.Where(iq)
q = q.Where("ID IN (?)", iq)
}
}
return
}

//
// RuleSet REST resource.
type RuleSet struct {
Resource
Kind string `json:"kind,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Image Ref `json:"image"`
Rules []Rule `json:"rules"`
Custom bool `json:"custom,omitempty"`
Repository *Repository `json:"repository,omitempty"`
Identity *Ref `json:"identity,omitempty"`
DependsOn []Ref `json:"dependsOn"`
Expand All @@ -240,13 +291,7 @@ func (r *RuleSet) With(m *model.RuleSet) {
r.Kind = m.Kind
r.Name = m.Name
r.Description = m.Description
r.Custom = m.Custom
r.Identity = r.refPtr(m.IdentityID, m.Identity)
imgRef := Ref{ID: m.ImageID}
if m.Image != nil {
imgRef.Name = m.Image.Name
}
r.Image = imgRef
_ = json.Unmarshal(m.Repository, &r.Repository)
r.Rules = []Rule{}
for i := range m.Rules {
Expand All @@ -271,10 +316,8 @@ func (r *RuleSet) Model() (m *model.RuleSet) {
Kind: r.Kind,
Name: r.Name,
Description: r.Description,
Custom: r.Custom,
}
m.ID = r.ID
m.ImageID = r.Image.ID
m.IdentityID = r.idPtr(r.Identity)
m.Rules = []model.Rule{}
for _, rule := range r.Rules {
Expand Down
Loading

0 comments on commit 0d0355b

Please sign in to comment.