Skip to content

Commit

Permalink
refactor: provisioner struct
Browse files Browse the repository at this point in the history
Removed event_bus package as well

Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak committed Apr 4, 2024
1 parent ebd6646 commit 14d9441
Show file tree
Hide file tree
Showing 18 changed files with 518 additions and 692 deletions.
49 changes: 14 additions & 35 deletions pkg/server/api/controllers/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ import (
"net/http"
"strconv"

"github.com/daytonaio/daytona/pkg/server/api/controllers/workspace/dto"
"github.com/daytonaio/daytona/pkg/server/db"
"github.com/daytonaio/daytona/pkg/server/provisioner"
"github.com/daytonaio/daytona/pkg/server/workspaceservice"
"github.com/daytonaio/daytona/pkg/types"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

// GetWorkspace godoc
Expand Down Expand Up @@ -53,41 +48,25 @@ func GetWorkspace(ctx *gin.Context) {
//
// @id ListWorkspaces
func ListWorkspaces(ctx *gin.Context) {
workspaces, err := db.ListWorkspaces()
verbose := ctx.Query("verbose")
verboseQuery := ctx.Query("verbose")
verbose := false
var err error

if verboseQuery != "" {
verbose, err = strconv.ParseBool(verboseQuery)
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, errors.New("invalid value for verbose flag"))
return
}
}

workspaceList, err := workspaceservice.ListWorkspaces(verbose)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, errors.New("failed to list workspaces"))
ctx.AbortWithError(http.StatusInternalServerError, fmt.Errorf("failed to list workspaces: %s", err.Error()))
return
}

response := []dto.WorkspaceDTO{}

for _, workspace := range workspaces {
var workspaceInfo *types.WorkspaceInfo
if verbose != "" {
isVerbose, err := strconv.ParseBool(verbose)
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, errors.New("invalid value for verbose flag"))
return
}

if isVerbose {
workspaceInfo, err = provisioner.GetWorkspaceInfo(workspace)
if err != nil {
log.Error(fmt.Errorf("failed to get workspace info for %s", workspace.Name))
// return
}
}
}

response = append(response, dto.WorkspaceDTO{
Workspace: *workspace,
Info: workspaceInfo,
})
}

ctx.JSON(200, response)
ctx.JSON(200, workspaceList)
}

// RemoveWorkspace godoc
Expand Down
64 changes: 0 additions & 64 deletions pkg/server/event_bus/event_bus.go

This file was deleted.

68 changes: 0 additions & 68 deletions pkg/server/event_bus/types.go

This file was deleted.

38 changes: 38 additions & 0 deletions pkg/server/provisioner/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0

package provisioner

import (
"github.com/daytonaio/daytona/pkg/provider"
"github.com/daytonaio/daytona/pkg/provider/manager"
"github.com/daytonaio/daytona/pkg/types"
)

func (p *Provisioner) CreateWorkspace(workspace *types.Workspace, target *provider.ProviderTarget) error {
targetProvider, err := manager.GetProvider(target.ProviderInfo.Name)
if err != nil {
return err
}

_, err = (*targetProvider).CreateWorkspace(&provider.WorkspaceRequest{
TargetOptions: target.Options,
Workspace: workspace,
})

return err
}

func (p *Provisioner) CreateProject(project *types.Project, target *provider.ProviderTarget) error {
targetProvider, err := manager.GetProvider(target.ProviderInfo.Name)
if err != nil {
return err
}

_, err = (*targetProvider).CreateProject(&provider.ProjectRequest{
TargetOptions: target.Options,
Project: project,
})

return err
}
38 changes: 38 additions & 0 deletions pkg/server/provisioner/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0

package provisioner

import (
"github.com/daytonaio/daytona/pkg/provider"
"github.com/daytonaio/daytona/pkg/provider/manager"
"github.com/daytonaio/daytona/pkg/types"
)

func (p *Provisioner) DestroyWorkspace(workspace *types.Workspace, target *provider.ProviderTarget) error {
targetProvider, err := manager.GetProvider(target.ProviderInfo.Name)
if err != nil {
return err
}

_, err = (*targetProvider).DestroyWorkspace(&provider.WorkspaceRequest{
TargetOptions: target.Options,
Workspace: workspace,
})

return err
}

func (p *Provisioner) DestroyProject(project *types.Project, target *provider.ProviderTarget) error {
targetProvider, err := manager.GetProvider(target.ProviderInfo.Name)
if err != nil {
return err
}

_, err = (*targetProvider).DestroyProject(&provider.ProjectRequest{
TargetOptions: target.Options,
Project: project,
})

return err
}
22 changes: 22 additions & 0 deletions pkg/server/provisioner/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0

package provisioner

import (
"github.com/daytonaio/daytona/pkg/provider"
"github.com/daytonaio/daytona/pkg/provider/manager"
"github.com/daytonaio/daytona/pkg/types"
)

func (p *Provisioner) GetWorkspaceInfo(workspace *types.Workspace, target *provider.ProviderTarget) (*types.WorkspaceInfo, error) {
targetProvider, err := manager.GetProvider(target.ProviderInfo.Name)
if err != nil {
return nil, err
}

return (*targetProvider).GetWorkspaceInfo(&provider.WorkspaceRequest{
TargetOptions: target.Options,
Workspace: workspace,
})
}
Loading

0 comments on commit 14d9441

Please sign in to comment.