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 <[email protected]>
  • Loading branch information
mansam committed Jul 28, 2023
1 parent eca0072 commit 958a8fc
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 26 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
26 changes: 12 additions & 14 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 @@ -239,14 +247,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 +269,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
237 changes: 237 additions & 0 deletions api/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
package api

import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/konveyor/tackle2-hub/model"
"gorm.io/gorm/clause"
"net/http"
)

//
// Routes
const (
TargetsRoot = "/targets"
TargetRoot = TargetsRoot + "/:" + ID
)

//
// TargetHandler handles Target resource routes.
type TargetHandler struct {
BaseHandler
}

func (h TargetHandler) AddRoutes(e *gin.Engine) {
routeGroup := e.Group("/")
routeGroup.Use(Required("rulesets"), Transaction)
routeGroup.GET(TargetsRoot, h.List)
routeGroup.GET(TargetsRoot+"/", h.List)
routeGroup.POST(TargetsRoot, h.Create)
routeGroup.GET(TargetRoot, h.Get)
routeGroup.PUT(TargetRoot, h.Update)
routeGroup.DELETE(TargetRoot, h.Delete)
}

// Get godoc
// @summary Get a Target by ID.
// @description Get a Target by ID.
// @tags targets
// @produce json
// @success 200 {object} Target
// @router /targets/{id} [get]
// @param id path string true "Target ID"
func (h TargetHandler) Get(ctx *gin.Context) {
id := h.pk(ctx)
target := &model.Target{}
db := h.preLoad(h.DB(ctx), clause.Associations)
result := db.First(target, id)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
r := Target{}
r.With(target)

h.Respond(ctx, http.StatusOK, r)
}

// List godoc
// @summary List all targets.
// @description List all targets.
// @tags targets
// @produce json
// @success 200 {object} []Target
// @router /targets [get]
func (h TargetHandler) List(ctx *gin.Context) {
var list []model.Target
db := h.preLoad(h.DB(ctx), clause.Associations)
result := db.Find(&list)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
resources := []Target{}
for i := range list {
r := Target{}
r.With(&list[i])
resources = append(resources, r)
}

h.Respond(ctx, http.StatusOK, resources)
}

// Create godoc
// @summary Create a target.
// @description Create a target.
// @tags targets
// @accept json
// @produce json
// @success 201 {object} Target
// @router /targets [post]
// @param target body Target true "Target data"
func (h TargetHandler) Create(ctx *gin.Context) {
target := &Target{}
err := h.Bind(ctx, target)
if err != nil {
return
}
m := target.Model()
m.CreateUser = h.BaseHandler.CurrentUser(ctx)
result := h.DB(ctx).Create(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
db := h.preLoad(h.DB(ctx), clause.Associations)
result = db.First(m)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
target.With(m)

h.Respond(ctx, http.StatusCreated, target)
}

// Delete godoc
// @summary Delete a target.
// @description Delete a target.
// @tags targets
// @success 204
// @router /targets/{id} [delete]
// @param id path string true "Target ID"
func (h TargetHandler) Delete(ctx *gin.Context) {
id := h.pk(ctx)
target := &model.Target{}
result := h.DB(ctx).First(target, id)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}
if target.Builtin() {
h.Status(ctx, http.StatusForbidden)
return
}
result = h.DB(ctx).Delete(target, id)
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}

h.Status(ctx, http.StatusNoContent)
}

// Update godoc
// @summary Update a target.
// @description Update a target.
// @tags targets
// @accept json
// @success 204
// @router /targets/{id} [put]
// @param id path string true "Target ID"
// @param target body Target true "Target data"
func (h TargetHandler) Update(ctx *gin.Context) {
id := h.pk(ctx)
r := &Target{}
err := h.Bind(ctx, r)
if err != nil {
_ = ctx.Error(err)
return
}

m := &model.Target{}
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
}
//
// Update target.
m = r.Model()
m.ID = id
m.UpdateUser = h.BaseHandler.CurrentUser(ctx)
db = h.DB(ctx).Model(m)
db = db.Omit(clause.Associations)
result = db.Updates(h.fields(m))
if result.Error != nil {
_ = ctx.Error(result.Error)
return
}

h.Status(ctx, http.StatusNoContent)
}

//
// Target REST resource.
type Target struct {
Resource
Name string `json:"name"`
Description string `json:"description"`
Choice bool `json:"choice,omitempty"`
Custom bool `json:"custom,omitempty"`
Labels []Label `json:"labels"`
Image Ref `json:"image"`
RuleSet *RuleSet `json:"ruleset,omitempty"`
}

type Label struct {
Name string `json:"name"`
Label string `json:"label"`
}

//
// With updates the resource with the model.
func (r *Target) With(m *model.Target) {
r.Resource.With(&m.Model)
r.Name = m.Name
r.Description = m.Description
r.Choice = m.Choice
r.Custom = !m.Builtin()
r.RuleSet = &RuleSet{}
r.RuleSet.With(m.RuleSet)
_ = json.Unmarshal(m.Labels, &r.Labels)
imgRef := Ref{ID: m.ImageID}
if m.Image != nil {
imgRef.Name = m.Image.Name
}
r.Image = imgRef
}

//
// Model builds a model.
func (r *Target) Model() (m *model.Target) {
m = &model.Target{
Name: r.Name,
Description: r.Description,
Choice: r.Choice,
}
m.ID = r.ID
m.ImageID = r.Image.ID

return
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/konveyor/tackle2-hub

go 1.18

replace github.com/konveyor/tackle2-seed => ../tackle2-seed

require (
github.com/Nerzal/gocloak/v10 v10.0.1
github.com/andygrunwald/go-jira v1.16.0
Expand Down
2 changes: 2 additions & 0 deletions migration/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
v5 "github.com/konveyor/tackle2-hub/migration/v5"
v6 "github.com/konveyor/tackle2-hub/migration/v6"
v7 "github.com/konveyor/tackle2-hub/migration/v7"
v8 "github.com/konveyor/tackle2-hub/migration/v8"
"github.com/konveyor/tackle2-hub/settings"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -47,5 +48,6 @@ func All() []Migration {
v5.Migration{},
v6.Migration{},
v7.Migration{},
v8.Migration{},
}
}
Loading

0 comments on commit 958a8fc

Please sign in to comment.