Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion controllers/task_definitions_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func GetAllTaskDefinitions(c *gin.Context) {
return
}

tasks := dao.GetAllTaskDefinitions(apiKey.OrganizationId)
offset := ParseIntQueryParameter(c, "offset", 0)
limit := ParseIntQueryParameter(c, "limit", 10)

tasks := dao.GetAllTaskDefinitions(apiKey.OrganizationId, offset, limit)
c.IndentedJSON(http.StatusOK, tasks)
}

Expand Down Expand Up @@ -263,3 +266,29 @@ func DeleteTaskDefinition(c *gin.Context) {
"message": "deleted",
})
}

// CountTaskDefinitions godoc
// @Summary Counts the number of task definitions for an account
// @Description Counts the number of task definitions for an account
// @Tags definition
// @Produce application/json
// @Success 200
// @Router /api/v1/task/count [get]
func CountTaskDefinitions(c *gin.Context) {
apiKey, authErr := AuthRequest(c)

if authErr != nil {
c.AbortWithStatus(http.StatusUnauthorized)
return
}

count, err := dao.CountTaskDefinitions(apiKey.OrganizationId)
if err != nil {
c.JSON(http.StatusBadRequest, c.Error(err))
return
}

c.JSON(http.StatusOK, gin.H{
"count": count,
})
}
18 changes: 15 additions & 3 deletions controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@ package controllers

import (
"errors"
"strconv"

"github.com/drorivry/rego/dao"
"github.com/drorivry/rego/models"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)

func AuthRequest(c *gin.Context) (*models.ApiKeys, error){
func AuthRequest(c *gin.Context) (*models.ApiKeys, error) {
token := c.GetHeader("X-Rego-Api-Key")

apiKey, authErr := dao.AuthApiKey(token)

if authErr != nil {
log.Error().Err(authErr).Msg("Invalid token")
return nil, errors.New("Invalid token");
return nil, errors.New("Invalid token")
}

return &apiKey, nil

}
}

func ParseIntQueryParameter(c *gin.Context, paramName string, defaultValue int) int {
param_str := c.DefaultQuery(paramName, strconv.Itoa(defaultValue))
param, err := strconv.Atoi(param_str)

if err != nil {
param = 0
}

return param
}
35 changes: 33 additions & 2 deletions dao/definitions_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ func CreateTaskDefinition(taskDef *models.TaskDefinition) error {
return nil
}

func GetAllTaskDefinitions(OrganizationId string) []models.TaskDefinition {
func GetAllTaskDefinitions(OrganizationId string, offset int, limit int) []models.TaskDefinition {
var tasks []models.TaskDefinition
initializers.GetTaskDefinitionsTable().Where(
"organization_id = ?",
OrganizationId,
).Find(&tasks)
).Order(
"created_at desc",
).Offset(
offset,
).Limit(
limit,
).Find(
&tasks,
)
return tasks
}

Expand Down Expand Up @@ -176,3 +184,26 @@ func UpdateDefinitionStatus(definitionId uuid.UUID, status models.Status, Organi
return
}
}

func CountTaskDefinitions(organizationId string) (int64, error) {
var count int64
result := initializers.GetTaskDefinitionsTable().Where(
"organization_id = ?",
organizationId,
).Count(
&count,
)

if result.Error != nil {
log.Error().Err(
result.Error,
).Str(
"organization_id",
organizationId,
).Msg(
"Couldn't count task definitions",
)
}

return count, result.Error
}
1 change: 1 addition & 0 deletions tasker/tasker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func GetServer(port int) *http.Server {
{
// Definitions
v1.GET("/task", ginZerologger, controllers.GetAllTaskDefinitions)
v1.GET("/task/count", ginZerologger, controllers.CountTaskDefinitions)
v1.POST("/task", ginZerologger, controllers.CreateTaskDefinition)
v1.POST("/task/:definitionId/rerun", ginZerologger, controllers.RerunTask)
v1.GET("/task/:definitionId/latest", ginZerologger, controllers.GetLatestExecution)
Expand Down