Skip to content

Commit

Permalink
Node Compose Project Variables API
Browse files Browse the repository at this point in the history
  • Loading branch information
salilponde committed Dec 23, 2023
1 parent 93620d9 commit 39504f7
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 18 deletions.
44 changes: 28 additions & 16 deletions pkg/server/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Handler struct {
userStore store.UserStore
nodeStore store.NodeStore
nodeComposeProjectStore store.NodeComposeProjectStore
nodeComposeProjectVariableStore store.NodeComposeProjectVariableStore
settingStore store.SettingStore
variableStore store.VariableStore
variableValueStore store.VariableValueStore
Expand All @@ -37,6 +38,7 @@ func NewHandler(
userStore store.UserStore,
nodeStore store.NodeStore,
nodeComposeProjectStore store.NodeComposeProjectStore,
nodeComposeProjectVariableStore store.NodeComposeProjectVariableStore,
settingStore store.SettingStore,
variableStore store.VariableStore,
variableValueStore store.VariableValueStore,
Expand All @@ -50,6 +52,7 @@ func NewHandler(
userStore: userStore,
nodeStore: nodeStore,
nodeComposeProjectStore: nodeComposeProjectStore,
nodeComposeProjectVariableStore: nodeComposeProjectVariableStore,
settingStore: settingStore,
variableStore: variableStore,
variableValueStore: variableValueStore,
Expand Down Expand Up @@ -146,22 +149,31 @@ func (h *Handler) Register(e *echo.Echo) {
githubcomposelibrary.DELETE("/:id", h.DeleteGitHubComposeProject)
githubcomposelibrary.GET("/:id", h.GetGitHubComposeProjectById)

node_compose := nodes.Group("/:nodeId/compose")
node_compose.GET("", h.GetNodeComposeProjectList)
node_compose.POST("/create/github", h.CreateGitHubNodeComposeProject)
node_compose.POST("/create/local", h.CreateLocalNodeComposeProject)
node_compose.POST("/create/library", h.AddNodeComposeProjectFromLibrary)
node_compose.GET("/uniquename", h.IsUniqueNodeComposeProjectName)
node_compose.GET("/:id/uniquename", h.IsUniqueNodeComposeProjectNameExcludeItself)
node_compose.PUT("/:id/github", h.UpdateGitHubNodeComposeProject)
node_compose.PUT("/:id/local", h.UpdateLocalNodeComposeProject)
node_compose.GET("/:id", h.GetNodeComposeProject)
node_compose.DELETE("/:id", h.DeleteNodeComposeProject)
node_compose.GET("/:id/containers", h.GetNodeComposeContainerList)
node_compose.GET("/:id/logs", h.GetNodeComposeLogs)
node_compose.GET("/:id/pull", h.GetNodeComposePull)
node_compose.GET("/:id/up", h.GetNodeComposeUp)
node_compose.GET("/:id/down", h.GetNodeComposeDown)
node_compose_project := nodes.Group("/:nodeId/compose")
node_compose_project.GET("", h.GetNodeComposeProjectList)
node_compose_project.POST("/create/github", h.CreateGitHubNodeComposeProject)
node_compose_project.POST("/create/local", h.CreateLocalNodeComposeProject)
node_compose_project.POST("/create/library", h.AddNodeComposeProjectFromLibrary)
node_compose_project.GET("/uniquename", h.IsUniqueNodeComposeProjectName)
node_compose_project.GET("/:id/uniquename", h.IsUniqueNodeComposeProjectNameExcludeItself)
node_compose_project.PUT("/:id/github", h.UpdateGitHubNodeComposeProject)
node_compose_project.PUT("/:id/local", h.UpdateLocalNodeComposeProject)
node_compose_project.GET("/:id", h.GetNodeComposeProject)
node_compose_project.DELETE("/:id", h.DeleteNodeComposeProject)
node_compose_project.GET("/:id/containers", h.GetNodeComposeContainerList)
node_compose_project.GET("/:id/logs", h.GetNodeComposeLogs)
node_compose_project.GET("/:id/pull", h.GetNodeComposePull)
node_compose_project.GET("/:id/up", h.GetNodeComposeUp)
node_compose_project.GET("/:id/down", h.GetNodeComposeDown)

node_compose_project_variables := node_compose_project.Group("/:node_compose_project_id/variables")
node_compose_project_variables.POST("", h.CreateVariable)
node_compose_project_variables.PUT("/:id", h.UpdateVariable)
node_compose_project_variables.GET("", h.GetVariableList)
node_compose_project_variables.GET("/:id", h.GetVariableById)
node_compose_project_variables.DELETE("/:id", h.DeleteVariableById)
node_compose_project_variables.GET("/uniquename", h.IsUniqueVariableName)
node_compose_project_variables.GET("/:id/uniquename", h.IsUniqueVariableNameExcludeItself)

variables := v1.Group("/variables")
variables.POST("", h.CreateVariable)
Expand Down
198 changes: 198 additions & 0 deletions pkg/server/handler/node_compose_project_variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package handler

import (
"strconv"

"github.com/productiveops/dokemon/pkg/server/model"

"github.com/labstack/echo/v4"
)

func (h *Handler) CreateNodeComposeProjectVariable(c echo.Context) error {
nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id"))
}

m := model.NodeComposeProjectVariable{}
r := &nodeComposeProjectVariableCreateRequest{}
if err := r.bind(c, &m); err != nil {
return unprocessableEntity(c, err)
}

isUnique, err := h.nodeComposeProjectVariableStore.IsUniqueName(uint(nodeComposeProjectid), r.Name)
if err != nil {
panic(err)
}

if !isUnique {
return unprocessableEntity(c, duplicateNameError())
}

if err := h.nodeComposeProjectVariableStore.Create(&m); err != nil {
panic(err)
}

return created(c, m.Id)
}

func (h *Handler) UpdateNodeComposeProjectVariable(c echo.Context) error {
nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id"))
}

id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("id"))
}

exists, err := h.nodeComposeProjectVariableStore.Exists(uint(nodeComposeProjectid), uint(id))
if err != nil {
panic(err)
}

if !exists {
return resourceNotFound(c, "NodeComposeProjectVariable")
}

m := model.NodeComposeProjectVariable{}
r := &nodeComposeProjectVariableUpdateRequest{Id: uint(id)}
if err := r.bind(c, &m); err != nil {
return unprocessableEntity(c, err)
}

isUnique, err := h.nodeComposeProjectVariableStore.IsUniqueNameExcludeItself(uint(nodeComposeProjectid), r.Name, r.Id)
if err != nil {
panic(err)
}

if !isUnique {
return unprocessableEntity(c, duplicateNameError())
}

if err := h.nodeComposeProjectVariableStore.Update(&m); err != nil {
panic(err)
}

return noContent(c)
}

