Skip to content

Commit

Permalink
Separate Target and RuleSet models/apis
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Lucidi <slucidi@redhat.com>
  • Loading branch information
mansam committed Jul 31, 2023
1 parent eca0072 commit 4bce4a4
Show file tree
Hide file tree
Showing 15 changed files with 736 additions and 38 deletions.
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
41 changes: 18 additions & 23 deletions api/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,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 +167,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 @@ -222,15 +230,12 @@ func (h RuleSetHandler) Update(ctx *gin.Context) {
// 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"`
Kind string `json:"kind,omitempty"`
Name string `json:"name"`
Rules []Rule `json:"rules"`
Repository *Repository `json:"repository,omitempty"`
Identity *Ref `json:"identity,omitempty"`
DependsOn []Ref `json:"dependsOn"`
}

//
Expand All @@ -239,14 +244,7 @@ func (r *RuleSet) With(m *model.RuleSet) {
r.Resource.With(&m.Model)
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 @@ -268,13 +266,10 @@ func (r *RuleSet) With(m *model.RuleSet) {
// Model builds a model.
func (r *RuleSet) Model() (m *model.RuleSet) {
m = &model.RuleSet{
Kind: r.Kind,
Name: r.Name,
Description: r.Description,
Custom: r.Custom,
Kind: r.Kind,
Name: r.Name,
}
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 4bce4a4

Please sign in to comment.