Skip to content

Commit

Permalink
feat: force build deletion (#1135)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic committed Sep 20, 2024
1 parent f93f579 commit ecbc042
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 44 deletions.
1 change: 1 addition & 0 deletions docs/daytona_build_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ daytona build delete [BUILD] [flags]

```
-a, --all Delete ALL builds
-f, --force Force delete build
--prebuild-id string Delete ALL builds from prebuild
```

Expand Down
4 changes: 4 additions & 0 deletions hack/docs/daytona_build_delete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ options:
shorthand: a
default_value: "false"
usage: Delete ALL builds
- name: force
shorthand: f
default_value: "false"
usage: Force delete build
- name: prebuild-id
usage: Delete ALL builds from prebuild
inherited_options:
Expand Down
4 changes: 2 additions & 2 deletions internal/testing/server/workspaces/mocks/build_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (m *MockBuildService) List(filter *build.Filter) ([]*build.Build, error) {
return args.Get(0).([]*build.Build), args.Error(1)
}

func (m *MockBuildService) MarkForDeletion(filter *build.Filter) []error {
args := m.Called(filter)
func (m *MockBuildService) MarkForDeletion(filter *build.Filter, force bool) []error {
args := m.Called(filter, force)
return args.Get(0).([]error)
}

Expand Down
47 changes: 43 additions & 4 deletions pkg/api/controllers/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package build

import (
"errors"
"fmt"
"net/http"
"strconv"

"github.com/daytonaio/daytona/pkg/api/controllers/build/dto"
"github.com/daytonaio/daytona/pkg/build"
Expand Down Expand Up @@ -139,14 +141,27 @@ func ListBuilds(ctx *gin.Context) {
// @Tags build
// @Summary Delete ALL builds
// @Description Delete ALL builds
// @Param force query bool false "Force"
// @Success 204
// @Router /build [delete]
//
// @id DeleteAllBuilds
func DeleteAllBuilds(ctx *gin.Context) {
forceQuery := ctx.Query("force")
var force bool
var err error

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

server := server.GetInstance(nil)

errs := server.BuildService.MarkForDeletion(nil)
errs := server.BuildService.MarkForDeletion(nil, force)
if len(errs) > 0 {
for _, err := range errs {
_ = ctx.Error(err)
Expand All @@ -164,18 +179,30 @@ func DeleteAllBuilds(ctx *gin.Context) {
// @Summary Delete build
// @Description Delete build
// @Param buildId path string true "Build ID"
// @Param force query bool false "Force"
// @Success 204
// @Router /build/{buildId} [delete]
//
// @id DeleteBuild
func DeleteBuild(ctx *gin.Context) {
buildId := ctx.Param("buildId")
forceQuery := ctx.Query("force")
var force bool
var err error

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

server := server.GetInstance(nil)

errs := server.BuildService.MarkForDeletion(&build.Filter{
Id: &buildId,
})
}, force)
if len(errs) > 0 {
for _, err := range errs {
_ = ctx.Error(err)
Expand All @@ -193,17 +220,29 @@ func DeleteBuild(ctx *gin.Context) {
// @Summary Delete builds
// @Description Delete builds
// @Param prebuildId path string true "Prebuild ID"
// @Param force query bool false "Force"
// @Success 204
// @Router /build/prebuild/{prebuildId} [delete]
//
// @id DeleteBuildsFromPrebuild
func DeleteBuildsFromPrebuild(ctx *gin.Context) {
prebuildId := ctx.Param("prebuildId")
forceQuery := ctx.Query("force")
var force bool
var err error

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

server := server.GetInstance(nil)

// Fail if prebuild does not exist
_, err := server.ProjectConfigService.FindPrebuild(nil, &config.PrebuildFilter{
_, err = server.ProjectConfigService.FindPrebuild(nil, &config.PrebuildFilter{
Id: &prebuildId,
})
if err != nil {
Expand All @@ -213,7 +252,7 @@ func DeleteBuildsFromPrebuild(ctx *gin.Context) {

errs := server.BuildService.MarkForDeletion(&build.Filter{
PrebuildIds: &[]string{prebuildId},
})
}, force)
if len(errs) > 0 {
for _, err := range errs {
_ = ctx.Error(err)
Expand Down
22 changes: 22 additions & 0 deletions pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ const docTemplate = `{
],
"summary": "Delete ALL builds",
"operationId": "DeleteAllBuilds",
"parameters": [
{
"type": "boolean",
"description": "Force",
"name": "force",
"in": "query"
}
],
"responses": {
"204": {
"description": "No Content"
Expand All @@ -173,6 +181,12 @@ const docTemplate = `{
"name": "prebuildId",
"in": "path",
"required": true
},
{
"type": "boolean",
"description": "Force",
"name": "force",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -225,6 +239,12 @@ const docTemplate = `{
"name": "buildId",
"in": "path",
"required": true
},
{
"type": "boolean",
"description": "Force",
"name": "force",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -2803,6 +2823,7 @@ const docTemplate = `{
"success",
"published",
"pending-delete",
"pending-forced-delete",
"deleting"
],
"x-enum-varnames": [
Expand All @@ -2812,6 +2833,7 @@ const docTemplate = `{
"BuildStateSuccess",
"BuildStatePublished",
"BuildStatePendingDelete",
"BuildStatePendingForcedDelete",
"BuildStateDeleting"
]
},
Expand Down
22 changes: 22 additions & 0 deletions pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@
],
"summary": "Delete ALL builds",
"operationId": "DeleteAllBuilds",
"parameters": [
{
"type": "boolean",
"description": "Force",
"name": "force",
"in": "query"
}
],
"responses": {
"204": {
"description": "No Content"
Expand All @@ -170,6 +178,12 @@
"name": "prebuildId",
"in": "path",
"required": true
},
{
"type": "boolean",
"description": "Force",
"name": "force",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -222,6 +236,12 @@
"name": "buildId",
"in": "path",
"required": true
},
{
"type": "boolean",
"description": "Force",
"name": "force",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -2800,6 +2820,7 @@
"success",
"published",
"pending-delete",
"pending-forced-delete",
"deleting"
],
"x-enum-varnames": [
Expand All @@ -2809,6 +2830,7 @@
"BuildStateSuccess",
"BuildStatePublished",
"BuildStatePendingDelete",
"BuildStatePendingForcedDelete",
"BuildStateDeleting"
]
},
Expand Down
15 changes: 15 additions & 0 deletions pkg/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ definitions:
- success
- published
- pending-delete
- pending-forced-delete
- deleting
type: string
x-enum-varnames:
Expand All @@ -762,6 +763,7 @@ definitions:
- BuildStateSuccess
- BuildStatePublished
- BuildStatePendingDelete
- BuildStatePendingForcedDelete
- BuildStateDeleting
provider.ProviderInfo:
properties:
Expand Down Expand Up @@ -883,6 +885,11 @@ paths:
delete:
description: Delete ALL builds
operationId: DeleteAllBuilds
parameters:
- description: Force
in: query
name: force
type: boolean
responses:
"204":
description: No Content
Expand Down Expand Up @@ -934,6 +941,10 @@ paths:
name: buildId
required: true
type: string
- description: Force
in: query
name: force
type: boolean
responses:
"204":
description: No Content
Expand Down Expand Up @@ -969,6 +980,10 @@ paths:
name: prebuildId
required: true
type: string
- description: Force
in: query
name: force
type: boolean
responses:
"204":
description: No Content
Expand Down
18 changes: 18 additions & 0 deletions pkg/apiclient/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ paths:
delete:
description: Delete ALL builds
operationId: DeleteAllBuilds
parameters:
- description: Force
in: query
name: force
schema:
type: boolean
responses:
"204":
content: {}
Expand Down Expand Up @@ -121,6 +127,11 @@ paths:
required: true
schema:
type: string
- description: Force
in: query
name: force
schema:
type: boolean
responses:
"204":
content: {}
Expand All @@ -139,6 +150,11 @@ paths:
required: true
schema:
type: string
- description: Force
in: query
name: force
schema:
type: boolean
responses:
"204":
content: {}
Expand Down Expand Up @@ -2567,6 +2583,7 @@ components:
- success
- published
- pending-delete
- pending-forced-delete
- deleting
type: string
x-enum-varnames:
Expand All @@ -2576,6 +2593,7 @@ components:
- BuildStateSuccess
- BuildStatePublished
- BuildStatePendingDelete
- BuildStatePendingForcedDelete
- BuildStateDeleting
provider.ProviderInfo:
example:
Expand Down
Loading

0 comments on commit ecbc042

Please sign in to comment.