func (h *Handler) DeleteNodeComposeProjectVariableById(c echo.Context) error {
nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id"))
}

id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("id"))
}

exists, err := h.nodeComposeProjectVariableStore.Exists(uint(nodeComposeProjectid), uint(id))
if err != nil {
panic(err)
}

if !exists {
return resourceNotFound(c, "NodeComposeProjectVariable")
}

if err := h.nodeComposeProjectVariableStore.DeleteById(uint(nodeComposeProjectid), uint(id)); err != nil {
panic(err)
}

return noContent(c)
}

func (h *Handler) GetNodeComposeProjectVariableList(c echo.Context) error {
nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id"))
}

p, err := strconv.Atoi(c.QueryParam("p"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("p"))
}

if p < 1 {
return unprocessableEntity(c, queryGte1ExpectedError("p"))
}

s, err := strconv.Atoi(c.QueryParam("s"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("s"))
}

if s < 1 {
return unprocessableEntity(c, queryGte1ExpectedError("s"))
}

rows, totalRows, err := h.nodeComposeProjectVariableStore.GetList(uint(nodeComposeProjectid), uint(p), uint(s))
if err != nil {
panic(err)
}

return ok(c, newPageResponse[nodeComposeProjectVariableResponse](newNodeComposeProjectVariableList(rows), uint(p), uint(s), uint(totalRows)))
}

