Skip to content

Commit

Permalink
1.4 (#11)
Browse files Browse the repository at this point in the history
* Bumped version to 1.4

* Compose at node level
  • Loading branch information
salilponde authored Dec 21, 2023
1 parent 26bc23d commit 96b353d
Show file tree
Hide file tree
Showing 22 changed files with 1,746 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
branches: ["main"]

env:
VERSION: "1.3.2"
VERSION: "1.4"

jobs:
docker:
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/common.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package common

const Version = "1.3.2"
const Version = "1.4"
7 changes: 6 additions & 1 deletion pkg/server/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ func (h *Handler) Register(e *echo.Echo) {

node_compose := nodes.Group("/:nodeId/compose")
node_compose.GET("", h.GetNodeComposeProjectList)
node_compose.POST("", h.CreateNodeComposeProject)
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)
Expand Down
249 changes: 240 additions & 9 deletions pkg/server/handler/node_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ func (h *Handler) GetNodeComposeProject(c echo.Context) error {
return unprocessableEntity(c, errors.New("Project not found"))
}

if ncp.LibraryProjectId != nil || ncp.LibraryProjectName != nil {
definition, credentialId, url, err := h.getComposeProjectDefinitionFromLibrary(ncp)
if err != nil {
panic(err)
}
ncp.Definition = &definition
ncp.CredentialId = credentialId
ncp.Url = url
}

req := dockerapi.DockerComposeGet{ProjectName: ncp.ProjectName}

var res *dockerapi.ComposeItem
Expand All @@ -90,7 +100,7 @@ func (h *Handler) GetNodeComposeProject(c echo.Context) error {
return unprocessableEntity(c, err)
}

return ok(c, newNodeComposeProjectItemHead(ncp, res))
return ok(c, newNodeComposeProjectItem(ncp, res))
}

