Skip to content

Commit

Permalink
feat: container registries command (#412)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
idagelic authored and Tpuljak committed Apr 19, 2024
1 parent 07afda7 commit f1a17c6
Show file tree
Hide file tree
Showing 23 changed files with 920 additions and 217 deletions.
4 changes: 2 additions & 2 deletions internal/testing/server/containerregistries/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func NewInMemoryContainerRegistryStore() containerregistry.Store {

func (s *InMemoryContainerRegistryStore) List() ([]*containerregistry.ContainerRegistry, error) {
crs := []*containerregistry.ContainerRegistry{}
// itterate server key
// iterate server key
for _, s := range s.crs {
// itterate username key
// iterate username key
for _, u := range s {
crs = append(crs, u)
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/api/controllers/containerregistry/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package containerregistry
import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/daytonaio/daytona/pkg/server"
Expand All @@ -18,18 +19,25 @@ import (
// @Summary Get container registry credentials
// @Description Get container registry credentials
// @Produce json
// @Param server path string true "Container Registry server name"
// @Param server path string true "Container Registry server name"
// @Param username path string true "Container Registry username"
// @Success 200 {object} ContainerRegistry
// @Success 200 {object} ContainerRegistry
// @Router /container-registry/{server}/{username} [get]
//
// @id GetContainerRegistry
func GetContainerRegistry(ctx *gin.Context) {
crServer := ctx.Param("server")
crUsername := ctx.Param("username")

decodedServerURL, err := url.QueryUnescape(crServer)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to decode server URL: %s", err.Error()))
return
}

server := server.GetInstance(nil)

cr, err := server.ContainerRegistryService.Find(crServer, crUsername)
cr, err := server.ContainerRegistryService.Find(decodedServerURL, crUsername)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to get container registry: %s", err.Error()))
return
Expand Down
9 changes: 8 additions & 1 deletion pkg/api/controllers/containerregistry/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package containerregistry
import (
"fmt"
"net/http"
"net/url"

"github.com/daytonaio/daytona/pkg/server"
"github.com/gin-gonic/gin"
Expand All @@ -26,9 +27,15 @@ func RemoveContainerRegistry(ctx *gin.Context) {
crServer := ctx.Param("server")
crUsername := ctx.Param("username")

decodedServerURL, err := url.QueryUnescape(crServer)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to decode server URL: %s", err.Error()))
return
}

server := server.GetInstance(nil)

err := server.ContainerRegistryService.Delete(crServer, crUsername)
err = server.ContainerRegistryService.Delete(decodedServerURL, crUsername)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to remove container registry: %s", err.Error()))
return
Expand Down
24 changes: 21 additions & 3 deletions pkg/api/controllers/containerregistry/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package containerregistry
import (
"fmt"
"net/http"
"net/url"

"github.com/daytonaio/daytona/pkg/containerregistry"
"github.com/daytonaio/daytona/pkg/server"
Expand All @@ -17,23 +18,40 @@ import (
// @Tags container-registry
// @Summary Set container registry credentials
// @Description Set container registry credentials
// @Param server path string true "Container Registry server name"
// @Param username path string true "Container Registry username"
// @Param containerRegistry body ContainerRegistry true "Container Registry credentials to set"
// @Success 201
// @Router /container-registry [put]
// @Router /container-registry/{server}/{username} [put]
//
// @id SetContainerRegistry
func SetContainerRegistry(ctx *gin.Context) {
crServer := ctx.Param("server")
crUsername := ctx.Param("username")

decodedServerURL, err := url.QueryUnescape(crServer)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to decode server URL: %s", err.Error()))
return
}

var req containerregistry.ContainerRegistry
err := ctx.BindJSON(&req)
err = ctx.BindJSON(&req)
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid request body: %s", err.Error()))
return
}

server := server.GetInstance(nil)

cr, err := server.ContainerRegistryService.Find(req.Server, req.Username)
cr, err := server.ContainerRegistryService.Find(decodedServerURL, crUsername)
if err == nil {
err = server.ContainerRegistryService.Delete(decodedServerURL, crUsername)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to remove container registry: %s", err.Error()))
return
}

cr.Server = req.Server
cr.Username = req.Username
cr.Password = req.Password
Expand Down
89 changes: 57 additions & 32 deletions pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,33 +113,9 @@ const docTemplate = `{
}
}
}
},
"put": {
"description": "Set container registry credentials",
"tags": [
"container-registry"
],
"summary": "Set container registry credentials",
"operationId": "SetContainerRegistry",
"parameters": [
{
"description": "Container Registry credentials to set",
"name": "containerRegistry",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ContainerRegistry"
}
}
],
"responses": {
"201": {
"description": "Created"
}
}
}
},
"/container-registry/{id}": {
"/container-registry/{server}/{username}": {
"get": {
"description": "Get container registry credentials",
"produces": [
Expand All @@ -153,8 +129,15 @@ const docTemplate = `{
"parameters": [
{
"type": "string",
"description": "Container Registry Id",
"name": "id",
"description": "Container Registry server name",
"name": "server",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Container Registry username",
"name": "username",
"in": "path",
"required": true
}
Expand All @@ -168,6 +151,44 @@ const docTemplate = `{
}
}
},
"put": {
"description": "Set container registry credentials",
"tags": [
"container-registry"
],
"summary": "Set container registry credentials",
"operationId": "SetContainerRegistry",
"parameters": [
{
"type": "string",
"description": "Container Registry server name",
"name": "server",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Container Registry username",
"name": "username",
"in": "path",
"required": true
},
{
"description": "Container Registry credentials to set",
"name": "containerRegistry",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ContainerRegistry"
}
}
],
"responses": {
"201": {
"description": "Created"
}
}
},
"delete": {
"description": "Remove a container registry credentials",
"tags": [
Expand All @@ -178,8 +199,15 @@ const docTemplate = `{
"parameters": [
{
"type": "string",
"description": "Container Registry Id",
"name": "id",
"description": "Container Registry server name",
"name": "server",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Container Registry username",
"name": "username",
"in": "path",
"required": true
}
Expand Down Expand Up @@ -1064,9 +1092,6 @@ const docTemplate = `{
"ContainerRegistry": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"password": {
"type": "string"
},
Expand Down
89 changes: 57 additions & 32 deletions pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,33 +110,9 @@
}
}
}
},
"put": {
"description": "Set container registry credentials",
"tags": [
"container-registry"
],
"summary": "Set container registry credentials",
"operationId": "SetContainerRegistry",
"parameters": [
{
"description": "Container Registry credentials to set",
"name": "containerRegistry",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ContainerRegistry"
}
}
],
"responses": {
"201": {
"description": "Created"
}
}
}
},
"/container-registry/{id}": {
"/container-registry/{server}/{username}": {
"get": {
"description": "Get container registry credentials",
"produces": [
Expand All @@ -150,8 +126,15 @@
"parameters": [
{
"type": "string",
"description": "Container Registry Id",
"name": "id",
"description": "Container Registry server name",
"name": "server",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Container Registry username",
"name": "username",
"in": "path",
"required": true
}
Expand All @@ -165,6 +148,44 @@
}
}
},
"put": {
"description": "Set container registry credentials",
"tags": [
"container-registry"
],
"summary": "Set container registry credentials",
"operationId": "SetContainerRegistry",
"parameters": [
{
"type": "string",
"description": "Container Registry server name",
"name": "server",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Container Registry username",
"name": "username",
"in": "path",
"required": true
},
{
"description": "Container Registry credentials to set",
"name": "containerRegistry",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ContainerRegistry"
}
}
],
"responses": {
"201": {
"description": "Created"
}
}
},
"delete": {
"description": "Remove a container registry credentials",
"tags": [
Expand All @@ -175,8 +196,15 @@
"parameters": [
{
"type": "string",
"description": "Container Registry Id",
"name": "id",
"description": "Container Registry server name",
"name": "server",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Container Registry username",
"name": "username",
"in": "path",
"required": true
}
Expand Down Expand Up @@ -1061,9 +1089,6 @@
"ContainerRegistry": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"password": {
"type": "string"
},
Expand Down
Loading

0 comments on commit f1a17c6

Please sign in to comment.