func (h *Handler) GetNodeComposeProjectVariableById(c echo.Context) error {
nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id"))
}

id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("id"))
}

m, err := h.nodeComposeProjectVariableStore.GetById(uint(nodeComposeProjectid), uint(id))
if err != nil {
panic(err)
}

if m == nil {
return resourceNotFound(c, "NodeComposeProjectVariable")
}

return ok(c, newNodeComposeProjectVariableResponse(m))
}

func (h *Handler) IsUniqueNodeComposeProjectVariableName(c echo.Context) error {
nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id"))
}

value := c.QueryParam("value")

unique, err := h.nodeComposeProjectVariableStore.IsUniqueName(uint(nodeComposeProjectid), value)
if err != nil {
panic(err)
}

return ok(c, newUniqueResponse(unique))
}

func (h *Handler) IsUniqueNodeComposeProjectVariableNameExcludeItself(c echo.Context) error {
nodeComposeProjectid, err := strconv.Atoi(c.Param("node_compose_project_id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("node_compose_project_id"))
}

id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return unprocessableEntity(c, routeIntExpectedError("id"))
}

value := c.QueryParam("value")

unique, err := h.nodeComposeProjectVariableStore.IsUniqueNameExcludeItself(uint(nodeComposeProjectid), value, uint(id))
if err != nil {
panic(err)
}

return ok(c, newUniqueResponse(unique))
}
12 changes: 10 additions & 2 deletions pkg/server/handler/response_node_compose_project_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ type nodeComposeProjectVariableResponse struct {
Value string `json:"value"`
}

func newNodeComposeProjectVariableResponse(m *model.NodeComposeProjectVariable) *nodeComposeProjectVariableResponse {
return &nodeComposeProjectVariableResponse{
func newNodeComposeProjectVariableResponse(m *model.NodeComposeProjectVariable) nodeComposeProjectVariableResponse {
return nodeComposeProjectVariableResponse{
Id: m.Id,
NodeComposeProjectId: m.NodeComposeProjectId,
Name: m.Name,
IsSecret: m.IsSecret,
Value: m.Value,
}
}

func newNodeComposeProjectVariableList(rows []model.NodeComposeProjectVariable) []nodeComposeProjectVariableResponse {
headRows := make([]nodeComposeProjectVariableResponse, len(rows))
for i, r := range rows {
headRows[i] = newNodeComposeProjectVariableResponse(&r)
}
return headRows
}
1 change: 1 addition & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func NewServer(dbConnectionString string, dataPath string, logLevel string, sslE
store.NewSqlUserStore(db),
store.NewSqlNodeStore(db),
sqlNodeComposeProjectStore,
store.NewSqlNodeComposeProjectVariableStore(db),
store.NewSqlSettingStore(db),
store.NewSqlVariableStore(db),
store.NewSqlVariableValueStore(db),
Expand Down
12 changes: 12 additions & 0 deletions pkg/server/store/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ type NodeComposeProjectStore interface {
IsUniqueNameExcludeItself(nodeId uint, name string, id uint) (bool, error)
}

type NodeComposeProjectVariableStore interface {
Create(m *model.NodeComposeProjectVariable) error
Update(m *model.NodeComposeProjectVariable) error
GetById(nodeComposeProjectId uint, id uint) (*model.NodeComposeProjectVariable, error)
GetList(nodeComposeProjectId uint, pageNo, pageSize uint) ([]model.NodeComposeProjectVariable, int64, error)
DeleteById(nodeComposeProjectId uint, id uint) error
Exists(nodeComposeProjectId uint, id uint) (bool, error)

IsUniqueName(nodeComposeProjectId uint, name string) (bool, error)
IsUniqueNameExcludeItself(nodeComposeProjectId uint, name string, id uint) (bool, error)
}

type SettingStore interface {
Create(m *model.Setting) error
Update(m *model.Setting) error
Expand Down

0 comments on commit 39504f7

Please sign in to comment.