func (h *Handler) GetNodeComposeContainerList(c echo.Context) error {
Expand Down Expand Up @@ -125,18 +135,179 @@ func (h *Handler) GetNodeComposeContainerList(c echo.Context) error {
return ok(c, res)
}

func (h *Handler) CreateNodeComposeProject(c echo.Context) error {
func (h *Handler) CreateGitHubNodeComposeProject(c echo.Context) error {
nodeId, err := strconv.Atoi(c.Param("nodeId"))
if err != nil {
return unprocessableEntity(c, errors.New("nodeId should be an integer"))
}

m := model.NodeComposeProject{NodeId: uint(nodeId)}
r := &nodeComposeGitHubCreateRequest{}
if err := r.bind(c, &m); err != nil {
return unprocessableEntity(c, err)
}

isUnique, err := h.nodeComposeProjectStore.IsUniqueName(uint(nodeId), r.ProjectName)
if err != nil {
panic(err)
}

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

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

return created(c, m.Id)
}

func (h *Handler) CreateLocalNodeComposeProject(c echo.Context) error {
nodeId, err := strconv.Atoi(c.Param("nodeId"))
if err != nil {
return unprocessableEntity(c, errors.New("nodeId should be an integer"))
}

m := model.NodeComposeProject{NodeId: uint(nodeId)}
r := &nodeComposeLocalCreateRequest{}
if err := r.bind(c, &m); err != nil {
return unprocessableEntity(c, err)
}

isUnique, err := h.nodeComposeProjectStore.IsUniqueName(uint(nodeId), r.ProjectName)
if err != nil {
panic(err)
}

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

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

return created(c, m.Id)
}

func (h *Handler) UpdateGitHubNodeComposeProject(c echo.Context) error {
nodeId, err := strconv.Atoi(c.Param("nodeId"))
if err != nil {
return unprocessableEntity(c, errors.New("nodeId should be an integer"))
}

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

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

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

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

isUnique, err := h.nodeComposeProjectStore.IsUniqueNameExcludeItself(uint(nodeId), r.ProjectName, r.Id)
if err != nil {
panic(err)
}

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

if m.LibraryProjectId != nil || m.LibraryProjectName != nil {
m.CredentialId = nil
m.Url = nil
}

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

return noContent(c)
}

func (h *Handler) UpdateLocalNodeComposeProject(c echo.Context) error {
nodeId, err := strconv.Atoi(c.Param("nodeId"))
if err != nil {
return unprocessableEntity(c, errors.New("nodeId should be an integer"))
}

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

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

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

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

isUnique, err := h.nodeComposeProjectStore.IsUniqueNameExcludeItself(uint(nodeId), r.ProjectName, r.Id)
if err != nil {
panic(err)
}

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

if m.LibraryProjectId != nil || m.LibraryProjectName != nil {
m.Definition = nil
}

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

return noContent(c)
}

func (h *Handler) AddNodeComposeProjectFromLibrary(c echo.Context) error {
nodeId, err := strconv.Atoi(c.Param("nodeId"))
if err != nil {
return unprocessableEntity(c, errors.New("nodeId should be an integer"))
}

m := model.NodeComposeProject{NodeId: uint(nodeId)}
r := &nodeComposeProjectCreateRequest{}
r := &nodeComposeProjectAddFromLibraryRequest{}
if err := r.bind(c, &m); err != nil {
return unprocessableEntity(c, err)
}

if r.LibraryProjectId != nil {
m.Type = "github"
_, err := h.composeLibraryStore.GetById(*r.LibraryProjectId)
if err != nil {
return unprocessableEntity(c, errors.New("Library Project not found"))
}
} else {
m.Type = "local"
_, err := h.fileSystemComposeLibraryStore.GetByName(r.LibraryProjectName)
if err != nil {
return unprocessableEntity(c, errors.New("Library Project not found"))
}
}

isUnique, err := h.nodeComposeProjectStore.IsUniqueName(uint(nodeId), r.ProjectName)
if err != nil {
panic(err)
Expand Down Expand Up @@ -169,25 +340,47 @@ func (h *Handler) IsUniqueNodeComposeProjectName(c echo.Context) error {
return ok(c, newUniqueResponse(unique))
}

func (h *Handler) getComposeProjectDefinition(ncp *model.NodeComposeProject) (string, error) {
definition := ""
func (h *Handler) IsUniqueNodeComposeProjectNameExcludeItself(c echo.Context) error {
nodeId, err := strconv.Atoi(c.Param("nodeId"))
if err != nil {
return unprocessableEntity(c, errors.New("nodeId should be an integer"))
}

id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return unprocessableEntity(c, errors.New("id should be an integer"))
}

value := c.QueryParam("value")

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

return ok(c, newUniqueResponse(unique))
}

func (h *Handler) getComposeProjectDefinitionFromLibrary(ncp *model.NodeComposeProject) (definition string, credentialId *uint, url *string, err error) {
if ncp.LibraryProjectId == nil {
clp, err := h.fileSystemComposeLibraryStore.GetByName(ncp.LibraryProjectName)
clp, err := h.fileSystemComposeLibraryStore.GetByName(*ncp.LibraryProjectName)
if err != nil {
return "", errors.New("Library Project not found")
return "", nil, nil, errors.New("Library Project not found")
}
definition = clp.Definition
credentialId = nil
url = nil
} else {
gclp, err := h.composeLibraryStore.GetById(*ncp.LibraryProjectId)
if err != nil {
return "", errors.New("Library Project not found")
return "", nil, nil, errors.New("Library Project not found")
}

decryptedSecret := ""
if gclp.CredentialId != nil {
credential, err := h.credentialStore.GetById(*gclp.CredentialId)
if err != nil {
return "", errors.New("Credentials not found")
return "", nil, nil, errors.New("Credentials not found")
}

decryptedSecret, err = ske.Decrypt(credential.Secret)
Expand All @@ -197,6 +390,44 @@ func (h *Handler) getComposeProjectDefinition(ncp *model.NodeComposeProject) (st
}

content, err := getGitHubFileContent(gclp.Url, decryptedSecret)
if err != nil {
return "", nil, nil, errors.New("Error while retrieving file content from GitHub")
}

definition = content
credentialId = gclp.CredentialId
url = &gclp.Url
}

return definition, credentialId, url, nil
}

func (h *Handler) getComposeProjectDefinition(ncp *model.NodeComposeProject) (string, error) {
var err error
definition := ""

if ncp.LibraryProjectId != nil || ncp.LibraryProjectName != nil {
definition, _, _, err = h.getComposeProjectDefinitionFromLibrary(ncp)
if err != nil {
return "", err
}
} else if ncp.Type == "local" {
definition = *ncp.Definition
} else if ncp.Type == "github" {
decryptedSecret := ""
if ncp.CredentialId != nil {
credential, err := h.credentialStore.GetById(*ncp.CredentialId)
if err != nil {
return "", errors.New("Credentials not found")
}

decryptedSecret, err = ske.Decrypt(credential.Secret)
if err != nil {
panic(err)
}
}

content, err := getGitHubFileContent(*ncp.Url, decryptedSecret)
if err != nil {
return "", errors.New("Error while retrieving file content from GitHub")
}
Expand Down
Loading

0 comments on commit 96b353d

Please sign in to comment.